Skip to content

Commit 05fb0f8

Browse files
committed
Pass data via arguments instead of global like variables - 2
1 parent 415bf31 commit 05fb0f8

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

darknet_video.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,23 @@ def video_capture(stop_flag, input_path, raw_frame_queue, preprocessed_frame_que
122122
cap.release()
123123

124124

125-
def inference(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue):
125+
def inference(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue,
126+
network, class_names, threshold):
126127
while not stop_flag.is_set():
127128
darknet_image = preprocessed_frame_queue.get()
128129
prev_time = time.time()
129-
detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh)
130-
detections_queue.put(detections)
130+
detections = darknet.detect_image(network, class_names, darknet_image, thresh=threshold)
131131
fps = 1 / (time.time() - prev_time)
132+
detections_queue.put(detections)
132133
fps_queue.put(int(fps))
133134
print("FPS: {:.2f}".format(fps))
134135
darknet.print_detections(detections, args.ext_output)
135136
darknet.free_image(darknet_image)
136137

137138

138-
def drawing(stop_flag, input_video_fps, raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, vid_h, vid_w):
139+
def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid_w):
139140
random.seed(3) # deterministic bbox colors
141+
raw_frame_queue, preprocessed_frame_queue, detections_queue, fps_queue = queues
140142
video = set_saved_video(args.out_filename, (vid_w, vid_h), input_video_fps)
141143
fps = 1
142144
while not stop_flag.is_set():
@@ -184,9 +186,9 @@ def drawing(stop_flag, input_video_fps, raw_frame_queue, detections_queue, fps_q
184186
cap.release()
185187
del cap
186188

187-
stop_flag = threading.Event()
188189
ExecUnit = threading.Thread
189190
Queue = queue.Queue
191+
stop_flag = threading.Event()
190192

191193
raw_frame_queue = Queue()
192194
preprocessed_frame_queue = Queue(maxsize=1)
@@ -196,8 +198,10 @@ def drawing(stop_flag, input_video_fps, raw_frame_queue, detections_queue, fps_q
196198
exec_units = (
197199
ExecUnit(target=video_capture, args=(stop_flag, input_path, raw_frame_queue, preprocessed_frame_queue,
198200
darknet_height, darknet_width)),
199-
ExecUnit(target=inference, args=(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue)),
200-
ExecUnit(target=drawing, args=(stop_flag, video_fps, raw_frame_queue, detections_queue, fps_queue,
201+
ExecUnit(target=inference, args=(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue,
202+
network, class_names, args.thresh)),
203+
ExecUnit(target=drawing, args=(stop_flag, video_fps,
204+
(raw_frame_queue, preprocessed_frame_queue, detections_queue, fps_queue),
201205
darknet_height, darknet_width, video_height, video_width)),
202206
)
203207
for exec_unit in exec_units:

0 commit comments

Comments
 (0)