Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit f213638

Browse files
committed
ROS build and exec ok
1 parent 9835c58 commit f213638

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

ros_ws/src/detection/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ generate_messages(
3232
## catkin specific configuration ##
3333
###################################
3434

35+
catkin_package()
3536

3637
###########
3738
## Build ##

ros_ws/src/detection/msg/Detection.msg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ float32 h
1717
# class
1818
uint8 cls
1919

20-
float32 confidence
20+
float32 confidence
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Detections.msg
22
## List of detected bounding boxes
33

4-
Header header
5-
Detection[] detections
6-
7-
# frame
8-
uint8 frame
4+
# Header header
5+
Detection[] detections

ros_ws/src/tracking/scripts/detection_test.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
import os
99
import cv2
10+
from urllib.request import urlretrieve
1011
import numpy as np
1112
from motpy.tracker import Tracker
1213
from detection.msg import Detections, Detection as DetectionROS
14+
import rospy
1315

1416
PROJECT_FOLDER = os.path.dirname(os.path.realpath(__file__))
1517
DATASET_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')
1719
YOLO_FOLDER = os.path.join(PROJECT_FOLDER, 'yolo')
1820
WEIGHTS_URL = 'https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights'
1921
WEIGHTS_PATH = os.path.join(YOLO_FOLDER, 'yolov4.weights')
@@ -24,10 +26,10 @@
2426
if not os.path.exists(YOLO_FOLDER):
2527
os.mkdir(YOLO_FOLDER)
2628
if 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)
2931
if 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)
3234
net = 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

4648
def 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

7375
def 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

ros_ws/src/tracking/scripts/track.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515

1616
from motpy import MultiObjectTracker, Track, Detection as DetectionMOT
1717

18-
dt = 1/24 # TODO : changer
18+
dt = 1 # TODO : changer
1919

2020
class Tracker:
2121
def __init__(self):
2222
# ROS
23-
self.sub = rospy.Subscriber(
24-
'detections_in', Detections, self.detections_callback, queue_size=1)
25-
23+
rospy.init_node('tracking', anonymous=False)
2624
self.pub = rospy.Publisher('tracks', Tracklets, queue_size=1)
2725

2826
# motpy - no confidence with the motion model (0.1)
@@ -38,16 +36,17 @@ def detections_callback(self, detections_ros: Detections) -> Tracklets:
3836
detections_mot = [self.det_ros2mot(detection_ros)
3937
for detection_ros in detections_ros.detections]
4038
tracks = self.mot.step(detections_mot)
41-
tracklets = Tracklets([self.trk_mot2ros(track) for track in tracks])
39+
tracks = [self.trk_mot2ros(track) for track in tracks]
40+
tracklets = Tracklets(tracklets=tracks)
4241
self.pub.publish(tracklets)
43-
44-
frame = detections_ros.frame
45-
img = cv2.imread(os.path.join(VIDEO_FOLDER, f'frame{frame}.jpg'))
46-
for det in detections_mot:
47-
img = draw_rectangle(img, det.box, color=(0, 255, 0), thickness=2)
48-
for trk in tracks:
49-
img = draw_rectangle(img, det.box, color=(255, 0, 0), thickness=5)
50-
cv2.imshow('Main', cv2.resize(img, (960, 540)))
42+
rospy.loginfo(tracklets)
43+
return tracklets
44+
#img = cv2.imread(os.path.join(VIDEO_FOLDER, f'frame{frame}.jpg'))
45+
#for det in detections_mot:
46+
# img = draw_rectangle(img, det.box, color=(0, 255, 0), thickness=2)
47+
#for trk in tracks:
48+
# img = draw_rectangle(img, det.box, color=(255, 0, 0), thickness=5)
49+
#cv2.imshow('Main', cv2.resize(img, (960, 540)))
5150

5251
def det_ros2mot(self, detection: DetectionROS) -> DetectionMOT:
5352
box = np.array([detection.x, detection.y,
@@ -59,8 +58,8 @@ def trk_mot2ros(self, track: Track) -> Tracklet:
5958
return tracklet
6059

6160
def main():
62-
rospy.init_node('tracking', anonymous=False)
6361
tracker = Tracker()
62+
rospy.Subscriber('detections', Detections, tracker.detections_callback)
6463
while not rospy.is_shutdown():
6564
rospy.spin()
6665

0 commit comments

Comments
 (0)