1、创建ROS工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
# 初始化文件
catkin_init_workspace
# 编译工作空间
cd ~/catkin_ws/
catkin_make
# 设置环境变量,避免每次开终端都要source
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
2、将darknet_ros代码克隆在/src文件夹中
cd catkin_ws/src
git clone --recursive [email protected]:leggedrobotics/darknet_ros.git
cd ../
注意:有可能darknet文件夹里的东西下不下来,建议手动从git下载然后放入。同时提前将权重文件下载好之后放入上述文件夹,避免编译时花费过多时间。
3、退出在catkin_ws文件夹下进行编译
catkin_make -DCMAKE_BUILD_TYPE=Release
编译不报错即可。
我在部署到Jetson AGX Xavier之前,先在自己的电脑上进行了测试,随后在控制器上操作一致。因此我们首先需要写一个能够发布视频话题的ROS文件。
1、在catkin_ws/src文件夹下创建一个新的文件夹
# 创建my_image_transport功能包
cd catkin_ws/
cd src/
catkin_create_pkg my_image_transport image_transport cv_bridge
# 重新编译
cd ..
catkin_make
2、发布视频的imagepub.cpp
文件,将下述复制粘贴在my_image_transport/src
文件夹中即可
#include
#include
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// ROS节点初始化
ros::init(argc, argv, "image_publisher");
ros::NodeHandle n;
ros::Time time = ros::Time::now();
ros::Rate loop_rate(5);
// 定义节点句柄
image_transport::ImageTransport it(n);
image_transport::Publisher pub = it.advertise("video_image", 1);
sensor_msgs::ImagePtr msg;
// opencv准备读取视频
cv::VideoCapture video;
video.open("/home/vlt/Pictures/1.mp4");
if( !video.isOpened() )
{
ROS_INFO("Read Video failed!\n");
return 0;
}
Mat frame;
int count = 0;
while(1)
{
video >> frame;
if( frame.empty() )
break;
count++;
msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
pub.publish(msg);
ROS_INFO( "read the %dth frame successfully!", count );
loop_rate.sleep();
ros::spinOnce();
}
video.release();
return 0;
}
3、修改my_image_transport
文件夹中的CMakeList.txt
文件,对照如下修改即可。
find_package(catkin REQUIRED COMPONENTS
cv_bridge
image_transport
roscpp
rospy
sensor_msgs
std_msgs
)
find_package(OpenCV REQUIRED)
include_directories(include ${catkin_INCLUDE_DIRS})
include_directories(include ${OpenCV_INCLUDE_DIRS})
add_executable(imagepub src/imagepub.cpp)
target_link_libraries(imagepub ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
#add_dependencies(mytest opencvexam_gencpp)
# add_executable(imagesub src/imagesub.cpp)
# target_link_libraries(imagesub ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
4、同样退出到catkin_ws
文件夹中catkin_make
编译成功即可。
5、从上述cpp文件可以看出发布的话题名称为video_image
,因此需要修改darknet_ros里面的话题订阅名称。
打开darknet_ros/config/ros.yaml文件,修改:
subscribers:
camera_reading:
topic: /camera/rgb/image_raw
queue_size: 1
为:
subscribers:
camera_reading:
topic: /video_image
queue_size: 1
!!!video_image前面一定要加/!!!不然就会接收不到图片,当时被这个bug困扰了好久。。。
随后同样catkin_make
编译一下,然后source devel/setup.bash
。其他的视频路径什么的自己看着修改
1、先开darknet_ros节点
roslaunch darknet_ros darknet_ros.launch
2、运行imagepub节点发布视频
rosrun my_image_transport imagepub
4、darknet_ros使用GPU和CUDA加速
可参考博客:darknet_ros加速–使用GPU和CUDA
后续部署到Jetson AGX Xavier上需要下载摄像头驱动
sudo apt-get install ros-kinetic-usb-cam
然后发布摄像头话题
roslaunch usb_cam usb_cam-test.launch
同时也需要自己修改一下darknet_ros的订阅话题名。