How do I parse a Detection2DArray message
Environment
- HW: Tutlebot3 Waffle with an INTEL NUC
- Ubuntu 20.04
- ROS2 Foxy
- ROS 2 TensorFlow (Alsora) ros2-tensorflow
- Vision_msgs/msg define at vision_msgs
- C++
Goal
I am subscribing to a message produced by tf_detection_py server from the ros2-tensorflow package. I am using the following callback which I wrote as a quick proof of concept to test if my callback worked and yielded a valid message.
void detections_callback(const vision_msgs::msg::Detection2DArray::SharedPtr detections) const
{
RCLCPP_INFO(this->get_logger(), "---- ");
// A quick object detection santiy check
RCLCPP_INFO(this->get_logger(), "Detections time stamp seconds %i",
detections->header.stamp.sec);
// Undefined behavior when array bounds are out of range
for (int objectCount=0; objectCount<5; objectCount++)
{
RCLCPP_INFO(this->get_logger(), " - Object %i", objectCount);
RCLCPP_INFO(this->get_logger(), " id %i %i %i",
detections->detections[objectCount].results[0].id[0],
detections->detections[objectCount].results[0].id[1],
detections->detections[objectCount].results[0].id[2]
);
RCLCPP_INFO(this->get_logger(), " score %f",
detections->detections[objectCount].results[0].score);
}
}
This is its output.
[INFO] [1644012380.017523928] [Image_Subscriber_OOP_Test]: Detections time stamp seconds 1644012379
[INFO] [1644012380.017541734] [Image_Subscriber_OOP_Test]: - Object 0
[INFO] [1644012380.017557160] [Image_Subscriber_OOP_Test]: id 56 52 0
[INFO] [1644012380.017571761] [Image_Subscriber_OOP_Test]: score 0.716635
[INFO] [1644012380.017589226] [Image_Subscriber_OOP_Test]: - Object 1
[INFO] [1644012380.017607646] [Image_Subscriber_OOP_Test]: id 56 52 0
[INFO] [1644012380.017621308] [Image_Subscriber_OOP_Test]: score 0.682781
[INFO] [1644012380.017635471] [Image_Subscriber_OOP_Test]: - Object 2
[INFO] [1644012380.017648167] [Image_Subscriber_OOP_Test]: id 55 50 0
[INFO] [1644012380.017660931] [Image_Subscriber_OOP_Test]: score 0.607861
[INFO] [1644012380.017674397] [Image_Subscriber_OOP_Test]: - Object 3
[INFO] [1644012380.017686806] [Image_Subscriber_OOP_Test]: id 49 0 59
[INFO] [1644012380.017699744] [Image_Subscriber_OOP_Test]: score 0.530216
The output is as expected. It displayed four detected objects:
id 84 (Hex ASCII 56 52) "book"
id 84 (Hex ASCII 56 52) "book"
id 72 (Hex ASCII 55 50) "tv"
id 1 (Hex ASCII 49) "person"
See coco_labels
This matches what I see in the detections_image I displayed with showimage. This means that my subscriber is working.
Question 1
I want to clean up my code by parsing the Detection2DArray message. I want to write things similar to
vision_msgs::msg::Detection2D detections2d = detections2dArray->detections;
vision_msgs::msg::BoundingBox2D boundingBox = detections2d->bbox;
I can't get my types to match. I get the following kinds of errors. How do I fix it.
/home/eepp/eepp_ws/src/eepp_tf_detections/src/detections_list.cpp: In member function ‘void MinimalImageSubscriber::detections_callback(vision_msgs::msg::Detection2DArray_<std::allocator<void> >::SharedPtr) const’:
/home/eepp/eepp_ws/src/eepp_tf_detections/src/detections_list.cpp:56:69: error: conversion from ‘vision_msgs::msg::Detection2DArray_<std::allocator<void> >::_detections_type’ {aka ‘std::vector<vision_msgs::msg::Detection2D_<std::allocator<void> >, std::allocator<vision_msgs::msg::Detection2D_<std::allocator<void> > > >’} to non-scalar type ‘vision_msgs::msg::Detection2D’ {aka ‘vision_msgs::msg::Detection2D_<std::allocator<void> >’} requested
56 | vision_msgs::msg::Detection2D detections2d = detections2dArray->detections;
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
make[2]: *** [CMakeFiles/detections_list.dir/build.make:63: CMakeFiles/detections_list.dir/src/detections_list.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/detections_list.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
---
Failed <<< eepp_tf_detections [4.33s, exited with code 2]
Looking at the message definitions in vision_msgs I get a data structure that I think should look something like
Detection2DArray
Header header
Detection2D[] detections
Header header
ObjectHypothesisWithPose[] results
int64 id
float64 score ...