1
+ import numpy as np
2
+ import cv2
3
+ import sys
4
+ import time
5
+
6
+
7
+
8
+
9
+ ARUCO_DICT = {
10
+ "DICT_4X4_50" : cv2 .aruco .DICT_4X4_50 ,
11
+ "DICT_4X4_100" : cv2 .aruco .DICT_4X4_100 ,
12
+ "DICT_4X4_250" : cv2 .aruco .DICT_4X4_250 ,
13
+ "DICT_4X4_1000" : cv2 .aruco .DICT_4X4_1000 ,
14
+ "DICT_5X5_50" : cv2 .aruco .DICT_5X5_50 ,
15
+ "DICT_5X5_100" : cv2 .aruco .DICT_5X5_100 ,
16
+ "DICT_5X5_250" : cv2 .aruco .DICT_5X5_250 ,
17
+ "DICT_5X5_1000" : cv2 .aruco .DICT_5X5_1000 ,
18
+ "DICT_6X6_50" : cv2 .aruco .DICT_6X6_50 ,
19
+ "DICT_6X6_100" : cv2 .aruco .DICT_6X6_100 ,
20
+ "DICT_6X6_250" : cv2 .aruco .DICT_6X6_250 ,
21
+ "DICT_6X6_1000" : cv2 .aruco .DICT_6X6_1000 ,
22
+ "DICT_7X7_50" : cv2 .aruco .DICT_7X7_50 ,
23
+ "DICT_7X7_100" : cv2 .aruco .DICT_7X7_100 ,
24
+ "DICT_7X7_250" : cv2 .aruco .DICT_7X7_250 ,
25
+ "DICT_7X7_1000" : cv2 .aruco .DICT_7X7_1000 ,
26
+ "DICT_ARUCO_ORIGINAL" : cv2 .aruco .DICT_ARUCO_ORIGINAL ,
27
+ "DICT_APRILTAG_16h5" : cv2 .aruco .DICT_APRILTAG_16h5 ,
28
+ "DICT_APRILTAG_25h9" : cv2 .aruco .DICT_APRILTAG_25h9 ,
29
+ "DICT_APRILTAG_36h10" : cv2 .aruco .DICT_APRILTAG_36h10 ,
30
+ "DICT_APRILTAG_36h11" : cv2 .aruco .DICT_APRILTAG_36h11
31
+ }
32
+
33
+ def aruco_display (corners , ids , rejected , image ):
34
+
35
+ if len (corners ) > 0 :
36
+
37
+ ids = ids .flatten ()
38
+
39
+ for (markerCorner , markerID ) in zip (corners , ids ):
40
+
41
+ corners = markerCorner .reshape ((4 , 2 ))
42
+ (topLeft , topRight , bottomRight , bottomLeft ) = corners
43
+
44
+ topRight = (int (topRight [0 ]), int (topRight [1 ]))
45
+ bottomRight = (int (bottomRight [0 ]), int (bottomRight [1 ]))
46
+ bottomLeft = (int (bottomLeft [0 ]), int (bottomLeft [1 ]))
47
+ topLeft = (int (topLeft [0 ]), int (topLeft [1 ]))
48
+
49
+ cv2 .line (image , topLeft , topRight , (0 , 255 , 0 ), 2 )
50
+ cv2 .line (image , topRight , bottomRight , (0 , 255 , 0 ), 2 )
51
+ cv2 .line (image , bottomRight , bottomLeft , (0 , 255 , 0 ), 2 )
52
+ cv2 .line (image , bottomLeft , topLeft , (0 , 255 , 0 ), 2 )
53
+
54
+ cX = int ((topLeft [0 ] + bottomRight [0 ]) / 2.0 )
55
+ cY = int ((topLeft [1 ] + bottomRight [1 ]) / 2.0 )
56
+ cv2 .circle (image , (cX , cY ), 4 , (0 , 0 , 255 ), - 1 )
57
+
58
+ cv2 .putText (image , str (markerID ),(topLeft [0 ], topLeft [1 ] - 10 ), cv2 .FONT_HERSHEY_SIMPLEX ,
59
+ 0.5 , (0 , 255 , 0 ), 2 )
60
+ print ("[Inference] ArUco marker ID: {}" .format (markerID ))
61
+
62
+ return image
63
+
64
+
65
+
66
+ def pose_estimation (frame , aruco_dict_type , matrix_coefficients , distortion_coefficients ):
67
+ gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
68
+ aruco_dict = cv2 .aruco .Dictionary_get (aruco_dict_type ) # Changed to use local variable
69
+ parameters = cv2 .aruco .DetectorParameters_create ()
70
+
71
+ corners , ids , rejected_img_points = cv2 .aruco .detectMarkers (
72
+ gray , aruco_dict , parameters = parameters ,
73
+ cameraMatrix = matrix_coefficients ,
74
+ distCoeff = distortion_coefficients
75
+ )
76
+
77
+ if len (corners ) > 0 :
78
+ # Draw detected markers
79
+ cv2 .aruco .drawDetectedMarkers (frame , corners , ids )
80
+
81
+ for i in range (len (ids )):
82
+ rvec , tvec , _ = cv2 .aruco .estimatePoseSingleMarkers (corners [i ], 0.02 , matrix_coefficients , distortion_coefficients )
83
+
84
+ # Draw the axis for each marker
85
+ cv2 .aruco .drawAxis (frame , matrix_coefficients , distortion_coefficients , rvec , tvec , 0.01 )
86
+
87
+ return frame
88
+
89
+
90
+
91
+ aruco_type = "DICT_5X5_100"
92
+
93
+ arucoDict = cv2 .aruco .Dictionary_get (ARUCO_DICT [aruco_type ])
94
+
95
+ arucoParams = cv2 .aruco .DetectorParameters_create ()
96
+
97
+
98
+ intrinsic_camera = np .array (((933.15867 , 0 , 657.59 ),(0 ,933.1586 , 400.36993 ),(0 ,0 ,1 )))
99
+ distortion = np .array ((- 0.43948 ,0.18514 ,0 ,0 ))
100
+
101
+
102
+ cap = cv2 .VideoCapture (0 )
103
+
104
+ cap .set (cv2 .CAP_PROP_FRAME_WIDTH , 1280 )
105
+ cap .set (cv2 .CAP_PROP_FRAME_HEIGHT , 720 )
106
+
107
+
108
+
109
+ while cap .isOpened ():
110
+
111
+ ret , img = cap .read ()
112
+
113
+ output = pose_estimation (img , ARUCO_DICT [aruco_type ], intrinsic_camera , distortion )
114
+
115
+ cv2 .imshow ('Estimated Pose' , output )
116
+
117
+ key = cv2 .waitKey (1 ) & 0xFF
118
+ if key == ord ('q' ):
119
+ break
120
+
121
+ cap .release ()
122
+ cv2 .destroyAllWindows ()
0 commit comments