实现目标检测与发布话题的功能包
实现的结果 :
position 里面的x,y,z分别对应于检测出来的物体的基于相机坐标系的实际的三维坐标检测 ,目前是基于相机的坐标系要实现机械臂的自动抓取,还需要手眼标定。
目标检测使用的是YOLOv5的模型
二YOLO工作空间的配置
安装依赖
sudo apt update
sudo apt install python3-pip ros-$ROS_DISTRO-vision-msgs
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple yolov5
配置工作空间
sudo -p mkdir yolo_ros2_3d/src
cd yolo_ros2_3d/src
源码包
conlcon git -i 网址.git
编译
cd yolo_ros2_3d
source /opt/ros/humble/setup.bash
colcon build
source install/setup.bash
三运行程序
首先需要启动相机发布话题
cd ~/YOLO/ros2_ws
. ./install/setup.bash
ros2 launch orbbec_camera dabai_dcw.launch.py
查看话题
ros2 topic list
在运行程序之前首先需要启动深度相机方便获取话题
运行程序
cd ~/YOLO/yolo_ros2_3d
. ./install/setup.bash
ros2 run yolov5_ros2 yolo_detect_2d --ros-args -p device:=cpu -p image_topic:=/camera/color/image_raw -p show_result:=True -p image_topic_depth:=/camera/depth/image_raw -p camera_info:=/camera/depth/camera_info
参数说明
device 设备默认cpu
model 加载的识别的模型
imag_topic 彩色图像的话题
image_topic_depth 深度图像的话题
camera_info_topic 输入相机的参数
camera_info 可自定义在文件内部
show_result 是否展示结果默认是flase
pub_result_img 是否发布结果 默认是true
查看发布出来的数据
ros2 topic echo /yolo_result
yolo_result话题
self.yolo_result_pub = self.create_publisher(Detection2DArray, "yolo_result", 10)
self.result_msg = Detection2DArray()
yolo_result 的数据类型是 Detection2DArray()
# yolo_result层次关系是
self.result_msg.detections.clear()
self.result_msg.header.frame_id = "camera"
self.result_msg.header.stamp = self.get_clock().now().to_msg()#时间戳stamp
detection2d = Detection2D()# 检测框的对象处理检测数据
#detection2d的层次
# 以像数坐标系检测的物体的中心位置
detection2d.bbox.center.x = center_x
detection2d.bbox.center.y = center_y
# 以像数坐标系检测的物体的框的大小
detection2d.bbox.size_x = float(x2-x1)
detection2d.bbox.size_y = float(y2-y1)
obj_pose 对象是 obj_pose = ObjectHypothesisWithPose()「#存储detection2d处理后的数据
# obj_pose的层次
obj_pose.hypothesis.class_id = name #识别到的类别
obj_pose.hypothesis.score = float(scores[index])#识别物体的得分
#具体的位置坐标信息在:
#相机坐标系下的实际物体的三维坐标以米为单位
obj_pose.pos