77
88import os
99import cv2
10+ from urllib .request import urlretrieve
1011import numpy as np
1112from motpy .tracker import Tracker
1213from detection .msg import Detections , Detection as DetectionROS
14+ import rospy
1315
1416PROJECT_FOLDER = os .path .dirname (os .path .realpath (__file__ ))
1517DATASET_FOLDER = os .path .join (PROJECT_FOLDER , 'TP3_data' )
16- VIDEO_FOLDER = os .path .join (DATASET_FOLDER , 'frames' )
18+ VIDEO_FOLDER = os .path .join (DATASET_FOLDER , 'TP3_data' , ' frames' )
1719YOLO_FOLDER = os .path .join (PROJECT_FOLDER , 'yolo' )
1820WEIGHTS_URL = 'https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights'
1921WEIGHTS_PATH = os .path .join (YOLO_FOLDER , 'yolov4.weights' )
2426if not os .path .exists (YOLO_FOLDER ):
2527 os .mkdir (YOLO_FOLDER )
2628if not os .path .isfile (WEIGHTS_PATH ):
27- logger . debug ('downloading weights to' + WEIGHTS_PATH )
29+ print ('downloading weights to' + WEIGHTS_PATH )
2830 urlretrieve (WEIGHTS_URL , WEIGHTS_PATH )
2931if not os .path .isfile (CONFIG_PATH ):
30- logger . debug ('downloading config to' + CONFIG_PATH )
32+ print ('downloading config to' + CONFIG_PATH )
3133 urlretrieve (CONFIG_URL , CONFIG_PATH )
3234net = cv2 .dnn .readNetFromDarknet (CONFIG_PATH , WEIGHTS_PATH )
3335
@@ -40,8 +42,8 @@ def init_detections():
4042 for line in lines :
4143 x , xmax , y , ymax = map (int , line .split ()[2 :6 ])
4244 detection = DetectionROS (x = x , y = y , w = xmax - x , h = ymax - y , confidence = 1 )
43- detections .append (Detection ( box = box ,) )
44- return Detections (detections = detections , frame = 1 )
45+ detections .append (detection )
46+ return Detections (detections = detections )
4547
4648def yolo_detections (frame , img , threshold = 0.3 ):
4749 """Generates bbox detections with yolo-v4.
@@ -52,7 +54,7 @@ def yolo_detections(frame, img, threshold=0.3):
5254 blob = cv2 .dnn .blobFromImage (img , 1 / 255.0 , (416 , 416 ), swapRB = True , crop = False )
5355 net .setInput (blob )
5456 ln = net .getLayerNames ()
55- ln = [ln [i [ 0 ] - 1 ] for i in net .getUnconnectedOutLayers ()]
57+ ln = [ln [i - 1 ] for i in net .getUnconnectedOutLayers ()]
5658 layer_outputs = net .forward (ln )
5759
5860 detections = []
@@ -68,13 +70,13 @@ def yolo_detections(frame, img, threshold=0.3):
6870 y = int (centerY - (h / 2 ))
6971 detection = DetectionROS (x = x , y = y , w = w , h = h , confidence = conf )
7072 detections .append (detection )
71- return Detections (detections = detections , frame = frame )
73+ return Detections (detections = detections )
7274
7375def main ():
7476 init = init_detections ()
7577 pub = rospy .Publisher ('detections' , Detections , queue_size = 1 )
7678 rospy .init_node ('detection_test' , anonymous = False )
77- r = rospy .Rate (24 )
79+ r = rospy .Rate (1 )
7880 nbframe = len (os .listdir (VIDEO_FOLDER ))
7981 imgs = (cv2 .imread (os .path .join (VIDEO_FOLDER , f'frame{ frame } .jpg' ))
8082 for frame in range (1 , nbframe + 1 ))
@@ -90,7 +92,7 @@ def main():
9092 detections = yolo_detections (frame , img )
9193 except StopIteration :
9294 break
93- rospy .spin ( )
95+ rospy .loginfo ( detections )
9496 pub .publish (detections )
9597 r .sleep ()
9698
0 commit comments