44from pathlib import Path
55from time import sleep
66
7- import apriltag
7+ # CHANGE 1: New Import
8+ from pupil_apriltags import Detector
9+
810import cv2
911import diskcache as dc
1012import numpy as np
@@ -40,7 +42,7 @@ def calibrate(
4042 input ()
4143 tries = 3
4244 while len (samples ) < 10 and tries > 0 :
43- logger .info ("not enought frames in recorded, waiting 2 seconds..." )
45+ logger .info ("not enough frames in recorded, waiting 2 seconds..." )
4446 tries = - 1
4547 sleep (2 )
4648 if tries == 0 :
@@ -51,7 +53,7 @@ def calibrate(
5153 with lock :
5254 for sample in samples :
5355 frames .append (sample .camera .color .data .copy ())
54- print (frames )
56+ # print(frames) # Removed print for cleaner logs, optional
5557
5658 _ , tag_to_cam = get_average_marker_pose (frames , intrinsics = intrinsics , calib_tag_id = 9 , show_live_window = False )
5759
@@ -71,9 +73,9 @@ def get_average_marker_pose(
7173 calib_tag_id ,
7274 show_live_window ,
7375):
74- # create detector
75- options = apriltag . DetectorOptions ( families = "tag25h9" )
76- detector = apriltag . Detector (options = options )
76+ # CHANGE 2: Simplified Initialization
77+ # No " DetectorOptions" object needed anymore.
78+ detector = Detector (families = "tag25h9" )
7779
7880 # make while loop with tqdm
7981 poses = []
@@ -97,6 +99,7 @@ def get_average_marker_pose(
9799 camera_matrix = intrinsics [:3 , :3 ]
98100
99101 if show_live_window :
102+ # Note: pose[:3, :3] works because we construct the 4x4 matrix in get_marker_pose below
100103 cv2 .drawFrameAxes (frame , camera_matrix , None , pose [:3 , :3 ], pose [:3 , 3 ], 0.1 ) # type: ignore
101104 # show frame
102105 cv2 .imshow ("frame" , frame )
@@ -120,7 +123,20 @@ def get_average_marker_pose(
120123
121124def get_marker_pose (calib_tag_id , detector , intrinsics , frame ):
122125 gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
123- detections = detector .detect (gray )
126+
127+ # CHANGE 3: Pose estimation happens INSIDE .detect()
128+ # We must extract camera params first to pass them here
129+ fx = intrinsics [0 , 0 ]
130+ fy = intrinsics [1 , 1 ]
131+ cx = intrinsics [0 , 2 ]
132+ cy = intrinsics [1 , 2 ]
133+
134+ detections = detector .detect (
135+ gray ,
136+ estimate_tag_pose = True ,
137+ camera_params = [fx , fy , cx , cy ],
138+ tag_size = 0.1
139+ )
124140
125141 # count detections
126142 n_det = 0
@@ -138,20 +154,10 @@ def get_marker_pose(calib_tag_id, detector, intrinsics, frame):
138154 if marker_det is None :
139155 return None , None
140156
141- fx = intrinsics [0 , 0 ]
142- fy = intrinsics [1 , 1 ]
143- cx = intrinsics [0 , 2 ]
144- cy = intrinsics [1 , 2 ]
145-
146- pose , _ , _ = detector .detection_pose (
147- marker_det ,
148- camera_params = (
149- fx ,
150- fy ,
151- cx ,
152- cy ,
153- ),
154- tag_size = 0.1 ,
155- )
157+ # CHANGE 4: Construct 4x4 Matrix manually
158+ # pupil-apriltags gives us R (3x3) and t (3x1). We must stack them.
159+ pose = np .eye (4 )
160+ pose [:3 , :3 ] = marker_det .pose_R
161+ pose [:3 , 3 ] = marker_det .pose_t .ravel () # ravel() flattens the (3,1) array to (3,)
156162
157163 return marker_det , pose
0 commit comments