forked from caomw/icra2017-visual-navigation-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_agent.py
65 lines (52 loc) · 2.57 KB
/
bot_agent.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
print("at the tippity top")
import ai2thor.controller
import pprint
import cv2
import numpy as np
import subprocess
# TODO: account for rotation as well (like with floor lamp)
def get_goal_position(goal_object_position, reachable_positions):
"""
Find the reachable position closest to our goal object position
:param goal_object_position:
:param reachable_positions:
:return final_pos:
"""
final_pos = {}
for coord_str in ['x', 'y', 'z']: # TODO: don't actually have to break them up, can just use whole positions
# reset the min difference per coordinate
min_pos_diff = np.inf
for rp in reachable_positions:
pos_diff = abs(goal_object_position[coord_str] - rp[coord_str])
# if this difference is less than the min, update the min
if pos_diff < min_pos_diff:
# update the coordinate to with the closer coord
final_pos[coord_str] = rp[coord_str]
min_pos_diff = pos_diff
return final_pos
print("Before controller")
# subprocess.call(['startx'])
process = subprocess.Popen(['xdpyinfo'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = process.communicate()[0]
print('STDOUT:{}'.format(stdout))
# the floor plan names correspond to those in constants.py
controller = ai2thor.controller.Controller(scene='FloorPlan227', gridSize=0.25, width=1000, height=1000)
# controller.docker_enabled = True
# # TV position is {'x': -3.503, 'y': 1.506, 'z': 0.001}
goal_object = {"name": "TV", "position": {'x': -3.503, 'y': 1.506, 'z': 0.001}}
# # Floor lamp position is {'x': -0.369, 'y': 0.026053369, 'z': 4.992}, rotation: {'x': 0.0, 'y': 59.9998856, 'z': 0.0}
# goal_object_position = {"name": "FloorLamp", "position": {'x': -0.369, 'y': 0.026053369, 'z': 4.992}}
# get the positions that the agent could possibly reach
event = controller.step(action='GetReachablePositions')
reachable_positions = event.metadata['reachablePositions']
# get the reachable position closest to our goal object
final_pos = get_goal_position(goal_object['position'], reachable_positions)
event = controller.step(dict(action='Teleport', x=final_pos['x'], y=final_pos['y'], z=final_pos['z']))
# # Numpy Array - shape (width, height, channels), channels are in RGB order
# current_image = event.frame
# # Numpy Array in BGR order suitable for use with OpenCV
current_image = event.cv2image()
cv2.imwrite("data/FP227_goal_{}.png".format(goal_object['name']), current_image)
# current metadata dictionary that includes the state of the scene
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(event.metadata)