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


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