Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apriltag not detected in clear image captured by acr010 webcam #61

Open
lizaibeim opened this issue Aug 7, 2023 · 1 comment
Open

Comments

@lizaibeim
Copy link

Hi, I am using the pupil-apriltag to do some detection. It works well on my built-in camera on my Mac. However, when I try with my usb acr010 webcam, it doesn't work. I have calibrated my webcam and the frame shows on the windows. The frame rate is about 12, however, it doesn't detect apriltag at all.

Here below is my code that running a webcam to detect the Apriltags continuously and a simple test for one of the image that captured by my webcam.

import os
import time

import cv2
import numpy as np
from pupil_apriltags import Detector

# Camera information, NOT CONFIGURED
# FPS = 50
RES = (1920, 1080)
camera_info = {}

# Camera Resolution
camera_info["res"] = RES

# Camera Intrinsic Matrix (3x3)
camera_info["K"] = np.array([[1129.106419355741, 0.0, 958.2530752561653],
                            [0.0, 1126.3550177287352, 546.954031682053],
                            [0.0, 0.0, 1.0]])
# Fisheye Camera Distortion Matrix
camera_info["D"] = np.array([[-0.09285509543388772],
                             [0.08508622717322474],
                             [-0.148664219994183],
                             [0.10812148818670442]])
# The non-default elements of the K array, in the AprilTag specification
camera_info["params"] = [1129.106, 1126.355, 958.253, 546.954]

# Fisheye flag
camera_info["fisheye"] = True
camera_info["map_1"], camera_info["map_2"] = cv2.fisheye.initUndistortRectifyMap(camera_info["K"], camera_info["D"],
                                                                                 np.eye(3), camera_info["K"],
                                                                                 camera_info["res"], cv2.CV_16SC2)

# Tag information
TAG_SIZE = .123
FAMILIES = 'tag36h11'


def main():
    print('\033]0;Video Base\007')

    # Initialize the camera capture
    camera = cv2.VideoCapture(0)
    camera.set(3, RES[0])  # Set horizontal resolution
    camera.set(4, RES[1])  # Set vertical resolution

    # Configure the AprilTags detector
    detector = Detector(families=FAMILIES, nthreads=4)

    # Initialize variables for calculating frame rate
    start_time = time.time()
    frame_count = 0

    while True:
        # Capture frame from the camera
        ret, frame = camera.read()

        # Undistort the frame
        # frame = cv2.remap(frame, camera_info["map_1"], camera_info["map_2"], interpolation=cv2.INTER_LINEAR,
        #                   borderMode=cv2.BORDER_CONSTANT)

        # Convert the frame to grayscale
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Detect AprilTags in the grayscale image
        results = detector.detect(gray, estimate_tag_pose=True, camera_params=camera_info["params"], tag_size=TAG_SIZE)
        detected_tags = [tag.tag_id for tag in results]

        # Draw bounding boxes and IDs on the frame
        for tag in results:
            # Add bounding rectangle
            cv2.polylines(frame, [np.int32(tag.corners)], True, (0, 255, 0), thickness=2)
            # Add Tag ID text
            cv2.putText(frame, str(tag.tag_id),
                        org=(tag.corners[0, 0].astype(int) + 10, tag.corners[0, 1].astype(int) + 10),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=0.8,
                        color=(0, 0, 255),
                        thickness=3)
            cv2.circle(frame, tuple(tag.corners[0].astype(int)), 2, color=(255, 0, 255), thickness=2)

            # Display rotation and translation
            cv2.putText(frame, f"Rot: {tag.pose_R}",
                        (tag.corners[0][0].astype(int), tag.corners[0][1].astype(int) - 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
            cv2.putText(frame, f"Trans: {tag.pose_t}",
                        (tag.corners[0][0].astype(int), tag.corners[0][1].astype(int) - 60),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

        # Calculate frame rate
        frame_count += 1
        elapsed_time = time.time() - start_time
        fps = frame_count / elapsed_time

        # Display the frame rate on the frame
        cv2.putText(frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

        # Display the frame
        cv2.imshow('AprilTags Detection', frame)

        # Prepare the message
        message = {
            "badge_id": "B",  # Replace with a unique ID for each badge
            "detected_tags": detected_tags
        }

        if len(results) != 0:
            print(message)

        # Exit program when 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release the camera and close windows
    camera.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()

The test image and the corresponding test code,
test

import pupil_apriltags as apriltag
import cv2

img = cv2.imread("test.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print("[INFO] detecting AprilTags...")
detector = apriltag.Detector(families="tag36h11")
results = detector.detect(gray)
print("[INFO] {} total AprilTags detected".format(len(results)))

for tag in results:
    cv2.circle(img, tuple(tag.corners[0].astype(int)), 4,(255,0,0), 2) # left-top
    cv2.circle(img, tuple(tag.corners[1].astype(int)), 4,(255,0,0), 2) # right-top
    cv2.circle(img, tuple(tag.corners[2].astype(int)), 4,(255,0,0), 2) # right-bottom
    cv2.circle(img, tuple(tag.corners[3].astype(int)), 4,(255,0,0), 2) # left-bottom

cv2.imshow("apriltag_test",img)
cv2.waitKey()
@Adblu
Copy link

Adblu commented Jan 11, 2024

Hi. I do not have answer to your question, but I am super curious of how did you calibrate this particular camera. Please reach me on [email protected] I have some questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants