-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpose_estimation.py
More file actions
208 lines (145 loc) · 9.74 KB
/
Copy pathpose_estimation.py
File metadata and controls
208 lines (145 loc) · 9.74 KB
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
# Import Necessary Libraries
import spot_robot_commands
import read_video_stream
import numpy as np
import utils
import cv2
# Define the Pose of Grasp location (Seatrest from above: X - down, Y - left) wrt AruCo on Chair (Seatrest behind chair: X - right, Y - up)
grasp_pose_location_wrt_aruco_on_chair = np.array([
[ 0, -1, 0, 0.0000],
[-1, 0, 0, 0.4000],
[ 0, 0, -1, -0.0100],
[ 0, 0, 0, 1.0000]
])
# Define a Function to Get Pose of AruCo tag in Image frame
def estimate_poses_of_aruco_tags(frame, aruco_dict_type, camera_calibration_params):
# Extract Camera Matrix and Distortion coefficients
matrix_coefficients, distortion_coefficients = camera_calibration_params['Calibration_matrix'], camera_calibration_params['Distortion_coefficients']
# Convert the Image into Grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Create the Parameters to Detect AruCo markers
arucoDict = cv2.aruco.getPredefinedDictionary(aruco_dict_type)
arucoParams = cv2.aruco.DetectorParameters()
# Adjust detector parameters for small markers
arucoParams.cornerRefinementMethod = cv2.aruco.CORNER_REFINE_SUBPIX
# Create AruCo detector with those Parameters
detector = cv2.aruco.ArucoDetector(arucoDict, arucoParams)
# Create the Parameters to Detect AruCo markers
corners, ids, _ = detector.detectMarkers(gray)
# If AruCo IDs are Detected
if ids is not None:
# Initialise List to store Poses of AruCo markers
poses_of_aruco_tags = []
# For every AruCo ID detected
for i in range(len(ids)):
# Initialise Dictionary to store Pose of an AruCo marker
aruco_tag_pose = {}
# Compute the Pose of AruCo markers
rvecs, tvecs, _ = cv2.aruco.estimatePoseSingleMarkers(corners, 0.20, matrix_coefficients, distortion_coefficients)
# Store all Parameters into Dictionary
aruco_tag_pose['ID'] = int(ids[i].item()) # ID of AruCo tag
aruco_tag_pose['Pose'] = utils.compute_pose_from_components(
translation = list(tvecs[i][0]),
rotation = list(rvecs[i][0]),
degrees = False
) # Pose of AruCo tag with Red-square at Top-Left
# Draw Pose axes in the AruCo tag image
cv2.aruco.drawDetectedMarkers(frame, corners)
cv2.drawFrameAxes(frame, matrix_coefficients, distortion_coefficients, rvecs[i], tvecs[i], 0.1)
# Append Pose of AruCo tag into List
poses_of_aruco_tags.append(aruco_tag_pose)
# Display Image with Estimated Poses of AruCo markers
read_video_stream.display_image(camera_calibration_params['Name'], frame)
# Return Estimated Poses of AruCo tags
return poses_of_aruco_tags
# Else Return None
else:
# Display Image
read_video_stream.display_image(camera_calibration_params['Name'], frame)
return None
# Define a Function to Localize SPOT wrt Origin AruCo tag
def localize_spot_wrt_origin(aruco_tags_data_wrt_spot_frame, objects):
# For every AruCo tag detected by SPOT
for aruco_tag_data_wrt_spot_frame in aruco_tags_data_wrt_spot_frame:
# Get the AruCo ID detected
aruco_id = aruco_tag_data_wrt_spot_frame['ID']
# Get the Oject name using AruCo ID
object_name = objects.get_name_of_object(aruco_id)
# If Object is Wall marker
if "Wall" in object_name:
# Get the Pose of Non-Origin marker wrt Origin marker
pose_of_wall = objects.get_pose_of_object(object_name)
# Return the Pose of SPOT body frame wrt Origin
return utils.round_matrix_list(pose_of_wall @ np.linalg.inv(aruco_tag_data_wrt_spot_frame['Pose']), 3)
# Define a Function to Update Poses of Chairs
def update_poses_of_chairs(images, objects, aruco_tags_data_wrt_spot_frame,
pose_of_spot_body_frame, aruco_type, camera_calibration_params):
# Get the Pose of AruCo tags wrt both Cameras
aruco_tags_data_wrt_camera_1_frame = estimate_poses_of_aruco_tags(images[0], aruco_type, camera_calibration_params['Camera_1'])
aruco_tags_data_wrt_camera_2_frame = estimate_poses_of_aruco_tags(images[1], aruco_type, camera_calibration_params['Camera_2'])
# For every Chair Object
for i in range(len(objects.chairs)):
# Get the Pose of Chair wrt Camera and SPOT frames
pose_of_chair_wrt_camera_1_frame = utils.get_pose_of_aruco_tag(aruco_tags_data_wrt_camera_1_frame, objects.chairs[i].aruco_id)
pose_of_chair_wrt_camera_2_frame = utils.get_pose_of_aruco_tag(aruco_tags_data_wrt_camera_2_frame, objects.chairs[i].aruco_id)
pose_of_chair_wrt_spot_frame = utils.get_pose_of_aruco_tag(aruco_tags_data_wrt_spot_frame, objects.chairs[i].aruco_id)
# If Chair is detected by Camera 1
if pose_of_chair_wrt_camera_1_frame is not None:
# Update Pose of Chair wrt Origin AruCo tag
objects.chairs[i].pose['Pose'] = utils.round_matrix_list(objects.get_pose_of_object('Camera_1') @ pose_of_chair_wrt_camera_1_frame, 3)
# Update Pose of Chair wrt Camera frame
objects.chairs[i].pose_wrt_camera['Pose'] = pose_of_chair_wrt_camera_1_frame
# If Chair is detected by Camera 2
elif pose_of_chair_wrt_camera_2_frame is not None:
# Update Pose of Chair wrt Origin AruCo tag
objects.chairs[i].pose['Pose'] = utils.round_matrix_list(objects.get_pose_of_object('Camera_2') @ pose_of_chair_wrt_camera_2_frame, 3)
# Update Pose of Chair wrt Camera frame
objects.chairs[i].pose_wrt_camera['Pose'] = pose_of_chair_wrt_camera_2_frame
# If Chair is detected by SPOT
elif pose_of_chair_wrt_spot_frame is not None:
# Update Pose of Chair wrt Origin AruCo tag
objects.chairs[i].pose['Pose'] = utils.round_matrix_list(pose_of_spot_body_frame @ pose_of_chair_wrt_spot_frame, 3)
# Update pose components of Chairs
objects.chairs[i].pose['Translation'], objects.chairs[i].pose['Rotation'] = utils.get_components_from_pose_for_chair(objects.chairs[i].pose['Pose'])
objects.chairs[i].pose_wrt_camera['Translation'], objects.chairs[i].pose_wrt_camera['Rotation'] = utils.get_components_from_pose(objects.chairs[i].pose_wrt_camera['Pose'])
# Return the Objects with updated Poses
return objects
# Define a Function to Update Final poses of Chairs wrt Camera Frame
def update_final_poses_of_chairs_wrt_camera(images, objects, aruco_type, camera_calibration_params):
# Get the Pose of AruCo tags wrt both Cameras
aruco_tags_data_wrt_camera_1_frame = estimate_poses_of_aruco_tags(images[0], aruco_type, camera_calibration_params['Camera_1'])
aruco_tags_data_wrt_camera_2_frame = estimate_poses_of_aruco_tags(images[1], aruco_type, camera_calibration_params['Camera_2'])
# For every Chair Object
for i in range(len(objects.chairs)):
# Get the Pose of Chair wrt Camera frame
pose_of_chair_wrt_camera_1_frame = utils.get_pose_of_aruco_tag(aruco_tags_data_wrt_camera_1_frame, objects.chairs[i].aruco_id)
pose_of_chair_wrt_camera_2_frame = utils.get_pose_of_aruco_tag(aruco_tags_data_wrt_camera_2_frame, objects.chairs[i].aruco_id)
# If Chair is detected by Camera 1
if pose_of_chair_wrt_camera_1_frame is not None:
# Update Pose of Chair wrt Camera
objects.chairs[i].final_pose_wrt_camera['Pose'] = pose_of_chair_wrt_camera_1_frame
# If Chair is detected by Camera 2
elif pose_of_chair_wrt_camera_2_frame is not None:
# Update Pose of Chair wrt Camera
objects.chairs[i].final_pose_wrt_camera['Pose'] = pose_of_chair_wrt_camera_2_frame
# Update pose components of Chairs
objects.chairs[i].final_pose_wrt_camera['Translation'], objects.chairs[i].final_pose_wrt_camera['Rotation'] = utils.get_components_from_pose(objects.chairs[i].final_pose_wrt_camera['Pose'])
# Return the Objects with updated Poses
return objects
# Define a Function to Compute Grasp Pose to Grasp Chair
def compute_grasp_pose(pose_of_chair_wrt_spot):
# Extract Translation vector for AruCo marker on Chair wrt SPOT
x, y, z = pose_of_chair_wrt_spot[0][3], pose_of_chair_wrt_spot[1][3], pose_of_chair_wrt_spot[2][3]
# Update Pose of AruCo on Chair wrt SPOT Robot
pose_of_chair_wrt_spot = np.array([
[ 0, 0, -1, x],
[-1, 0, 0, y],
[ 0, 1, 0, z],
[ 0, 0, 0, 1]
])
# Compute and Return the Grasp Pose of Chair wrt SPOT
grasp_pose_wrt_spot = utils.round_matrix_list(pose_of_chair_wrt_spot @ grasp_pose_location_wrt_aruco_on_chair, 3)
return grasp_pose_wrt_spot
# Define a Function to Get the Pose of ArUco on Chair wrt SPOT using Grasp pose
def get_pose_of_chair_wrt_SPOT(robot):
return utils.round_matrix_list(spot_robot_commands.get_pose_of_arm(robot) @ np.linalg.inv(grasp_pose_location_wrt_aruco_on_chair), 3)