# AprilTags from camera

Utilizes the [AprilTag](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/april_tag.md) node to detect
apriltag markers on the camera stream at 1080P. It outlines detected tags and their IDs on the screen. Check the [AprilTags on
12MP frames](https://docs.luxonis.com/software-v3/depthai/examples/april_tags/april_tags_12mp.md) example for potentially better
detection results.

## Demo

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.md).

## Pipeline

Pipeline nodes:
- Camera(0)
- XLinkOut (2)

Pipeline connections:
- Camera(0) -> XLinkOut (2) (0 -> in)

## Source code

#### Python

```python
#!/usr/bin/env python3

import cv2
import depthai as dai
import time

with dai.Pipeline() as pipeline:
    hostCamera = pipeline.create(dai.node.Camera).build()
    aprilTagNode = pipeline.create(dai.node.AprilTag)
    hostCamera.requestOutput((1920, 1080)).link(aprilTagNode.inputImage)
    passthroughOutputQueue = aprilTagNode.passthroughInputImage.createOutputQueue()
    outQueue = aprilTagNode.out.createOutputQueue()

    color = (0, 255, 0)
    startTime = time.monotonic()
    counter = 0
    fps = 0.0

    pipeline.start()
    while pipeline.isRunning():
        aprilTagMessage = outQueue.get()
        assert(isinstance(aprilTagMessage, dai.AprilTags))
        aprilTags = aprilTagMessage.aprilTags

        counter += 1
        currentTime = time.monotonic()
        if (currentTime - startTime) > 1:
            fps = counter / (currentTime - startTime)
            counter = 0
            startTime = currentTime

        passthroughImage: dai.ImgFrame = passthroughOutputQueue.get()
        frame = passthroughImage.getCvFrame()

        def to_int(tag):
            return (int(tag.x), int(tag.y))

        for tag in aprilTags:
            topLeft = to_int(tag.topLeft)
            topRight = to_int(tag.topRight)
            bottomRight = to_int(tag.bottomRight)
            bottomLeft = to_int(tag.bottomLeft)

            center = (int((topLeft[0] + bottomRight[0]) / 2), int((topLeft[1] + bottomRight[1]) / 2))

            cv2.line(frame, topLeft, topRight, color, 2, cv2.LINE_AA, 0)
            cv2.line(frame, topRight,bottomRight, color, 2, cv2.LINE_AA, 0)
            cv2.line(frame, bottomRight,bottomLeft, color, 2, cv2.LINE_AA, 0)
            cv2.line(frame, bottomLeft,topLeft, color, 2, cv2.LINE_AA, 0)

            idStr = "ID: " + str(tag.id)
            cv2.putText(frame, idStr, center, cv2.FONT_HERSHEY_TRIPLEX, 0.5, color)

            cv2.putText(frame, f"fps: {fps:.1f}", (200, 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color)

        cv2.imshow("detections", frame)
        if cv2.waitKey(1) == ord("q"):
            break
```

#### C++

```cpp
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <memory>
#include <opencv2/opencv.hpp>

#include "depthai/depthai.hpp"

std::atomic<bool> quitEvent(false);

void signalHandler(int) {
    quitEvent = true;
}

int main() {
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    // Create device
    std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();

    // Create pipeline
    dai::Pipeline pipeline(device);

    // Create nodes
    auto cameraNode = pipeline.create<dai::node::Camera>()->build();
    auto aprilTagNode = pipeline.create<dai::node::AprilTag>();

    // Configure nodes
    auto outputCam = cameraNode->requestOutput(std::make_pair(1280, 720));
    outputCam->link(aprilTagNode->inputImage);

    // Create output queues
    auto passthroughOutputQueue = aprilTagNode->passthroughInputImage.createOutputQueue();
    auto outQueue = aprilTagNode->out.createOutputQueue();

    // Start pipeline
    pipeline.start();

    // Variables for FPS calculation
    auto startTime = std::chrono::steady_clock::now();
    int counter = 0;
    float fps = 0.0f;
    const cv::Scalar color(0, 255, 0);

    while(pipeline.isRunning() && !quitEvent) {
        auto aprilTagMessage = outQueue->get<dai::AprilTags>();
        if(aprilTagMessage == nullptr) continue;

        // FPS calculation
        counter++;
        auto currentTime = std::chrono::steady_clock::now();
        auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count();
        if(elapsed > 1000) {
            fps = counter * 1000.0f / elapsed;
            counter = 0;
            startTime = currentTime;
        }

        auto frame = passthroughOutputQueue->get<dai::ImgFrame>();
        if(frame == nullptr) continue;

        cv::Mat cvFrame = frame->getCvFrame();

        // Helper function to convert points to integers
        auto to_int = [](const dai::Point2f& p) { return cv::Point(static_cast<int>(p.x), static_cast<int>(p.y)); };

        for(const auto& tag : aprilTagMessage->aprilTags) {
            auto topLeft = to_int(tag.topLeft);
            auto topRight = to_int(tag.topRight);
            auto bottomRight = to_int(tag.bottomRight);
            auto bottomLeft = to_int(tag.bottomLeft);

            cv::Point center((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);

            // Draw tag boundaries
            cv::line(cvFrame, topLeft, topRight, color, 2, cv::LINE_AA, 0);
            cv::line(cvFrame, topRight, bottomRight, color, 2, cv::LINE_AA, 0);
            cv::line(cvFrame, bottomRight, bottomLeft, color, 2, cv::LINE_AA, 0);
            cv::line(cvFrame, bottomLeft, topLeft, color, 2, cv::LINE_AA, 0);

            // Draw tag ID
            std::string idStr = "ID: " + std::to_string(tag.id);
            cv::putText(cvFrame, idStr, center, cv::FONT_HERSHEY_TRIPLEX, 0.5, color);

            // Draw FPS
            cv::putText(cvFrame, "fps: " + std::to_string(fps).substr(0, 4), cv::Point(200, 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
        }

        cv::imshow("detections", cvFrame);

        if(cv::waitKey(1) == 'q') {
            break;
        }
    }

    pipeline.stop();
    pipeline.wait();

    return 0;
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
