detection_parser.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <cstdio>
3 #include <iostream>
4 
5 #include "utility.hpp"
6 
7 // Includes common necessary includes for development using depthai library
8 #include "depthai/depthai.hpp"
9 
10 // MobilenetSSD label texts
11 static const std::vector<std::string> labelMap = {"background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
12  "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
13  "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
14 
15 static std::atomic<bool> syncNN{true};
16 
17 int main(int argc, char** argv) {
18  using namespace std;
19  using namespace std::chrono;
20  // Default blob path provided by Hunter private data download
21  // Applicable for easier example usage only
22  std::string nnPath(BLOB_PATH);
23 
24  // If path to blob specified, use that
25  if(argc > 1) {
26  nnPath = std::string(argv[1]);
27  }
28 
29  // Print which blob we are using
30  printf("Using blob at path: %s\n", nnPath.c_str());
31 
32  // Create pipeline
33  dai::Pipeline pipeline;
34 
35  // Define sources and outputs
36  auto camRgb = pipeline.create<dai::node::ColorCamera>();
37  auto nn = pipeline.create<dai::node::NeuralNetwork>();
38  auto det = pipeline.create<dai::node::DetectionParser>();
39  auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
40  auto nnOut = pipeline.create<dai::node::XLinkOut>();
41 
42  xoutRgb->setStreamName("rgb");
43  nnOut->setStreamName("nn");
44 
45  // Properties
46  camRgb->setPreviewSize(300, 300); // NN input
47  camRgb->setInterleaved(false);
48  camRgb->setFps(40);
49  // Define a neural network that will make predictions based on the source frames
50  nn->setNumInferenceThreads(2);
51  nn->input.setBlocking(false);
52 
53  dai::OpenVINO::Blob blob(nnPath);
54  nn->setBlob(blob);
55  det->setBlob(blob);
56  det->setNNFamily(DetectionNetworkType::MOBILENET);
57  det->setConfidenceThreshold(0.5);
58 
59  // Linking
60  if(syncNN) {
61  nn->passthrough.link(xoutRgb->input);
62  } else {
63  camRgb->preview.link(xoutRgb->input);
64  }
65 
66  camRgb->preview.link(nn->input);
67  nn->out.link(det->input);
68  det->out.link(nnOut->input);
69 
70  // Connect to device and start pipeline
71  dai::Device device(pipeline);
72 
73  // Output queues will be used to get the rgb frames and nn data from the outputs defined above
74  auto qRgb = device.getOutputQueue("rgb", 4, false);
75  auto qDet = device.getOutputQueue("nn", 4, false);
76 
77  cv::Mat frame;
78  std::vector<dai::ImgDetection> detections;
79  auto startTime = steady_clock::now();
80  int counter = 0;
81  float fps = 0;
82  auto color2 = cv::Scalar(255, 255, 255);
83 
84  // Add bounding boxes and text to the frame and show it to the user
85  auto displayFrame = [](std::string name, cv::Mat frame, std::vector<dai::ImgDetection>& detections) {
86  auto color = cv::Scalar(255, 0, 0);
87  // nn data, being the bounding box locations, are in <0..1> range - they need to be normalized with frame width/height
88  for(auto& detection : detections) {
89  int x1 = detection.xmin * frame.cols;
90  int y1 = detection.ymin * frame.rows;
91  int x2 = detection.xmax * frame.cols;
92  int y2 = detection.ymax * frame.rows;
93 
94  uint32_t labelIndex = detection.label;
95  std::string labelStr = to_string(labelIndex);
96  if(labelIndex < labelMap.size()) {
97  labelStr = labelMap[labelIndex];
98  }
99  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
100  std::stringstream confStr;
101  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
102  cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
103  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
104  }
105  // Show the frame
106  cv::imshow(name, frame);
107  };
108 
109  while(true) {
110  std::shared_ptr<dai::ImgFrame> inRgb;
111  std::shared_ptr<dai::ImgDetections> inDet;
112 
113  if(syncNN) {
114  inRgb = qRgb->get<dai::ImgFrame>();
115  inDet = qDet->get<dai::ImgDetections>();
116  } else {
117  inRgb = qRgb->tryGet<dai::ImgFrame>();
118  inDet = qDet->tryGet<dai::ImgDetections>();
119  }
120 
121  counter++;
122  auto currentTime = steady_clock::now();
123  auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
124  if(elapsed > seconds(1)) {
125  fps = counter / elapsed.count();
126  counter = 0;
127  startTime = currentTime;
128  }
129 
130  if(inRgb) {
131  frame = inRgb->getCvFrame();
132  std::stringstream fpsStr;
133  fpsStr << "NN fps: " << std::fixed << std::setprecision(2) << fps;
134  cv::putText(frame, fpsStr.str(), cv::Point(2, inRgb->getHeight() - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color2);
135  }
136 
137  if(inDet) {
138  detections = inDet->detections;
139  }
140 
141  if(!frame.empty()) {
142  displayFrame("video", frame, detections);
143  }
144 
145  int key = cv::waitKey(1);
146  if(key == 'q' || key == 'Q') {
147  return 0;
148  }
149  }
150  return 0;
151 }
dai::node::NeuralNetwork::setBlob
void setBlob(OpenVINO::Blob blob)
Definition: NeuralNetwork.cpp:34
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
labelMap
static const std::vector< std::string > labelMap
Definition: detection_parser.cpp:11
dai::OpenVINO::Blob
OpenVINO Blob.
Definition: OpenVINO.hpp:23
fps
static constexpr int fps
Definition: rgb_depth_aligned.cpp:12
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::node::NeuralNetwork
NeuralNetwork node. Runs a neural inference on input data.
Definition: NeuralNetwork.hpp:18
dai::node::NeuralNetwork::input
Input input
Definition: NeuralNetwork.hpp:34
dai::node::DetectionParser::input
Input input
Definition: DetectionParser.hpp:36
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
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: detection_parser.cpp:17
dai::ImgFrame
Definition: ImgFrame.hpp:25
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::DetectionParser
DetectionParser node. Parses detection results from different neural networks and is being used inter...
Definition: DetectionParser.hpp:18
std
Definition: Node.hpp:366
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
syncNN
static std::atomic< bool > syncNN
Definition: detection_parser.cpp:15
dai::ImgDetections
Definition: ImgDetections.hpp:14
utility.hpp
dai::node::NeuralNetwork::out
Output out
Definition: NeuralNetwork.hpp:39
DetectionNetworkType::MOBILENET
@ MOBILENET


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