-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.py
383 lines (304 loc) · 12.9 KB
/
utils.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import copy
import json
import numpy as np
from PIL import Image
from tqdm import tqdm
from argparse import ArgumentParser
from ai2thor.controller import Controller
from ai2thor.hooks.procedural_asset_hook import ProceduralAssetHookRunner
from moviepy.editor import TextClip, CompositeVideoClip, concatenate_videoclips, ImageSequenceClip
def all_edges_white(img):
# Define a white pixel
white = [255, 255, 255]
# Check top edge
if not np.all(np.all(img[0, :] == white, axis=-1)):
return False
# Check bottom edge
if not np.all(np.all(img[-1, :] == white, axis=-1)):
return False
# Check left edge
if not np.all(np.all(img[:, 0] == white, axis=-1)):
return False
# Check right edge
if not np.all(np.all(img[:, -1] == white, axis=-1)):
return False
# If all the conditions met
return True
def get_top_down_frame(scene, objaverse_asset_dir, width=1024, height=1024):
controller = Controller(
agentMode="default",
makeAgentsVisible=False,
visibilityDistance=1.5,
scene=scene,
width=width,
height=height,
fieldOfView=90,
action_hook_runner=ProceduralAssetHookRunner(
asset_directory=objaverse_asset_dir,
asset_symlink=True,
verbose=True,
)
)
# Setup the top-down camera
event = controller.step(action="GetMapViewCameraProperties", raise_for_failure=True)
pose = copy.deepcopy(event.metadata["actionReturn"])
bounds = event.metadata["sceneBounds"]["size"]
pose["fieldOfView"] = 60
pose["position"]["y"] = bounds["y"]
del pose["orthographicSize"]
try: wall_height = wall_height = max([point["y"] for point in scene["walls"][0]["polygon"]])
except: wall_height = 2.5
for i in range(20):
pose["orthographic"] = False
pose["farClippingPlane"] = pose["position"]["y"] + 10
pose["nearClippingPlane"] = pose["position"]["y"] - wall_height
# add the camera to the scene
event = controller.step(
action="AddThirdPartyCamera",
**pose,
skyboxColor="white",
raise_for_failure=True,
)
top_down_frame = event.third_party_camera_frames[-1]
# check if the edge of the frame is white
if all_edges_white(top_down_frame): break
pose["position"]["y"] += 0.75
controller.stop()
image = Image.fromarray(top_down_frame)
return image
def get_top_down_frame_ithor(scene, objaverse_asset_dir, width=1024, height=1024):
controller = Controller(
agentMode="default",
makeAgentsVisible=False,
visibilityDistance=1.5,
scene=scene,
width=width,
height=height,
fieldOfView=90,
action_hook_runner=ProceduralAssetHookRunner(
asset_directory=objaverse_asset_dir,
asset_symlink=True,
verbose=True,
)
)
controller.reset(scene)
event = controller.step(action="GetMapViewCameraProperties")
pose = copy.deepcopy(event.metadata["actionReturn"])
event = controller.step(
action="AddThirdPartyCamera",
**pose,
skyboxColor="white",
raise_for_failure=True,
)
controller.stop()
top_down_frame = event.third_party_camera_frames[0]
return Image.fromarray(top_down_frame)
def main(save_path):
scene = json.load(open(save_path + f"scene.json", "r"))
image = get_top_down_frame(scene)
image.save(f"test1.png")
with open(save_path + f"scene.json", "w") as f:
json.dump(scene, f, indent=4)
def visualize_asset(asset_id, version):
empty_house = json.load(open("empty_house.json", "r"))
empty_house["objects"] = [{
"assetId": asset_id,
"id": "test_asset",
"kinematic": True,
"position": {
"x": 0,
"y": 0,
"z": 0
},
"rotation": {
"x": 0,
"y": 0,
"z": 0
},
"material": None
}]
image = get_top_down_frame(empty_house, version)
image.show()
def get_room_images(scene, objaverse_asset_dir, width=1024, height=1024):
controller = Controller(
agentMode="default",
makeAgentsVisible=False,
visibilityDistance=1.5,
scene=scene,
width=width,
height=height,
fieldOfView=135,
action_hook_runner=ProceduralAssetHookRunner(
asset_directory=objaverse_asset_dir,
asset_symlink=True,
verbose=True,
),
)
wall_height = max([point["y"] for point in scene["walls"][0]["polygon"]])
room_images = {}
for room in scene["rooms"]:
room_name = room["roomType"]
camera_height = wall_height - 0.2
room_vertices = [[point["x"], point["z"]] for point in room['floorPolygon']]
room_center = np.mean(room_vertices, axis=0)
floor_center = np.array([room_center[0], 0, room_center[1]])
camera_center = np.array([room_center[0], camera_height, room_center[1]])
corners = np.array([[point[0], camera_height, point[1]] for point in room_vertices])
farest_corner = np.argmax(np.linalg.norm(corners - camera_center, axis=1))
vector_1 = floor_center - camera_center
vector_2 = farest_corner - camera_center
x_angle = 90 - np.arccos(np.dot(vector_1, vector_2) / (np.linalg.norm(vector_1) * np.linalg.norm(vector_2))) * 180 / np.pi
if not controller.last_event.third_party_camera_frames:
controller.step(
action="AddThirdPartyCamera",
position=dict(x=camera_center[0], y=camera_center[1], z=camera_center[2]),
rotation=dict(x=0, y=0, z=0),
)
images = []
for angle in tqdm(range(0, 360, 90)):
controller.step(
action="UpdateThirdPartyCamera",
rotation=dict(x=x_angle, y=angle+45, z=0),
position=dict(x=camera_center[0], y=camera_center[1], z=camera_center[2]),
)
images.append(Image.fromarray(controller.last_event.third_party_camera_frames[0]))
room_images[room_name] = images
controller.stop()
return room_images
def ithor_video(scene, objaverse_asset_dir, width, height, scene_type):
controller = Controller(
agentMode="default",
makeAgentsVisible=False,
visibilityDistance=2,
scene=scene,
width=width,
height=height,
fieldOfView=90,
action_hook_runner=ProceduralAssetHookRunner(
asset_directory=objaverse_asset_dir,
asset_symlink=True,
verbose=True,
),
)
event = controller.step(action="GetMapViewCameraProperties", raise_for_failure=True)
pose = copy.deepcopy(event.metadata["actionReturn"])
wall_height = 2.5
camera_height = wall_height - 0.2
if not controller.last_event.third_party_camera_frames:
controller.step(
action="AddThirdPartyCamera",
position=dict(x=pose["position"]["x"], y=camera_height, z=pose["position"]["z"]),
rotation=dict(x=0, y=0, z=0),
)
images = []
for angle in tqdm(range(0, 360, 1)):
controller.step(
action="UpdateThirdPartyCamera",
rotation=dict(x=45, y=angle, z=0),
position=dict(x=pose["position"]["x"], y=camera_height, z=pose["position"]["z"]),
)
images.append(controller.last_event.third_party_camera_frames[0])
imsn = ImageSequenceClip(images, fps=30)
# Create text clips
txt_clip_query = (TextClip(f"Query: {scene_type}", fontsize=30, color='white', font='Arial-Bold')
.set_pos(('center', 'top')).set_duration(imsn.duration))
txt_clip_room = (TextClip(f"Room Type: {scene_type}", fontsize=30, color='white', font='Arial-Bold')
.set_pos(('center', 'bottom')).set_duration(imsn.duration))
# Overlay the text clip on the first video clip
video = CompositeVideoClip([imsn, txt_clip_query, txt_clip_room])
controller.stop()
return video
def room_video(scene, objaverse_asset_dir, width, height):
def add_line_breaks(text, max_line_length):
words = text.split(' ')
lines = []
current_line = []
for word in words:
if len(' '.join(current_line + [word])) <= max_line_length:
current_line.append(word)
else:
lines.append(' '.join(current_line))
current_line = [word]
lines.append(' '.join(current_line))
return '\n'.join(lines)
"""Saves a top-down video of the house."""
controller = Controller(
agentMode="default",
makeAgentsVisible=False,
visibilityDistance=2,
scene=scene,
width=width,
height=height,
fieldOfView=90,
action_hook_runner=ProceduralAssetHookRunner(
asset_directory=objaverse_asset_dir,
asset_symlink=True,
verbose=True,
),
)
try: query = scene["query"]
except: query = scene["rooms"][0]['roomType']
wall_height = max([point["y"] for point in scene["walls"][0]["polygon"]])
text_query = add_line_breaks(query, 60)
videos = []
for room in scene["rooms"]:
room_name = room["roomType"]
camera_height = wall_height - 0.2
print("camera height: ", camera_height)
room_vertices = [[point["x"], point["z"]] for point in room['floorPolygon']]
room_center = np.mean(room_vertices, axis=0)
floor_center = np.array([room_center[0], 0, room_center[1]])
camera_center = np.array([room_center[0], camera_height, room_center[1]])
corners = np.array([[point["x"], point["y"], point["z"]] for point in room['floorPolygon']])
farest_corner = corners[np.argmax(np.linalg.norm(corners - camera_center, axis=1))]
vector_1 = floor_center - camera_center
vector_2 = farest_corner - camera_center
x_angle = 90 - np.arccos(np.dot(vector_1, vector_2) / (np.linalg.norm(vector_1) * np.linalg.norm(vector_2))) * 180 / np.pi
images = []
if not controller.last_event.third_party_camera_frames:
controller.step(
action="AddThirdPartyCamera",
position=dict(x=camera_center[0], y=camera_center[1], z=camera_center[2]),
rotation=dict(x=0, y=0, z=0),
)
for angle in tqdm(range(0, 360, 1)):
controller.step(
action="UpdateThirdPartyCamera",
rotation=dict(x=x_angle, y=angle, z=0),
position=dict(x=camera_center[0], y=camera_center[1], z=camera_center[2]),
)
images.append(controller.last_event.third_party_camera_frames[0])
imsn = ImageSequenceClip(images, fps=30)
# Create text clips
txt_clip_query = (TextClip(f"Query: {text_query}", fontsize=30, color='white', font='Arial-Bold')
.set_pos(('center', 'top')).set_duration(imsn.duration))
txt_clip_room = (TextClip(f"Room Type: {room_name}", fontsize=30, color='white', font='Arial-Bold')
.set_pos(('center', 'bottom')).set_duration(imsn.duration))
# Overlay the text clip on the first video clip
video = CompositeVideoClip([imsn, txt_clip_query, txt_clip_room])
# Add this room's video to the list
videos.append(video)
# Concatenate all room videos into one final video
final_video = concatenate_videoclips(videos)
controller.stop()
return final_video
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--mode", help = "Mode to run (top_down_frame, room_video, room_image).", default = "top_down_frame")
parser.add_argument("--objaverse_asset_dir", help = "Directory to load assets from.", default = "./data/objaverse_holodeck/09_23_combine_scale/processed_2023_09_23_combine_scale")
parser.add_argument("--scene", help = "Scene to load.", default = "data/scenes/a_living_room/a_living_room.json")
args = parser.parse_args()
scene = json.load(open(args.scene, "r"))
if "query" not in scene: scene["query"] = args.scene.split("/")[-1].split(".")[0]
if args.mode == "top_down_frame":
image = get_top_down_frame(scene, args.objaverse_asset_dir)
image.show()
elif args.mode == "room_video":
video = room_video(scene, args.objaverse_asset_dir, 1024, 1024)
video.write_videofile(args.scene.replace(".json", ".mp4"), fps=30)
elif args.mode == "room_image":
room_images = get_room_images(scene, args.objaverse_asset_dir, 1024, 1024)
save_folder = "/".join(args.scene.split("/")[:-1])
for room_name, images in room_images.items():
for i, image in enumerate(images):
image.save(f"{save_folder}/{room_name}_{i}.png")