Note

This is the documentation for the latest development branch and may refer to features that are not available in released versions. If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

AprilTags Recognition Example Explanation

AprilTags Recognition Example Explanation#

Overview#

AprilTags is a visual marker system widely used in the computer vision field for localization, recognition, and tracking. Developed by April Robotics, AprilTags is an efficient binary recognition tag system particularly suitable for applications such as robotics and augmented reality.

CanMV supports the OpenMV algorithm and can recognize AprilTags. The related interface is find_apriltags.

Example#

This example sets the camera output to a 320x240 grayscale image and uses image.find_apriltags to recognize AprilTags.

Tip

If the recognition success rate is low, try adjusting the camera’s mirror and flip settings.

# AprilTags Example
# This example demonstrates the powerful features of CanMV for detecting April Tags.

import time
import math
import os
import gc
import sys

from media.sensor import *
from media.display import *
from media.media import *

# Define the width and height of the detection image
DETECT_WIDTH = 320
DETECT_HEIGHT = 240

# Define available tag families
tag_families = 0
tag_families |= image.TAG16H5  # 4x4 square tag
tag_families |= image.TAG25H7   # 5x7 square tag
tag_families |= image.TAG25H9   # 5x9 square tag
tag_families |= image.TAG36H10  # 6x10 square tag
tag_families |= image.TAG36H11  # 6x11 square tag (default)
tag_families |= image.ARTOOLKIT # ARToolKit tag

# Function: get the name of the tag family
def family_name(tag):
    family_dict = {
        image.TAG16H5: "TAG16H5",
        image.TAG25H7: "TAG25H7",
        image.TAG25H9: "TAG25H9",
        image.TAG36H10: "TAG36H10",
        image.TAG36H11: "TAG36H11",
        image.ARTOOLKIT: "ARTOOLKIT",
    }
    return family_dict.get(tag.family(), "Unknown Tag Family")

sensor = None

try:
    # Construct a Sensor object using the default configuration
    sensor = Sensor(width=DETECT_WIDTH, height=DETECT_HEIGHT)
    # Reset the sensor
    sensor.reset()

    # Set the output size and format
    sensor.set_framesize(width=DETECT_WIDTH, height=DETECT_HEIGHT)
    sensor.set_pixformat(Sensor.GRAYSCALE)

    # Initialize display
    Display.init(Display.VIRT, width=DETECT_WIDTH, height=DETECT_HEIGHT, fps=100)

    # Initialize the media manager
    MediaManager.init()
    # Start the sensor
    sensor.run()

    fps = time.clock()

    while True:
        fps.tick()

        # Check whether it should exit
        os.exitpoint()

        img = sensor.snapshot()
        for tag in img.find_apriltags(families=tag_families):
            # Draw the rectangle and center cross of the recognized tag
            img.draw_rectangle([v for v in tag.rect()], color=(255, 0, 0))
            img.draw_cross(tag.cx(), tag.cy(), color=(0, 255, 0))
            print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi)
            print("Tag Family %s, Tag ID %d, Rotation %f (degrees)" % print_args)

        # Draw the result on the screen
        Display.show_image(img)
        gc.collect()

except KeyboardInterrupt:
    print("User stopped")
except BaseException as e:
    print(f"Exception '{e}'")
finally:
    # Stop the sensor
    if isinstance(sensor, Sensor):
        sensor.stop()
    # Destroy the display
    Display.deinit()

    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)

    # Release media buffers
    MediaManager.deinit()

Code Explanation#

  • Import modules: Import the necessary libraries to use sensor, display, and media management functions.

  • Define image dimensions: Set the width and height of the detection image to 320x240.

  • Define tag families: Configure recognizable tag families, including TAG16H5, TAG25H7, TAG25H9, TAG36H10, TAG36H11, and ARToolkit. Certain tag families can be disabled by commenting out the relevant lines.

  • family_name function: Returns the name based on the tag’s type, facilitating subsequent processing and display.

  • Sensor configuration: Create and configure the sensor object, setting the image output size and format (grayscale image).

  • Display initialization: Configure the display output method, choosing IDE output.

  • Main loop:

    • Capture the image and check the exit condition.

    • Recognize AprilTags in the image, and draw a red rectangle and center cross around the tag.

    • Print detailed information about the tag, including tag family, tag ID, and rotation angle.

  • Exception handling: Capture user interrupts or other exceptions to ensure the sensor stops normally and resources are released.

Tip

For specific interface definitions, please refer to find_apriltags.

Comments list
Comments
Log in