webcam_mobilenet_example.cpp
Go to the documentation of this file.
1 
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 int main(int argc, char** argv) {
10  using namespace std;
11 
12  // Default blob path provided by Hunter private data download
13  // Applicable for easier example usage only
14  std::string nnPath(BLOB_PATH);
15 
16  // If path to blob specified, use that
17  int camId = 0;
18  if(argc > 1) {
19  camId = std::stoi(argv[1]);
20  if(argc > 2) {
21  nnPath = std::string(argv[2]);
22  }
23  }
24 
25  using namespace std;
26 
27  // Create pipeline
28  dai::Pipeline pipeline;
29 
30  // Define sources and outputs
31  auto nn = pipeline.create<dai::node::NeuralNetwork>();
32  auto xin = pipeline.create<dai::node::XLinkIn>();
33  auto xout = pipeline.create<dai::node::XLinkOut>();
34 
35  xin->setStreamName("nn_in");
36  xout->setStreamName("nn_out");
37 
38  // Properties
39  nn->setBlobPath(nnPath);
40 
41  xin->setMaxDataSize(300 * 300 * 3);
42  xin->setNumFrames(4);
43 
44  // Linking
45  xin->out.link(nn->input);
46  nn->out.link(xout->input);
47 
48  // Open Webcam
49  cv::VideoCapture webcam(camId);
50 
51  // Connect to device and start pipeline
52  dai::Device device(pipeline);
53 
54  cv::Mat frame;
55  auto in = device.getInputQueue("nn_in");
56  auto detections = device.getOutputQueue("nn_out");
57 
58  while(true) {
59  // data to send further
60  auto tensor = std::make_shared<dai::RawBuffer>();
61 
62  // Read frame from webcam
63  webcam >> frame;
64 
65  // crop and resize
66  frame = resizeKeepAspectRatio(frame, cv::Size(300, 300), cv::Scalar(0));
67 
68  // transform to BGR planar 300x300
69  toPlanar(frame, tensor->data);
70 
71  // tensor->data = std::vector<std::uint8_t>(frame.data, frame.data + frame.total());
72  in->send(tensor);
73 
74  struct Detection {
75  unsigned int label;
76  float score;
77  float x_min;
78  float y_min;
79  float x_max;
80  float y_max;
81  };
82 
83  vector<Detection> dets;
84 
85  auto det = detections->get<dai::NNData>();
86  std::vector<float> detData = det->getFirstLayerFp16();
87  if(detData.size() > 0) {
88  int i = 0;
89  while(detData[i * 7] != -1.0f) {
90  Detection d;
91  d.label = detData[i * 7 + 1];
92  d.score = detData[i * 7 + 2];
93  d.x_min = detData[i * 7 + 3];
94  d.y_min = detData[i * 7 + 4];
95  d.x_max = detData[i * 7 + 5];
96  d.y_max = detData[i * 7 + 6];
97  i++;
98  dets.push_back(d);
99  }
100  }
101 
102  for(const auto& d : dets) {
103  int x1 = d.x_min * frame.cols;
104  int y1 = d.y_min * frame.rows;
105  int x2 = d.x_max * frame.cols;
106  int y2 = d.y_max * frame.rows;
107 
108  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), cv::Scalar(255, 255, 255));
109  }
110 
111  printf("===================== %lu detection(s) =======================\n", static_cast<unsigned long>(dets.size()));
112  for(unsigned det = 0; det < dets.size(); ++det) {
113  printf("%5d | %6.4f | %7.4f | %7.4f | %7.4f | %7.4f\n",
114  dets[det].label,
115  dets[det].score,
116  dets[det].x_min,
117  dets[det].y_min,
118  dets[det].x_max,
119  dets[det].y_max);
120  }
121 
122  cv::imshow("preview", frame);
123 
124  int key = cv::waitKey(1);
125  if(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
dai::NNData::getFirstLayerFp16
std::vector< float > getFirstLayerFp16() const
Definition: NNData.cpp:255
dai::NNData
Definition: NNData.hpp:16
dai::node::NeuralNetwork
NeuralNetwork node. Runs a neural inference on input data.
Definition: NeuralNetwork.hpp:18
main
int main(int argc, char **argv)
Definition: webcam_mobilenet_example.cpp:9
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
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::Output::link
void link(const Input &in)
Definition: Node.cpp:84
utility.hpp
dai::node::NeuralNetwork::out
Output out
Definition: NeuralNetwork.hpp:39


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