JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
AprilTag.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: import libjevois as jevois
4
5import cv2
6import numpy as np
7import apriltag
8
9## Detect apriltag robotic fiducial markers in Python
10#
11# This module detects apriltag markers, which are small 2D barcode-like patterns used in many robotics applications.
12# The code here is derived from https://pyimagesearch.com/2020/11/02/apriltag-with-python/
13#
14# On host, make sure the apriltag library is installed with 'pip install apriltag'; it is pre-installed on platform.
15#
16# If you need full 3D pose revovery, see our other module \jvmod{DemoArUco}, which also supports AprilTag.
17#
18# @author Laurent Itti
19#
20# @displayname AprilTag
21# @videomapping JVUI 0 0 30.0 CropScale=GREY@1024x576 YUYV 1920 1080 30.0 JeVois AprilTag
22# @email itti\@usc.edu
23# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
24# @copyright Copyright (C) 2024 by Laurent Itti, iLab and the University of Southern California
25# @mainurl http://jevois.org
26# @supporturl http://jevois.org/doc
27# @otherurl http://iLab.usc.edu
28# @license GPL v3
29# @distribution Unrestricted
30# @restrictions None
31# @ingroup modules
33 # ###################################################################################################
34 ## Constructor
35 def __init__(self):
36 # Instantiate a JeVois Timer to measure our processing framerate:
37 self.timer = jevois.Timer("apriltag", 100, jevois.LOG_INFO)
38 self.families = [ 'tag36h11', 'tag36h10', 'tag36artoolkit', 'tag25h9', 'tag25h7', 'tag16h5' ]
39 self.detector = None
40
41 # ###################################################################################################
42 ## JeVois optional extra init once the instance is fully constructed
43 def init(self):
44 # Create some parameters that users can adjust in the GUI:
45 self.pc = jevois.ParameterCategory("AprilTag Parameters", "")
46
47 self.dic = jevois.Parameter(self, 'dictionary', 'str', 'Dictionary to use. One of: ' +
48 ', '.join(self.families), 'tag36h11', self.pc)
49 self.dic.setCallback(self.setDicsetDic);
50
51 # ###################################################################################################
52 # Instantiate a detector each time dictionary is changed:
53 def setDic(self, dicname):
54 # Nuke any current detector instance:
55 self.detector = None
56 self.options = None
57
58 # Check that family supported, see https://github.com/swatbotics/apriltag/blob/master/core/apriltag_family.c
59 if dicname in self.families:
60 self.options = apriltag.DetectorOptions(families = dicname)
61 self.detector = apriltag.Detector(self.options)
62 else:
63 jevois.LFATAL('Unsupported AprilTag family. Must be one of: ' + ', '.join(self.families))
64
65 # ###################################################################################################
66 ## Process function with no USB output
67 def processNoUSB(self, inframe):
68 # Get the next camera image as grayscale and lower resolution for processing (may block until it is captured):
69 ingray = inframe.getCvGRAYp()
70
71 # Detect AprilTag markers:
72 if self.detector:
73 results = self.detector.detect(ingray)
74
75 # Loop over the AprilTag detection results and send serial messages:
76 for r in results:
77 jevois.sendSerial('ATAG' + r.tag_id + ' ' + str(r.center[0]) + ' ' + str(r.center[1]))
78
79 # ###################################################################################################
80 ## Process function with GUI output on JeVois-Pro
81 def processGUI(self, inframe, helper):
82 # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
83 idle, winw, winh = helper.startFrame()
84
85 # Draw full-resolution input frame from camera:
86 x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
87 helper.itext('JeVois-Pro AprilTag detection')
88
89 # Get the next camera image as grayscale and lower resolution for processing (may block until it is captured):
90 ingray = inframe.getCvGRAYp()
91
92 # Start measuring image processing time (NOTE: does not account for input conversion time):
93 self.timer.start()
94
95 # Detect AprilTag markers:
96 if self.detector:
97 results = self.detector.detect(ingray)
98 col = 0xffffff7f # ARGB color for our drawings
99
100 # Loop over the AprilTag detection results and draw them + send serial messages:
101 for r in results:
102 # Draw tag outline:
103 (ptA, ptB, ptC, ptD) = r.corners.astype(float)
104 helper.drawLine(ptA[0], ptA[1], ptB[0], ptB[1], col)
105 helper.drawLine(ptB[0], ptB[1], ptC[0], ptC[1], col)
106 helper.drawLine(ptC[0], ptC[1], ptD[0], ptD[1], col)
107 helper.drawLine(ptD[0], ptD[1], ptA[0], ptA[1], col)
108
109 # Draw a circle at the center of the AprilTag:
110 helper.drawCircle(float(r.center[0]), float(r.center[1]), 5, col, True)
111
112 # Show tag ID:
113 helper.drawText(float(r.center[0] + 7), float(r.center[1] + 7), str(r.tag_id), col)
114
115 # Send a serial message:
116 jevois.sendSerial('ATAG' + str(r.tag_id) + ' ' + str(r.center[0]) + ' ' + str(r.center[1]))
117
118 # Write frames/s info from our timer:
119 fps = self.timer.stop()
120 helper.iinfo(inframe, fps, winw, winh);
121
122 # End of frame:
123 helper.endFrame()
Detect apriltag robotic fiducial markers in Python.
Definition AprilTag.py:32
processGUI(self, inframe, helper)
Process function with GUI output on JeVois-Pro.
Definition AprilTag.py:81
init(self)
JeVois optional extra init once the instance is fully constructed.
Definition AprilTag.py:43
setDic(self, dicname)
Definition AprilTag.py:53
processNoUSB(self, inframe)
Process function with no USB output.
Definition AprilTag.py:67
__init__(self)
Constructor.
Definition AprilTag.py:35