apriltag_rgb.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 
4 // Inludes common necessary includes for development using depthai library
5 #include "depthai/depthai.hpp"
6 
7 int main() {
8  using namespace std;
9  using namespace std::chrono;
10 
11  // Create pipeline
12  dai::Pipeline pipeline;
13 
14  // Define sources and outputs
15  auto camRgb = pipeline.create<dai::node::ColorCamera>();
16  auto aprilTag = pipeline.create<dai::node::AprilTag>();
17  auto manip = pipeline.create<dai::node::ImageManip>();
18 
19  auto xoutAprilTag = pipeline.create<dai::node::XLinkOut>();
20  auto xoutAprilTagImage = pipeline.create<dai::node::XLinkOut>();
21 
22  xoutAprilTag->setStreamName("aprilTagData");
23  xoutAprilTagImage->setStreamName("aprilTagImage");
24 
25  // Properties
27  camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A);
28 
29  manip->initialConfig.setResize(480, 270);
30  manip->initialConfig.setFrameType(dai::ImgFrame::Type::GRAY8);
31 
32  aprilTag->initialConfig.setFamily(dai::AprilTagConfig::Family::TAG_36H11);
33 
34  // Linking
35  aprilTag->passthroughInputImage.link(xoutAprilTagImage->input);
36  camRgb->video.link(manip->inputImage);
37  manip->out.link(aprilTag->inputImage);
38  aprilTag->out.link(xoutAprilTag->input);
39  // always take the latest frame as apriltag detections are slow
40  aprilTag->inputImage.setBlocking(false);
41  aprilTag->inputImage.setQueueSize(1);
42 
43  // Connect to device and start pipeline
44  dai::Device device(pipeline);
45 
46  // Output queue will be used to get the mono frames from the outputs defined above
47  auto manipQueue = device.getOutputQueue("aprilTagImage", 8, false);
48  auto aprilTagQueue = device.getOutputQueue("aprilTagData", 8, false);
49 
50  auto color = cv::Scalar(0, 255, 0);
51 
52  auto startTime = steady_clock::now();
53  int counter = 0;
54  float fps = 0;
55 
56  while(true) {
57  auto inFrame = manipQueue->get<dai::ImgFrame>();
58 
59  counter++;
60  auto currentTime = steady_clock::now();
61  auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
62  if(elapsed > seconds(1)) {
63  fps = counter / elapsed.count();
64  counter = 0;
65  startTime = currentTime;
66  }
67 
68  cv::Mat monoFrame = inFrame->getFrame();
69  cv::Mat frame;
70  cv::cvtColor(monoFrame, frame, cv::COLOR_GRAY2BGR);
71 
72  auto aprilTagData = aprilTagQueue->get<dai::AprilTags>()->aprilTags;
73  for(auto aprilTag : aprilTagData) {
74  auto& topLeft = aprilTag.topLeft;
75  auto& topRight = aprilTag.topRight;
76  auto& bottomRight = aprilTag.bottomRight;
77  auto& bottomLeft = aprilTag.bottomLeft;
78 
79  cv::Point center = cv::Point((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);
80 
81  cv::line(frame, cv::Point(topLeft.x, topLeft.y), cv::Point(topRight.x, topRight.y), color, 2, cv::LINE_AA, 0);
82  cv::line(frame, cv::Point(topRight.x, topRight.y), cv::Point(bottomRight.x, bottomRight.y), color, 2, cv::LINE_AA, 0);
83  cv::line(frame, cv::Point(bottomRight.x, bottomRight.y), cv::Point(bottomLeft.x, bottomLeft.y), color, 2, cv::LINE_AA, 0);
84  cv::line(frame, cv::Point(bottomLeft.x, bottomLeft.y), cv::Point(topLeft.x, topLeft.y), color, 2, cv::LINE_AA, 0);
85 
86  std::stringstream idStr;
87  idStr << "ID: " << aprilTag.id;
88  cv::putText(frame, idStr.str(), center, cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
89  }
90 
91  std::stringstream fpsStr;
92  fpsStr << "fps:" << std::fixed << std::setprecision(2) << fps;
93  cv::putText(frame, fpsStr.str(), cv::Point(2, inFrame->getHeight() - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color);
94 
95  cv::imshow("April tag frame", frame);
96 
97  int key = cv::waitKey(1);
98  if(key == 'q') {
99  return 0;
100  }
101  }
102  return 0;
103 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Node::id
const Id id
Id of node.
Definition: Node.hpp:288
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
dai::ImgFrame::getFrame
void getFrame(T...)
Definition: ImgFrame.hpp:218
dai::RawImgFrame::Type::GRAY8
@ GRAY8
fps
static constexpr int fps
Definition: rgb_depth_aligned.cpp:12
dai::CameraBoardSocket::CAM_A
@ CAM_A
dai::node::AprilTag::initialConfig
AprilTagConfig initialConfig
Definition: AprilTag.hpp:33
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::node::AprilTag::out
Output out
Definition: AprilTag.hpp:49
main
int main()
Definition: apriltag_rgb.cpp:7
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::AprilTags
Definition: AprilTags.hpp:14
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::node::ImageManip
ImageManip node. Capability to crop, resize, warp, ... incoming image frames.
Definition: ImageManip.hpp:15
dai::ColorCameraProperties::SensorResolution::THE_1080_P
@ THE_1080_P
1920 × 1080
dai::node::AprilTag::inputImage
Input inputImage
Definition: AprilTag.hpp:44
dai::node::ColorCamera::video
Output video
Definition: ColorCamera.hpp:62
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
dai::Node::Output::link
void link(const Input &in)
Definition: Node.cpp:84
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
dai::node::AprilTag
AprilTag node.
Definition: AprilTag.hpp:16
dai::RawAprilTagConfig::Family::TAG_36H11
@ TAG_36H11


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