-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_model_measure.py
executable file
·339 lines (177 loc) · 9.28 KB
/
batch_model_measure.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
"""
Version: 1.5
Summary: Compute traits from a 3D model
Author: suxing liu
Author-email: suxingliu@gmail.com
USAGE:
python3 batch_model_measure.py -i /input_path/
INPUT:
3D Sorghum model
OUTPUT:
3D Sorghum model, aligned along Z direction in 3D coordinates
Excel file contains traits computation results
PARAMETERS:
("-i", "--input", dest = "input", required = True, type = str, help = "full path to 3D model file")
("-o", "--output_path", dest = "output_path", required = False, type = str, help = "result path")
("--n_plane", dest = "n_plane", type = int, required = False, default = 5, help = "Number of planes to segment the 3d model along Z direction")
("--slicing_ratio", dest = "slicing_ratio", type = float, required = False, default = 0.10, help = "ratio of slicing the model from the bottom")
("--adjustment", dest = "adjustment", type = int, required = False, default = 0, help = "adjust model manually or automatically, 0: automatically, 1: manually")
("--visualize", dest = "visualize", type = int, required = False, default = 0, help = "Display model or not, default as no due to headless display in cluster")
"""
import subprocess, os, glob
import sys
import argparse
import pathlib
# execute script inside program
def execute_script(cmd_line):
try:
#print(cmd_line)
#os.system(cmd_line)
process = subprocess.getoutput(cmd_line)
print(process)
#process = subprocess.Popen(cmd_line, shell = True, stdout = subprocess.PIPE)
#process.wait()
#print (process.communicate())
except OSError:
print("Failed ...!\n")
# execute pipeline scripts in order
def model_analysis_pipeline(file_path, filename, basename, result_path):
# step 2 python3 /opt/code/model_measurement.py -i ~/example/result/test_aligned.ply -o ~/example/result/ --n_plane 5
print("Step 2: Compute 3D traits from the aligned 3D point cloud model...\n")
traits_computation = "python3 model_measurement.py -i " + result_path + basename + "_cleaned_aligned.ply " + " -o " + result_path + " --n_plane " + str(n_plane)
print(traits_computation)
execute_script(traits_computation)
'''
# parallel processing of folders for local test only
def parallel_folders(subfolder_path):
folder_name = os.path.basename(subfolder_path)
subfolder_path = os.path.join(subfolder_path, '')
m_file = subfolder_path + folder_name + '.' + ext
print("Processing 3d model point cloud file '{}'...\n".format(m_file))
(filename, basename) = get_fname(m_file)
#print("Processing 3d model point cloud file '{}'...\n".format(filename))
#print("Processing 3d model point cloud file basename '{}'...\n".format(basename))
model_analysis_pipeline(subfolder_path, filename, basename, subfolder_path)
'''
# get file information from the file path using os for python 2.7
def get_fname(file_full_path):
abs_path = os.path.abspath(file_full_path)
filename= os.path.basename(abs_path)
base_name = os.path.splitext(os.path.basename(filename))[0]
return filename, base_name
# get sub folders from a inout path for local test only
def fast_scandir(dirname):
subfolders= sorted([f.path for f in os.scandir(dirname) if f.is_dir()])
return subfolders
# get file information from the file path using pathon 3
def get_file_info(file_full_path):
p = pathlib.Path(file_full_path)
filename = p.name
basename = p.stem
file_path = p.parent.absolute()
file_path = os.path.join(file_path, '')
return file_path, filename, basename
if __name__ == '__main__':
# construct the argument and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", dest = "input", type = str, required = True, help = "full path to 3D model file")
ap.add_argument("-o", "--output_path", dest = "output_path", type = str, required = False, help = "result path")
ap.add_argument("--std_ratio", required = False, type = float, default = 5.0, help = "outlier remove ratio, small number = aggresive")
ap.add_argument( "--n_plane", dest = "n_plane", type = int, required = False, default = 5, help = "Number of planes to segment the 3d model along Z direction")
ap.add_argument( "--slicing_ratio", dest = "slicing_ratio", type = float, required = False, default = 0.10, help = "ratio of slicing the model from the bottom")
ap.add_argument( "--adjustment", dest = "adjustment", type = int, required = False, default = 0, help = "adjust model manually or automatically, 0: automatically, 1: manually")
args = vars(ap.parse_args())
# get input file information
'''
if os.path.isfile(args["input"]):
# get input file path, name, base name.
(file_path, filename, basename) = get_file_info(args["input"])
print("Input 3d model point cloud file path: {}, filename: {}\n".format(file_path, filename))
# result path
result_path = args["output_path"] if args["output_path"] is not None else file_path
result_path = os.path.join(result_path, '')
# print out result path
print ("Output path: {}\n".format(result_path))
std_ratio = args["std_ratio"]
# number of slices for cross-section
n_plane = args['n_plane']
slicing_ratio = args["slicing_ratio"]
adjustment = args["adjustment"]
# start pipeline
########################################################################################
model_analysis_pipeline(file_path, filename, basename, result_path)
else:
# exception handle
print("The input file is missing or not readable!\n")
print("Exiting the program...")
sys.exit(0)
'''
'''
# path to model file
file_path = args["path"]
# setting path to model file
file_path = args["path"]
ext = args['filetype'].split(',') if 'filetype' in args else []
patterns = [os.path.join(file_path, f"*.{p}") for p in ext]
model_List = [f for fs in [glob.glob(pattern) for pattern in patterns] for f in fs]
# load input model files
if len(model_List) > 0:
print("Model files in input folder: '{}'\n".format(model_List))
else:
print("3D model file does not exist")
sys.exit()
'''
'''
#loop execute
for model_id, model_file in enumerate(model_List):
print("Processing 3d model point cloud file '{}'...\n".format(model_file))
(filename, basename) = get_fname(model_file)
#print("Processing 3d model point cloud file {} {}\n".format(filename, basename))
model_analysis_pipeline(file_path, filename, basename, result_path)
'''
'''
######################################################################################
# docker version
folder_name = os.path.basename(file_path[:-1])
file_path = os.path.join(file_path, '')
m_file = file_path + folder_name + '.' + ext
print("Processing 3d model point cloud file '{}'...\n".format(m_file))
(filename, basename) = get_fname(m_file)
print("Processing 3d model point cloud file {} {}\n".format(filename, basename))
model_analysis_pipeline(file_path, filename, basename, result_path)
'''
####################################################################################
# local test loop version
# get input file path, name, base name.
#(file_path, filename, basename) = get_file_info(args["input"])
std_ratio = args["std_ratio"]
# number of slices for cross-section
n_plane = args['n_plane']
slicing_ratio = args["slicing_ratio"]
adjustment = args["adjustment"]
file_path = args["input"]
subfolders = fast_scandir(file_path)
for subfolder_id, subfolder_path in enumerate(subfolders):
folder_name = os.path.basename(subfolder_path)
subfolder_path = os.path.join(subfolder_path, '')
m_file = subfolder_path + folder_name + '.ply'
#print("Processing 3d model point cloud file '{}'...\n".format(m_file))
(filename, basename) = get_fname(m_file)
print("Current sub folder path '{}'...\n".format(subfolder_path))
print("Processing 3d model point cloud file '{}'...\n".format(filename))
print("Processing 3d model point cloud file basename '{}'...\n".format(basename))
model_analysis_pipeline(subfolder_path, filename, basename, subfolder_path)
########################################################################################
# local test parellel version
'''
subfolders = fast_scandir(file_path)
print(len(subfolders))
# get cpu number for parallel processing
agents = psutil.cpu_count() - 4
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(parellel_folders, subfolders)
pool.terminate()
'''