rgb_encoding_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 videoEncoder = pipeline.create<dai::node::VideoEncoder>();
31  auto nn = pipeline.create<dai::node::MobileNetDetectionNetwork>();
32 
33  auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
34  auto videoOut = pipeline.create<dai::node::XLinkOut>();
35  auto nnOut = pipeline.create<dai::node::XLinkOut>();
36 
37  xoutRgb->setStreamName("rgb");
38  videoOut->setStreamName("h265");
39  nnOut->setStreamName("nn");
40 
41  // Properties
42  camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
44  camRgb->setPreviewSize(300, 300);
45  camRgb->setInterleaved(false);
46 
47  videoEncoder->setDefaultProfilePreset(30, dai::VideoEncoderProperties::Profile::H265_MAIN);
48 
49  nn->setConfidenceThreshold(0.5);
50  nn->setBlobPath(nnPath);
51  nn->setNumInferenceThreads(2);
52  nn->input.setBlocking(false);
53 
54  // Linking
55  camRgb->video.link(videoEncoder->input);
56  camRgb->preview.link(xoutRgb->input);
57  camRgb->preview.link(nn->input);
58  videoEncoder->bitstream.link(videoOut->input);
59  nn->out.link(nnOut->input);
60 
61  // Connect to device and start pipeline
62  dai::Device device(pipeline);
63 
64  // Queues
65  int queueSize = 8;
66  auto qRgb = device.getOutputQueue("rgb", queueSize);
67  auto qDet = device.getOutputQueue("nn", queueSize);
68  auto qRgbEnc = device.getOutputQueue("h265", 30, true);
69 
70  cv::Mat frame;
71  std::vector<dai::ImgDetection> detections;
72 
73  // Add bounding boxes and text to the frame and show it to the user
74  auto displayFrame = [](std::string name, cv::Mat frame, std::vector<dai::ImgDetection>& detections) {
75  auto color = cv::Scalar(255, 0, 0);
76  // nn data, being the bounding box locations, are in <0..1> range - they need to be normalized with frame width/height
77  for(auto& detection : detections) {
78  int x1 = detection.xmin * frame.cols;
79  int y1 = detection.ymin * frame.rows;
80  int x2 = detection.xmax * frame.cols;
81  int y2 = detection.ymax * frame.rows;
82 
83  uint32_t labelIndex = detection.label;
84  std::string labelStr = to_string(labelIndex);
85  if(labelIndex < labelMap.size()) {
86  labelStr = labelMap[labelIndex];
87  }
88  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
89  std::stringstream confStr;
90  confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
91  cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
92  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
93  }
94  // Show the frame
95  cv::imshow(name, frame);
96  };
97 
98  auto videoFile = std::ofstream("video.h264", std::ios::binary);
99 
100  while(true) {
101  auto inRgb = qRgb->tryGet<dai::ImgFrame>();
102  auto inDet = qDet->tryGet<dai::ImgDetections>();
103 
104  auto out = qRgbEnc->get<dai::ImgFrame>();
105  videoFile.write((char*)out->getData().data(), out->getData().size());
106 
107  if(inRgb) {
108  frame = inRgb->getCvFrame();
109  }
110 
111  if(inDet) {
112  detections = inDet->detections;
113  }
114 
115  if(!frame.empty()) {
116  displayFrame("rgb", frame, detections);
117  }
118 
119  int key = cv::waitKey(1);
120  if(key == 'q' || key == 'Q') {
121  break;
122  }
123  }
124  cout << "To view the encoded data, convert the stream file (.h265) into a video file (.mp4), using a command below:" << endl;
125  cout << "ffmpeg -framerate 30 -i video.h264 -c copy video.mp4" << endl;
126  return 0;
127 }
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::CameraBoardSocket::CAM_A
@ CAM_A
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::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
main
int main(int argc, char **argv)
Definition: rgb_encoding_mobilenet.cpp:11
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::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
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
labelMap
static const std::vector< std::string > labelMap
Definition: rgb_encoding_mobilenet.cpp:7
dai::ImgDetections
Definition: ImgDetections.hpp:14


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