mono_mobilenet.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "utility.hpp"
4 
5 // Includes common necessary includes for development using depthai library
6 #include "depthai/depthai.hpp"
7 
8 // MobilenetSSD label texts
9 static const std::vector<std::string> labelMap = {"background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
10  "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
11  "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
12 
13 int main(int argc, char** argv) {
14  using namespace std;
15  // Default blob path provided by Hunter private data download
16  // Applicable for easier example usage only
17  std::string nnPath(BLOB_PATH);
18 
19  // If path to blob specified, use that
20  if(argc > 1) {
21  nnPath = std::string(argv[1]);
22  }
23 
24  // Print which blob we are using
25  printf("Using blob at path: %s\n", nnPath.c_str());
26 
27  // Create pipeline
28  dai::Pipeline pipeline;
29 
30  // Define sources and outputs
31  auto monoRight = pipeline.create<dai::node::MonoCamera>();
32  auto manip = pipeline.create<dai::node::ImageManip>();
33  auto nn = pipeline.create<dai::node::MobileNetDetectionNetwork>();
34  auto manipOut = pipeline.create<dai::node::XLinkOut>();
35  auto nnOut = pipeline.create<dai::node::XLinkOut>();
36 
37  manipOut->setStreamName("right");
38  nnOut->setStreamName("nn");
39 
40  // Properties
41  monoRight->setCamera("right");
43 
44  // Convert the grayscale frame into the nn-acceptable form
45  manip->initialConfig.setResize(300, 300);
46  // The NN model expects BGR input. By default ImageManip output type would be same as input (gray in this case)
47  manip->initialConfig.setFrameType(dai::ImgFrame::Type::BGR888p);
48 
49  nn->setConfidenceThreshold(0.5);
50  nn->setBlobPath(nnPath);
51  nn->setNumInferenceThreads(2);
52  nn->input.setBlocking(false);
53 
54  // Linking
55  monoRight->out.link(manip->inputImage);
56  manip->out.link(nn->input);
57  manip->out.link(manipOut->input);
58  nn->out.link(nnOut->input);
59 
60  // Connect to device and start pipeline
61  dai::Device device(pipeline);
62 
63  // Output queues will be used to get the grayscale frames and nn data from the outputs defined above
64  auto qRight = device.getOutputQueue("right", 4, false);
65  auto qDet = device.getOutputQueue("nn", 4, false);
66 
67  cv::Mat frame;
68  std::vector<dai::ImgDetection> detections;
69 
70  // Add bounding boxes and text to the frame and show it to the user
71  auto displayFrame = [](std::string name, cv::Mat frame, std::vector<dai::ImgDetection>& detections) {
72  auto color = cv::Scalar(255, 0, 0);
73  // nn data, being the bounding box locations, are in <0..1> range - they need to be normalized with frame width/height
74  for(auto& detection : detections) {
75  int x1 = detection.xmin * frame.cols;
76  int y1 = detection.ymin * frame.rows;
77  int x2 = detection.xmax * frame.cols;
78  int y2 = detection.ymax * frame.rows;
79 
80  uint32_t labelIndex = detection.label;
81  std::string labelStr = to_string(labelIndex);
82  if(labelIndex < labelMap.size()) {
83  labelStr = labelMap[labelIndex];
84  }
85  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
86  std::stringstream confStr;
87  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
88  cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
89  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
90  }
91  // Show the frame
92  cv::imshow(name, frame);
93  };
94 
95  while(true) {
96  // Instead of get (blocking), we use tryGet (non-blocking) which will return the available data or None otherwise
97  auto inRight = qRight->tryGet<dai::ImgFrame>();
98  auto inDet = qDet->tryGet<dai::ImgDetections>();
99 
100  if(inRight) {
101  frame = inRight->getCvFrame();
102  }
103 
104  if(inDet) {
105  detections = inDet->detections;
106  }
107 
108  if(!frame.empty()) {
109  displayFrame("right", frame, detections);
110  }
111 
112  int key = cv::waitKey(1);
113  if(key == 'q' || key == 'Q') return 0;
114  }
115  return 0;
116 }
dai::node::MonoCamera::out
Output out
Definition: MonoCamera.hpp:47
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: mono_mobilenet.cpp:13
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::RawImgFrame::Type::BGR888p
@ BGR888p
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::MonoCameraProperties::SensorResolution::THE_720_P
@ THE_720_P
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::node::ImageManip::out
Output out
Definition: ImageManip.hpp:51
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::ImageManip
ImageManip node. Capability to crop, resize, warp, ... incoming image frames.
Definition: ImageManip.hpp:15
dai::Device
Definition: Device.hpp:21
labelMap
static const std::vector< std::string > labelMap
Definition: mono_mobilenet.cpp:9
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
utility.hpp


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