77import cv2
88import os
99import argparse
10+ import queue
11+ import threading
1012
1113from utils import *
1214from pipeline import initialize_pipeline
1315
16+ SAVE_QUEUE_MAXSIZE = 200 # max frames buffered for saving; when full, capture blocks until the saver catches up
17+
18+
19+ def _saver_worker (save_queue ):
20+ while True :
21+ try :
22+ item = save_queue .get (timeout = 0.5 )
23+ except queue .Empty :
24+ continue
25+ if item is None :
26+ save_queue .task_done ()
27+ break
28+ output_folder , name , timestamp , frame , do_npy , do_png = item
29+ try :
30+ if do_npy : np .save (f'{ output_folder } /{ name } _{ timestamp } .npy' , frame )
31+ if do_png : cv2 .imwrite (f'{ output_folder } /{ name } _{ timestamp } .png' , frame )
32+ finally :
33+ del frame
34+ save_queue .task_done ()
35+
1436print (f"[System] DepthAI version: { dai .__version__ } " )
1537
1638script_dir = os .path .dirname (os .path .abspath (__file__ ))
@@ -87,6 +109,9 @@ def main(args):
87109 save_npy = args .npy or not args .png
88110 save_png = args .png
89111 png_streams = ('left' , 'right' , 'rgb' )
112+ save_queue = queue .Queue (maxsize = SAVE_QUEUE_MAXSIZE )
113+ saver_thread = threading .Thread (target = _saver_worker , args = (save_queue ,), daemon = False )
114+ saver_thread .start ()
90115 if no_streams :
91116 cv2 .namedWindow (CONTROL_WINDOW_NAME )
92117
@@ -142,10 +167,12 @@ def main(args):
142167 if name in ['left' , 'right' ]:
143168 if len (cvFrame .shape ) == 3 :
144169 cvFrame = cv2 .cvtColor (cvFrame , cv2 .COLOR_BGR2GRAY )
145- if save_npy :
146- np .save (f'{ output_folder } /{ name } _{ timestamp } .npy' , cvFrame )
147- if save_png and name in png_streams :
148- cv2 .imwrite (f'{ output_folder } /{ name } _{ timestamp } .png' , cvFrame )
170+ do_png = save_png and name in png_streams
171+ if save_npy or do_png :
172+ save_queue .put (
173+ (output_folder , name , timestamp , cvFrame .copy (), save_npy , do_png ),
174+ block = True
175+ )
149176 num_captures += 1
150177
151178 if not no_streams :
@@ -167,10 +194,12 @@ def main(args):
167194 if name in ['left' , 'right' ]:
168195 if len (cvFrame .shape ) == 3 :
169196 cvFrame = cv2 .cvtColor (cvFrame , cv2 .COLOR_BGR2GRAY )
170- if save_npy :
171- np .save (f'{ output_folder } /{ name } _{ timestamp } .npy' , cvFrame )
172- if save_png and name in png_streams :
173- cv2 .imwrite (f'{ output_folder } /{ name } _{ timestamp } .png' , cvFrame )
197+ do_png = save_png and name in png_streams
198+ if save_npy or do_png :
199+ save_queue .put (
200+ (output_folder , name , timestamp , cvFrame .copy (), save_npy , do_png ),
201+ block = True
202+ )
174203 num_captures += 1
175204
176205 if not no_streams :
@@ -199,6 +228,11 @@ def main(args):
199228 stop_capture (start_time , num_captures , streams , pipeline )
200229 break
201230
231+ save_queue .put (None )
232+ saver_thread .join (timeout = 60 )
233+ if saver_thread .is_alive ():
234+ print ("[Capture] Warning: saver thread did not finish in time" )
235+
202236
203237if __name__ == "__main__" :
204238 args = parse_arguments (root_path )
0 commit comments