forked from StevenLOL/icra2017-visual-navigation
-
Notifications
You must be signed in to change notification settings - Fork 12
/
keyboard_agent.py
executable file
·92 lines (72 loc) · 2.22 KB
/
keyboard_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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import signal
import argparse
import numpy as np
from scene_loader import THORDiscreteEnvironment
from utils.tools import SimpleImageViewer
#
# Navigate the scene using your keyboard
#
def key_press(key, mod):
global human_agent_action, human_wants_restart, stop_requested
if key == ord('R') or key == ord('r'): # r/R
human_wants_restart = True
if key == ord('Q') or key == ord('q'): # q/Q
stop_requested = True
if key == 0xFF52: # up
human_agent_action = 0
if key == 0xFF53: # right
human_agent_action = 1
if key == 0xFF51: # left
human_agent_action = 2
if key == 0xFF54: # down
human_agent_action = 3
def rollout(env):
global human_agent_action, human_wants_restart, stop_requested
human_agent_action = None
human_wants_restart = False
while True:
# waiting for keyboard input
if human_agent_action is not None:
# move actions
env.step(human_agent_action)
human_agent_action = None
# waiting for reset command
if human_wants_restart:
# reset agent to random location
env.reset()
human_wants_restart = False
# check collision
if env.collided:
print('Collision occurs.')
env.collided = False
# check quit command
if stop_requested: break
viewer.imshow(env.observation)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--scene_dump", type=str, default="./data/bedroom_04.h5",
help="path to a hdf5 scene dump file")
args = parser.parse_args()
print("Loading scene dump {}".format(args.scene_dump))
env = THORDiscreteEnvironment({
'h5_file_path': args.scene_dump
})
# manually disable terminal states
env.terminals = np.zeros_like(env.terminals)
env.terminal_states, = np.where(env.terminals)
env.reset()
human_agent_action = None
human_wants_restart = False
stop_requested = False
viewer = SimpleImageViewer()
viewer.imshow(env.observation)
viewer.window.on_key_press = key_press
print("Use arrow keys to move the agent.")
print("Press R to reset agent\'s location.")
print("Press Q to quit.")
rollout(env)
print("Goodbye.")