-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhabitat_test.py
199 lines (174 loc) · 6.78 KB
/
habitat_test.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
"""
This script is used to test the habitat-sim library together with OneMap
"""
import time
# habitat
import habitat_sim
from habitat_sim.utils import common as utils
# numpy
import numpy as np
# rerun
import rerun as rr
import rerun.blueprint as rrb
from habitat_sim import ActionSpec, ActuationSpec
from numpy.lib.function_base import angle
# keyboard input
# from pynput import keyboard
# scipy
from scipy.spatial.transform import Rotation as R
# MON
from mapping import Navigator
from vision_models.clip_dense import ClipModel
from vision_models.yolo_world_detector import YOLOWorldDetector
# from onemap_utils import log_map_rerun
from planning import Planning, Controllers
from config import *
from mapping import rerun_logger
# Global variables
running = True
if __name__ == "__main__":
config = load_config().Conf
if type(config.controller) == HabitatControllerConf:
pass
else:
raise NotImplementedError("Spot controller not suited for habitat sim")
model = ClipModel("weights/clip.pth")
detector = YOLOWorldDetector(0.3)
mapper = Navigator(model, detector, config)
logger = rerun_logger.RerunLogger(mapper, False, "", debug=False) if config.log_rerun else None
mapper.set_query(["A Couch"])
hm3d_path = "datasets/scene_datasets/hm3d"
backend_cfg = habitat_sim.SimulatorConfiguration()
backend_cfg.scene_id = hm3d_path + "/val/00853-5cdEh9F2hJL/5cdEh9F2hJL.basis.glb"
backend_cfg.scene_dataset_config_file = hm3d_path + "/hm3d_annotated_basis.scene_dataset_config.json"
hfov = 90
rgb = habitat_sim.CameraSensorSpec()
rgb.uuid = "rgb"
rgb.hfov = hfov
rgb.position = np.array([0, 0.88, 0])
rgb.sensor_type = habitat_sim.SensorType.COLOR
res_x = 640
res_y = 640
rgb.resolution = [res_y, res_x]
depth = habitat_sim.CameraSensorSpec()
depth.uuid = "depth"
depth.hfov = hfov
depth.position = np.array([0, 0.88, 0])
depth.sensor_type = habitat_sim.SensorType.DEPTH
depth.resolution = [res_y, res_x]
hfov = np.deg2rad(hfov)
focal_length = (res_x / 2) / np.tan(hfov / 2)
principal_point_x = res_x / 2
principal_point_y = res_y / 2
K = np.array([
[focal_length, 0, principal_point_x],
[0, focal_length, principal_point_y],
[0, 0, 1]
])
agent_cfg = habitat_sim.agent.AgentConfiguration(action_space=dict(
move_forward=ActionSpec("move_forward", ActuationSpec(amount=0.25)),
turn_left=ActionSpec("turn_left", ActuationSpec(amount=5.0)),
turn_right=ActionSpec("turn_right", ActuationSpec(amount=5.0)),
))
agent_cfg.sensor_specifications = [rgb, depth]
sim_cfg = habitat_sim.Configuration(backend_cfg, [agent_cfg])
sim = habitat_sim.Simulator(sim_cfg)
objects = sim.semantic_scene.objects
categories = [ob.category.name() for ob in objects]
scene_categories = sim.semantic_scene.categories
scene_categories = [cat.name() for cat in scene_categories]
for cat in categories:
if cat not in scene_categories:
print("Object category not in scene categories:", cat)
for cat in scene_categories:
if cat not in categories:
print("Scene category not in object categories:", cat)
print(len(categories), len(scene_categories))
print("Unique categories:", len(set(categories)))
print(set(categories))
# Global flag to control the simulation loop
running = True
action_mapping = {
"w": "move_forward",
"a": "turn_left",
"d": "turn_right",
"q": "enter_query",
"o": "autonomous",
}
# Main simulation loop
initial_sequence = ["turn_left"] * 28 * 2 # + ["move_forward"]*10
# initial_sequence = ["turn_left"]*5 + ["move_forward"]*5
qs = ["A fridge", "A TV", "A toilet", "A Couch", "A bed"]
running = True
autonomous = True
controller = Controllers.HabitatController(sim, config.controller)
while running:
action = None
if len(initial_sequence):
action = initial_sequence[0]
initial_sequence.pop(0)
if not len(initial_sequence):
mapper.one_map.reset_checked_map()
elif autonomous:
action = None
# print("Goal pos: ", goal_pos)
state = sim.get_agent(0).get_state()
orientation = state.rotation
q0 = orientation.x
q1 = orientation.y
q2 = orientation.z
q3 = orientation.w
r = R.from_quat([q0, q1, q2, q3])
# r to euler
pitch, yaw, roll = r.as_euler("yxz")
# pitch is actually around z
# orientation is pitch!
yaw = pitch
current_pos = np.array([[-state.position[2]], [-state.position[0]], [state.position[1]]])
path = mapper.get_path()
# rr.log("map/path", rr.LineStrips2D(path))
if path and len(path) > 1:
path = Planning.simplify_path(np.array(path))
path = path.astype(np.float32)
for i in range(path.shape[0]):
path[i, :] = mapper.one_map.px_to_metric(path[i, 0], path[i, 1])
controller.control(current_pos, yaw, path)
observations = sim.get_sensor_observations()
if action and action != "enter_query":
observations = sim.step(action)
elif not autonomous:
continue
state = sim.get_agent(0).get_state()
pos = np.array(([[-state.position[2]], [-state.position[0]], [state.position[1]]]))
# print(pos)
mapper.set_camera_matrix(K)
orientation = state.rotation
q0 = orientation.x
q1 = orientation.y
q2 = orientation.z
q3 = orientation.w
r = R.from_quat([q0, q1, q2, q3])
# r to euler
pitch, yaw, roll = r.as_euler("yxz")
# pitch is actually around z
r = R.from_euler("xyz", [0, 0, pitch])
r = r.as_matrix()
transformation_matrix = np.hstack((r, pos))
transformation_matrix = np.vstack((transformation_matrix, np.array([0, 0, 0, 1])))
t = time.time()
obj_found = mapper.add_data(
observations["rgb"][:, :, :-1].transpose(2, 0, 1), observations["depth"].astype(np.float32),
transformation_matrix)
print("Time taken to add data: ", time.time() - t)
cam_x = pos[0, 0]
cam_y = pos[1, 0]
if logger:
rr.log("camera/rgb", rr.Image(observations["rgb"]))
rr.log("camera/depth", rr.Image((observations["depth"] - observations["depth"].min()) / (
observations["depth"].max() - observations["depth"].min())))
logger.log_map()
logger.log_pos(cam_x, cam_y)
if obj_found:
mapper.set_query([qs[0]])
qs.pop(0)
continue