前言

现在的机器人定位、导航等技术都会涉及到感知、规划、控制算法。本次将在ROS中配置yolov3目标检测算法,以此学习视觉感知算法在ROS中的搭建。

本文已经提前将ROS环境搭建好了。

darknet_ros

源码编译

首先创建一个ros工作空间,然后下载源码,

mkdir -p ~/yolo_ws/src
cd ~/yolo_ws/src
git clone https://github.com/leggedrobotics/darknet_ros.git

为了在加速编译,先提前把yolo的权重下载下来,

cd darknet_ros/darknet_ros/yolo_network_config/weights/
wget http://pjreddie.com/media/files/yolov2.weights
wget http://pjreddie.com/media/files/yolov2-tiny.weights
wget http://pjreddie.com/media/files/yolov3-tiny.weights
wget http://pjreddie.com/media/files/yolov3.weights

然后就可以开始编译了,

catkin_make -DCMAKE_BUILD_TYPE=Release
# 编译成功后,准备进行测试

运行ROS YOLOv3

首先配置darknet_ros的运行环境

source ~/yolo_ws/devel/setup.bash

然后选择你要使用的yolo模型,这里可以选择之前下载的yolov2、yolov3以及tiny模型,通过修改darknet_ros.launch文件来进行切换,

rosed darknet_ros darknet_ros.launch

该launch文件的注释如下:

<?xml version="1.0" encoding="utf-8"?>

<launch>
  <!-- Console launch prefix -->
  <arg name="launch_prefix" default=""/>
  <arg name="image" default="/camera/rgb/image_raw" />
# 定义yolo结构和权重的路径
  <!-- Config and weights folder. -->
  <arg name="yolo_weights_path"          default="$(find darknet_ros)/yolo_network_config/weights"/>
  <arg name="yolo_config_path"           default="$(find darknet_ros)/yolo_network_config/cfg"/>
# 定义ros通信的方式 与 yolo的配置
  <!-- ROS and network parameter files -->
  <arg name="ros_param_file"             default="$(find darknet_ros)/config/ros.yaml"/>
  <arg name="network_param_file"         default="$(find darknet_ros)/config/yolov2-tiny.yaml"/>
# 载入ros和yolo配置
  <!-- Load parameters -->
  <rosparam command="load" ns="darknet_ros" file="$(arg ros_param_file)"/>
  <rosparam command="load" ns="darknet_ros" file="$(arg network_param_file)"/>
# 开启节点,接收摄像头图像
  <!-- Start darknet and ros wrapper -->
  <node pkg="darknet_ros" type="darknet_ros" name="darknet_ros" output="screen" launch-prefix="$(arg launch_prefix)">
    <param name="weights_path"          value="$(arg yolo_weights_path)" />
    <param name="config_path"           value="$(arg yolo_config_path)" />
    <remap from="camera/rgb/image_raw"  to="$(arg image)" />
  </node>

 <!--<node name="republish" type="republish" pkg="image_transport" output="screen" 	args="compressed in:=/front_camera/image_raw raw out:=/camera/image_raw" /> -->
</launch>

由于我们想使用yolov3,因此将launch中的yolov2-tiny.yaml改为yolov3.yaml

接着测试一下usb摄像头是否能够打开,并找到摄像头发布的话题名,并修改darknet_ros中的话题订阅

# 测试摄像头
roslaunch usb_cam usb_cam-test.launch 
# 找到摄像头发布的话题
rostopic list
# 一般是类似 /usb_cam/image_raw 这种话题
# 然后编辑ros yolo需要接收图像时需要订阅的话题
rosed darknet_ros ros.yaml
# camera_reading中的topic改成找到的摄像头发布的话题

然后测试ros yolov3,

# 打开两个terminal
roslaunch darknet_ros darnet_ros.launch
roslaunch usb_cam usb_cam-test.launch
# 看到出现了一个名为yolo的新视图,则表明运行成功

此外,我还打开了一个新窗口查看运行中的topic,

# rostopic list -v
Published topics:
 * /rosout_agg [rosgraph_msgs/Log] 1 publisher
 * /rosout [rosgraph_msgs/Log] 3 publishers
 * /darknet_ros/found_object [darknet_ros_msgs/ObjectCount] 1 publisher
 * /darknet_ros/bounding_boxes [darknet_ros_msgs/BoundingBoxes] 1 publisher
 * /darknet_ros/detection_image [sensor_msgs/Image] 1 publisher
 * /darknet_ros/check_for_objects/result [darknet_ros_msgs/CheckForObjectsActionResult] 1 publisher
 * /darknet_ros/check_for_objects/feedback [darknet_ros_msgs/CheckForObjectsActionFeedback] 1 publisher
 * /darknet_ros/check_for_objects/status [actionlib_msgs/GoalStatusArray] 1 publisher
 * /image_view/parameter_descriptions [dynamic_reconfigure/ConfigDescription] 1 publisher
 * /image_view/parameter_updates [dynamic_reconfigure/Config] 1 publisher
 * /image_view/output [sensor_msgs/Image] 1 publisher
 * /usb_cam/image_raw/compressedDepth [sensor_msgs/CompressedImage] 1 publisher
 * /usb_cam/image_raw/compressedDepth/parameter_descriptions [dynamic_reconfigure/ConfigDescription] 1 publisher
 * /usb_cam/image_raw/compressedDepth/parameter_updates [dynamic_reconfigure/Config] 1 publisher
 * /usb_cam/image_raw/compressed [sensor_msgs/CompressedImage] 1 publisher
 * /usb_cam/image_raw/compressed/parameter_descriptions [dynamic_reconfigure/ConfigDescription] 1 publisher
 * /usb_cam/image_raw/compressed/parameter_updates [dynamic_reconfigure/Config] 1 publisher
 * /usb_cam/image_raw [sensor_msgs/Image] 1 publisher
 * /usb_cam/image_raw/theora [theora_image_transport/Packet] 1 publisher
 * /usb_cam/image_raw/theora/parameter_descriptions [dynamic_reconfigure/ConfigDescription] 1 publisher
 * /usb_cam/image_raw/theora/parameter_updates [dynamic_reconfigure/Config] 1 publisher
 * /usb_cam/camera_info [sensor_msgs/CameraInfo] 1 publisher

Subscribed topics:
 * /rosout [rosgraph_msgs/Log] 1 subscriber
 * /usb_cam/image_raw [sensor_msgs/Image] 2 subscribers
 * /darknet_ros/check_for_objects/goal [darknet_ros_msgs/CheckForObjectsActionGoal] 1 subscriber
 * /darknet_ros/check_for_objects/cancel [actionlib_msgs/GoalID] 1 subscriber

后记

过段时间我会对darknet_ros进行更细致的源码和配置文件解析~

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