Skip to content

Commit 52567f9

Browse files
...
1 parent 60a48a3 commit 52567f9

File tree

5 files changed

+135
-5
lines changed

5 files changed

+135
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55

6+
psdenv/
7+
68
# C extensions
79
*.so
810

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ To make the setup script executable, run the following command in your terminal:
2525
chmod +x setup.sh
2626
```
2727

28+
pip install opencv-contrib-python==4.6.0.66
29+
30+
2831
<br>
2932

3033
<div align="center">

main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def main():
1414

1515
scripts = {
1616
"1": {
17-
"name": "Run 'Script00'",
18-
"description": "This is Script01",
17+
"name": "Run 'marker.py'",
18+
"description": "This is marker.py",
1919
"file_name": "scripts/marker.py"
2020
},
2121
"2": {
22-
"name": "Run 'Script01",
23-
"description": "This is Script01",
24-
"file_name": "scripts/marker.py"
22+
"name": "Run 'pose_estimation.py",
23+
"description": "This is pose_estimation.py",
24+
"file_name": "scripts/pose_estimation.py"
2525
},
2626
"00": {
2727
"name": "Run 'install_dependencies.py'",

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
opencv-python==1.26.2
2+
opencv-contrib-python>=4.5.0
3+
matplotlib>=3.0.0
4+
numpy>=1.19.0

scripts/pose_estimation.py

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

Comments
 (0)