Skip to content

Commit cbbaf6d

Browse files
...
1 parent 52567f9 commit cbbaf6d

File tree

3 files changed

+159
-14
lines changed

3 files changed

+159
-14
lines changed

scripts/arucoDetection.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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()

scripts/pose_estimation.py renamed to scripts/arucoPoseEstimation.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import time
55

66

7-
8-
97
ARUCO_DICT = {
108
"DICT_4X4_50": cv2.aruco.DICT_4X4_50,
119
"DICT_4X4_100": cv2.aruco.DICT_4X4_100,
@@ -64,28 +62,30 @@ def aruco_display(corners, ids, rejected, image):
6462

6563

6664
def pose_estimation(frame, aruco_dict_type, matrix_coefficients, distortion_coefficients):
65+
6766
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
68-
aruco_dict = cv2.aruco.Dictionary_get(aruco_dict_type) # Changed to use local variable
67+
cv2.aruco_dict = cv2.aruco.Dictionary_get(aruco_dict_type)
6968
parameters = cv2.aruco.DetectorParameters_create()
7069

71-
corners, ids, rejected_img_points = cv2.aruco.detectMarkers(
72-
gray, aruco_dict, parameters=parameters,
70+
71+
corners, ids, rejected_img_points = cv2.aruco.detectMarkers(gray, cv2.aruco_dict,parameters=parameters,
7372
cameraMatrix=matrix_coefficients,
74-
distCoeff=distortion_coefficients
75-
)
73+
distCoeff=distortion_coefficients)
7674

75+
7776
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)
77+
for i in range(0, len(ids)):
78+
79+
rvec, tvec, markerPoints = cv2.aruco.estimatePoseSingleMarkers(corners[i], 0.02, matrix_coefficients,
80+
distortion_coefficients)
81+
82+
cv2.aruco.drawDetectedMarkers(frame, corners)
8383

84-
# Draw the axis for each marker
85-
cv2.aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01)
84+
cv2.aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01)
8685

8786
return frame
8887

88+
8989

9090

9191
aruco_type = "DICT_5X5_100"

scripts/generateAruco.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import numpy as np
2+
import cv2
3+
4+
ARUCO_DICT = {
5+
"DICT_4X4_50": cv2.aruco.DICT_4X4_50,
6+
"DICT_4X4_100": cv2.aruco.DICT_4X4_100,
7+
"DICT_4X4_250": cv2.aruco.DICT_4X4_250,
8+
"DICT_4X4_1000": cv2.aruco.DICT_4X4_1000,
9+
"DICT_5X5_50": cv2.aruco.DICT_5X5_50,
10+
"DICT_5X5_100": cv2.aruco.DICT_5X5_100,
11+
"DICT_5X5_250": cv2.aruco.DICT_5X5_250,
12+
"DICT_5X5_1000": cv2.aruco.DICT_5X5_1000,
13+
"DICT_6X6_50": cv2.aruco.DICT_6X6_50,
14+
"DICT_6X6_100": cv2.aruco.DICT_6X6_100,
15+
"DICT_6X6_250": cv2.aruco.DICT_6X6_250,
16+
"DICT_6X6_1000": cv2.aruco.DICT_6X6_1000,
17+
"DICT_7X7_50": cv2.aruco.DICT_7X7_50,
18+
"DICT_7X7_100": cv2.aruco.DICT_7X7_100,
19+
"DICT_7X7_250": cv2.aruco.DICT_7X7_250,
20+
"DICT_7X7_1000": cv2.aruco.DICT_7X7_1000,
21+
"DICT_ARUCO_ORIGINAL": cv2.aruco.DICT_ARUCO_ORIGINAL,
22+
"DICT_APRILTAG_16h5": cv2.aruco.DICT_APRILTAG_16h5,
23+
"DICT_APRILTAG_25h9": cv2.aruco.DICT_APRILTAG_25h9,
24+
"DICT_APRILTAG_36h10": cv2.aruco.DICT_APRILTAG_36h10,
25+
"DICT_APRILTAG_36h11": cv2.aruco.DICT_APRILTAG_36h11
26+
}
27+
28+
29+
aruco_type = "DICT_5X5_250"
30+
id = 1
31+
32+
arucoDict = cv2.aruco.Dictionary_get(ARUCO_DICT[aruco_type])
33+
34+
print("ArUCo type '{}' with ID '{}'".format(aruco_type, id))
35+
tag_size = 250
36+
tag = np.zeros((tag_size, tag_size, 1), dtype="uint8")
37+
cv2.aruco.drawMarker(arucoDict, id, tag_size, tag, 1)
38+
39+
# Save the tag g
40+
# enerated
41+
tag_name = "arucoMarkers/" + aruco_type + "_" + str(id) + ".png"
42+
cv2.imwrite(tag_name, tag)
43+
cv2.imshow("ArUCo Tag", tag)
44+
45+
cv2.waitKey(0)
46+
47+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)