-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhabitat_utils.py
162 lines (146 loc) · 5.81 KB
/
habitat_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
import math
import glob
import cv2
import json
import os
import shutil
import random
import sys
import magnum as mn
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
import habitat_sim
from habitat_sim.utils import common as ut
from habitat_sim.utils import viz_utils as vut
from habitat_sim.gfx import LightInfo, LightPositionModel
from habitat_sim.utils.common import quat_from_two_vectors, quat_rotate_vector
from habitat_sim import geo
def make_video_cv2(observations, prefix=""):
output_path = "./data/vids/"
if not os.path.exists(output_path):
os.makedirs(output_path)
shp = observations[0]['color_sensor_1st_person'].shape
videodims = (shp[1], shp[0])
fourcc = cv2.VideoWriter_fourcc("m", "p", "4", "v")
vid_name = output_path + prefix + ".mp4"
video = cv2.VideoWriter(vid_name, fourcc, 60, videodims)
for ob in observations:
bgr_im_1st_person = ob['color_sensor_1st_person'][..., 0:3][..., ::-1]
video.write(bgr_im_1st_person)
video.release()
print("Saved to", vid_name)
def make_cfg(settings):
sim_cfg = habitat_sim.SimulatorConfiguration()
sim_cfg.gpu_device_id = settings["gpu_device_id"]
sim_cfg.scene_id = settings["scene"]
sim_cfg.enable_physics = settings["enable_physics"]
# Note: all sensors must have the same resolution
sensor_specs = []
HFOV = 62
if settings["color_sensor_1st_person"]:
color_sensor_1st_person_spec = habitat_sim.CameraSensorSpec()
color_sensor_1st_person_spec.uuid = "color_sensor_1st_person"
color_sensor_1st_person_spec.hfov = mn.Deg(HFOV)
# color_sensor_1st_person_spec.near = 1e-1
color_sensor_1st_person_spec.sensor_type = habitat_sim.SensorType.COLOR
color_sensor_1st_person_spec.resolution = [
settings["height"],
settings["width"],
]
color_sensor_1st_person_spec.position = [
0.0, settings["sensor_height"], 0.0
]
color_sensor_1st_person_spec.orientation = [
settings["sensor_pitch"],
0.0,
0.0,
]
color_sensor_1st_person_spec.sensor_subtype = habitat_sim.SensorSubType.PINHOLE
sensor_specs.append(color_sensor_1st_person_spec)
if settings["depth_sensor_1st_person"]:
depth_sensor_1st_person_spec = habitat_sim.CameraSensorSpec()
depth_sensor_1st_person_spec.uuid = "depth_sensor_1st_person"
depth_sensor_1st_person_spec.sensor_type = habitat_sim.SensorType.DEPTH
depth_sensor_1st_person_spec.hfov = mn.Deg(HFOV)
depth_sensor_1st_person_spec.resolution = [
settings["height"],
settings["width"],
]
depth_sensor_1st_person_spec.position = [
0.0, settings["sensor_height"], 0.0
]
depth_sensor_1st_person_spec.orientation = [
settings["sensor_pitch"],
0.0,
0.0,
]
depth_sensor_1st_person_spec.sensor_subtype = habitat_sim.SensorSubType.PINHOLE
sensor_specs.append(depth_sensor_1st_person_spec)
if settings["semantic_sensor_1st_person"]:
semantic_sensor_1st_person_spec = habitat_sim.CameraSensorSpec()
semantic_sensor_1st_person_spec.uuid = "semantic_sensor_1st_person"
semantic_sensor_1st_person_spec.sensor_type = habitat_sim.SensorType.SEMANTIC
semantic_sensor_1st_person_spec.hfov = mn.Deg(HFOV)
semantic_sensor_1st_person_spec.resolution = [
settings["height"],
settings["width"],
]
semantic_sensor_1st_person_spec.position = [
0.0,
settings["sensor_height"],
0.0,
]
semantic_sensor_1st_person_spec.orientation = [
settings["sensor_pitch"],
0.0,
0.0,
]
semantic_sensor_1st_person_spec.sensor_subtype = (
habitat_sim.SensorSubType.PINHOLE)
sensor_specs.append(semantic_sensor_1st_person_spec)
# Here you can specify the amount of displacement in a forward action and the turn angle
agent_cfg = habitat_sim.agent.AgentConfiguration()
agent_cfg.sensor_specifications = sensor_specs
return habitat_sim.Configuration(sim_cfg, [agent_cfg])
def make_default_settings(args, scene=None):
scene = args.scene if scene is None else scene
settings = {
"width": args.width, # Spatial resolution of the observations
"height": args.height,
"scene": scene, # Scene path
"default_agent": 0,
"sensor_height": 0.0, # Height of sensors in meters
"sensor_pitch": 0, # sensor pitch (x rotation in rads)
"color_sensor_1st_person": True, # RGB sensor
"color_sensor_3rd_person": False, # RGB sensor 3rd person
"depth_sensor_1st_person": False, # Depth sensor
"semantic_sensor_1st_person": True, # Semantic sensor
"seed": 1,
"enable_physics": True, # enable dynamics simulation
"gpu_device_id": args.process_id % args.num_gpu
}
return settings
def simulate(sim, dt=1.0, get_frames=True):
# simulate dt seconds at 60Hz to the nearest fixed timestep
print("Simulating " + str(dt) + " world seconds.")
observations = []
start_time = sim.get_world_time()
while sim.get_world_time() < start_time + dt:
sim.step_physics(1.0 / 60.0)
if get_frames:
observations.append(sim.get_sensor_observations())
print('finish simulation')
return observations
def rotationMatrixToEulerAngles(R):
sy = math.sqrt(R[0, 0] * R[0, 0] + R[1, 0] * R[1, 0])
singular = sy < 1e-6
if not singular:
x = math.atan2(R[2, 1], R[2, 2])
y = math.atan2(-R[2, 0], sy)
z = math.atan2(R[1, 0], R[0, 0])
else:
x = math.atan2(-R[1, 2], R[1, 1])
y = math.atan2(-R[2, 0], sy)
z = 0
return np.array([x, y, z])