From 03e526a9d53f5bccb6200bbc5f6db514a93c8093 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Thu, 16 Jun 2022 22:43:05 +0300 Subject: [PATCH 01/12] Minor: float fps (useful for small values - e.g. CPU) --- darknet_video.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 darknet_video.py diff --git a/darknet_video.py b/darknet_video.py old mode 100644 new mode 100755 index 5e7036203a7..686c8397e5e --- a/darknet_video.py +++ b/darknet_video.py @@ -126,9 +126,9 @@ def inference(darknet_image_queue, detections_queue, fps_queue): prev_time = time.time() detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh) detections_queue.put(detections) - fps = int(1/(time.time() - prev_time)) - fps_queue.put(fps) - print("FPS: {}".format(fps)) + fps = 1 / (time.time() - prev_time) + fps_queue.put(int(fps)) + print("FPS: {:.2f}".format(fps)) darknet.print_detections(detections, args.ext_output) darknet.free_image(darknet_image) cap.release() From 46a88ac286a3956b2e9c04ea42faa2f600a1c519 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 00:13:04 +0300 Subject: [PATCH 02/12] Refactor import --- darknet_video.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) mode change 100755 => 100644 darknet_video.py diff --git a/darknet_video.py b/darknet_video.py old mode 100755 new mode 100644 index 686c8397e5e..4d113f67b7a --- a/darknet_video.py +++ b/darknet_video.py @@ -6,7 +6,7 @@ import darknet import argparse from threading import Thread, enumerate -from queue import Queue +import queue def parser(): @@ -159,10 +159,10 @@ def drawing(frame_queue, detections_queue, fps_queue): if __name__ == '__main__': - frame_queue = Queue() - darknet_image_queue = Queue(maxsize=1) - detections_queue = Queue(maxsize=1) - fps_queue = Queue(maxsize=1) + frame_queue = queue.Queue() + darknet_image_queue = queue.Queue(maxsize=1) + detections_queue = queue.Queue(maxsize=1) + fps_queue = queue.Queue(maxsize=1) args = parser() check_arguments_errors(args) From ea2db1c582b6ffb8dc598815517a3b6e9754f348 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 00:30:13 +0300 Subject: [PATCH 03/12] Fix multiple code readability issues --- darknet_video.py | 76 +++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/darknet_video.py b/darknet_video.py index 4d113f67b7a..0ecd24e51ae 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -1,11 +1,10 @@ -from ctypes import * import random import os import cv2 import time import darknet import argparse -from threading import Thread, enumerate +import threading import queue @@ -64,10 +63,10 @@ def convert2relative(bbox): """ YOLO format use relative coordinates for annotation """ - x, y, w, h = bbox - _height = darknet_height - _width = darknet_width - return x/_width, y/_height, w/_width, h/_height + x, y, w, h = bbox + _height = darknet_height + _width = darknet_width + return x / _width, y / _height, w / _width, h / _height def convert2original(image, bbox): @@ -75,10 +74,10 @@ def convert2original(image, bbox): image_h, image_w, __ = image.shape - orig_x = int(x * image_w) - orig_y = int(y * image_h) - orig_width = int(w * image_w) - orig_height = int(h * image_h) + orig_x = int(x * image_w) + orig_y = int(y * image_h) + orig_width = int(w * image_w) + orig_height = int(h * image_h) bbox_converted = (orig_x, orig_y, orig_width, orig_height) @@ -90,22 +89,26 @@ def convert4cropping(image, bbox): image_h, image_w, __ = image.shape - orig_left = int((x - w / 2.) * image_w) - orig_right = int((x + w / 2.) * image_w) - orig_top = int((y - h / 2.) * image_h) - orig_bottom = int((y + h / 2.) * image_h) + orig_left = int((x - w / 2.) * image_w) + orig_right = int((x + w / 2.) * image_w) + orig_top = int((y - h / 2.) * image_h) + orig_bottom = int((y + h / 2.) * image_h) - if (orig_left < 0): orig_left = 0 - if (orig_right > image_w - 1): orig_right = image_w - 1 - if (orig_top < 0): orig_top = 0 - if (orig_bottom > image_h - 1): orig_bottom = image_h - 1 + if orig_left < 0: + orig_left = 0 + if orig_right > image_w - 1: + orig_right = image_w - 1 + if orig_top < 0: + orig_top = 0 + if orig_bottom > image_h - 1: + orig_bottom = image_h - 1 bbox_cropping = (orig_left, orig_top, orig_right, orig_bottom) return bbox_cropping -def video_capture(frame_queue, darknet_image_queue): +def video_capture(raw_frame_queue, preprocessed_frame_queue): while cap.isOpened(): ret, frame = cap.read() if not ret: @@ -113,16 +116,16 @@ def video_capture(frame_queue, darknet_image_queue): frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (darknet_width, darknet_height), interpolation=cv2.INTER_LINEAR) - frame_queue.put(frame) + raw_frame_queue.put(frame) img_for_detect = darknet.make_image(darknet_width, darknet_height, 3) darknet.copy_image_from_bytes(img_for_detect, frame_resized.tobytes()) - darknet_image_queue.put(img_for_detect) + preprocessed_frame_queue.put(img_for_detect) cap.release() -def inference(darknet_image_queue, detections_queue, fps_queue): +def inference(preprocessed_frame_queue, detections_queue, fps_queue): while cap.isOpened(): - darknet_image = darknet_image_queue.get() + darknet_image = preprocessed_frame_queue.get() prev_time = time.time() detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh) detections_queue.put(detections) @@ -134,11 +137,11 @@ def inference(darknet_image_queue, detections_queue, fps_queue): cap.release() -def drawing(frame_queue, detections_queue, fps_queue): +def drawing(raw_frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (video_width, video_height)) while cap.isOpened(): - frame = frame_queue.get() + frame = raw_frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() detections_adjusted = [] @@ -148,7 +151,7 @@ def drawing(frame_queue, detections_queue, fps_queue): detections_adjusted.append((str(label), confidence, bbox_adjusted)) image = darknet.draw_boxes(detections_adjusted, frame, class_colors) if not args.dont_show: - cv2.imshow('Inference', image) + cv2.imshow("Inference", image) if args.out_filename is not None: video.write(image) if cv2.waitKey(fps) == 27: @@ -158,26 +161,25 @@ def drawing(frame_queue, detections_queue, fps_queue): cv2.destroyAllWindows() -if __name__ == '__main__': - frame_queue = queue.Queue() - darknet_image_queue = queue.Queue(maxsize=1) +if __name__ == "__main__": + raw_frame_queue = queue.Queue() + preprocessed_frame_queue = queue.Queue(maxsize=1) detections_queue = queue.Queue(maxsize=1) fps_queue = queue.Queue(maxsize=1) args = parser() check_arguments_errors(args) network, class_names, class_colors = darknet.load_network( - args.config_file, - args.data_file, - args.weights, - batch_size=1 - ) + args.config_file, + args.data_file, + args.weights, + batch_size=1) darknet_width = darknet.network_width(network) darknet_height = darknet.network_height(network) input_path = str2int(args.input) cap = cv2.VideoCapture(input_path) video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) - Thread(target=video_capture, args=(frame_queue, darknet_image_queue)).start() - Thread(target=inference, args=(darknet_image_queue, detections_queue, fps_queue)).start() - Thread(target=drawing, args=(frame_queue, detections_queue, fps_queue)).start() + threading.Thread(target=video_capture, args=(raw_frame_queue, preprocessed_frame_queue)).start() + threading.Thread(target=inference, args=(preprocessed_frame_queue, detections_queue, fps_queue)).start() + threading.Thread(target=drawing, args=(raw_frame_queue, detections_queue, fps_queue)).start() From b4532080ddd910ae1d1d558c711ed6c7ab1f8f4f Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 00:39:15 +0300 Subject: [PATCH 04/12] Better synchoronize threads (when user interrupts video) --- darknet_video.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/darknet_video.py b/darknet_video.py index 0ecd24e51ae..747976e925e 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -140,6 +140,7 @@ def inference(preprocessed_frame_queue, detections_queue, fps_queue): def drawing(raw_frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (video_width, video_height)) + fps = 1 while cap.isOpened(): frame = raw_frame_queue.get() detections = detections_queue.get() @@ -159,6 +160,12 @@ def drawing(raw_frame_queue, detections_queue, fps_queue): cap.release() video.release() cv2.destroyAllWindows() + timeout = 1 / (fps if fps > 0 else 0.5) + for q in (detections_queue, fps_queue, preprocessed_frame_queue): + try: + q.get(block=True, timeout=timeout) + except queue.Empty: + pass if __name__ == "__main__": @@ -180,6 +187,7 @@ def drawing(raw_frame_queue, detections_queue, fps_queue): cap = cv2.VideoCapture(input_path) video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + threading.Thread(target=video_capture, args=(raw_frame_queue, preprocessed_frame_queue)).start() threading.Thread(target=inference, args=(preprocessed_frame_queue, detections_queue, fps_queue)).start() threading.Thread(target=drawing, args=(raw_frame_queue, detections_queue, fps_queue)).start() From b6b86c44376f3a2c8428b62ebb7b553c2c72a6e8 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 00:47:02 +0300 Subject: [PATCH 05/12] Better thread objects handling --- darknet_video.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/darknet_video.py b/darknet_video.py index 747976e925e..ca4e363b491 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -188,6 +188,15 @@ def drawing(raw_frame_queue, detections_queue, fps_queue): video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) - threading.Thread(target=video_capture, args=(raw_frame_queue, preprocessed_frame_queue)).start() - threading.Thread(target=inference, args=(preprocessed_frame_queue, detections_queue, fps_queue)).start() - threading.Thread(target=drawing, args=(raw_frame_queue, detections_queue, fps_queue)).start() + exec_units = ( + threading.Thread(target=video_capture, args=(raw_frame_queue, preprocessed_frame_queue)), + threading.Thread(target=inference, args=(preprocessed_frame_queue, detections_queue, fps_queue)), + threading.Thread(target=drawing, args=(raw_frame_queue, detections_queue, fps_queue)), + ) + for exec_unit in exec_units: + exec_unit.start() + for exec_unit in exec_units: + exec_unit.join() + + print("\nDone.") + From 29c584e0867fd0ed5df11974bcacec1b5aeb1fab Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 01:07:03 +0300 Subject: [PATCH 06/12] Pass data via arguments instead of global like variables - 0 --- darknet_video.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/darknet_video.py b/darknet_video.py index ca4e363b491..0787cc0087a 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -59,18 +59,16 @@ def set_saved_video(input_video, output_video, size): return video -def convert2relative(bbox): +def convert2relative(bbox, preproc_h, preproc_w): """ YOLO format use relative coordinates for annotation """ x, y, w, h = bbox - _height = darknet_height - _width = darknet_width - return x / _width, y / _height, w / _width, h / _height + return x / preproc_w, y / preproc_h, w / preproc_w, h / preproc_h -def convert2original(image, bbox): - x, y, w, h = convert2relative(bbox) +def convert2original(image, bbox, preproc_h, preproc_w): + x, y, w, h = convert2relative(bbox, preproc_h, preproc_w) image_h, image_w, __ = image.shape @@ -84,8 +82,9 @@ def convert2original(image, bbox): return bbox_converted -def convert4cropping(image, bbox): - x, y, w, h = convert2relative(bbox) +# @TODO - cfati: Unused +def convert4cropping(image, bbox, preproc_h, preproc_w): + x, y, w, h = convert2relative(bbox, preproc_h, preproc_w) image_h, image_w, __ = image.shape @@ -108,16 +107,16 @@ def convert4cropping(image, bbox): return bbox_cropping -def video_capture(raw_frame_queue, preprocessed_frame_queue): +def video_capture(raw_frame_queue, preprocessed_frame_queue, preproc_h, preproc_w): while cap.isOpened(): ret, frame = cap.read() if not ret: break frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - frame_resized = cv2.resize(frame_rgb, (darknet_width, darknet_height), + frame_resized = cv2.resize(frame_rgb, (preproc_w, preproc_h), interpolation=cv2.INTER_LINEAR) raw_frame_queue.put(frame) - img_for_detect = darknet.make_image(darknet_width, darknet_height, 3) + img_for_detect = darknet.make_image(preproc_w, preproc_h, 3) darknet.copy_image_from_bytes(img_for_detect, frame_resized.tobytes()) preprocessed_frame_queue.put(img_for_detect) cap.release() @@ -137,9 +136,9 @@ def inference(preprocessed_frame_queue, detections_queue, fps_queue): cap.release() -def drawing(raw_frame_queue, detections_queue, fps_queue): +def drawing(raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, vid_h, vid_w): random.seed(3) # deterministic bbox colors - video = set_saved_video(cap, args.out_filename, (video_width, video_height)) + video = set_saved_video(cap, args.out_filename, (vid_w, vid_h)) fps = 1 while cap.isOpened(): frame = raw_frame_queue.get() @@ -148,7 +147,7 @@ def drawing(raw_frame_queue, detections_queue, fps_queue): detections_adjusted = [] if frame is not None: for label, confidence, bbox in detections: - bbox_adjusted = convert2original(frame, bbox) + bbox_adjusted = convert2original(frame, bbox, preproc_h, preproc_w) detections_adjusted.append((str(label), confidence, bbox_adjusted)) image = darknet.draw_boxes(detections_adjusted, frame, class_colors) if not args.dont_show: @@ -189,9 +188,11 @@ def drawing(raw_frame_queue, detections_queue, fps_queue): video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) exec_units = ( - threading.Thread(target=video_capture, args=(raw_frame_queue, preprocessed_frame_queue)), + threading.Thread(target=video_capture, args=(raw_frame_queue, preprocessed_frame_queue, + darknet_height, darknet_width)), threading.Thread(target=inference, args=(preprocessed_frame_queue, detections_queue, fps_queue)), - threading.Thread(target=drawing, args=(raw_frame_queue, detections_queue, fps_queue)), + threading.Thread(target=drawing, args=(raw_frame_queue, detections_queue, fps_queue, + darknet_height, darknet_width, video_height, video_width)), ) for exec_unit in exec_units: exec_unit.start() From 6af33f1e7ed238382f45ebfa3968006791398a60 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 01:21:16 +0300 Subject: [PATCH 07/12] Minor code readability fixes --- darknet.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/darknet.py b/darknet.py index ebb0eede210..67d760f3f43 100644 --- a/darknet.py +++ b/darknet.py @@ -9,9 +9,10 @@ """ from ctypes import * -import math import random import os +import cv2 +import numpy as np class BOX(Structure): @@ -36,6 +37,7 @@ class DETECTION(Structure): ("sim", c_float), ("track_id", c_int)] + class DETNUMPAIR(Structure): _fields_ = [("num", c_int), ("dets", POINTER(DETECTION))] @@ -67,10 +69,10 @@ def bbox2points(bbox): to corner points cv2 rectangle """ x, y, w, h = bbox - xmin = int(round(x - (w / 2))) - xmax = int(round(x + (w / 2))) - ymin = int(round(y - (h / 2))) - ymax = int(round(y + (h / 2))) + xmin = round(x - (w / 2)) + xmax = round(x + (w / 2)) + ymin = round(y - (h / 2)) + ymax = round(y + (h / 2)) return xmin, ymin, xmax, ymax @@ -134,6 +136,7 @@ def decode_detection(detections): decoded.append((str(label), confidence, bbox)) return decoded + # https://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/ # Malisiewicz et al. def non_max_suppression_fast(detections, overlap_thresh): @@ -185,6 +188,7 @@ def non_max_suppression_fast(detections, overlap_thresh): # integer data type return [detections[i] for i in pick] + def remove_negatives(detections, class_names, num): """ Remove all classes with 0% confidence within the detection @@ -236,11 +240,12 @@ def detect_image(network, class_names, image, thresh=.5, hier_thresh=.5, nms=.45 lib = CDLL(cwd + "/libdarknet.so", RTLD_GLOBAL) elif os.name == "nt": cwd = os.path.dirname(__file__) - os.environ['PATH'] = cwd + ';' + os.environ['PATH'] + os.environ["PATH"] = os.path.pathsep.join((cwd, os.environ["PATH"])) lib = CDLL("darknet.dll", RTLD_GLOBAL) else: + lib = None # Intellisense print("Unsupported OS") - exit + exit() lib.network_width.argtypes = [c_void_p] lib.network_width.restype = c_int @@ -330,5 +335,6 @@ def detect_image(network, class_names, image, thresh=.5, hier_thresh=.5, nms=.45 network_predict_batch = lib.network_predict_batch network_predict_batch.argtypes = [c_void_p, IMAGE, c_int, c_int, c_int, - c_float, c_float, POINTER(c_int), c_int, c_int] + c_float, c_float, POINTER(c_int), c_int, c_int] network_predict_batch.restype = POINTER(DETNUMPAIR) + From fa4bd8228754447850986adc38c8a95ccdcba3b0 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 01:47:38 +0300 Subject: [PATCH 08/12] CTypes definitions --- darknet.py | 163 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 92 insertions(+), 71 deletions(-) diff --git a/darknet.py b/darknet.py index 67d760f3f43..603ecae92dc 100644 --- a/darknet.py +++ b/darknet.py @@ -8,51 +8,71 @@ Use pip3 instead of pip on some systems to be sure to install modules for python3 """ -from ctypes import * +import ctypes as ct import random import os import cv2 import numpy as np -class BOX(Structure): - _fields_ = [("x", c_float), - ("y", c_float), - ("w", c_float), - ("h", c_float)] +class BOX(ct.Structure): + _fields_ = ( + ("x", ct.c_float), + ("y", ct.c_float), + ("w", ct.c_float), + ("h", ct.c_float), + ) -class DETECTION(Structure): - _fields_ = [("bbox", BOX), - ("classes", c_int), - ("best_class_idx", c_int), - ("prob", POINTER(c_float)), - ("mask", POINTER(c_float)), - ("objectness", c_float), - ("sort_class", c_int), - ("uc", POINTER(c_float)), - ("points", c_int), - ("embeddings", POINTER(c_float)), - ("embedding_size", c_int), - ("sim", c_float), - ("track_id", c_int)] +FloatPtr = ct.POINTER(ct.c_float) +IntPtr = ct.POINTER(ct.c_int) -class DETNUMPAIR(Structure): - _fields_ = [("num", c_int), - ("dets", POINTER(DETECTION))] +class DETECTION(ct.Structure): + _fields_ = ( + ("bbox", BOX), + ("classes", ct.c_int), + ("best_class_idx", ct.c_int), + ("prob", FloatPtr), + ("mask", FloatPtr), + ("objectness", ct.c_float), + ("sort_class", ct.c_int), + ("uc", FloatPtr), + ("points", ct.c_int), + ("embeddings", FloatPtr), + ("embedding_size", ct.c_int), + ("sim", ct.c_float), + ("track_id", ct.c_int), + ) -class IMAGE(Structure): - _fields_ = [("w", c_int), - ("h", c_int), - ("c", c_int), - ("data", POINTER(c_float))] +DETECTIONPtr = ct.POINTER(DETECTION) -class METADATA(Structure): - _fields_ = [("classes", c_int), - ("names", POINTER(c_char_p))] +class DETNUMPAIR(ct.Structure): + _fields_ = ( + ("num", ct.c_int), + ("dets", DETECTIONPtr), + ) + + +DETNUMPAIRPtr = ct.POINTER(DETNUMPAIR) + + +class IMAGE(ct.Structure): + _fields_ = ( + ("w", ct.c_int), + ("h", ct.c_int), + ("c", ct.c_int), + ("data", FloatPtr), + ) + + +class METADATA(ct.Structure): + _fields_ = ( + ("classes", ct.c_int), + ("names", ct.POINTER(ct.c_char_p)), + ) def network_width(net): @@ -222,7 +242,7 @@ def detect_image(network, class_names, image, thresh=.5, hier_thresh=.5, nms=.45 """ Returns a list with highest confidence class and their bbox """ - pnum = pointer(c_int(0)) + pnum = ct.pointer(ct.c_int(0)) predict_image(network, image) detections = get_network_boxes(network, image.w, image.h, thresh, hier_thresh, None, 0, pnum, 0) @@ -237,104 +257,105 @@ def detect_image(network, class_names, image, thresh=.5, hier_thresh=.5, nms=.45 if os.name == "posix": cwd = os.path.dirname(__file__) - lib = CDLL(cwd + "/libdarknet.so", RTLD_GLOBAL) + lib = ct.CDLL(cwd + "/libdarknet.so", ct.RTLD_GLOBAL) elif os.name == "nt": cwd = os.path.dirname(__file__) os.environ["PATH"] = os.path.pathsep.join((cwd, os.environ["PATH"])) - lib = CDLL("darknet.dll", RTLD_GLOBAL) + lib = ct.CDLL("darknet.dll", ct.RTLD_GLOBAL) else: lib = None # Intellisense print("Unsupported OS") exit() -lib.network_width.argtypes = [c_void_p] -lib.network_width.restype = c_int -lib.network_height.argtypes = [c_void_p] -lib.network_height.restype = c_int +lib.network_width.argtypes = (ct.c_void_p,) +lib.network_width.restype = ct.c_int +lib.network_height.argtypes = (ct.c_void_p,) +lib.network_height.restype = ct.c_int copy_image_from_bytes = lib.copy_image_from_bytes -copy_image_from_bytes.argtypes = [IMAGE,c_char_p] +copy_image_from_bytes.argtypes = (IMAGE, ct.c_char_p) predict = lib.network_predict_ptr -predict.argtypes = [c_void_p, POINTER(c_float)] -predict.restype = POINTER(c_float) +predict.argtypes = (ct.c_void_p, FloatPtr) +predict.restype = FloatPtr set_gpu = lib.cuda_set_device init_cpu = lib.init_cpu make_image = lib.make_image -make_image.argtypes = [c_int, c_int, c_int] +make_image.argtypes = (ct.c_int, ct.c_int, ct.c_int) make_image.restype = IMAGE get_network_boxes = lib.get_network_boxes -get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int), c_int] -get_network_boxes.restype = POINTER(DETECTION) +get_network_boxes.argtypes = (ct.c_void_p, ct.c_int, ct.c_int, ct.c_float, ct.c_float, IntPtr, ct.c_int, IntPtr, + ct.c_int) +get_network_boxes.restype = DETECTIONPtr make_network_boxes = lib.make_network_boxes -make_network_boxes.argtypes = [c_void_p] -make_network_boxes.restype = POINTER(DETECTION) +make_network_boxes.argtypes = (ct.c_void_p,) +make_network_boxes.restype = DETECTIONPtr free_detections = lib.free_detections -free_detections.argtypes = [POINTER(DETECTION), c_int] +free_detections.argtypes = (DETECTIONPtr, ct.c_int) free_batch_detections = lib.free_batch_detections -free_batch_detections.argtypes = [POINTER(DETNUMPAIR), c_int] +free_batch_detections.argtypes = (DETNUMPAIRPtr, ct.c_int) free_ptrs = lib.free_ptrs -free_ptrs.argtypes = [POINTER(c_void_p), c_int] +free_ptrs.argtypes = (ct.POINTER(ct.c_void_p), ct.c_int) network_predict = lib.network_predict_ptr -network_predict.argtypes = [c_void_p, POINTER(c_float)] +network_predict.argtypes = (ct.c_void_p, FloatPtr) reset_rnn = lib.reset_rnn -reset_rnn.argtypes = [c_void_p] +reset_rnn.argtypes = (ct.c_void_p,) load_net = lib.load_network -load_net.argtypes = [c_char_p, c_char_p, c_int] -load_net.restype = c_void_p +load_net.argtypes = (ct.c_char_p, ct.c_char_p, ct.c_int) +load_net.restype = ct.c_void_p load_net_custom = lib.load_network_custom -load_net_custom.argtypes = [c_char_p, c_char_p, c_int, c_int] -load_net_custom.restype = c_void_p +load_net_custom.argtypes = (ct.c_char_p, ct.c_char_p, ct.c_int, ct.c_int) +load_net_custom.restype = ct.c_void_p free_network_ptr = lib.free_network_ptr -free_network_ptr.argtypes = [c_void_p] -free_network_ptr.restype = c_void_p +free_network_ptr.argtypes = (ct.c_void_p,) +free_network_ptr.restype = ct.c_void_p do_nms_obj = lib.do_nms_obj -do_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float] +do_nms_obj.argtypes = (DETECTIONPtr, ct.c_int, ct.c_int, ct.c_float) do_nms_sort = lib.do_nms_sort -do_nms_sort.argtypes = [POINTER(DETECTION), c_int, c_int, c_float] +do_nms_sort.argtypes = (DETECTIONPtr, ct.c_int, ct.c_int, ct.c_float) free_image = lib.free_image -free_image.argtypes = [IMAGE] +free_image.argtypes = (IMAGE,) letterbox_image = lib.letterbox_image -letterbox_image.argtypes = [IMAGE, c_int, c_int] +letterbox_image.argtypes = (IMAGE, ct.c_int, ct.c_int) letterbox_image.restype = IMAGE load_meta = lib.get_metadata -lib.get_metadata.argtypes = [c_char_p] +lib.get_metadata.argtypes = (ct.c_char_p,) lib.get_metadata.restype = METADATA load_image = lib.load_image_color -load_image.argtypes = [c_char_p, c_int, c_int] +load_image.argtypes = (ct.c_char_p, ct.c_int, ct.c_int) load_image.restype = IMAGE rgbgr_image = lib.rgbgr_image -rgbgr_image.argtypes = [IMAGE] +rgbgr_image.argtypes = (IMAGE,) predict_image = lib.network_predict_image -predict_image.argtypes = [c_void_p, IMAGE] -predict_image.restype = POINTER(c_float) +predict_image.argtypes = (ct.c_void_p, IMAGE) +predict_image.restype = FloatPtr predict_image_letterbox = lib.network_predict_image_letterbox -predict_image_letterbox.argtypes = [c_void_p, IMAGE] -predict_image_letterbox.restype = POINTER(c_float) +predict_image_letterbox.argtypes = (ct.c_void_p, IMAGE) +predict_image_letterbox.restype = FloatPtr network_predict_batch = lib.network_predict_batch -network_predict_batch.argtypes = [c_void_p, IMAGE, c_int, c_int, c_int, - c_float, c_float, POINTER(c_int), c_int, c_int] -network_predict_batch.restype = POINTER(DETNUMPAIR) +network_predict_batch.argtypes = (ct.c_void_p, IMAGE, ct.c_int, ct.c_int, ct.c_int, + ct.c_float, ct.c_float, IntPtr, ct.c_int, ct.c_int) +network_predict_batch.restype = DETNUMPAIRPtr From e562f4bd275aaae1996a7505182424dd40ded2e1 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 02:27:03 +0300 Subject: [PATCH 09/12] Pass data via arguments instead of global like variables - 1 --- darknet_video.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/darknet_video.py b/darknet_video.py index 0787cc0087a..3cb4748c994 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -52,11 +52,9 @@ def check_arguments_errors(args): raise(ValueError("Invalid video path {}".format(os.path.abspath(args.input)))) -def set_saved_video(input_video, output_video, size): +def set_saved_video(output_video, size, fps): fourcc = cv2.VideoWriter_fourcc(*"MJPG") - fps = int(input_video.get(cv2.CAP_PROP_FPS)) - video = cv2.VideoWriter(output_video, fourcc, fps, size) - return video + return cv2.VideoWriter(output_video, fourcc, fps, size) def convert2relative(bbox, preproc_h, preproc_w): @@ -107,8 +105,9 @@ def convert4cropping(image, bbox, preproc_h, preproc_w): return bbox_cropping -def video_capture(raw_frame_queue, preprocessed_frame_queue, preproc_h, preproc_w): - while cap.isOpened(): +def video_capture(stop_event, input_path, raw_frame_queue, preprocessed_frame_queue, preproc_h, preproc_w): + cap = cv2.VideoCapture(input_path) + while cap.isOpened() and not stop_event.is_set(): ret, frame = cap.read() if not ret: break @@ -119,11 +118,12 @@ def video_capture(raw_frame_queue, preprocessed_frame_queue, preproc_h, preproc_ img_for_detect = darknet.make_image(preproc_w, preproc_h, 3) darknet.copy_image_from_bytes(img_for_detect, frame_resized.tobytes()) preprocessed_frame_queue.put(img_for_detect) + stop_event.set() cap.release() -def inference(preprocessed_frame_queue, detections_queue, fps_queue): - while cap.isOpened(): +def inference(stop_event, preprocessed_frame_queue, detections_queue, fps_queue): + while not stop_event.is_set(): darknet_image = preprocessed_frame_queue.get() prev_time = time.time() detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh) @@ -133,14 +133,13 @@ def inference(preprocessed_frame_queue, detections_queue, fps_queue): print("FPS: {:.2f}".format(fps)) darknet.print_detections(detections, args.ext_output) darknet.free_image(darknet_image) - cap.release() -def drawing(raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, vid_h, vid_w): +def drawing(stop_event, input_video_fps, raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, vid_h, vid_w): random.seed(3) # deterministic bbox colors - video = set_saved_video(cap, args.out_filename, (vid_w, vid_h)) + video = set_saved_video(args.out_filename, (vid_w, vid_h), input_video_fps) fps = 1 - while cap.isOpened(): + while not stop_event.is_set(): frame = raw_frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() @@ -156,7 +155,7 @@ def drawing(raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, video.write(image) if cv2.waitKey(fps) == 27: break - cap.release() + stop_event.set() video.release() cv2.destroyAllWindows() timeout = 1 / (fps if fps > 0 else 0.5) @@ -183,16 +182,22 @@ def drawing(raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, darknet_width = darknet.network_width(network) darknet_height = darknet.network_height(network) input_path = str2int(args.input) - cap = cv2.VideoCapture(input_path) + cap = cv2.VideoCapture(input_path) # Open video twice :( video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + video_fps = int(cap.get(cv2.CAP_PROP_FPS)) + cap.release() + del cap + + stop_event = threading.Event() + ExecUnit = threading.Thread exec_units = ( - threading.Thread(target=video_capture, args=(raw_frame_queue, preprocessed_frame_queue, - darknet_height, darknet_width)), - threading.Thread(target=inference, args=(preprocessed_frame_queue, detections_queue, fps_queue)), - threading.Thread(target=drawing, args=(raw_frame_queue, detections_queue, fps_queue, - darknet_height, darknet_width, video_height, video_width)), + ExecUnit(target=video_capture, args=(stop_event, input_path, raw_frame_queue, preprocessed_frame_queue, + darknet_height, darknet_width)), + ExecUnit(target=inference, args=(stop_event, preprocessed_frame_queue, detections_queue, fps_queue)), + ExecUnit(target=drawing, args=(stop_event, video_fps, raw_frame_queue, detections_queue, fps_queue, + darknet_height, darknet_width, video_height, video_width)), ) for exec_unit in exec_units: exec_unit.start() From 415bf3188ea25f330cfc403dd0be732f0256e7db Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 04:32:03 +0300 Subject: [PATCH 10/12] Code reordering + (minor) renames --- darknet_video.py | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/darknet_video.py b/darknet_video.py index 3cb4748c994..943e521180b 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -16,9 +16,9 @@ def parser(): help="inference video name. Not saved if empty") parser.add_argument("--weights", default="yolov4.weights", help="yolo weights path") - parser.add_argument("--dont_show", action='store_true', - help="windown inference display. For headless systems") - parser.add_argument("--ext_output", action='store_true', + parser.add_argument("--dont_show", action="store_true", + help="window inference display. For headless systems") + parser.add_argument("--ext_output", action="store_true", help="display bbox coordinates of detected objects") parser.add_argument("--config_file", default="./cfg/yolov4.cfg", help="path to config file") @@ -31,7 +31,7 @@ def parser(): def str2int(video_path): """ - argparse returns and string althout webcam uses int (0, 1 ...) + argparse returns and string although webcam uses int (0, 1 ...) Cast to int if needed """ try: @@ -105,9 +105,9 @@ def convert4cropping(image, bbox, preproc_h, preproc_w): return bbox_cropping -def video_capture(stop_event, input_path, raw_frame_queue, preprocessed_frame_queue, preproc_h, preproc_w): +def video_capture(stop_flag, input_path, raw_frame_queue, preprocessed_frame_queue, preproc_h, preproc_w): cap = cv2.VideoCapture(input_path) - while cap.isOpened() and not stop_event.is_set(): + while cap.isOpened() and not stop_flag.is_set(): ret, frame = cap.read() if not ret: break @@ -118,12 +118,12 @@ def video_capture(stop_event, input_path, raw_frame_queue, preprocessed_frame_qu img_for_detect = darknet.make_image(preproc_w, preproc_h, 3) darknet.copy_image_from_bytes(img_for_detect, frame_resized.tobytes()) preprocessed_frame_queue.put(img_for_detect) - stop_event.set() + stop_flag.set() cap.release() -def inference(stop_event, preprocessed_frame_queue, detections_queue, fps_queue): - while not stop_event.is_set(): +def inference(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue): + while not stop_flag.is_set(): darknet_image = preprocessed_frame_queue.get() prev_time = time.time() detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh) @@ -135,11 +135,11 @@ def inference(stop_event, preprocessed_frame_queue, detections_queue, fps_queue) darknet.free_image(darknet_image) -def drawing(stop_event, input_video_fps, raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, vid_h, vid_w): +def drawing(stop_flag, input_video_fps, raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, vid_h, vid_w): random.seed(3) # deterministic bbox colors video = set_saved_video(args.out_filename, (vid_w, vid_h), input_video_fps) fps = 1 - while not stop_event.is_set(): + while not stop_flag.is_set(): frame = raw_frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() @@ -155,11 +155,11 @@ def drawing(stop_event, input_video_fps, raw_frame_queue, detections_queue, fps_ video.write(image) if cv2.waitKey(fps) == 27: break - stop_event.set() + stop_flag.set() video.release() cv2.destroyAllWindows() timeout = 1 / (fps if fps > 0 else 0.5) - for q in (detections_queue, fps_queue, preprocessed_frame_queue): + for q in (preprocessed_frame_queue, detections_queue, fps_queue): try: q.get(block=True, timeout=timeout) except queue.Empty: @@ -167,11 +167,6 @@ def drawing(stop_event, input_video_fps, raw_frame_queue, detections_queue, fps_ if __name__ == "__main__": - raw_frame_queue = queue.Queue() - preprocessed_frame_queue = queue.Queue(maxsize=1) - detections_queue = queue.Queue(maxsize=1) - fps_queue = queue.Queue(maxsize=1) - args = parser() check_arguments_errors(args) network, class_names, class_colors = darknet.load_network( @@ -189,14 +184,20 @@ def drawing(stop_event, input_video_fps, raw_frame_queue, detections_queue, fps_ cap.release() del cap - stop_event = threading.Event() + stop_flag = threading.Event() ExecUnit = threading.Thread + Queue = queue.Queue + + raw_frame_queue = Queue() + preprocessed_frame_queue = Queue(maxsize=1) + detections_queue = Queue(maxsize=1) + fps_queue = Queue(maxsize=1) exec_units = ( - ExecUnit(target=video_capture, args=(stop_event, input_path, raw_frame_queue, preprocessed_frame_queue, + ExecUnit(target=video_capture, args=(stop_flag, input_path, raw_frame_queue, preprocessed_frame_queue, darknet_height, darknet_width)), - ExecUnit(target=inference, args=(stop_event, preprocessed_frame_queue, detections_queue, fps_queue)), - ExecUnit(target=drawing, args=(stop_event, video_fps, raw_frame_queue, detections_queue, fps_queue, + ExecUnit(target=inference, args=(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue)), + ExecUnit(target=drawing, args=(stop_flag, video_fps, raw_frame_queue, detections_queue, fps_queue, darknet_height, darknet_width, video_height, video_width)), ) for exec_unit in exec_units: From 05fb0f8bceccad8069f2bf2f6cd7cac12418ef1d Mon Sep 17 00:00:00 2001 From: CristiFati Date: Fri, 17 Jun 2022 04:53:26 +0300 Subject: [PATCH 11/12] Pass data via arguments instead of global like variables - 2 --- darknet_video.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/darknet_video.py b/darknet_video.py index 943e521180b..74b5ead7f78 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -122,21 +122,23 @@ def video_capture(stop_flag, input_path, raw_frame_queue, preprocessed_frame_que cap.release() -def inference(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue): +def inference(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue, + network, class_names, threshold): while not stop_flag.is_set(): darknet_image = preprocessed_frame_queue.get() prev_time = time.time() - detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh) - detections_queue.put(detections) + detections = darknet.detect_image(network, class_names, darknet_image, thresh=threshold) fps = 1 / (time.time() - prev_time) + detections_queue.put(detections) fps_queue.put(int(fps)) print("FPS: {:.2f}".format(fps)) darknet.print_detections(detections, args.ext_output) darknet.free_image(darknet_image) -def drawing(stop_flag, input_video_fps, raw_frame_queue, detections_queue, fps_queue, preproc_h, preproc_w, vid_h, vid_w): +def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid_w): random.seed(3) # deterministic bbox colors + raw_frame_queue, preprocessed_frame_queue, detections_queue, fps_queue = queues video = set_saved_video(args.out_filename, (vid_w, vid_h), input_video_fps) fps = 1 while not stop_flag.is_set(): @@ -184,9 +186,9 @@ def drawing(stop_flag, input_video_fps, raw_frame_queue, detections_queue, fps_q cap.release() del cap - stop_flag = threading.Event() ExecUnit = threading.Thread Queue = queue.Queue + stop_flag = threading.Event() raw_frame_queue = Queue() preprocessed_frame_queue = Queue(maxsize=1) @@ -196,8 +198,10 @@ def drawing(stop_flag, input_video_fps, raw_frame_queue, detections_queue, fps_q exec_units = ( ExecUnit(target=video_capture, args=(stop_flag, input_path, raw_frame_queue, preprocessed_frame_queue, darknet_height, darknet_width)), - ExecUnit(target=inference, args=(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue)), - ExecUnit(target=drawing, args=(stop_flag, video_fps, raw_frame_queue, detections_queue, fps_queue, + ExecUnit(target=inference, args=(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue, + network, class_names, args.thresh)), + ExecUnit(target=drawing, args=(stop_flag, video_fps, + (raw_frame_queue, preprocessed_frame_queue, detections_queue, fps_queue), darknet_height, darknet_width, video_height, video_width)), ) for exec_unit in exec_units: From 341d71075627217fecbc33ada6e6beed941c8ee2 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Sat, 18 Jun 2022 00:37:12 +0300 Subject: [PATCH 12/12] Multiprocessing (failed or partial) attempt --- darknet.py | 35 ++++++++++++++++++++++++++-- darknet_video.py | 60 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/darknet.py b/darknet.py index 603ecae92dc..e37aec24fa2 100644 --- a/darknet.py +++ b/darknet.py @@ -15,6 +15,17 @@ import numpy as np +class PickleableStructure(ct.Structure): + def _compute_state(self): + raise NotImplementedError + + def __reduce__(self): + return self.__class__, (), self._compute_state() + + def __setstate__(self, state): + raise NotImplementedError + + class BOX(ct.Structure): _fields_ = ( ("x", ct.c_float), @@ -59,7 +70,7 @@ class DETNUMPAIR(ct.Structure): DETNUMPAIRPtr = ct.POINTER(DETNUMPAIR) -class IMAGE(ct.Structure): +class IMAGE(PickleableStructure): _fields_ = ( ("w", ct.c_int), ("h", ct.c_int), @@ -67,6 +78,13 @@ class IMAGE(ct.Structure): ("data", FloatPtr), ) + def _compute_state(self): + return self.w, self.h, self.c, self.data[:self.w * self.h * self.c] + + def __setstate__(self, state): + self.w, self.h, self.c = state[:3] + self.data = ct.cast((self.data._type_ * (self.w * self.h * self.c))(*state[-1]), FloatPtr) + class METADATA(ct.Structure): _fields_ = ( @@ -261,7 +279,20 @@ def detect_image(network, class_names, image, thresh=.5, hier_thresh=.5, nms=.45 elif os.name == "nt": cwd = os.path.dirname(__file__) os.environ["PATH"] = os.path.pathsep.join((cwd, os.environ["PATH"])) - lib = ct.CDLL("darknet.dll", ct.RTLD_GLOBAL) + #lib = ct.CDLL("darknet.dll", ct.RTLD_GLOBAL) + os.add_dll_directory(os.getcwd()) + _GPU = 1 + if _GPU: + nvs = ( + r"F:\Install\pc064\NVidia\CUDAToolkit\11.3\bin", + r"F:\Install\pc064\NVidia\cuDNN\8.2.0-CUDA11\bin", + ) + for nv in nvs: + os.add_dll_directory(nv) + os.environ["PATH"] += ";" + ";".join(nvs) # ! Strangely, crashes (can't find cudnn_ops_infer64_8.dll) without ! + lib = ct.CDLL("darknet_gpu.dll", ct.RTLD_GLOBAL) + else: + lib = ct.CDLL("darknet_nogpu.dll", ct.RTLD_GLOBAL) else: lib = None # Intellisense print("Unsupported OS") diff --git a/darknet_video.py b/darknet_video.py index 74b5ead7f78..e322e3c94e9 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -5,6 +5,7 @@ import darknet import argparse import threading +import multiprocessing as mp import queue @@ -26,6 +27,8 @@ def parser(): help="path to data file") parser.add_argument("--thresh", type=float, default=.25, help="remove detections with confidence below this value") + parser.add_argument("--multiprocess", action="store_true", + help="use processes instead of threads") return parser.parse_args() @@ -107,7 +110,7 @@ def convert4cropping(image, bbox, preproc_h, preproc_w): def video_capture(stop_flag, input_path, raw_frame_queue, preprocessed_frame_queue, preproc_h, preproc_w): cap = cv2.VideoCapture(input_path) - while cap.isOpened() and not stop_flag.is_set(): + while cap.isOpened() and stop_flag.empty(): ret, frame = cap.read() if not ret: break @@ -118,13 +121,19 @@ def video_capture(stop_flag, input_path, raw_frame_queue, preprocessed_frame_que img_for_detect = darknet.make_image(preproc_w, preproc_h, 3) darknet.copy_image_from_bytes(img_for_detect, frame_resized.tobytes()) preprocessed_frame_queue.put(img_for_detect) - stop_flag.set() + stop_flag.put(None) cap.release() + print("video_capture end:", os.getpid()) def inference(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue, - network, class_names, threshold): - while not stop_flag.is_set(): + config_file, data_file, weights_file, batch_size, threshold, ext_output): + network, class_names, _ = darknet.load_network( + config_file, + data_file, + weights_file, + batch_size=batch_size) + while stop_flag.empty(): darknet_image = preprocessed_frame_queue.get() prev_time = time.time() detections = darknet.detect_image(network, class_names, darknet_image, thresh=threshold) @@ -132,16 +141,18 @@ def inference(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue, detections_queue.put(detections) fps_queue.put(int(fps)) print("FPS: {:.2f}".format(fps)) - darknet.print_detections(detections, args.ext_output) + #darknet.print_detections(detections, ext_output) darknet.free_image(darknet_image) + darknet.free_network_ptr(network) + print("inference end:", os.getpid()) -def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid_w): +def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid_w, out_filename, dont_show, class_colors): random.seed(3) # deterministic bbox colors raw_frame_queue, preprocessed_frame_queue, detections_queue, fps_queue = queues - video = set_saved_video(args.out_filename, (vid_w, vid_h), input_video_fps) + video = set_saved_video(out_filename, (vid_w, vid_h), input_video_fps) fps = 1 - while not stop_flag.is_set(): + while stop_flag.empty(): frame = raw_frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() @@ -151,13 +162,13 @@ def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid bbox_adjusted = convert2original(frame, bbox, preproc_h, preproc_w) detections_adjusted.append((str(label), confidence, bbox_adjusted)) image = darknet.draw_boxes(detections_adjusted, frame, class_colors) - if not args.dont_show: + if not dont_show: cv2.imshow("Inference", image) - if args.out_filename is not None: + if out_filename is not None: video.write(image) if cv2.waitKey(fps) == 27: break - stop_flag.set() + stop_flag.put(None) video.release() cv2.destroyAllWindows() timeout = 1 / (fps if fps > 0 else 0.5) @@ -166,18 +177,22 @@ def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid q.get(block=True, timeout=timeout) except queue.Empty: pass + print("drawing end:", os.getpid()) if __name__ == "__main__": args = parser() check_arguments_errors(args) - network, class_names, class_colors = darknet.load_network( + batch_size = 1 + network, class_names, class_colors = darknet.load_network( # Load network twice :( args.config_file, args.data_file, args.weights, - batch_size=1) + batch_size=batch_size) darknet_width = darknet.network_width(network) darknet_height = darknet.network_height(network) + darknet.free_network_ptr(network) + del network input_path = str2int(args.input) cap = cv2.VideoCapture(input_path) # Open video twice :( video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) @@ -186,9 +201,14 @@ def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid cap.release() del cap - ExecUnit = threading.Thread - Queue = queue.Queue - stop_flag = threading.Event() + if args.multiprocess: + ExecUnit = mp.Process + Queue = mp.Queue + else: + ExecUnit = threading.Thread + Queue = queue.Queue + + stop_flag = Queue() raw_frame_queue = Queue() preprocessed_frame_queue = Queue(maxsize=1) @@ -199,15 +219,19 @@ def drawing(stop_flag, input_video_fps, queues, preproc_h, preproc_w, vid_h, vid ExecUnit(target=video_capture, args=(stop_flag, input_path, raw_frame_queue, preprocessed_frame_queue, darknet_height, darknet_width)), ExecUnit(target=inference, args=(stop_flag, preprocessed_frame_queue, detections_queue, fps_queue, - network, class_names, args.thresh)), + args.config_file, args.data_file, args.weights, batch_size, args.thresh, + args.ext_output)), ExecUnit(target=drawing, args=(stop_flag, video_fps, (raw_frame_queue, preprocessed_frame_queue, detections_queue, fps_queue), - darknet_height, darknet_width, video_height, video_width)), + darknet_height, darknet_width, video_height, video_width, + args.out_filename, args.dont_show, class_colors)), ) for exec_unit in exec_units: exec_unit.start() + print("------- EXEC UNIT:", ExecUnit) for exec_unit in exec_units: exec_unit.join() + print("------- EXEC UNIT:", ExecUnit) print("\nDone.")