-
Notifications
You must be signed in to change notification settings - Fork 0
/
aruko_pose_estimation.py
228 lines (186 loc) · 9.34 KB
/
aruko_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import cv2
import imutils
import pickle
import numpy as np
marker_robot_len = 0.13 # cm
marker_plane_len = 0.22 # cm
# Загрузка параметров калибровки камеры
mtx, dist, rvecs, tvecs = np.zeros(1),np.zeros(1),np.zeros(1),np.zeros(1)
camera_param = [mtx, dist, rvecs, tvecs]
name_list = ['mtx', 'dist', 'rvecs', 'tvecs']
path = '/home/adminuser/Рабочий стол/camera_calibration_data/'
for i in range(len(name_list)):
with open(path+f'{name_list[i]}.pickle', 'rb') as handle:
camera_param[i] = pickle.load(handle)
def aruco_detection(image):
# load the ArUCo dictionary, grab the ArUCo parameters, and detect
# the markers
arucoDict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_250)
arucoParams = cv2.aruco.DetectorParameters_create()
(corners, ids, rejected) = cv2.aruco.detectMarkers(image, arucoDict,
parameters=arucoParams)
# verify *at least* one ArUco marker was detected
if len(corners) > 0:
# flatten the ArUco IDs list
ids = ids.flatten()
# loop over the detected ArUCo corners
for (markerCorner, markerID) in zip(corners, ids):
# extract the marker corners (which are always returned in
# top-left, top-right, bottom-right, and bottom-left order)
corners = markerCorner.reshape((4, 2))
(topLeft, topRight, bottomRight, bottomLeft) = corners
# convert each of the (x, y)-coordinate pairs to integers
topRight = (int(topRight[0]), int(topRight[1]))
bottomRight = (int(bottomRight[0]), int(bottomRight[1]))
bottomLeft = (int(bottomLeft[0]), int(bottomLeft[1]))
topLeft = (int(topLeft[0]), int(topLeft[1]))
# draw the bounding box of the ArUCo detection
cv2.line(image, topLeft, topRight, (0, 255, 0), 2)
cv2.line(image, topRight, bottomRight, (0, 255, 0), 2)
cv2.line(image, bottomRight, bottomLeft, (0, 255, 0), 2)
cv2.line(image, bottomLeft, topLeft, (0, 255, 0), 2)
# compute and draw the center (x, y)-coordinates of the ArUco
# marker
cX = int((topLeft[0] + bottomRight[0]) / 2.0)
cY = int((topLeft[1] + bottomRight[1]) / 2.0)
cv2.circle(image, (cX, cY), 4, (0, 0, 255), -1)
# draw the ArUco marker ID on the image
cv2.putText(image, str(markerID),
(topLeft[0], topLeft[1] - 15), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
# print("[INFO] ArUco marker ID: {}".format(markerID))
# show the output image
cv2.imshow("Aruco detedtion", image)
# cv2.waitKey(0)
def draw_Aruco_marker_ID(image, markerCorner, markerID):
# extract the marker corners (which are always returned in
# top-left, top-right, bottom-right, and bottom-left order)
corners = markerCorner.reshape((4, 2))
(topLeft, topRight, bottomRight, bottomLeft) = corners
# convert each of the (x, y)-coordinate pairs to integers
topRight = (int(topRight[0]), int(topRight[1]))
bottomRight = (int(bottomRight[0]), int(bottomRight[1]))
bottomLeft = (int(bottomLeft[0]), int(bottomLeft[1]))
topLeft = (int(topLeft[0]), int(topLeft[1]))
# draw the ArUco marker ID on the image
cv2.putText(image, str(markerID),
(topLeft[0], topLeft[1] - 15), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
# print("[INFO] ArUco marker ID: {}".format(markerID))
def pose_estimation(frame, matrix_coefficients, distortion_coefficients, base_id, target_id):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_250)
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)
# Print marker's ids
for i, id in zip(range(0, len(ids)),ids):
draw_Aruco_marker_ID(frame, corners[i], id)
path_vector = []
is_target_detected = False
# If markers are detected
if len(corners) > 0:
# Chacke the target presence
for id in ids:
if id == target_id[0]:
is_target_detected = True
# if no target stop robot
if is_target_detected == False:
path_vector = [0., 0., 0.]
print("No target. Robot stopped")
else:
coors0 = []
coors2 = []
rvec0 = []
tvec0 = []
for i, id in zip(range(0, len(ids)),ids):
if id == base_id:
# 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], marker_robot_len, matrix_coefficients,
distortion_coefficients)
print("ID=0:")
coors0 = get_marker_coors(rvec, tvec)[0]
rvec0 = rvec
tvec0 = tvec
print(coors0)
elif id == target_id[0]:
# 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], marker_plane_len, matrix_coefficients,
distortion_coefficients)
print("ID=1:")
coors2 = get_marker_coors(rvec, tvec)[0]
print(coors2)
else:
rvec, tvec, markerPoints = cv2.aruco.estimatePoseSingleMarkers(corners[i], marker_plane_len, 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.1)
if len(rvec0)!=0 and len(tvec0)!=0 and len(coors2)!=0:
path_vector = get_path_vector(rvec0, tvec0, coors2)
print("Path_vector: \n", path_vector)
if len(coors0)!=0 and len(coors2)!=0:
dist = pow(pow((coors0[0]-coors2[0]),2)+pow((coors0[1]-coors2[1]),2), 0.5)
print("DIST: ",round(dist[0], 3), "m")
if dist <= 0.25:
path_vector = [-1., 0., 0.] # change terget code
print("ROBOT FINISHED")
return frame, path_vector
def get_path_vector(base_rvec, base_tvec, target_coors_camera_3d):
path_vector = np.linalg.inv(make_trans_mat(base_rvec, base_tvec)).dot(target_coors_camera_3d)
return path_vector
def make_trans_mat(rvec, tvec):
R, _ = cv2.Rodrigues(rvec) # 3x3 representation of rvec
R = np.matrix(R).T # transpose of 3x3 rotation matrix
transformMatrix = np.zeros((4, 4), dtype=float) # Transformation matrix
# Transformation matrix fill operation, matrix should be [R|t,0|1]
transformMatrix[0:3, 0:3] = R
transformMatrix[0:3, 3] = tvec
transformMatrix[3, 3] = 1
return transformMatrix
def get_marker_coors(rvec, tvec):
transformMatrix = make_trans_mat(rvec, tvec)
# коррдинаты маркера в его системе координат
world_coors = np.array([[0],[0],[0],[1]])
# на изображении X - красная ось, У - зеленая ось, Z - синяя, 1
# координаты маркера относительно камеры в 3д
# координатные оси камеры совпадают с осями маркера, но
# ось У направлена противоположно
camera_3d_coors = transformMatrix.dot(world_coors)
# print("RES:\n", camera_3d_coors)
P_matr = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0]])
# координаты (х,у) в плоскости изображения
# (0,0) - верхний левый угол, х - влево, у - вниз
camera_2d_coors = camera_param[0].dot(P_matr.dot(camera_3d_coors))
# print("RES:\n", camera_2d_coors)
return camera_3d_coors, camera_2d_coors
if __name__ == '__main__':
# захват видео потока
cam = cv2.VideoCapture(0)
while True:
# получение кадров
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
# изменить размер окна
frame = imutils.resize(frame, width=860)
# ARUCO DETECTION
# aruco_detection(frame)
base_id = 0
target_id = 140
#POSE ESTIMATION
estim_frame, _ = pose_estimation(frame, camera_param[0], camera_param[1], base_id, target_id)
cv2.imshow("aruko test", estim_frame)
k = cv2.waitKey(1)
# выход
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
cam.release()
cv2.destroyAllWindows()