-
Notifications
You must be signed in to change notification settings - Fork 31
/
trt_yolo_v3.py
executable file
·171 lines (133 loc) · 5.76 KB
/
trt_yolo_v3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
import os
import time
import cv2
import pycuda.autoinit # For initializing CUDA driver
import pycuda.driver as cuda
from utils.yolo_classes import get_cls_dict
from utils.display import open_window, set_display, show_fps
from utils.visualization import BBoxVisualization
from utils.yolo_with_plugins import TrtYOLO
import rospy
import rospkg
from yolov4_trt_ros.msg import Detector2DArray
from yolov4_trt_ros.msg import Detector2D
from vision_msgs.msg import BoundingBox2D
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
class yolov4(object):
def __init__(self):
""" Constructor """
self.bridge = CvBridge()
self.init_params()
self.init_yolo()
self.cuda_ctx = cuda.Device(0).make_context()
self.trt_yolo = TrtYOLO(
(self.model_path + self.model), (self.h, self.w), self.category_num)
def __del__(self):
""" Destructor """
self.cuda_ctx.pop()
del self.trt_yolo
del self.cuda_ctx
def clean_up(self):
""" Backup destructor: Release cuda memory """
if self.trt_yolo is not None:
self.cuda_ctx.pop()
del self.trt_yolo
del self.cuda_ctx
def init_params(self):
""" Initializes ros parameters """
rospack = rospkg.RosPack()
package_path = rospack.get_path("yolov4_trt_ros")
self.video_topic = rospy.get_param("/video_topic", "/video_source/raw")
self.model = rospy.get_param("/model", "yolov3")
self.model_path = rospy.get_param(
"/model_path", package_path + "/yolo/")
self.input_shape = rospy.get_param("/input_shape", "416")
self.category_num = rospy.get_param("/category_number", 80)
self.conf_th = rospy.get_param("/confidence_threshold", 0.5)
self.show_img = rospy.get_param("/show_image", True)
self.image_sub = rospy.Subscriber(
self.video_topic, Image, self.img_callback, queue_size=1, buff_size=1920*1080*3)
self.detection_pub = rospy.Publisher(
"detections", Detector2DArray, queue_size=1)
self.overlay_pub = rospy.Publisher(
"/result/overlay", Image, queue_size=1)
def init_yolo(self):
""" Initialises yolo parameters required for trt engine """
if self.model.find('-') == -1:
self.model = self.model + "-" + self.input_shape
yolo_dim = self.model.split('-')[-1]
if 'x' in yolo_dim:
dim_split = yolo_dim.split('x')
if len(dim_split) != 2:
raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)
self.w, self.h = int(dim_split[0]), int(dim_split[1])
else:
self.h = self.w = int(yolo_dim)
if self.h % 32 != 0 or self.w % 32 != 0:
raise SystemExit('ERROR: bad yolo_dim (%s)!' % yolo_dim)
cls_dict = get_cls_dict(self.category_num)
self.vis = BBoxVisualization(cls_dict)
def img_callback(self, ros_img):
"""Continuously capture images from camera and do object detection """
tic = time.time()
# converts from ros_img to cv_img for processing
try:
cv_img = self.bridge.imgmsg_to_cv2(
ros_img, desired_encoding="bgr8")
rospy.logdebug("ROS Image converted for processing")
except CvBridgeError as e:
rospy.loginfo("Failed to convert image %s", str(e))
if cv_img is not None:
boxes, confs, clss = self.trt_yolo.detect(cv_img, self.conf_th)
cv_img = self.vis.draw_bboxes(cv_img, boxes, confs, clss)
toc = time.time()
fps = 1.0 / (toc - tic)
self.publisher(boxes, confs, clss)
if self.show_img:
cv_img = show_fps(cv_img, fps)
cv2.imshow("YOLOv4 DETECTION RESULTS", cv_img)
cv2.waitKey(1)
# converts back to ros_img type for publishing
try:
overlay_img = self.bridge.cv2_to_imgmsg(
cv_img, encoding="passthrough")
rospy.logdebug("CV Image converted for publishing")
self.overlay_pub.publish(overlay_img)
except CvBridgeError as e:
rospy.loginfo("Failed to convert image %s", str(e))
def publisher(self, boxes, confs, clss):
""" Publishes to detector_msgs
Parameters:
boxes (List(List(int))) : Bounding boxes of all objects
confs (List(double)) : Probability scores of all objects
clss (List(int)) : Class ID of all classes
"""
detection2d = Detector2DArray()
detection = Detector2D()
detection2d.header.stamp = rospy.Time.now()
for i in range(len(boxes)):
# boxes : xmin, ymin, xmax, ymax
for _ in boxes:
detection.header.stamp = rospy.Time.now()
detection.header.frame_id = "camera" # change accordingly
detection.results.id = clss[i]
detection.results.score = confs[i]
detection.bbox.center.x = boxes[i][0] + (boxes[i][2] - boxes[i][0])/2
detection.bbox.center.y = boxes[i][1] + (boxes[i][3] - boxes[i][1])/2
detection.bbox.center.theta = 0.0 # change if required
detection.bbox.size_x = abs(boxes[i][0] - boxes[i][2])
detection.bbox.size_y = abs(boxes[i][1] - boxes[i][3])
detection2d.detections.append(detection)
self.detection_pub.publish(detection2d)
def main():
yolo = yolov4()
rospy.init_node('yolov4_detection', anonymous=True)
try:
rospy.spin()
except KeyboardInterrupt:
rospy.on_shutdown(yolo.clean_up())
print("Shutting down")
if __name__ == '__main__':
main()