ROS基于OpenCV::dnn的检测器

正文索引 [隐藏]


rospass
代码已经上传到 GItHub Github

特点还是和之前的相同,但是做了一些小修改,可以调用摄像头,当然之前的那一份代码,调用摄像头也不是难事,只需要改一下Videocapture构建时的代码。
这个包会以ros信息的形式发送检测到的物体类别,置信度,中心坐标
下图为使用 包中所写的 监听端监听到的信息

使用摄像头的检测效果如图

包配置文件 package.xml

我们使用ROS的catkin_make进行编译,所以需要编译工具依赖

  <buildtool_depend>catkin</buildtool_depend>

因为我们使用到了C++进行编译所以需要在构建依赖和构建导出依赖中添加roscpp,因为使用C++11的缘故还需要添加roslib编译依赖,并且因为使用了消息生成功能,所以我们还需要std_msgs 的构建依赖以及 message_generation构建依赖和构建导出依赖。

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>roslib</build_depend>

然后添加运行依赖

  <exec_depend>message_runtime</exec_depend>
  <exec_depend>roslib</exec_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>std_msgs</exec_depend>

完整ROS包配置文件如下:

<?xml version="1.0"?>
<package format="2">
  <name>detector</name>
  <version>0.0.0</version>
  <description>The detector package</description>

  <maintainer email="m.c.chen@outlook.com">Michael.Chen</maintainer>

  <license>GPLv3</license>

  <url type="website">http://www.tgeek.tech</url>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>roslib</build_depend>

  <exec_depend>message_runtime</exec_depend>
  <exec_depend>roslib</exec_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>std_msgs</exec_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

CMAKE配置文件 CMakeLists.txt

设置CMAKE最低版本2.8.3,命名这个项目名为detector(就是构建节点名,当然我们这里最终构建时,使用了${PROJECT_NAME}_node,所以节点名实际为detector_node)

cmake_minimum_required(VERSION 2.8.3)
project(detector)

因为我使用了OpenCV4.1,4.1只支持C++11以上版本,所以需要设置C++版本为11 -std=c++11以及多线程编译-pthread。因为我有多版本OpenCV,所以还需要设置使用的OpenCV路径

# using C++11 
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -std=c++11 -pthread")
# if u have OpenCV version more than one, set the build path which one u want to use
set(OpenCV_DIR "/home/mingcongchen/app/opencv-4.1.0/build/")

然后就是寻找编译所使用的包以及配置信息和catkin包等

find_package(catkin REQUIRED COMPONENTS
  OpenCV REQUIRED
  roscpp
  roslib
  std_msgs
  message_generation
)
add_message_files(
  FILES
  Detector_Info.msg
)

generate_messages(
  DEPENDENCIES
  std_msgs
)

catkin_package(
CATKIN_DEPENDS message_runtime
)

添加include

include_directories(
  ${OpenCV_INCLUDE_DIRS}
  include
  ${catkin_INCLUDE_DIRS}
)

这里我们编译生成两个可执行文件,一个是我们的主要功能detector_node,一个是我们的订阅器

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/talker
DIR_SRCS)

add_executable(${PROJECT_NAME}_node ${DIR_SRCS})

add_executable(listener ${CMAKE_CURRENT_SOURCE_DIR}/src/listener/listener.cpp)

target_link_libraries( ${PROJECT_NAME}_node 
${OpenCV_LIBS}
${catkin_LIBRARIES})

target_link_libraries(listener ${catkin_LIBRARIES})

add_dependencies(${PROJECT_NAME}_node  detector_generate_messages_cpp)

完整CMakeLists.txt如下

cmake_minimum_required(VERSION 2.8.3)
project(detector)

# using C++11 
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -std=c++11 -pthread")

# if u have OpenCV version more than one, set the build path which one u want to use
set(OpenCV_DIR "/home/mingcongchen/app/opencv-4.1.0/build/")

find_package(catkin REQUIRED COMPONENTS
  OpenCV REQUIRED
  roscpp
  roslib
  std_msgs
  message_generation
)

add_message_files(
  FILES
  Detector_Info.msg
)

generate_messages(
  DEPENDENCIES
  std_msgs
)

catkin_package(
CATKIN_DEPENDS message_runtime
)

include_directories(
  ${OpenCV_INCLUDE_DIRS}
  include
  ${catkin_INCLUDE_DIRS}
)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/talker
DIR_SRCS)

add_executable(${PROJECT_NAME}_node ${DIR_SRCS})

