ImgDetectionConverter.cpp
Go to the documentation of this file.
2 
3 namespace dai {
4 
5 namespace ros {
6 
7 ImgDetectionConverter::ImgDetectionConverter(std::string frameName, int width, int height, bool normalized)
8  : _frameName(frameName), _width(width), _height(height), _normalized(normalized) {}
9 
10 void ImgDetectionConverter::toRosMsg(std::shared_ptr<dai::ImgDetections> inNetData, std::deque<VisionMsgs::Detection2DArray>& opDetectionMsgs) {
11  // setting the header
12  auto tstamp = inNetData->getTimestamp();
13  VisionMsgs::Detection2DArray opDetectionMsg;
14 #ifndef IS_ROS2
15  auto rosNow = ::ros::Time::now();
16  auto steadyTime = std::chrono::steady_clock::now();
17  auto diffTime = steadyTime - tstamp;
18  uint64_t nsec = rosNow.toNSec() - diffTime.count();
19  auto rosStamp = rosNow.fromNSec(nsec);
20  opDetectionMsg.header.stamp = rosStamp;
21  opDetectionMsg.header.seq = inNetData->getSequenceNum();
22 #else
23  auto rclNow = rclcpp::Clock().now();
24  auto steadyTime = std::chrono::steady_clock::now();
25  auto diffTime = steadyTime - tstamp;
26  auto rclStamp = rclNow - diffTime;
27  opDetectionMsg.header.stamp = rclStamp;
28 #endif
29 
30  opDetectionMsg.header.frame_id = _frameName;
31  opDetectionMsg.detections.resize(inNetData->detections.size());
32 
33  // TODO(Sachin): check if this works fine for normalized detection
34  // publishing
35  for(int i = 0; i < inNetData->detections.size(); ++i) {
36  int xMin, yMin, xMax, yMax;
37  if(_normalized) {
38  xMin = inNetData->detections[i].xmin;
39  yMin = inNetData->detections[i].ymin;
40  xMax = inNetData->detections[i].xmax;
41  yMax = inNetData->detections[i].ymax;
42  } else {
43  xMin = inNetData->detections[i].xmin * _width;
44  yMin = inNetData->detections[i].ymin * _height;
45  xMax = inNetData->detections[i].xmax * _width;
46  yMax = inNetData->detections[i].ymax * _height;
47  }
48 
49  float xSize = xMax - xMin;
50  float ySize = yMax - yMin;
51  float xCenter = xMin + xSize / 2;
52  float yCenter = yMin + ySize / 2;
53 
54  opDetectionMsg.detections[i].results.resize(1);
55 
56 #ifdef IS_GALACTIC
57  opDetectionMsg.detections[i].id = std::to_string(inNetData->detections[i].label);
58  opDetectionMsg.detections[i].results[0].hypothesis.class_id = std::to_string(inNetData->detections[i].label);
59  opDetectionMsg.detections[i].results[0].hypothesis.score = inNetData->detections[i].confidence;
60 #elif IS_ROS2
61  opDetectionMsg.detections[i].results[0].id = std::to_string(inNetData->detections[i].label);
62  opDetectionMsg.detections[i].results[0].score = inNetData->detections[i].confidence;
63 #else
64  opDetectionMsg.detections[i].results[0].id = inNetData->detections[i].label;
65  opDetectionMsg.detections[i].results[0].score = inNetData->detections[i].confidence;
66 #endif
67  opDetectionMsg.detections[i].bbox.center.x = xCenter;
68  opDetectionMsg.detections[i].bbox.center.y = yCenter;
69  opDetectionMsg.detections[i].bbox.size_x = xSize;
70  opDetectionMsg.detections[i].bbox.size_y = ySize;
71  }
72 
73  opDetectionMsgs.push_back(opDetectionMsg);
74 }
75 
76 Detection2DArrayPtr ImgDetectionConverter::toRosMsgPtr(std::shared_ptr<dai::ImgDetections> inNetData) {
77  std::deque<VisionMsgs::Detection2DArray> msgQueue;
78  toRosMsg(inNetData, msgQueue);
79  auto msg = msgQueue.front();
80 #ifdef IS_ROS2
81  Detection2DArrayPtr ptr = std::make_shared<VisionMsgs::Detection2DArray>(msg);
82 #else
83  Detection2DArrayPtr ptr = boost::make_shared<VisionMsgs::Detection2DArray>(msg);
84 #endif
85  return ptr;
86 }
87 
88 } // namespace ros
89 } // namespace dai
ImgDetectionConverter(std::string frameName, int width, int height, bool normalized=false)
VisionMsgs::Detection2DArray::Ptr Detection2DArrayPtr
Detection2DArrayPtr toRosMsgPtr(std::shared_ptr< dai::ImgDetections > inNetData)
static Time now()
void toRosMsg(std::shared_ptr< dai::ImgDetections > inNetData, std::deque< VisionMsgs::Detection2DArray > &opDetectionMsgs)


depthai_bridge
Author(s): Sachin Guruswamy
autogenerated on Tue May 10 2022 03:01:27