video_mobilenet.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 
4 #include "utility.hpp"
5 
6 // Includes common necessary includes for development using depthai library
7 #include "depthai/depthai.hpp"
8 
9 // MobilenetSSD label texts
10 static const std::vector<std::string> labelMap = {"background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
11  "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
12  "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
13 
14 int main(int argc, char** argv) {
15  using namespace std;
16  using namespace std::chrono;
17  // Default blob path provided by Hunter private data download
18  // Applicable for easier example usage only
19  std::string nnPath(BLOB_PATH);
20  std::string videoPath(VIDEO_PATH);
21 
22  // If path to blob specified, use that
23  if(argc > 2) {
24  nnPath = std::string(argv[1]);
25  videoPath = std::string(argv[2]);
26  }
27 
28  // Print which blob we are using
29  printf("Using blob at path: %s\n", nnPath.c_str());
30  printf("Using video at path: %s\n", videoPath.c_str());
31 
32  // Create pipeline
33  dai::Pipeline pipeline;
34 
35  // Define source and outputs
36  auto nn = pipeline.create<dai::node::MobileNetDetectionNetwork>();
37 
38  auto xinFrame = pipeline.create<dai::node::XLinkIn>();
39  auto nnOut = pipeline.create<dai::node::XLinkOut>();
40 
41  xinFrame->setStreamName("inFrame");
42  nnOut->setStreamName("nn");
43 
44  // Properties
45  nn->setConfidenceThreshold(0.5);
46  nn->setBlobPath(nnPath);
47  nn->setNumInferenceThreads(2);
48  nn->input.setBlocking(false);
49 
50  // Linking
51  xinFrame->out.link(nn->input);
52  nn->out.link(nnOut->input);
53 
54  // Connect to device and start pipeline
55  dai::Device device(pipeline);
56 
57  // Input queue will be used to send video frames to the device.
58  auto qIn = device.getInputQueue("inFrame");
59  // Output queue will be used to get nn data from the video frames.
60  auto qDet = device.getOutputQueue("nn", 4, false);
61 
62  // Add bounding boxes and text to the frame and show it to the user
63  auto displayFrame = [](std::string name, cv::Mat frame, std::vector<dai::ImgDetection>& detections) {
64  auto color = cv::Scalar(255, 0, 0);
65  // nn data, being the bounding box locations, are in <0..1> range - they need to be normalized with frame width/height
66  for(auto& detection : detections) {
67  int x1 = detection.xmin * frame.cols;
68  int y1 = detection.ymin * frame.rows;
69  int x2 = detection.xmax * frame.cols;
70  int y2 = detection.ymax * frame.rows;
71 
72  uint32_t labelIndex = detection.label;
73  std::string labelStr = to_string(labelIndex);
74  if(labelIndex < labelMap.size()) {
75  labelStr = labelMap[labelIndex];
76  }
77  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
78  std::stringstream confStr;
79  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
80  cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
81  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
82  }
83  // Show the frame
84  cv::imshow(name, frame);
85  };
86 
87  cv::Mat frame;
88  cv::VideoCapture cap(videoPath);
89 
90  cv::namedWindow("inFrame", cv::WINDOW_NORMAL);
91  cv::resizeWindow("inFrame", 1280, 720);
92  std::cout << "Resize video window with mouse drag!" << std::endl;
93 
94  while(cap.isOpened()) {
95  // Read frame from video
96  cap >> frame;
97  if(frame.empty()) break;
98 
99  auto img = std::make_shared<dai::ImgFrame>();
100  frame = resizeKeepAspectRatio(frame, cv::Size(300, 300), cv::Scalar(0));
101  toPlanar(frame, img->getData());
102  img->setTimestamp(steady_clock::now());
103  img->setWidth(300);
104  img->setHeight(300);
105  qIn->send(img);
106 
107  auto inDet = qDet->get<dai::ImgDetections>();
108  auto detections = inDet->detections;
109 
110  displayFrame("inFrame", frame, detections);
111 
112  int key = cv::waitKey(1);
113  if(key == 'q' || key == 'Q') return 0;
114  }
115  return 0;
116 }
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
dai::ImgDetections::detections
std::vector< ImgDetection > & detections
Detections.
Definition: ImgDetections.hpp:25
labelMap
static const std::vector< std::string > labelMap
Definition: video_mobilenet.cpp:10
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
toPlanar
void toPlanar(cv::Mat &bgr, std::vector< std::uint8_t > &data)
Definition: utility.cpp:100
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::XLinkIn::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkIn.cpp:12
nanorpc::core::exception::to_string
std::string to_string(std::exception const &e)
Definition: exception.h:46
dai::Device
Definition: Device.hpp:21
resizeKeepAspectRatio
cv::Mat resizeKeepAspectRatio(const cv::Mat &input, const cv::Size &dstSize, const cv::Scalar &bgcolor)
Definition: utility.cpp:113
std
Definition: Node.hpp:366
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::node::MobileNetDetectionNetwork
MobileNetDetectionNetwork node. Parses MobileNet results.
Definition: DetectionNetwork.hpp:56
main
int main(int argc, char **argv)
Definition: video_mobilenet.cpp:14
dai::ImgDetections
Definition: ImgDetections.hpp:14
utility.hpp


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