add_executable(listener ${CMAKE_CURRENT_SOURCE_DIR}/src/listener/listener.cpp)

target_link_libraries( ${PROJECT_NAME}_node 
${OpenCV_LIBS}
${catkin_LIBRARIES})

target_link_libraries(listener ${catkin_LIBRARIES})

add_dependencies(${PROJECT_NAME}_node  detector_generate_messages_cpp)

消息文件

msg/中,我们定义了消息Detector_Info.msg其中有
– string time 时间戳
– string[] names 检测物体类别
– float64[] confidences 检测物体置信度
– int64[] centers_x 检测物体中心x
– int64[] centers_y 检测物体中心y

使用方法

要求

ROS::Kinect, OpenCV with OpenCV_contrib no less than 3.3.

文件描述

build/ : 编译空间

devel/ : 开发空间

src/ : 源码空间 :

detector/ : detector ROS包
dnn_nets/ : 网络结构,标签,预训练模型
  • yolo/ -Yolo 配置文件
  • ssd/ -SSD 配置文件
include:头文件
  • DetectorNode.hpp
  • dnndetector.hpp
msg/: mesages for ROS
param/: configurations
  • param_config.xml -视频流配置
  • dnn_param.xml -神经网络配置
src/ : 源码
  • listener/ -订阅器
  • talker/ -发布器

video/ : 测试视频文件夹

package.xml: 包描述文件
CMakeLists.txt : cmake 配置文件
CMakeLists.txt : 包 cmake 配置文件

安装

配置 Cmake

ConfigureCMakeLists.txt

gedit CMakeLists.txt

多版本OpenCV设置路径,否则注释此行

#if u have OpenCV version more than one, set the build path which one u want to use
set(OpenCV_DIR "YOUR_PATH")

Ex:

#if u have OpenCV version more than one, set the build path which one u want to use
set(OpenCV_DIR "/home/test/app/opencv-3.4.0/build/")

编译

确保在工作空间

$ catkin_make

运行

打开一个ros master

$ roscore

打开新终端,确保当然工作空间在顶部

$ source devel/setup.sh #(option, if workspace not on top)
$ rosrun detector detector_node

可选: Listener 订阅器

如果想监听消息,可以打开一个订阅器

消息为:

[时间戳]

[检测到物体类别]

[检测到物体置信度]

[检测到物体中心点坐标]

打开新终端,确保当然工作空间在顶部

$ rosrun detector listener

使用

视频流配置

不需要重新编译

视频路径为 {ROS_Package Path}/video/{VIdeo Name}

<?xml version="1.0"?>
<opencv_storage>
                                            <!--0-disable     1-enable-->
<show_time>1</show_time>                    <!--display time on output image-->
<debug_mode>1</debug_mode>                  <!--wait for keyboard to process next frame-->
<show_fps>0</show_fps>                      <!--display fps on output image-->

<use_camera>1</use_camera>                  <!--use camera or video-->
<video_file>test.mp4</video_file>           <!--video file name in {ProjectFolder}/video-->
<camera_index>1</camera_index>              <!--camera index-->
<window_width>1280</window_width>           <!--window size width-->
<window_height>720</window_height>          <!--window size height-->

</opencv_storage>

Network configurations

不需要重新编译

网络文件为 {ROS_Package Path}/dnn/{File Name}

<?xml version="1.0"?>
<opencv_storage>

<!-->Configration<-->
<net_type>1</net_type>              <!-->0-ssd 1-yolo<-->
<thresh>0.35</thresh>               <!-->confidence threshold<-->
<nms_thresh>0.25</nms_thresh>       <!-->nms threshold<-->

<!-->Yolo configration files<-->
<Yolo_meanVal>1</Yolo_meanVal> 
<Yolo_scaleFactor>0.003921569</Yolo_scaleFactor>
<Yolo_config>/dnn_nets/yolo/yolov3-tiny.cfg</Yolo_config>
<Yolo_model>/dnn_nets/yolo/yolov3-tiny.weights</Yolo_model>
<coco_name>/dnn_nets/yolo/coco.names</coco_name>

<!-->ssd configration files<-->
<ssd_meanVal>127.5</ssd_meanVal> 
<ssd_scaleFactor>0.007843</ssd_scaleFactor>
<ssd_config>/dnn_nets/ssd/deploy.prototxt</ssd_config>
<ssd_model>/dnn_nets/ssd/mobilenet_iter_73000.caffemodel</ssd_model>
<ssd_name>/dnn_nets/ssd/ssd.names</ssd_name>
</opencv_storage>