Skip to content

Commit 84cfbb2

Browse files
committed
start to support parallel computing
1 parent c923a6e commit 84cfbb2

File tree

1 file changed

+81
-39
lines changed

1 file changed

+81
-39
lines changed

rofunc/devices/zed/export.py

Lines changed: 81 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import enum
2+
import multiprocessing
3+
import os
24
import sys
3-
from pathlib import Path
45

56
import cv2
67
import numpy as np
@@ -20,47 +21,56 @@ def progress_bar(percent_done, bar_length=50):
2021
sys.stdout.flush()
2122

2223

23-
def export(filepath):
24-
if not sys.argv or len(sys.argv) != 4:
25-
sys.stdout.write("Usage: \n\n")
26-
sys.stdout.write(" ZED_SVO_Export A B C \n\n")
27-
sys.stdout.write("Please use the following parameters from the command line:\n")
28-
sys.stdout.write(" A - SVO file path (input) : \"path/to/file.svo\"\n")
29-
sys.stdout.write(" B - AVI file path (output) or image sequence folder(output) :\n")
30-
sys.stdout.write(" \"path/to/output/file.avi\" or \"path/to/output/folder\"\n")
31-
sys.stdout.write(" C - Export mode: 0=Export LEFT+RIGHT AVI.\n")
32-
sys.stdout.write(" 1=Export LEFT+DEPTH_VIEW AVI.\n")
33-
sys.stdout.write(" 2=Export LEFT+RIGHT image sequence.\n")
34-
sys.stdout.write(" 3=Export LEFT+DEPTH_VIEW image sequence.\n")
35-
sys.stdout.write(" 4=Export LEFT+DEPTH_16Bit image sequence.\n")
36-
sys.stdout.write(" A and B need to end with '/' or '\\'\n\n")
37-
sys.stdout.write("Examples: \n")
38-
sys.stdout.write(" (AVI LEFT+RIGHT): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/file.avi\" 0\n")
39-
sys.stdout.write(" (AVI LEFT+DEPTH): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/file.avi\" 1\n")
40-
sys.stdout.write(" (SEQUENCE LEFT+RIGHT): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\" 2\n")
41-
sys.stdout.write(" (SEQUENCE LEFT+DEPTH): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\" 3\n")
42-
sys.stdout.write(" (SEQUENCE LEFT+DEPTH_16Bit): ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\""
43-
" 4\n")
44-
exit()
24+
def export(filepath, mode=1):
25+
"""
26+
Export the svo file with specific mode.
27+
Args:
28+
filepath: SVO file path (input) : path/to/file.svo
29+
mode: Export mode: 0=Export LEFT+RIGHT AVI.
30+
1=Export LEFT+DEPTH_VIEW AVI.
31+
2=Export LEFT+RIGHT image sequence.
32+
3=Export LEFT+DEPTH_VIEW image sequence.
33+
4=Export LEFT+DEPTH_16Bit image sequence.
34+
35+
Returns:
36+
37+
"""
4538

4639
# Get input parameters
4740
svo_input_path = filepath
48-
output_path = filepath.split('.')
41+
root_path = filepath.split('.')[0]
42+
output_dir = root_path + '_export'
43+
if not os.path.exists(output_dir):
44+
os.mkdir(output_dir)
45+
4946
output_as_video = True
50-
app_type = AppType.LEFT_AND_RIGHT
51-
if sys.argv[3] == "1" or sys.argv[3] == "3":
47+
if mode == 0:
48+
app_type = AppType.LEFT_AND_RIGHT
49+
mode_name = 'left_and_right.avi'
50+
elif mode == 1:
5251
app_type = AppType.LEFT_AND_DEPTH
53-
if sys.argv[3] == "4":
52+
mode_name = 'left_and_depth.avi'
53+
elif mode == 2:
54+
app_type = AppType.LEFT_AND_RIGHT
55+
mode_name = 'left_and_right_img_seq'
56+
elif mode == 3:
57+
app_type = AppType.LEFT_AND_DEPTH
58+
mode_name = 'left_and_depth_img_seq'
59+
elif mode == 4:
5460
app_type = AppType.LEFT_AND_DEPTH_16
61+
mode_name = 'left_and_depth_16_img_seq'
62+
else:
63+
raise Exception('Wrong mode index, should be one of [0, 1, 2, 3, 4]')
64+
65+
output_path = os.path.join(output_dir, mode_name)
5566

