-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_sim.py
152 lines (125 loc) · 5.74 KB
/
run_sim.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
"""
Original code:
https://github.com/facebookresearch/habitat-sim/blob/main/examples/tutorials/colabs/ECCV_2020_Navigation.ipynb
The original code is released under the MIT license.
Modified by KC-ML2.
"""
import argparse
import json
import os
import random
import cv2
from habitat.utils.visualizations import maps
import numpy as np
from global_localization.sim import HabitatSimWithMap
from utils.config_import import load_config_module
from utils.habitat_utils import (
display_map,
display_opencv_cam,
init_map_display,
init_opencv_cam,
make_output_path,
open_env_related_files,
save_observation,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", default="config/concat_fourview_69FOV_HD.py")
parser.add_argument("--scene-list-file", default="./data/scene_list_test.txt")
parser.add_argument("--scene-index", type=int)
parser.add_argument("--map-height-json", default="./data/map_height.json")
parser.add_argument("--output-path", default="./output")
parser.add_argument("--save-all", action="store_true")
parser.add_argument("--save-except-rotation", action="store_true")
args, _ = parser.parse_known_args()
module_name = args.config
scene_list_file = args.scene_list_file
scene_index = args.scene_index
height_json_path = args.map_height_json
output_path = args.output_path
is_save_all = args.save_all
is_save_except_rotation = args.save_except_rotation
check_arg = is_save_all + is_save_except_rotation
if check_arg >= 2:
raise ValueError("Argument Error. Put only one flag.")
config = load_config_module(module_name)
image_dir = config.PathConfig.LOCALIZATION_TEST_PATH
scene_list, height_data = open_env_related_files(scene_list_file, height_json_path, scene_index)
for scene_number in scene_list:
sim = HabitatSimWithMap(scene_number, config, height_data)
for level, recolored_topdown_map in enumerate(sim.recolored_topdown_map_list):
print("scene: ", scene_number, " level: ", level)
image_dir_by_scene, pos_record_json = make_output_path(
output_path, scene_number, config.PathConfig.POS_RECORD_FILE_PREFIX
)
img_id = 0
pos_record = {}
pos_record.update({"scene_number": scene_number})
# Sample initial agent position from topdown map grey area
# This is for fixing height of agent position
binary_topdown_map = sim.topdown_map_list[level]
explorable_area_index = list(zip(*np.where(binary_topdown_map == 1)))
grid_pos = random.sample(explorable_area_index, 1)[0]
sim.set_state_from_grid(grid_pos, level)
# Initialize opencv display window
init_map_display()
init_opencv_cam()
while True:
# Get current position & set it to unified height
current_state = sim.agent.get_state()
current_state.position[1] = sim.height_list[level]
position = current_state.position
sim.agent.set_state(current_state)
# Get camera observation
observations = sim.get_cam_observations()
color_img = observations["all_view"]
key = display_opencv_cam(color_img)
# Update map data
previous_level = sim.closest_level
sim.update_closest_map(position)
map_image = cv2.cvtColor(sim.recolored_topdown_map, cv2.COLOR_GRAY2BGR)
# If level is changed, re-initialize localization instance
current_level = sim.closest_level
node_point = maps.to_grid(position[2], position[0], sim.recolored_topdown_map.shape[0:2], sim)
display_map(map_image, key_points=[node_point])
# Set action according to key input
if key == ord("w"):
action = "move_forward"
if key == ord("s"):
action = "move_backward"
if key == ord("a"):
action = "turn_left"
if key == ord("d"):
action = "turn_right"
if key == ord("n"):
break
if key == ord("q"):
break
# Save observation & position record according to the flag
# Save observation every step
if is_save_all:
save_observation(color_img, image_dir_by_scene, img_id, pos_record, position, node_point)
img_id = img_id + 1
# Save observation only when forward & backward movement
if is_save_except_rotation:
if key == ord("w") or key == ord("s"):
save_observation(color_img, image_dir_by_scene, img_id, pos_record, position, node_point)
img_id = img_id + 1
# Save observation when "o" key input
if key == ord("o"):
if is_save_all or is_save_except_rotation:
pass
else:
save_observation(color_img, image_dir_by_scene, img_id, pos_record, position, node_point)
img_id = img_id + 1
continue
sim.step(action)
file_saved = os.listdir(image_dir_by_scene)
if file_saved:
with open(pos_record_json, "w") as record_json: # pylint: disable=unspecified-encoding
json.dump(pos_record, record_json, indent=4)
else:
os.rmdir(image_dir_by_scene)
sim.close()
if key == ord("q"):
break