rgb_encoding_mono_mobilenet.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 monoRight = pipeline.create<dai::node::MonoCamera>();
31  auto videoEncoder = pipeline.create<dai::node::VideoEncoder>();
32  auto nn = pipeline.create<dai::node::MobileNetDetectionNetwork>();
33  auto manip = pipeline.create<dai::node::ImageManip>();
34 
35  auto videoOut = pipeline.create<dai::node::XLinkOut>();
36  auto xoutRight = pipeline.create<dai::node::XLinkOut>();
37  auto manipOut = pipeline.create<dai::node::XLinkOut>();
38  auto nnOut = pipeline.create<dai::node::XLinkOut>();
39 
40  videoOut->setStreamName("h265");
41  xoutRight->setStreamName("right");
42  manipOut->setStreamName("manip");
43  nnOut->setStreamName("nn");
44 
45  // Properties
46  camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
48  monoRight->setCamera("right");
50  videoEncoder->setDefaultProfilePreset(30, dai::VideoEncoderProperties::Profile::H265_MAIN);
51 
52  nn->setConfidenceThreshold(0.5);
53  nn->setBlobPath(nnPath);
54  nn->setNumInferenceThreads(2);
55  nn->input.setBlocking(false);
56 
57  // The NN model expects BGR input. By default ImageManip output type would be same as input (gray in this case)
58  manip->initialConfig.setFrameType(dai::ImgFrame::Type::BGR888p);
59  manip->initialConfig.setResize(300, 300);
60 
61  // Linking
62  camRgb->video.link(videoEncoder->input);
63  videoEncoder->bitstream.link(videoOut->input);
64  monoRight->out.link(manip->inputImage);
65  manip->out.link(nn->input);
66  monoRight->out.link(xoutRight->input);
67  manip->out.link(manipOut->input);
68  nn->out.link(nnOut->input);
69 
70  // Connect to device and start pipeline
71  dai::Device device(pipeline);
72 
73  // Queues
74  int queueSize = 8;
75  auto qRight = device.getOutputQueue("right", queueSize);
76  auto qManip = device.getOutputQueue("manip", queueSize);
77  auto qDet = device.getOutputQueue("nn", queueSize);
78  auto qRgbEnc = device.getOutputQueue("h265", 30, true);
79 
80  cv::Mat frame;
81  cv::Mat frameManip;
82  std::vector<dai::ImgDetection> detections;
83  int offsetX = (monoRight->getResolutionWidth() - monoRight->getResolutionHeight()) / 2;
84  auto color = cv::Scalar(255, 0, 0);
85 
86  auto videoFile = std::ofstream("video.h265", std::ios::binary);
87  cv::namedWindow("right", cv::WINDOW_NORMAL);
88  cv::namedWindow("manip", cv::WINDOW_NORMAL);
89 
90  while(true) {
91  auto inRight = qRight->tryGet<dai::ImgFrame>();
92  auto inManip = qManip->tryGet<dai::ImgFrame>();
93  auto inDet = qDet->tryGet<dai::ImgDetections>();
94 
95  auto out1 = qRgbEnc->get<dai::ImgFrame>();
96  videoFile.write((char*)out1->getData().data(), out1->getData().size());
97 
98  if(inRight) {
99  frame = inRight->getCvFrame();
100  }
101 
102  if(inManip) {
103  frameManip = inManip->getCvFrame();
104  }
105 
106  if(inDet) {
107  detections = inDet->detections;
108  }
109 
110  if(!frame.empty()) {
111  for(auto& detection : detections) {
112  int x1 = detection.xmin * monoRight->getResolutionHeight() + offsetX;
113  int y1 = detection.ymin * monoRight->getResolutionHeight();
114  int x2 = detection.xmax * monoRight->getResolutionHeight() + offsetX;
115  int y2 = detection.ymax * monoRight->getResolutionHeight();
116 
117  uint32_t labelIndex = detection.label;
118  std::string labelStr = to_string(labelIndex);
119  if(labelIndex < labelMap.size()) {
120  labelStr = labelMap[labelIndex];
121  }
122  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
123  std::stringstream confStr;
124  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
125  cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
126  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
127  }
128  // Show the frame
129  cv::imshow("right", frame);
130  }
131 
132  if(!frameManip.empty()) {
133  for(auto& detection : detections) {
134  int x1 = detection.xmin * frameManip.cols;
135  int y1 = detection.ymin * frameManip.rows;
136  int x2 = detection.xmax * frameManip.cols;
137  int y2 = detection.ymax * frameManip.rows;
138 
139  uint32_t labelIndex = detection.label;
140  std::string labelStr = to_string(labelIndex);
141  if(labelIndex < labelMap.size()) {
142  labelStr = labelMap[labelIndex];
143  }
144  cv::putText(frameManip, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
145  std::stringstream confStr;
146  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
147  cv::putText(frameManip, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
148  cv::rectangle(frameManip, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
149  }
150  // Show the frame
151  cv::imshow("manip", frameManip);
152  }
153 
154  int key = cv::waitKey(1);
155  if(key == 'q' || key == 'Q') {
156  break;
157  }
158  }
159  cout << "To view the encoded data, convert the stream file (.h265) into a video file (.mp4), using a command below:" << endl;
160  cout << "ffmpeg -framerate 30 -i video.h265 -c copy video.mp4" << endl;
161  return 0;
162 }
labelMap
static const std::vector< std::string > labelMap
Definition: rgb_encoding_mono_mobilenet.cpp:7
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::node::ColorCamera::setResolution
void setResolution(Properties::SensorResolution resolution)
Set sensor resolution.
Definition: ColorCamera.cpp:169
dai::node::ColorCamera::getResolutionWidth
int getResolutionWidth() const
Get sensor resolution width.
Definition: ColorCamera.cpp:429
dai::CameraBoardSocket::CAM_A
@ CAM_A
dai::node::MonoCamera
MonoCamera node. For use with grayscale sensors.
Definition: MonoCamera.hpp:17
dai::Buffer::getData
std::vector< std::uint8_t > & getData() const
Get non-owning reference to internal buffer.
Definition: Buffer.cpp:13
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
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::node::VideoEncoder::out
Output out
Definition: VideoEncoder.hpp:35
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::ImageManip
ImageManip node. Capability to crop, resize, warp, ... incoming image frames.
Definition: ImageManip.hpp:15
dai::node::ColorCamera::getResolutionHeight
int getResolutionHeight() const
Get sensor resolution height.
Definition: ColorCamera.cpp:433
dai::Node::Input::setBlocking
void setBlocking(bool blocking)
Definition: Node.cpp:94
dai::ColorCameraProperties::SensorResolution::THE_1080_P
@ THE_1080_P
1920 × 1080
dai::Device
Definition: Device.hpp:21
dai::VideoEncoderProperties::Profile::H265_MAIN
@ H265_MAIN
std
Definition: Node.hpp:366
main
int main(int argc, char **argv)
Definition: rgb_encoding_mono_mobilenet.cpp:11
dai::node::VideoEncoder
VideoEncoder node. Encodes frames into MJPEG, H264 or H265.
Definition: VideoEncoder.hpp:14
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::node::VideoEncoder::input
Input input
Definition: VideoEncoder.hpp:25
dai::ImgDetections
Definition: ImgDetections.hpp:14
dai::node::ColorCamera::setCamera
void setCamera(std::string name)
Definition: ColorCamera.cpp:44


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