normalization_multi_input.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <cstdio>
3 #include <iostream>
4 
5 // Inludes common necessary includes for development using depthai library
6 #include "depthai/depthai.hpp"
7 #include "utility.hpp"
8 
9 int main(int argc, char** argv) {
10  using namespace std;
11  // Default blob path provided by Hunter private data download
12  // Applicable for easier example usage only
13  std::string nnPath(BLOB_PATH);
14 
15  // If path to blob specified, use that
16  if(argc > 1) {
17  nnPath = std::string(argv[1]);
18  }
19 
20  // Print which blob we are using
21  printf("Using blob at path: %s\n", nnPath.c_str());
22 
23  // Create pipeline
24  dai::Pipeline pipeline;
25  pipeline.setOpenVINOVersion(dai::OpenVINO::Version::VERSION_2021_4);
26 
27  // Define sources and outputs
28  auto camRgb = pipeline.create<dai::node::ColorCamera>();
29  // Model expects values in FP16, as we have compiled it with `-ip FP16`
30  camRgb->setFp16(true);
31  camRgb->setInterleaved(false);
32  camRgb->setPreviewSize(300, 300); // NN input
33 
34  auto nn = pipeline.create<dai::node::NeuralNetwork>();
35  nn->setBlobPath(nnPath);
36  nn->setNumInferenceThreads(2);
37 
38  auto script = pipeline.create<dai::node::Script>();
39  script->setScript(R"(
40  # Run script only once
41  # Model formula:
42  # output = (input - mean) / scale
43 
44  # This configuration will subtract all frame values (pixels) by 127.5
45  # 0.0 .. 255.0 -> -127.5 .. 127.5
46  data = NNData(2)
47  data.setLayer("mean", [127.5])
48  node.io['mean'].send(data)
49 
50  # This configuration will divide all frame values (pixels) by 255.0
51  # -127.5 .. 127.5 -> -0.5 .. 0.5
52  data = NNData(2)
53  data.setLayer("scale", [255.0])
54  node.io['scale'].send(data)
55  )");
56  // Re-use the initial values for mean/scale
57  script->outputs["mean"].link(nn->inputs["mean"]);
58  nn->inputs["mean"].setWaitForMessage(false);
59 
60  script->outputs["scale"].link(nn->inputs["scale"]);
61  nn->inputs["scale"].setWaitForMessage(false);
62  // Always wait for the new frame before starting inference
63  camRgb->preview.link(nn->inputs["frame"]);
64 
65  auto xout = pipeline.create<dai::node::XLinkOut>();
66  xout->setStreamName("nn");
67  nn->out.link(xout->input);
68 
69  // Connect to device and start pipeline
70  dai::Device device(pipeline);
71 
72  // Output queues will be used to get the rgb frames and nn data from the outputs defined above
73  auto qNn = device.getOutputQueue("nn", 4, false);
74 
75  while(true) {
76  auto inNn = qNn->get<dai::NNData>();
77  // To get original frame back (0-255), we add multiply all frame values (pixels) by 255 and then add 127.5 to them.
78  cv::imshow("Original Frame", fromPlanarFp16(inNn->getFirstLayerFp16(), 300, 300, 127.5, 255.0));
79 
80  int key = cv::waitKey(1);
81  if(key == 'q' || key == 'Q') {
82  return 0;
83  }
84  }
85  return 0;
86 }
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
Definition: NNData.hpp:16
main
int main(int argc, char **argv)
Definition: normalization_multi_input.cpp:9
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::node::NeuralNetwork
NeuralNetwork node. Runs a neural inference on input data.
Definition: NeuralNetwork.hpp:18
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::Pipeline::setOpenVINOVersion
void setOpenVINOVersion(OpenVINO::Version version)
Set a specific OpenVINO version to use with this pipeline.
Definition: Pipeline.hpp:224
depthai.hpp
dai::node::Script::setScript
void setScript(const std::string &script, const std::string &name="")
Definition: Script.cpp:32
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::Script
Definition: Script.hpp:15
dai::node::NeuralNetwork::setBlobPath
void setBlobPath(const dai::Path &path)
Definition: NeuralNetwork.cpp:26
dai::node::ColorCamera::setFp16
void setFp16(bool fp16)
Set fp16 (0..255) data type of preview output frames.
Definition: ColorCamera.cpp:111
dai::Device
Definition: Device.hpp:21
fromPlanarFp16
cv::Mat fromPlanarFp16(const std::vector< float > &data, int w, int h, float mean, float scale)
Definition: utility.cpp:27
std
Definition: Node.hpp:366
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
utility.hpp


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