Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions apriltag_cc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

import cv2
from apriltag import apriltag

# Create detector once
detector = apriltag("tag36h11")

# Open default camera
cap = cv2.VideoCapture(0)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

while True:
ok, frame = cap.read()

if not ok:
print("Failed to read camera frame")
break

# AprilTag detector expects grayscale image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect AprilTags
detections = detector.detect(gray)

for detection in detections:

# Your library returns a dictionary:
# {
# 'hamming': ...,
# 'margin': ...,
# 'id': ...,
# 'center': ...,
# 'lb-rb-rt-lt': ...,
# 'homography': ...
# }

corners = detection["lb-rb-rt-lt"].astype(int)
tag_id = detection["id"]
center = detection["center"].astype(int)

# Draw tag boundary
cv2.polylines(
frame,
[corners],
True,
(0, 255, 0),
2
)

# Draw tag center
cv2.circle(
frame,
tuple(center),
5,
(0, 0, 255),
-1
)

# Display Tag ID
x, y = corners[0]

cv2.putText(
frame,
f"ID: {tag_id}",
(x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
2
)

print(
f"Tag ID: {tag_id}, "
f"Center: {center}, "
f"Decision Margin: {detection['margin']:.2f}"
)

cv2.imshow("AprilTag Detection", frame)

# Press Q to exit
if cv2.waitKey(1) & 0xFF == ord("q"):
break

cap.release()
cv2.destroyAllWindows()
45 changes: 45 additions & 0 deletions deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Local deployment

## Build and install

Clone the repository and enter it:

```bash
git clone <repository-url>
cd apriltag
```

Configure a Release build, then build and install it:

```bash
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --target install
```

The default install prefix is usually `/usr/local`. If your account cannot write
there, run the install command with the appropriate administrator privileges, or
configure CMake with a writable `CMAKE_INSTALL_PREFIX`.

## Select the camera

Before running, set the camera index in `apriltag_cc.py` to the correct device:

```python
cap = cv2.VideoCapture(0)
```

Replace `0` with the index for the desired camera. On Linux, camera device nodes
are typically `/dev/video0`, `/dev/video1`, and so on; the intended camera may
not be index `0`.

## Run

If your default Python package path is `/usr/local/lib/python3.12/dist-packages/`,
run the script with the installed package directory included in `PYTHONPATH`:

```bash
PYTHONPATH=/usr/local/lib/python3.12/site-packages python3 apriltag_cc.py
```

Adjust the Python version and package path if your installation uses different
locations.