Skip to content

Commit f6b0a2b

Browse files
authored
Merge pull request #1325 from luxonis/lnotspotl/cpp_examples
Add C++ examples
2 parents 1caead2 + 6a17960 commit f6b0a2b

File tree

108 files changed

+5085
-459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+5085
-459
lines changed

bindings/python/docs/source/components/nodes/image_manip.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ Usage
6464
dai::Pipeline pipeline;
6565
auto manip = pipeline.create<dai::node::ImageManip>();
6666

67-
manip->initialConfig.setResize(300, 300);
68-
manip->initialConfig.setFrameType(dai::ImgFrame::Type::BGR888p);
67+
manip->initialConfig->setResize(300, 300);
68+
manip->initialConfig->setFrameType(dai::ImgFrame::Type::BGR888p);
6969

7070
Image formats supported
7171
#######################

bindings/python/docs/source/components/nodes/spatial_location_calculator.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Usage
8787
dai::Point2f bottomRight(0.6f, 0.6f);
8888
config.roi = dai::Rect(topLeft, bottomRight);
8989

90-
spatialCalc->initialConfig.addROI(config);
90+
spatialCalc->initialConfig->addROI(config);
9191

9292
// You can later send configs from the host (XLinkIn) / scripting node to the InputConfig
9393

bindings/python/docs/source/components/nodes/stereo_depth.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ as:
286286
(this confidence score is kind-of inverted, if say comparing with NN)
287287

288288
For the final disparity map, a filtering is applied based on the confidence threshold value: the pixels that have their confidence score larger than
289-
the threshold get invalidated, i.e. their disparity value is set to zero. You can set the confidence threshold with :code:`stereo.initialConfig.setConfidenceThreshold()`.
289+
the threshold get invalidated, i.e. their disparity value is set to zero. You can set the confidence threshold with :code:`stereo.initialConfig->setConfidenceThreshold()`.
290290

291291
Calculate depth using disparity map
292292
===================================

