-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_slice.py
executable file
·352 lines (207 loc) · 9.6 KB
/
extract_slice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
'''
Name: extract_slice.py
Version: 1.0
Summary: extract the intersection plane/cross section from 3D model in obj format
Author: suxing liu
Author-email: [email protected]
Created: 2018-04-29
USAGE:
python3 extract_slice.py -p ~/path/ -f model.obj -n 10
'''
#!/usr/bin/env python3
import sys
import numpy as np
import extract_intersection
import struct
import os
import glob
import argparse
import shutil
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
import psutil
import multiprocessing
from multiprocessing import Pool
from contextlib import closing
from cairosvg import svg2png
import open3d as o3d
from PIL import Image
import io
import cv2
# generate foloder to store the output results
def mkdir(path):
# import module
import os
# remove space at the beginning
path=path.strip()
# remove slash at the end
path=path.rstrip("\\")
# path exist? # True # False
isExists=os.path.exists(path)
# process
if not isExists:
# construct the path and folder
#print path + ' folder constructed!'
# make dir
os.makedirs(path)
return True
else:
# if exists, return
#print path+' path exists!'
shutil.rmtree(path)
os.makedirs(path)
return False
# change format from from obj to stl
def OBJ2STL(current_path, model_name, base_name, result_path):
model_file = current_path + model_name
print("Converting file format for 3D point cloud model {}...\n".format(model_name))
#model_name_base = os.path.splitext(model_file)[0]
mesh = o3d.io.read_triangle_mesh(model_file)
print("3D model file infomation: {}\n".format(mesh))
stl_mesh = o3d.geometry.TriangleMesh.compute_triangle_normals(mesh)
#stl_output = model_name_base + '.stl'
stl_output = result_path + base_name + '.stl'
#print(stl_output)
o3d.io.write_triangle_mesh(stl_output, stl_mesh)
# check saved file
if os.path.exists(stl_output):
print("Converted 3d model was saved at {0}\n".format(stl_output))
return True
else:
return False
print("Model file converter failed !\n")
sys.exit(0)
return stl_mesh
#get data from STL file as point coordinates
def get_data(stl_file):
data_points = []
with open(stl_file, "rb") as f:
stl_bytes = f.read()
n_triangle = (len(stl_bytes)-84)//50
fmt = '<'
for k in range(n_triangle):
fmt += 'ffffffffffffh'
#All of a sudden otherwise '<' does everything buggy
data_temp = struct.unpack(fmt,stl_bytes[84:])
for i in range(n_triangle): #sort the data to have only the points
data_points += data_temp[i*13+3:i*13+12]
return data_points
# normalize data
def normalize_data(data_points, data_range):
#Normalize data
min_max_scaler = preprocessing.MinMaxScaler(feature_range = (0,data_range))
#min_max_scaler = preprocessing.MinMaxScaler(feature_range = (-1*data_range, data_range))
point_normalized = min_max_scaler.fit_transform(np.asarray(data_points).reshape(-1, 1))
return list(point_normalized.flatten())
#Generate svg file from slice
def save_file(data_list, width, height, slice_name, xmin, ymin):
abs_path = os.path.abspath(slice_name)
filename, file_extension = os.path.splitext(abs_path)
base_name = os.path.splitext(os.path.basename(filename))[0]
result_file = (save_path + base_name + '.png')
print("Generating slice image '{}' ...\n".format(base_name))
#svg_data = bytearray()
header_line = '<svg width="{}" height="{}" viewBox="0 0 {} {}" >\n'.format(width + 300, height + 300, width + 2, height + 2)
svg_data = bytearray(header_line, 'utf-8')
for indice in range(0, len(data_list), 2):
data = '<line x1="{}" x2="{}" y1="{}" y2="{}" stroke = "black" stroke-width="{}"/>\n'.format(data_list[indice].c_x-xmin, data_list[indice+1].c_x-xmin, data_list[indice].c_y-ymin, data_list[indice+1].c_y-ymin, height/200)
svg_data.extend(bytearray(data, 'utf-8'))
svg_data.extend(b'</svg>')
'''
# save svg format
file_svg = open(slice_name, "w")
file_svg.write('<svg width="{}" height="{}" viewBox="0 0 {} {}" >\n'.format(width + 300, height + 300, width + 2, height + 2))
for indice in range(0, len(data_list), 2):
data = '<line x1="{}" x2="{}" y1="{}" y2="{}" stroke = "black" stroke-width="{}"/>\n'.format(data_list[indice].c_x-xmin, data_list[indice+1].c_x-xmin, data_list[indice].c_y-ymin, data_list[indice+1].c_y-ymin, height/200)
svg_data.extend(bytearray(data, 'utf-8'))
file_svg.write(data)
file_svg.write('</svg>')
'''
#image_bytes = svg2png( url = image_file, write_to = result_file, scale = 1.0)
png_data = svg2png(bytestring = svg_data, scale = 1.0)
pil_image = Image.open(io.BytesIO(png_data))
#pil_image_gray = pil_image.convert('LA')
#pil_image_gray.save(result_file)
#print(pil_image)
opencvImage = np.array(pil_image)
#print(opencvImage.shape)
#make mask of where the transparent bits are
trans_mask = opencvImage[:,:,3] == 0
#replace areas of transparency with white and not transparent
opencvImage[trans_mask] = [255, 255, 255, 255]
#new image without alpha channel...
opencvImage_BRG = cv2.cvtColor(opencvImage, cv2.COLOR_BGRA2BGR)
opencvImage_BRG = ~opencvImage_BRG
opencvImage_gray = cv2.cvtColor(opencvImage_BRG, cv2.COLOR_BGR2GRAY)
cv2.imwrite(result_file, opencvImage_gray)
return opencvImage
# get data point from model files
def get_slice_data(plane_h):
slice_planes = extract_intersection.filter_data(data_points, plane_h)
data_slice_planes = extract_intersection.intersection(slice_planes, plane_h)
width = max(xmax-xmin, ymax-ymin)
height = max(xmax-xmin, ymax-ymin)
save_file(data_slice_planes, max(xmax-xmin, ymax-ymin), max(xmax-xmin, ymax-ymin), save_path + "slice_" + str(int(plane_h)).zfill(3) + ".svg", xmin, ymin)
return data_slice_planes
# compute slice data at a set of depth values
def slice_model(file_model, n_slices, save_path):
global data_points, xmin, xmax, ymin, ymax, zmin, zmax
data_points_model = get_data(file_model)
data_range = n_slices
data_points = normalize_data(data_points_model, data_range)
(xmin, xmax, ymin, ymax, zmin, zmax) = extract_intersection.find_boundary(data_points)
print(xmin, xmax, ymin, ymax, zmin, zmax)
planes = np.linspace(zmin + 1, zmax - 1, n_slices - 2)
for index, plane_h in enumerate(planes):
#slice_planes = extract_intersection.filter_data(data_points, plane_h)
#data_slice_planes = extract_intersection.intersection(slice_planes, plane_h)
#save_file(data_slice_planes, max(xmax-xmin, ymax-ymin), max(xmax-xmin, ymax-ymin), save_path + "slice_" + str(index).zfill(3) + ".svg", xmin, ymin)
data_slice_planes = get_slice_data(plane_h)
'''
# get cpu number for parallel processing
agents = psutil.cpu_count()
#agents = multiprocessing.cpu_count()
#agents = 8
print("Using {0} cores to perfrom parallel processing... \n".format(int(agents)))
# Create a pool of processes. By default, one is created for each CPU in the machine.
# extract the bouding box for each image in file list
with closing(Pool(processes = agents)) as pool:
result_list = pool.map(get_slice_data, planes)
pool.terminate()
'''
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", dest = "path", required = True, type = str, help = "path to stl file")
ap.add_argument("-f", "--filename", dest = "filename", required = True, type = str, help = "model file name, in obj format")
ap.add_argument("-o", "--output_path", dest = "output_path", type = str, required = False, help = "result path")
ap.add_argument('-n', '--num_slices', dest = "num_slices", required = False, type = int, default = 100, help = 'Number of slices')
args = vars(ap.parse_args())
#path to model file
file_path = args["path"]
file_name = args['filename']
#model file full path
model_file = file_path + file_name
# output input file info
if os.path.isfile(model_file):
print("Converting file format for 3D point cloud model {}...\n".format(model_file))
else:
print("File not exist")
sys.exit()
#model_name_base = os.path.splitext(model_file)[0]
abs_path = os.path.abspath(model_file)
filename, file_extension = os.path.splitext(abs_path)
base_name = os.path.splitext(os.path.basename(filename))[0]
print(base_name)
# output path
result_path = args["output_path"] if args["output_path"] is not None else os.getcwd()
result_path = os.path.join(result_path, '')
# result path
print("results_folder: {}\n".format(result_path))
#create result file folder
mkpath = os.path.dirname(result_path) +'/slices'
mkdir(mkpath)
save_path = mkpath + '/'
stl_mesh = OBJ2STL(file_path, file_name, base_name, result_path)
stl_model_file = result_path + base_name + '.stl'
print("stl_model_file: {}\n".format(stl_model_file))
slice_model(stl_model_file, args['num_slices'], save_path)