spatial_tiny_yolo_tof.cpp
Go to the documentation of this file.
1 #include <atomic>
2 #include <chrono>
3 #include <depthai/depthai.hpp>
4 #include <iostream>
5 #include <opencv2/opencv.hpp>
6 
7 constexpr auto FPS = 15;
8 
9 static const std::vector<std::string> labelMap = {
10  "person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
11  "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse",
12  "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag",
13  "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
14  "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon",
15  "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza",
16  "donut", "cake", "chair", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor",
17  "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink",
18  "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"};
19 
20 static std::atomic<bool> syncNN{true};
21 
22 int main(int argc, char** argv) {
23  dai::Pipeline pipeline;
24 
25  auto camRgb = pipeline.create<dai::node::ColorCamera>();
26  auto spatialDetectionNetwork = pipeline.create<dai::node::YoloSpatialDetectionNetwork>();
27  auto tof = pipeline.create<dai::node::ToF>();
28  auto camTof = pipeline.create<dai::node::Camera>();
29  auto imageAlign = pipeline.create<dai::node::ImageAlign>();
30 
31  auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
32  auto xoutNN = pipeline.create<dai::node::XLinkOut>();
33  auto xoutDepth = pipeline.create<dai::node::XLinkOut>();
34 
35  xoutRgb->setStreamName("rgb");
36  xoutNN->setStreamName("detections");
37  xoutDepth->setStreamName("depth");
38 
39  camRgb->setPreviewSize(416, 416);
41  camRgb->setInterleaved(false);
42  camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::BGR);
43  camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_B);
44  camRgb->setFps(FPS);
45 
46  camTof->setFps(FPS);
47  camTof->setImageOrientation(dai::CameraImageOrientation::ROTATE_180_DEG);
48  camTof->setBoardSocket(dai::CameraBoardSocket::CAM_A);
49 
50  imageAlign->setOutputSize(640, 400);
51 
52  spatialDetectionNetwork->setBlobPath(BLOB_PATH);
53  spatialDetectionNetwork->setConfidenceThreshold(0.5f);
54  spatialDetectionNetwork->input.setBlocking(false);
55  spatialDetectionNetwork->setBoundingBoxScaleFactor(0.5);
56  spatialDetectionNetwork->setDepthLowerThreshold(100);
57  spatialDetectionNetwork->setDepthUpperThreshold(5000);
58 
59  spatialDetectionNetwork->setNumClasses(80);
60  spatialDetectionNetwork->setCoordinateSize(4);
61  spatialDetectionNetwork->setAnchors({10, 14, 23, 27, 37, 58, 81, 82, 135, 169, 344, 319});
62  spatialDetectionNetwork->setAnchorMasks({{"side26", {1, 2, 3}}, {"side13", {3, 4, 5}}});
63  spatialDetectionNetwork->setIouThreshold(0.5f);
64 
65  camTof->raw.link(tof->input);
66  tof->depth.link(imageAlign->input);
67 
68  camRgb->preview.link(spatialDetectionNetwork->input);
69  if(syncNN) {
70  spatialDetectionNetwork->passthrough.link(xoutRgb->input);
71  } else {
72  camRgb->preview.link(xoutRgb->input);
73  }
74 
75  spatialDetectionNetwork->out.link(xoutNN->input);
76 
77  camRgb->isp.link(imageAlign->inputAlignTo);
78  imageAlign->outputAligned.link(spatialDetectionNetwork->inputDepth);
79  spatialDetectionNetwork->passthroughDepth.link(xoutDepth->input);
80 
81  dai::Device device(pipeline);
82 
83  auto previewQueue = device.getOutputQueue("rgb", 4, false);
84  auto detectionNNQueue = device.getOutputQueue("detections", 4, false);
85  auto depthQueue = device.getOutputQueue("depth", 4, false);
86 
87  auto startTime = std::chrono::steady_clock::now();
88  int counter = 0;
89  float fps = 0;
90  auto color = cv::Scalar(255, 255, 255);
91 
92  while(true) {
93  auto inPreview = previewQueue->get<dai::ImgFrame>();
94  auto inDet = detectionNNQueue->get<dai::SpatialImgDetections>();
95  auto depth = depthQueue->get<dai::ImgFrame>();
96 
97  cv::Mat frame = inPreview->getCvFrame();
98  cv::Mat depthFrame = depth->getFrame(); // depthFrame values are in millimeters
99 
100  cv::Mat depthFrameColor;
101  cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
102  cv::equalizeHist(depthFrameColor, depthFrameColor);
103  cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_HOT);
104 
105  counter++;
106  auto currentTime = std::chrono::steady_clock::now();
107  auto elapsed = std::chrono::duration_cast<std::chrono::duration<float>>(currentTime - startTime);
108  if(elapsed > std::chrono::seconds(1)) {
109  fps = counter / elapsed.count();
110  counter = 0;
111  startTime = currentTime;
112  }
113 
114  auto detections = inDet->detections;
115 
116  for(const auto& detection : detections) {
117  auto roiData = detection.boundingBoxMapping;
118  auto roi = roiData.roi;
119  roi = roi.denormalize(depthFrameColor.cols, depthFrameColor.rows);
120  auto topLeft = roi.topLeft();
121  auto bottomRight = roi.bottomRight();
122  auto xmin = static_cast<int>(topLeft.x);
123  auto ymin = static_cast<int>(topLeft.y);
124  auto xmax = static_cast<int>(bottomRight.x);
125  auto ymax = static_cast<int>(bottomRight.y);
126  cv::rectangle(depthFrameColor, cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax)), color, 1);
127 
128  int x1 = detection.xmin * frame.cols;
129  int y1 = detection.ymin * frame.rows;
130  int x2 = detection.xmax * frame.cols;
131  int y2 = detection.ymax * frame.rows;
132 
133  uint32_t labelIndex = detection.label;
134  std::string labelStr = std::to_string(labelIndex);
135  if(labelIndex < labelMap.size()) {
136  labelStr = labelMap[labelIndex];
137  }
138  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
139  std::stringstream confStr;
140  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
141  cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 35), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
142 
143  std::stringstream depthX;
144  depthX << "X: " << static_cast<int>(detection.spatialCoordinates.x) << " mm";
145  cv::putText(frame, depthX.str(), cv::Point(x1 + 10, y1 + 50), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
146  std::stringstream depthY;
147  depthY << "Y: " << static_cast<int>(detection.spatialCoordinates.y) << " mm";
148  cv::putText(frame, depthY.str(), cv::Point(x1 + 10, y1 + 65), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
149  std::stringstream depthZ;
150  depthZ << "Z: " << static_cast<int>(detection.spatialCoordinates.z) << " mm";
151  cv::putText(frame, depthZ.str(), cv::Point(x1 + 10, y1 + 80), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
152 
153  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
154  }
155 
156  std::stringstream fpsStr;
157  fpsStr << std::fixed << std::setprecision(2) << fps;
158  cv::putText(frame, fpsStr.str(), cv::Point(2, frame.rows - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color);
159 
160  cv::imshow("depth", depthFrameColor);
161  cv::imshow("rgb", frame);
162 
163  if(cv::waitKey(1) == 'q') {
164  break;
165  }
166  }
167 
168  return 0;
169 }
syncNN
static std::atomic< bool > syncNN
Definition: spatial_tiny_yolo_tof.cpp:20
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
fps
static constexpr int fps
Definition: rgb_depth_aligned.cpp:12
dai::CameraBoardSocket::CAM_A
@ CAM_A
dai::node::ImageAlign::input
Input input
Definition: ImageAlign.hpp:45
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::SpatialImgDetections
Definition: SpatialImgDetections.hpp:15
dai::CameraBoardSocket::CAM_B
@ CAM_B
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::node::ColorCamera::isp
Output isp
Definition: ColorCamera.hpp:83
dai::node::YoloSpatialDetectionNetwork
Definition: SpatialDetectionNetwork.hpp:117
dai::ColorCameraProperties::SensorResolution::THE_800_P
@ THE_800_P
1280 × 800
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
main
int main(int argc, char **argv)
Definition: spatial_tiny_yolo_tof.cpp:22
labelMap
static const std::vector< std::string > labelMap
Definition: spatial_tiny_yolo_tof.cpp:9
dai::ImgFrame::getCvFrame
void getCvFrame(T...)
Definition: ImgFrame.hpp:222
dai::node::ImageAlign
ImageAlign node. Calculates spatial location data on a set of ROIs on depth map.
Definition: ImageAlign.hpp:16
dai::ColorCameraProperties::ColorOrder::BGR
@ BGR
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::CameraImageOrientation::ROTATE_180_DEG
@ ROTATE_180_DEG
nanorpc::core::exception::to_string
std::string to_string(std::exception const &e)
Definition: exception.h:46
dai::Device
Definition: Device.hpp:21
dai::node::ColorCamera::preview
Output preview
Definition: ColorCamera.hpp:69
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
FPS
constexpr auto FPS
Definition: spatial_tiny_yolo_tof.cpp:7
dai::node::Camera
Camera node. Experimental node, for both mono and color types of sensors.
Definition: Camera.hpp:18
dai::node::ToF
ToF node.
Definition: ToF.hpp:16


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19