rgb_mobilenet_4k.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 // Includes common necessary includes for development using depthai library
4 #include "depthai/depthai.hpp"
5 
6 // MobilenetSSD label texts
7 static const std::vector<std::string> labelMap = {"background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
8  "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
9  "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
10 
11 int main(int argc, char** argv) {
12  using namespace std;
13  // Default blob path provided by Hunter private data download
14  // Applicable for easier example usage only
15  std::string nnPath(BLOB_PATH);
16 
17  // If path to blob specified, use that
18  if(argc > 1) {
19  nnPath = std::string(argv[1]);
20  }
21 
22  // Print which blob we are using
23  printf("Using blob at path: %s\n", nnPath.c_str());
24 
25  // Create pipeline
26  dai::Pipeline pipeline;
27 
28  // Define sources and outputs
29  auto camRgb = pipeline.create<dai::node::ColorCamera>();
30  auto nn = pipeline.create<dai::node::MobileNetDetectionNetwork>();
31 
32  auto xoutVideo = pipeline.create<dai::node::XLinkOut>();
33  auto xoutPreview = pipeline.create<dai::node::XLinkOut>();
34  auto nnOut = pipeline.create<dai::node::XLinkOut>();
35 
36  xoutVideo->setStreamName("video");
37  xoutPreview->setStreamName("preview");
38  nnOut->setStreamName("nn");
39 
40  // Properties
41  camRgb->setPreviewSize(300, 300); // NN input
43  camRgb->setInterleaved(false);
44  camRgb->setPreviewKeepAspectRatio(false);
45  // Define a neural network that will make predictions based on the source frames
46  nn->setConfidenceThreshold(0.5);
47  nn->setBlobPath(nnPath);
48  nn->setNumInferenceThreads(2);
49  nn->input.setBlocking(false);
50 
51  // Linking
52  camRgb->video.link(xoutVideo->input);
53  camRgb->preview.link(xoutPreview->input);
54  camRgb->preview.link(nn->input);
55  nn->out.link(nnOut->input);
56 
57  // Connect to device and start pipeline
58  dai::Device device(pipeline);
59 
60  // Output queues will be used to get the frames and nn data from the outputs defined above
61  auto qVideo = device.getOutputQueue("video", 4, false);
62  auto qPreview = device.getOutputQueue("preview", 4, false);
63  auto qDet = device.getOutputQueue("nn", 4, false);
64 
65  cv::Mat previewFrame;
66  cv::Mat videoFrame;
67  std::vector<dai::ImgDetection> detections;
68 
69  // Add bounding boxes and text to the frame and show it to the user
70  auto displayFrame = [](std::string name, cv::Mat frame, std::vector<dai::ImgDetection>& detections) {
71  auto color = cv::Scalar(255, 0, 0);
72  // nn data, being the bounding box locations, are in <0..1> range - they need to be normalized with frame width/height
73  for(auto& detection : detections) {
74  int x1 = detection.xmin * frame.cols;
75  int y1 = detection.ymin * frame.rows;
76  int x2 = detection.xmax * frame.cols;
77  int y2 = detection.ymax * frame.rows;
78 
79  uint32_t labelIndex = detection.label;
80  std::string labelStr = to_string(labelIndex);
81  if(labelIndex < labelMap.size()) {
82  labelStr = labelMap[labelIndex];
83  }
84  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
85  std::stringstream confStr;
86  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
87  cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
88  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
89  }
90  // Show the frame
91  cv::imshow(name, frame);
92  };
93 
94  cv::namedWindow("video", cv::WINDOW_NORMAL);
95  cv::resizeWindow("video", 1280, 720);
96  cout << "Resize video window with mouse drag!" << endl;
97 
98  while(true) {
99  // Instead of get (blocking), we use tryGet (non-blocking) which will return the available data or None otherwise
100  auto inVideo = qVideo->tryGet<dai::ImgFrame>();
101  auto inPreview = qPreview->tryGet<dai::ImgFrame>();
102  auto inDet = qDet->tryGet<dai::ImgDetections>();
103 
104  if(inVideo) {
105  videoFrame = inVideo->getCvFrame();
106  }
107 
108  if(inPreview) {
109  previewFrame = inPreview->getCvFrame();
110  }
111 
112  if(inDet) {
113  detections = inDet->detections;
114  }
115 
116  if(!videoFrame.empty()) {
117  displayFrame("video", videoFrame, detections);
118  }
119 
120  if(!previewFrame.empty()) {
121  displayFrame("preview", previewFrame, detections);
122  }
123 
124  int key = cv::waitKey(1);
125  if(key == 'q' || key == 'Q') {
126  return 0;
127  }
128  }
129  return 0;
130 }
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
main
int main(int argc, char **argv)
Definition: rgb_mobilenet_4k.cpp:11
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::ColorCameraProperties::SensorResolution::THE_4_K
@ THE_4_K
3840 × 2160
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
dai::ImgFrame::getCvFrame
void getCvFrame(T...)
Definition: ImgFrame.hpp:222
dai::ImgFrame
Definition: ImgFrame.hpp:25
nanorpc::core::exception::to_string
std::string to_string(std::exception const &e)
Definition: exception.h:46
dai::node::ColorCamera::video
Output video
Definition: ColorCamera.hpp:62
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
dai::node::MobileNetDetectionNetwork
MobileNetDetectionNetwork node. Parses MobileNet results.
Definition: DetectionNetwork.hpp:56
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
dai::ImgDetections
Definition: ImgDetections.hpp:14
labelMap
static const std::vector< std::string > labelMap
Definition: rgb_mobilenet_4k.cpp:7


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