1
+ import numpy as np
2
+ import time
3
+ import cv2
4
+
5
+
6
+ ARUCO_DICT = {
7
+ "DICT_4X4_50" : cv2 .aruco .DICT_4X4_50 ,
8
+ "DICT_4X4_100" : cv2 .aruco .DICT_4X4_100 ,
9
+ "DICT_4X4_250" : cv2 .aruco .DICT_4X4_250 ,
10
+ "DICT_4X4_1000" : cv2 .aruco .DICT_4X4_1000 ,
11
+ "DICT_5X5_50" : cv2 .aruco .DICT_5X5_50 ,
12
+ "DICT_5X5_100" : cv2 .aruco .DICT_5X5_100 ,
13
+ "DICT_5X5_250" : cv2 .aruco .DICT_5X5_250 ,
14
+ "DICT_5X5_1000" : cv2 .aruco .DICT_5X5_1000 ,
15
+ "DICT_6X6_50" : cv2 .aruco .DICT_6X6_50 ,
16
+ "DICT_6X6_100" : cv2 .aruco .DICT_6X6_100 ,
17
+ "DICT_6X6_250" : cv2 .aruco .DICT_6X6_250 ,
18
+ "DICT_6X6_1000" : cv2 .aruco .DICT_6X6_1000 ,
19
+ "DICT_7X7_50" : cv2 .aruco .DICT_7X7_50 ,
20
+ "DICT_7X7_100" : cv2 .aruco .DICT_7X7_100 ,
21
+ "DICT_7X7_250" : cv2 .aruco .DICT_7X7_250 ,
22
+ "DICT_7X7_1000" : cv2 .aruco .DICT_7X7_1000 ,
23
+ "DICT_ARUCO_ORIGINAL" : cv2 .aruco .DICT_ARUCO_ORIGINAL ,
24
+ "DICT_APRILTAG_16h5" : cv2 .aruco .DICT_APRILTAG_16h5 ,
25
+ "DICT_APRILTAG_25h9" : cv2 .aruco .DICT_APRILTAG_25h9 ,
26
+ "DICT_APRILTAG_36h10" : cv2 .aruco .DICT_APRILTAG_36h10 ,
27
+ "DICT_APRILTAG_36h11" : cv2 .aruco .DICT_APRILTAG_36h11
28
+ }
29
+
30
+
31
+ def aruco_display (corners , ids , rejected , image ):
32
+ if len (corners ) > 0 :
33
+
34
+ ids = ids .flatten ()
35
+
36
+ for (markerCorner , markerID ) in zip (corners , ids ):
37
+
38
+ corners = markerCorner .reshape ((4 , 2 ))
39
+ (topLeft , topRight , bottomRight , bottomLeft ) = corners
40
+
41
+ topRight = (int (topRight [0 ]), int (topRight [1 ]))
42
+ bottomRight = (int (bottomRight [0 ]), int (bottomRight [1 ]))
43
+ bottomLeft = (int (bottomLeft [0 ]), int (bottomLeft [1 ]))
44
+ topLeft = (int (topLeft [0 ]), int (topLeft [1 ]))
45
+
46
+ cv2 .line (image , topLeft , topRight , (0 , 255 , 0 ), 2 )
47
+ cv2 .line (image , topRight , bottomRight , (0 , 255 , 0 ), 2 )
48
+ cv2 .line (image , bottomRight , bottomLeft , (0 , 255 , 0 ), 2 )
49
+ cv2 .line (image , bottomLeft , topLeft , (0 , 255 , 0 ), 2 )
50
+
51
+ cX = int ((topLeft [0 ] + bottomRight [0 ]) / 2.0 )
52
+ cY = int ((topLeft [1 ] + bottomRight [1 ]) / 2.0 )
53
+ cv2 .circle (image , (cX , cY ), 4 , (0 , 0 , 255 ), - 1 )
54
+
55
+ cv2 .putText (image , str (markerID ),(topLeft [0 ], topLeft [1 ] - 10 ), cv2 .FONT_HERSHEY_SIMPLEX ,
56
+ 0.5 , (0 , 255 , 0 ), 2 )
57
+ print ("[Inference] ArUco marker ID: {}" .format (markerID ))
58
+
59
+ return image
60
+
61
+
62
+
63
+
64
+ aruco_type = "DICT_5X5_100"
65
+
66
+ arucoDict = cv2 .aruco .Dictionary_get (ARUCO_DICT [aruco_type ])
67
+
68
+ arucoParams = cv2 .aruco .DetectorParameters_create ()
69
+
70
+
71
+ cap = cv2 .VideoCapture (0 )
72
+
73
+ cap .set (cv2 .CAP_PROP_FRAME_WIDTH , 1280 )
74
+ cap .set (cv2 .CAP_PROP_FRAME_HEIGHT , 720 )
75
+
76
+
77
+ while cap .isOpened ():
78
+
79
+ ret , img = cap .read ()
80
+
81
+ h , w , _ = img .shape
82
+
83
+ width = 1000
84
+ height = int (width * (h / w ))
85
+ img = cv2 .resize (img , (width , height ), interpolation = cv2 .INTER_CUBIC )
86
+
87
+ corners , ids , rejected = cv2 .aruco .detectMarkers (img , arucoDict , parameters = arucoParams )
88
+
89
+ detected_markers = aruco_display (corners , ids , rejected , img )
90
+
91
+ cv2 .imshow ("Image" , detected_markers )
92
+
93
+ key = cv2 .waitKey (1 ) & 0xFF
94
+ if key == ord ("q" ):
95
+ break
96
+
97
+ cv2 .destroyAllWindows ()
98
+ cap .release ()
0 commit comments