bindings/python/docs/source/tutorials/configuring-stereo-depth.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ This means that with the confidence threshold, users can prioritize **fill-rate
133133
134134
// Create the StereoDepth node
135135
auto stereo_depth = pipeline.create<dai::node::StereoDepth>();
136-
stereo_depth->initialConfig.setConfidenceThreshold(threshold);
136+
stereo_depth->initialConfig->setConfidenceThreshold(threshold);
137137
138138
// Or, alternatively, set the Stereo Preset Mode:
139139
// Prioritize fill-rate, sets Confidence threshold to 245
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
project(april_tags_examples)
2+
cmake_minimum_required(VERSION 3.10)
3+
4+
## function: dai_add_example(example_name example_src enable_test use_pcl)
5+
## function: dai_set_example_test_labels(example_name ...)
6+
7+
# Download lenna :0
8+
private_data(
9+
URL "https://artifacts.luxonis.com/artifactory/luxonis-depthai-data-local/images/april_tags.jpg"
10+
FILE "april_tags.jpg"
11+
SHA1 "6818a531e71948bd28e1f0ab3e76b18aff6150fb"
12+
LOCATION april_tags
13+
)
14+
15+
dai_add_example(april_tags april_tags.cpp OFF OFF)
16+
17+
dai_add_example(april_tags_12mp april_tags_12mp.cpp OFF OFF)
18+
19+
dai_add_example(april_tags_replay april_tags_replay.cpp OFF OFF)
20+
target_compile_definitions(april_tags_replay PRIVATE APRIL_TAGS_PATH="${april_tags}")
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <chrono>
2+
#include <iostream>
3+
#include <memory>
4+
#include <opencv2/opencv.hpp>
5+
6+
#include "depthai/depthai.hpp"
7+
8+
int main() {
9+
// Create device
10+
std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();
11+
12+
// Create pipeline
13+
dai::Pipeline pipeline(device);
14+
15+
// Create nodes
16+
auto cameraNode = pipeline.create<dai::node::Camera>()->build();
17+
auto aprilTagNode = pipeline.create<dai::node::AprilTag>();
18+
19+
// Configure nodes
20+
auto outputCam = cameraNode->requestOutput(std::make_pair(1280, 720));
21+
outputCam->link(aprilTagNode->inputImage);
22+
23+
// Create output queues
24+
auto passthroughOutputQueue = aprilTagNode->passthroughInputImage.createOutputQueue();
25+
auto outQueue = aprilTagNode->out.createOutputQueue();
26+
27+
// Start pipeline
28+
pipeline.start();
29+
30+
// Variables for FPS calculation
31+
auto startTime = std::chrono::steady_clock::now();
32+
int counter = 0;
33+
float fps = 0.0f;
34+
const cv::Scalar color(0, 255, 0);
35+
36+
while(true) {
37+
auto aprilTagMessage = outQueue->get<dai::AprilTags>();
38+
if(aprilTagMessage == nullptr) continue;
39+
40+
// FPS calculation
41+
counter++;
42+
auto currentTime = std::chrono::steady_clock::now();
43+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count();
44+
if(elapsed > 1000) {
45+
fps = counter * 1000.0f / elapsed;
46+
counter = 0;
47+
startTime = currentTime;
48+
}
49+
50+
auto frame = passthroughOutputQueue->get<dai::ImgFrame>();
51+
if(frame == nullptr) continue;
52+
53+
cv::Mat cvFrame = frame->getCvFrame();
54+
55+
// Helper function to convert points to integers
56+
auto to_int = [](const dai::Point2f& p) { return cv::Point(static_cast<int>(p.x), static_cast<int>(p.y)); };
57+
58+
for(const auto& tag : aprilTagMessage->aprilTags) {
59+
auto topLeft = to_int(tag.topLeft);
60+
auto topRight = to_int(tag.topRight);
61+
auto bottomRight = to_int(tag.bottomRight);
62+
auto bottomLeft = to_int(tag.bottomLeft);
63+
64+
cv::Point center((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);
65+
66+
// Draw tag boundaries
67+
cv::line(cvFrame, topLeft, topRight, color, 2, cv::LINE_AA, 0);
68+
cv::line(cvFrame, topRight, bottomRight, color, 2, cv::LINE_AA, 0);
69+
cv::line(cvFrame, bottomRight, bottomLeft, color, 2, cv::LINE_AA, 0);
70+
cv::line(cvFrame, bottomLeft, topLeft, color, 2, cv::LINE_AA, 0);
71+
72+
// Draw tag ID
73+
std::string idStr = "ID: " + std::to_string(tag.id);
74+
cv::putText(cvFrame, idStr, center, cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
75+
76+
// Draw FPS
77+
cv::putText(cvFrame, "fps: " + std::to_string(fps).substr(0, 4), cv::Point(200, 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
78+
}
79+
80+
cv::imshow("detections", cvFrame);
81+
82+
if(cv::waitKey(1) == 'q') {
83+
break;
84+
}
85+
}
86+
87+
return 0;
88+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <chrono>
2+
#include <iostream>
3+
#include <memory>
4+
#include <opencv2/opencv.hpp>
5+
6+
#include "depthai/depthai.hpp"
7+
8+
int main() {
9+
// Create device
10+
std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();
11+
12+
// Create pipeline
13+
dai::Pipeline pipeline(device);
14+
15+
// Constants
16+
const cv::Size FULL_RES(4000, 3000); // 12MP
17+
const cv::Size PREVIEW_SIZE(1332, 1000); // 1/3 of 12MP
18+
19+
// Create nodes
20+
auto hostCamera = pipeline.create<dai::node::Camera>()->build();
21+
auto aprilTagNode = pipeline.create<dai::node::AprilTag>();
22+
auto manip = pipeline.create<dai::node::ImageManip>();
23+
24+
// Configure nodes
25+
auto outputCam = hostCamera->requestOutput(std::make_pair(FULL_RES.width, FULL_RES.height));
26+
outputCam->link(aprilTagNode->inputImage);
27+
28+
// Configure ImageManip
29+
manip->initialConfig->setOutputSize(PREVIEW_SIZE.width, PREVIEW_SIZE.height, dai::ImageManipConfig::ResizeMode::STRETCH);
30+
manip->setMaxOutputFrameSize(2162688);
31+
outputCam->link(manip->inputImage);
32+
33+
// Create output queues
34+
auto outQueue = aprilTagNode->out.createOutputQueue();
35+
auto frameQ = manip->out.createOutputQueue();
36+
37+
// Start pipeline
38+
pipeline.start();
39+
40+
// Variables for FPS calculation
41+
auto startTime = std::chrono::steady_clock::now();
42+
int counter = 0;
43+
float fps = 0.0f;
44+
const cv::Scalar color(0, 255, 0);
45+
46+
while(true) {
47+
auto aprilTagMessage = outQueue->get<dai::AprilTags>();
48+
if(aprilTagMessage == nullptr) continue;
49+
50+
// FPS calculation
51+
counter++;
52+
auto currentTime = std::chrono::steady_clock::now();
53+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count();
54+
if(elapsed > 1000) {
55+
fps = counter * 1000.0f / elapsed;
56+
counter = 0;
57+
startTime = currentTime;
58+
}
59+
60+
// Helper function to rescale points
61+
auto rescale = [&FULL_RES, &PREVIEW_SIZE](const dai::Point2f& p) {
62+
return cv::Point(static_cast<int>(p.x / FULL_RES.width * PREVIEW_SIZE.width), static_cast<int>(p.y / FULL_RES.height * PREVIEW_SIZE.height));
63+
};
64+
65+
auto frame = frameQ->get<dai::ImgFrame>();
66+
if(frame == nullptr) continue;
67+
68+
cv::Mat cvFrame = frame->getCvFrame();
69+
for(const auto& tag : aprilTagMessage->aprilTags) {
70+
auto topLeft = rescale(tag.topLeft);
71+
auto topRight = rescale(tag.topRight);
72+
auto bottomRight = rescale(tag.bottomRight);
73+
auto bottomLeft = rescale(tag.bottomLeft);
74+
75+
cv::Point center((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);
76+
77+
// Draw tag boundaries
78+
cv::line(cvFrame, topLeft, topRight, color, 2, cv::LINE_AA, 0);
79+
cv::line(cvFrame, topRight, bottomRight, color, 2, cv::LINE_AA, 0);
80+
cv::line(cvFrame, bottomRight, bottomLeft, color, 2, cv::LINE_AA, 0);
81+
cv::line(cvFrame, bottomLeft, topLeft, color, 2, cv::LINE_AA, 0);
82+
83+
// Draw tag ID
84+
std::string idStr = "ID: " + std::to_string(tag.id);
85+
cv::putText(cvFrame, idStr, center, cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
86+
87+
// Draw FPS
88+
cv::putText(cvFrame, "fps: " + std::to_string(fps).substr(0, 4), cv::Point(200, 20), cv::FONT_HERSHEY_TRIPLEX, 1, color);
89+
}
90+
91+
cv::imshow("detections", cvFrame);
92+
93+
if(cv::waitKey(1) == 'q') {
94+
break;
95+
}
96+
}
97+
98+
return 0;
99+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <chrono>
2+
#include <iostream>
3+
#include <opencv2/opencv.hpp>
4+
#include <thread>
5+
6+
#include "depthai/depthai.hpp"
7+
8+
class ImageReplay : public dai::NodeCRTP<dai::node::ThreadedHostNode, ImageReplay> {
9+
public:
10+
constexpr static const char* NAME = "ImageReplay";
11+
12+
Output output{*this, {"out", DEFAULT_GROUP, {{{dai::DatatypeEnum::ImgFrame, true}}}}};
13+
14+
ImageReplay() {
15+
// Load and prepare the image
16+
cv::Mat frame = cv::imread(APRIL_TAGS_PATH);
17+
cv::cvtColor(frame, frame, cv::COLOR_BGR2GRAY);
18+
19+
// Create ImgFrame
20+
auto imgFrame = std::make_shared<dai::ImgFrame>();
21+
std::vector<uint8_t> data(frame.data, frame.data + frame.total() * frame.elemSize());
22+
imgFrame->setData(data);
23+
imgFrame->setWidth(frame.cols);
24+
imgFrame->setHeight(frame.rows);
25+
imgFrame->setType(dai::ImgFrame::Type::GRAY8);
26+
_imgFrame = imgFrame;
27+
}
28+
29+
void run() override {
30+
while(isRunning()) {
31+
output.send(_imgFrame);
32+
std::this_thread::sleep_for(std::chrono::milliseconds(30));
33+
}
34+
}
35+
36+
private:
37+
std::shared_ptr<dai::ImgFrame> _imgFrame;
38+
};
39+
40+
int main() {
41+
// Create pipeline
42+
dai::Pipeline pipeline;
43+
44+
// Create nodes
45+
auto imageReplay = pipeline.create<ImageReplay>();
46+
auto aprilTagNode = pipeline.create<dai::node::AprilTag>();
47+
48+
// Link nodes
49+
imageReplay->output.link(aprilTagNode->inputImage);
50+
aprilTagNode->initialConfig->setFamily(dai::AprilTagConfig::Family::TAG_16H5);
51+
52+
// Create output queues
53+
auto passthroughOutputQueue = aprilTagNode->passthroughInputImage.createOutputQueue();
54+
auto outQueue = aprilTagNode->out.createOutputQueue();
55+
56+
// Start pipeline
57+
pipeline.start();
58+
59+
// FPS calculation variables
60+
cv::Scalar color(0, 255, 0);
61+
auto startTime = std::chrono::steady_clock::now();
62+
int counter = 0;
63+
float fps = 0.0f;
64+
65+
// Main loop
66+
while(pipeline.isRunning()) {
67+
auto aprilTagMessage = outQueue->get<dai::AprilTags>();
68+
auto aprilTags = aprilTagMessage->aprilTags;
69+
70+
// Calculate FPS
71+
counter++;
72+
auto currentTime = std::chrono::steady_clock::now();
73+
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count();
74+
if(elapsed >= 1) {
75+
fps = counter / static_cast<float>(elapsed);
76+
counter = 0;
77+
startTime = currentTime;
78+
}
79+
80+
// Get passthrough image
81+
auto passthroughImage = passthroughOutputQueue->get<dai::ImgFrame>();
82+
cv::Mat frame = passthroughImage->getCvFrame();
83+
cv::cvtColor(frame, frame, cv::COLOR_GRAY2BGR);
84+
85+
// Helper function to convert points to integers
86+
auto to_int = [](const dai::Point2f& p) { return cv::Point(static_cast<int>(p.x), static_cast<int>(p.y)); };
87+
88+
// Draw detections
89+
for(const auto& tag : aprilTags) {
90+
auto topLeft = to_int(tag.topLeft);
91+
auto topRight = to_int(tag.topRight);
92+
auto bottomRight = to_int(tag.bottomRight);
93+
auto bottomLeft = to_int(tag.bottomLeft);
94+
95+
auto center = cv::Point((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);
96+
97+
// Draw rectangle
98+
cv::line(frame, topLeft, topRight, color, 2, cv::LINE_AA, 0);
99+
cv::line(frame, topRight, bottomRight, color, 2, cv::LINE_AA, 0);
100+
cv::line(frame, bottomRight, bottomLeft, color, 2, cv::LINE_AA, 0);
101+
cv::line(frame, bottomLeft, topLeft, color, 2, cv::LINE_AA, 0);
102+
103+
// Draw ID
104+
std::string idStr = "ID: " + std::to_string(tag.id);
105+
cv::putText(frame, idStr, center, cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
106+
107+
// Draw FPS
108+
cv::putText(frame, "fps: " + std::to_string(fps).substr(0, 4), cv::Point(200, 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
109+
}
110+
111+
cv::imshow("detections", frame);
112+
if(cv::waitKey(1) == 'q') {
113+
break;
114+
}
115+
}
116+
117+
return 0;
118+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
project(benchmark_examples)
2+
cmake_minimum_required(VERSION 3.10)
3+
4+
## function: dai_add_example(example_name example_src enable_test use_pcl)
5+
## function: dai_set_example_test_labels(example_name ...)
6+
7+
dai_add_example(benchmark_camera "benchmark_camera.cpp" FALSE FALSE)
8+
9+
dai_add_example(benchmark_nn "benchmark_nn.cpp" FALSE FALSE)
10+
11+
dai_add_example(benchmark_simple "benchmark_simple.cpp" FALSE FALSE)

0 commit comments

Comments
 (0)