33from urllib .request import urlretrieve
44
55import cv2
6- from motpy import Detection , MultiObjectTracker , NpImage
6+ from motpy import Detection , MultiObjectTracker , NpImage , Box
77from motpy .core import setup_logger
88from motpy .detector import BaseObjectDetector
99from motpy .testing_viz import draw_detection , draw_track
1515
1616"""
1717
18- logger = setup_logger (__name__ , is_main = True )
18+ logger = setup_logger (__name__ , 'DEBUG' , is_main = True )
1919
2020
2121WEIGHTS_URL = 'https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel'
@@ -30,7 +30,7 @@ def __init__(self,
3030 weights_path : str = WEIGHTS_PATH ,
3131 config_url : str = CONFIG_URL ,
3232 config_path : str = CONFIG_PATH ,
33- conf_threshold : float = 0.5 ):
33+ conf_threshold : float = 0.5 ) -> None :
3434 super (FaceDetector , self ).__init__ ()
3535
3636 if not os .path .isfile (weights_path ) or not os .path .isfile (config_path ):
@@ -43,23 +43,23 @@ def __init__(self,
4343 # specify detector hparams
4444 self .conf_threshold = conf_threshold
4545
46- def process_image (self , image : NpImage ) -> Sequence [NpImage ]:
46+ def process_image (self , image : NpImage ) -> Sequence [Detection ]:
4747 blob = cv2 .dnn .blobFromImage (image , 1.0 , (300 , 300 ), [104 , 117 , 123 ], False , False )
4848 self .net .setInput (blob )
4949 detections = self .net .forward ()
5050
5151 # convert output from OpenCV detector to tracker expected format [xmin, ymin, xmax, ymax]
52- bboxes = []
52+ out_detections = []
5353 for i in range (detections .shape [2 ]):
5454 confidence = detections [0 , 0 , i , 2 ]
5555 if confidence > self .conf_threshold :
5656 xmin = int (detections [0 , 0 , i , 3 ] * image .shape [1 ])
5757 ymin = int (detections [0 , 0 , i , 4 ] * image .shape [0 ])
5858 xmax = int (detections [0 , 0 , i , 5 ] * image .shape [1 ])
5959 ymax = int (detections [0 , 0 , i , 6 ] * image .shape [0 ])
60- bboxes .append ([xmin , ymin , xmax , ymax ])
61-
62- return bboxes
60+ out_detections .append (Detection ( box = [xmin , ymin , xmax , ymax ], score = confidence ) )
61+
62+ return out_detections
6363
6464
6565def run ():
@@ -84,8 +84,7 @@ def run():
8484 frame = cv2 .resize (frame , dsize = None , fx = 0.5 , fy = 0.5 )
8585
8686 # run face detector on current frame
87- bboxes = face_detector .process_image (frame )
88- detections = [Detection (box = bbox ) for bbox in bboxes ]
87+ detections = face_detector .process_image (frame )
8988 logger .debug (f'detections: { detections } ' )
9089
9190 tracker .step (detections )
0 commit comments