国际惯例:Src https://github.com/stereolabs/zed-examples
查看本人系列文章了解ZED2 SDK in Linux build(CUDA 10.0 used): 双目立体视觉(2)- ZED2 SDK配置 Ubuntu18.04 + ROS melodic_Techblog of HaoWANG-优快云博客_ros zed2
ZED2官方文档:Stereolabs Docs: API Reference, Tutorials, and Integration
目录
1.1 zed_obj_det_sub_tuttorial.cpp
1.3 配置zed_wrapper/params/zed2.yaml
2.3 查看/zed2/zed_node/obj_det/objects话题内容
官方教程C++历程源码阅读及编译使用教程
Building a C++ Application on Linux and Jetson | Stereolabshttps://www.stereolabs.com/docs/app-development/cpp/linux/
1. Object Detect demo (ROS)
近日收到优快云小伙伴私信求助,ZED相机做物体识别和深度值估计时出现问题,即节点起来之后pipline没有数据,直呼,马老师发生甚摸事了?很简单,三步走。你先这样,然后再这样,最后再那样就可以了。
1.1 zed_obj_det_sub_tuttorial.cpp
/**
* This tutorial demonstrates how to receive the list of detected objects from a ZED node
* from the ZED node
*/
#include <ros/ros.h>
#include <zed_interfaces/Object.h>
#include <zed_interfaces/ObjectsStamped.h>
/**
* Subscriber callbacks. The argument of the callback is a constant pointer to the received message
*/
void objectListCallback(const zed_interfaces::ObjectsStamped::ConstPtr& msg)
{
ROS_INFO("***** New object list *****");
for (int i = 0; i < msg->objects.size(); i++)
{
if (msg->objects[i].label_id == -1)
continue;
ROS_INFO_STREAM(msg->objects[i].label << " [" << msg->objects[i].label_id << "] - Pos. ["
<< msg->objects[i].position[0] << "," << msg->objects[i].position[1] << ","
<< msg->objects[i].position[2] << "] [m]"
<< "- Conf. " << msg->objects[i].confidence
<< " - Tracking state: " << static_cast<int>(msg->objects[i].tracking_state));
}
}
/**
* Node main function
*/
int main(int argc, char** argv)
{
ros::init(argc, argv, "zed_obj_det_sub_tutorial");
ros::NodeHandle n;
ros::Subscriber subObjList = n.subscribe("objects", 1, objectListCallback);
/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
ros::spin();
return 0;
}
代码非常简单,即创建一个obt_det 订阅者节点,callback回调函数用于处理接收到的检测结果。
注意到, ros::Subscriber subObjList = n.subscribe("objects", 1, objectListCallback);
该节点订阅的Topic是"objects",至于ZED2 SDK发布的Obj是不是该话题我们暂时不得而知。
(一定不是,不然不会出问题。)
1.2 CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(zed_obj_det_sub_tutorial)
# if CMAKE_BUILD_TYPE is not specified, take 'Release' as default
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF(NOT CMAKE_BUILD_TYPE)
## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp zed_interfaces)
## Declare a catkin package
catkin_package()
include_directories(include ${catkin_INCLUDE_DIRS})
## Build
add_executable(zed_obj_det_sub src/zed_obj_det_sub_tutorial.cpp)
target_link_libraries(zed_obj_det_sub ${catkin_LIBRARIES})
add_dependencies(zed_obj_det_sub ${catkin_EXPORTED_TARGETS})
查看CMakerlists文件,发现该节点的依赖和编译设置没有问题。
1.3 配置zed_wrapper/params/zed2.yaml
更改objetc_detection 栏目下的相应内容即可。
2. 测试
2.1 运行zed2.launch
查看zed2参数配置及初始化结果:
2.2 查看Topic列表
/zed2/zed_node/obj_det/objects没有问题
2.3 查看/zed2/zed_node/obj_det/objects话题内容
2.4 测试demo出错
节点运行没有反应,这就机霸的见鬼了。
3. 解决
3.1 分析
节点没问题、话题没问题、ZED2驱动没问题、CUDA没问题。。。
自然而然的想到,可能是obj节点并没有接收到Topic中的消息。
修改代码:
/**
* This tutorial demonstrates how to receive the list of detected objects from a ZED node
* from the ZED node
*/
#include <ros/ros.h>
#include <zed_interfaces/Object.h>
#include <zed_interfaces/ObjectsStamped.h>
/**
* Subscriber callbacks. The argument of the callback is a constant pointer to the received message
*/
void objectListCallback(const zed_interfaces::ObjectsStamped::ConstPtr& msg)
{
ROS_INFO("***** New object list *****");
for (int i = 0; i < msg->objects.size(); i++)
{
if (msg->objects[i].label_id == -1)
continue;
ROS_INFO_STREAM(msg->objects[i].label << " [" << msg->objects[i].label_id << "] - Pos. ["
<< msg->objects[i].position[0] << "," << msg->objects[i].position[1] << ","
<< msg->objects[i].position[2] << "] [m]"
<< "- Conf. " << msg->objects[i].confidence
<< " - Tracking state: " << static_cast<int>(msg->objects[i].tracking_state));
}
}
/**
* Node main function
*/
int main(int argc, char** argv)
{
ros::init(argc, argv, "zed_obj_det_sub_tutorial");
ros::NodeHandle n;
ros::Subscriber subObjList = n.subscribe("/zed2/zed_node/obj_det/objects", 1, objectListCallback);
/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
ros::spin();
return 0;
}
ros::Subscriber subObjList = n.subscribe("/zed2/zed_node/obj_det/objects", 1, objectListCallback);
重新编译catkin_make + source,再次测试。
3.2 再次测试
ok,成功
识别结果:
数据结构 - person 数量 【1】 位置 pos [ x, y ,z ] 单位 米, confidence: 【】