5667
# Check if exporting to AVI or SEQUENCE
57-
if sys.argv[3] != "0" and sys.argv[3] != "1":
68+
if mode != 0 and mode != 1:
5869
output_as_video = False
5970

60-
if not output_as_video and not output_path.is_dir():
61-
sys.stdout.write("Input directory doesn't exist. Check permissions or create it.\n",
62-
output_path, "\n")
63-
exit()
71+
if not output_as_video:
72+
if not os.path.exists(output_path):
73+
os.mkdir(output_path)
6474

6575
# Specify SVO path parameter
6676
init_params = sl.InitParameters()
@@ -110,7 +120,7 @@ def export(filepath):
110120
rt_param.sensing_mode = sl.SENSING_MODE.FILL
111121

112122
# Start SVO conversion to AVI/SEQUENCE
113-
sys.stdout.write("Converting SVO... Use Ctrl-C to interrupt conversion.\n")
123+
sys.stdout.write("Converting SVO to {}. Use Ctrl-C to interrupt conversion.\n".format(mode_name))
114124

115125
nb_frames = zed.get_svo_number_of_frames()
116126

@@ -142,9 +152,10 @@ def export(filepath):
142152
video_writer.write(ocv_image_sbs_rgb)
143153
else:
144154
# Generate file names
145-
filename1 = output_path / ("left%s.png" % str(svo_position).zfill(6))
146-
filename2 = output_path / (("right%s.png" if app_type == AppType.LEFT_AND_RIGHT
147-
else "depth%s.png") % str(svo_position).zfill(6))
155+
filename1 = os.path.join(output_path, "left_{}.png".format(str(svo_position).zfill(6)))
156+
filename2 = os.path.join(output_path, "right_{}.png".format(
157+
str(svo_position).zfill(6))) if app_type == AppType.LEFT_AND_RIGHT \
158+
else os.path.join(output_path, "depth_{}.png".format( str(svo_position).zfill(6)))
148159

149160
# Save Left images
150161
cv2.imwrite(str(filename1), left_image.get_data())
@@ -163,6 +174,9 @@ def export(filepath):
163174
if svo_position >= (nb_frames - 1): # End of SVO
164175
sys.stdout.write("\nSVO end has been reached. Exiting now.\n")
165176
break
177+
elif zed.grab(rt_param) == sl.ERROR_CODE.END_OF_SVOFILE_REACHED:
178+
sys.stdout.write("\nSVO end has been reached. Exiting now.\n")
179+
break
166180

167181
if output_as_video:
168182
# Close the video writer
@@ -172,7 +186,35 @@ def export(filepath):
172186
return 0
173187

174188

175-
if __name__ == "__main__":
176-
import rofunc as rf
189+
def parallel(z):
190+
return export(z[0], z[1])
191+
192+
193+
def export_batch(filedir, all_mode=True, mode=None, core_num=10):
194+
files = os.listdir(filedir)
195+
pool = multiprocessing.Pool(core_num)
196+
197+
if all_mode and mode is None:
198+
filepaths = []
199+
for mode in range(5):
200+
for file in files:
201+
if file[-3:] == 'svo':
202+
filepaths.append((os.path.join(filedir, file), mode))
203+
pool.map(parallel, filepaths)
204+
elif mode is not None:
205+
filepaths = []
206+
for file in files:
207+
if file[-3:] == 'svo':
208+
filepaths.append((os.path.join(filedir, file), mode))
209+
pool.map(parallel, filepaths)
210+
else:
211+
raise Exception('Wrong parameters')
177212

178-
rf.zed.export('/home/ubuntu/Data/06_24/Video/20220624_1649/38709363.svo')
213+
pool.close()
214+
pool.join()
215+
216+
217+
if __name__ == "__main__":
218+
# for i in range(5):
219+
# export('/home/ubuntu/Data/06_24/Video/20220624_1649/38709363.svo', 2)
220+
export_batch('/home/ubuntu/Data/06_24/Video/20220624_1649', mode=1, core_num=20)

0 commit comments

Comments
 (0)