forked from kwonathan/language-models-trajectory-generators
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.py
241 lines (162 loc) · 10.2 KB
/
api.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import numpy as np
import sys
import torch
import math
import config
import models
import utils
from PIL import Image
from prompts.success_detection_prompt import SUCCESS_DETECTION_PROMPT
from config import OK, PROGRESS, FAIL, ENDC
from config import CAPTURE_IMAGES, ADD_BOUNDING_CUBES, ADD_TRAJECTORY_POINTS, EXECUTE_TRAJECTORY, OPEN_GRIPPER, CLOSE_GRIPPER, TASK_COMPLETED, RESET_ENVIRONMENT
class API:
def __init__(self, args, main_connection, logger, langsam_model, xmem_model, device):
self.args = args
self.main_connection = main_connection
self.logger = logger
self.langsam_model = langsam_model
self.xmem_model = xmem_model
self.device = device
self.segmentation_texts = []
self.segmentation_count = 0
self.trajectory_length = 0
self.attempted_task = False
self.completed_task = False
self.failed_task = False
self.head_camera_position = None
self.head_camera_orientation_q = None
self.wrist_camera_position = None
self.wrist_camera_orientation_q = None
self.command = None
def detect_object(self, segmentation_text):
self.logger.info(PROGRESS + "Capturing head and wrist camera images..." + ENDC)
self.main_connection.send([CAPTURE_IMAGES])
[head_camera_position, head_camera_orientation_q, wrist_camera_position, wrist_camera_orientation_q, env_connection_message] = self.main_connection.recv()
self.logger.info(env_connection_message)
self.head_camera_position = head_camera_position
self.head_camera_orientation_q = head_camera_orientation_q
self.wrist_camera_position = wrist_camera_position
self.wrist_camera_orientation_q = wrist_camera_orientation_q
rgb_image_head = Image.open(config.rgb_image_head_path).convert("RGB")
depth_image_head = Image.open(config.depth_image_head_path).convert("L")
depth_array = np.array(depth_image_head) / 255.
if self.segmentation_count == 0:
xmem_image = Image.fromarray(np.zeros_like(depth_array)).convert("L")
xmem_image.save(config.xmem_input_path)
segmentation_texts = [segmentation_text]
self.logger.info(PROGRESS + "Segmenting head camera image..." + ENDC)
model_predictions, boxes, segmentation_texts = models.get_langsam_output(rgb_image_head, self.langsam_model, segmentation_texts, self.segmentation_count)
self.logger.info(OK + "Finished segmenting head camera image!" + ENDC)
masks = utils.get_segmentation_mask(model_predictions, config.segmentation_threshold)
bounding_cubes_world_coordinates, bounding_cubes_orientations = utils.get_bounding_cube_from_point_cloud(rgb_image_head, masks, depth_array, self.head_camera_position, self.head_camera_orientation_q, self.segmentation_count)
utils.save_xmem_image(masks)
self.segmentation_texts.extend(segmentation_texts)
self.logger.info(PROGRESS + "Adding bounding cubes to the environment..." + ENDC)
self.main_connection.send([ADD_BOUNDING_CUBES, bounding_cubes_world_coordinates])
[env_connection_message] = self.main_connection.recv()
self.logger.info(env_connection_message)
for i, bounding_cube_world_coordinates in enumerate(bounding_cubes_world_coordinates):
bounding_cube_world_coordinates[4][2] -= config.depth_offset
object_width = np.around(np.linalg.norm(bounding_cube_world_coordinates[1] - bounding_cube_world_coordinates[0]), 3)
object_length = np.around(np.linalg.norm(bounding_cube_world_coordinates[2] - bounding_cube_world_coordinates[1]), 3)
object_height = np.around(np.linalg.norm(bounding_cube_world_coordinates[5] - bounding_cube_world_coordinates[0]), 3)
print("Position of " + segmentation_texts[i] + ":", list(np.around(bounding_cube_world_coordinates[4], 3)))
print("Dimensions:")
print("Width:", object_width)
print("Length:", object_length)
print("Height:", object_height)
if object_width < object_length:
print("Orientation along shorter side (width):", np.around(bounding_cubes_orientations[i][0], 3))
print("Orientation along longer side (length):", np.around(bounding_cubes_orientations[i][1], 3), "\n")
else:
print("Orientation along shorter side (length):", np.around(bounding_cubes_orientations[i][1], 3))
print("Orientation along longer side (width):", np.around(bounding_cubes_orientations[i][0], 3), "\n")
self.segmentation_count += 1
def execute_trajectory(self, trajectory):
self.logger.info(PROGRESS + "Adding trajectory points to the environment..." + ENDC)
self.main_connection.send([ADD_TRAJECTORY_POINTS, trajectory])
self.logger.info(PROGRESS + "Executing generated trajectory..." + ENDC)
self.main_connection.send([EXECUTE_TRAJECTORY, trajectory])
self.trajectory_length += len(trajectory)
def open_gripper(self):
self.logger.info(PROGRESS + "Opening gripper..." + ENDC)
self.main_connection.send([OPEN_GRIPPER])
def close_gripper(self):
self.logger.info(PROGRESS + "Closing gripper..." + ENDC)
self.main_connection.send([CLOSE_GRIPPER])
def task_completed(self):
if self.attempted_task:
self.completed_task = True
else:
self.logger.info(PROGRESS + "Waiting to execute all generated trajectories..." + ENDC)
self.main_connection.send([TASK_COMPLETED])
[env_connection_message] = self.main_connection.recv()
self.logger.info(env_connection_message)
self.logger.info(PROGRESS + "Generating XMem output..." + ENDC)
masks = models.get_xmem_output(self.xmem_model, self.device, self.trajectory_length)
self.logger.info(OK + "Finished generating XMem output!" + ENDC)
num_objects = len(np.unique(masks[0])) - 1
new_prompt = SUCCESS_DETECTION_PROMPT.replace("[INSERT TASK]", self.command)
new_prompt += "\n"
self.logger.info(PROGRESS + "Calculating object bounding cubes..." + ENDC)
for object in range(1, num_objects + 1):
object_positions = []
object_orientations = []
idx_offset = 0
for i, mask in enumerate(masks):
rgb_image = Image.open(config.rgb_image_trajectory_path.format(step=i * config.xmem_output_every)).convert("RGB")
depth_image = Image.open(config.depth_image_trajectory_path.format(step=i * config.xmem_output_every)).convert("L")
depth_array = np.array(depth_image) / 255.
object_mask = mask.copy()
object_mask[object_mask != object] = False
object_mask[object_mask == object] = True
object_mask = torch.Tensor(object_mask)
bounding_cubes, orientations = utils.get_bounding_cube_from_point_cloud(rgb_image, [object_mask], depth_array, self.head_camera_position, self.head_camera_orientation_q, object - 1)
if len(bounding_cubes) == 0:
self.logger.info("No bounding cube found: removed.")
idx_offset += 1
else:
[bounding_cube] = bounding_cubes
[orientation] = orientations
position = bounding_cube[4]
orientation = orientation[0]
orientation = np.mod(orientation + math.pi, 2 * math.pi) - math.pi
object_positions.append(position)
if i == 0:
object_orientations.append(orientation)
else:
previous_orientation = object_orientations[i - 1 - idx_offset]
possible_orientations = np.array([np.mod(orientation + i * math.pi / 2 + math.pi, 2 * math.pi) - math.pi for i in range(4)])
circular_difference = np.minimum(np.abs(possible_orientations - previous_orientation), 2 * math.pi - np.abs(possible_orientations - previous_orientation))
min_index = np.argmin(circular_difference)
orientation = possible_orientations[min_index]
object_orientations.append(orientation)
new_prompt += self.segmentation_texts[object - 1] + " trajectory positions and orientations:\n"
new_prompt += "Positions:\n"
new_prompt += str(np.around([position for p, position in enumerate(object_positions) if p % config.xmem_lm_input_every == 0], 3)) + "\n"
new_prompt += "Orientations:\n"
new_prompt += str(np.around([orientation for o, orientation in enumerate(object_orientations) if o % config.xmem_lm_input_every == 0], 3)) + "\n"
new_prompt += "\n"
self.logger.info(OK + "Finished calculating object bounding cubes!" + ENDC)
self.attempted_task = True
messages = []
self.logger.info(PROGRESS + "Generating ChatGPT output..." + ENDC)
messages = models.get_chatgpt_output(self.args.language_model, new_prompt, messages, "system", file=sys.stderr)
self.logger.info(OK + "Finished generating ChatGPT output!" + ENDC)
code_block = messages[-1]["content"].split("```python")
task_completed = self.task_completed
task_failed = self.task_failed
for block in code_block:
if len(block.split("```")) > 1:
code = block.split("```")[0]
exec(code)
def task_failed(self):
self.failed_task = True
self.logger.info(PROGRESS + "Resetting environment..." + ENDC)
self.main_connection.send([RESET_ENVIRONMENT])
[env_connection_message] = self.main_connection.recv()
self.logger.info(env_connection_message)
self.segmentation_count = 0
self.trajectory_length = 0
self.segmentation_texts = []
self.attempted_task = False