-
Notifications
You must be signed in to change notification settings - Fork 0
/
ros_python_yolo.py
42 lines (34 loc) · 1.07 KB
/
ros_python_yolo.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
#在ros中使用torch.hub.load加载yolo检测模型
import rospy
import cv2
import yolov5
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import torch
class test():
def __init__(self):
self.bridge = CvBridge()
self.detect = torch.hub.load('ultralytics/yolov5', 'yolov5s') #从网络中加载yolo模型
def subcriber(self):
#img = rospy.wait_for_message('/usb_cam/image_raw', Image)
img = rospy.wait_for_message('/usb_cam/image_raw', Image)
print('work1')
return img
def image_detect(self, img):
bridge = CvBridge()
img = bridge.imgmsg_to_cv2(img, "bgr8")
#img = cv2.cvtColor(imgC, cv2.COLOR_RGB2GRAY)
print(img.shape)
result = self.detect(img)
#print(result)
result.print()
def main():
rospy.init_node('test', anonymous=True)
test_ = test()
#print('work')
while(1):
img = test_.subcriber()
#print('work2')
test_.image_detect(img)
if __name__=='__main__':
main()