-
Notifications
You must be signed in to change notification settings - Fork 90
/
pose_estimation.py
87 lines (62 loc) · 2.78 KB
/
pose_estimation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'''
Sample Usage:-
python pose_estimation.py --K_Matrix calibration_matrix.npy --D_Coeff distortion_coefficients.npy --type DICT_5X5_100
'''
import numpy as np
import cv2
import sys
from utils import ARUCO_DICT
import argparse
import time
def pose_esitmation(frame, aruco_dict_type, matrix_coefficients, distortion_coefficients):
'''
frame - Frame from the video stream
matrix_coefficients - Intrinsic matrix of the calibrated camera
distortion_coefficients - Distortion coefficients associated with your camera
return:-
frame - The frame with the axis drawn on it
'''
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.aruco_dict = cv2.aruco.Dictionary_get(aruco_dict_type)
parameters = cv2.aruco.DetectorParameters_create()
corners, ids, rejected_img_points = cv2.aruco.detectMarkers(gray, cv2.aruco_dict,parameters=parameters,
cameraMatrix=matrix_coefficients,
distCoeff=distortion_coefficients)
# If markers are detected
if len(corners) > 0:
for i in range(0, len(ids)):
# Estimate pose of each marker and return the values rvec and tvec---(different from those of camera coefficients)
rvec, tvec, markerPoints = cv2.aruco.estimatePoseSingleMarkers(corners[i], 0.02, matrix_coefficients,
distortion_coefficients)
# Draw a square around the markers
cv2.aruco.drawDetectedMarkers(frame, corners)
# Draw Axis
cv2.aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01)
return frame
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-k", "--K_Matrix", required=True, help="Path to calibration matrix (numpy file)")
ap.add_argument("-d", "--D_Coeff", required=True, help="Path to distortion coefficients (numpy file)")
ap.add_argument("-t", "--type", type=str, default="DICT_ARUCO_ORIGINAL", help="Type of ArUCo tag to detect")
args = vars(ap.parse_args())
if ARUCO_DICT.get(args["type"], None) is None:
print(f"ArUCo tag type '{args['type']}' is not supported")
sys.exit(0)
aruco_dict_type = ARUCO_DICT[args["type"]]
calibration_matrix_path = args["K_Matrix"]
distortion_coefficients_path = args["D_Coeff"]
k = np.load(calibration_matrix_path)
d = np.load(distortion_coefficients_path)
video = cv2.VideoCapture(0)
time.sleep(2.0)
while True:
ret, frame = video.read()
if not ret:
break
output = pose_esitmation(frame, aruco_dict_type, k, d)
cv2.imshow('Estimated Pose', output)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()