|
| 1 | +import gym, roboschool, sys, os |
| 2 | +import numpy as np |
| 3 | +import pyglet, pyglet.window as pw, pyglet.window.key as pwk |
| 4 | +from pyglet import gl |
| 5 | +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' |
| 6 | +import tensorflow as tf |
| 7 | + |
| 8 | +# |
| 9 | +# This opens a third-party window (not test window), shows rendered chase camera, allows to control humanoid |
| 10 | +# using keyboard (in a different way) |
| 11 | +# |
| 12 | + |
| 13 | +class PygletInteractiveWindow(pw.Window): |
| 14 | + def __init__(self, env): |
| 15 | + pw.Window.__init__(self, width=600, height=400, vsync=False, resizable=True) |
| 16 | + self.theta = 0 |
| 17 | + self.still_open = True |
| 18 | + |
| 19 | + @self.event |
| 20 | + def on_close(): |
| 21 | + self.still_open = False |
| 22 | + |
| 23 | + @self.event |
| 24 | + def on_resize(width, height): |
| 25 | + self.win_w = width |
| 26 | + self.win_h = height |
| 27 | + |
| 28 | + self.keys = {} |
| 29 | + self.human_pause = False |
| 30 | + self.human_done = False |
| 31 | + |
| 32 | + def imshow(self, arr): |
| 33 | + H, W, C = arr.shape |
| 34 | + assert C==3 |
| 35 | + image = pyglet.image.ImageData(W, H, 'RGB', arr.tobytes(), pitch=W*-3) |
| 36 | + self.clear() |
| 37 | + self.switch_to() |
| 38 | + self.dispatch_events() |
| 39 | + texture = image.get_texture() |
| 40 | + gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) |
| 41 | + texture.width = W |
| 42 | + texture.height = H |
| 43 | + texture.blit(0, 0, width=self.win_w, height=self.win_h) |
| 44 | + self.flip() |
| 45 | + |
| 46 | + def on_key_press(self, key, modifiers): |
| 47 | + self.keys[key] = +1 |
| 48 | + if key==pwk.ESCAPE: self.still_open = False |
| 49 | + |
| 50 | + def on_key_release(self, key, modifiers): |
| 51 | + self.keys[key] = 0 |
| 52 | + |
| 53 | + def each_frame(self): |
| 54 | + self.theta += 0.05 * (self.keys.get(pwk.LEFT, 0) - self.keys.get(pwk.RIGHT, 0)) |
| 55 | + |
| 56 | +def demo_run(): |
| 57 | + env = gym.make("RoboschoolHumanoidFlagrun-v1") |
| 58 | + |
| 59 | + config = tf.ConfigProto( |
| 60 | + inter_op_parallelism_threads=1, |
| 61 | + intra_op_parallelism_threads=1, |
| 62 | + device_count = { "GPU": 0 } ) |
| 63 | + sess = tf.InteractiveSession(config=config) |
| 64 | + |
| 65 | + from RoboschoolHumanoidFlagrunHarder_v1_2017jul import ZooPolicyTensorflow |
| 66 | + pi = ZooPolicyTensorflow("humanoid1", env.observation_space, env.action_space) |
| 67 | + |
| 68 | + control_me = PygletInteractiveWindow(env.unwrapped) |
| 69 | + |
| 70 | + env.reset() |
| 71 | + eu = env.unwrapped |
| 72 | + |
| 73 | + obs = env.reset() |
| 74 | + |
| 75 | + while 1: |
| 76 | + a = pi.act(obs, env) |
| 77 | + |
| 78 | + x, y, z = eu.body_xyz |
| 79 | + eu.walk_target_x = x + 1.1*np.cos(control_me.theta) # 1.0 or less will trigger flag reposition by env itself |
| 80 | + eu.walk_target_y = y + 1.1*np.sin(control_me.theta) |
| 81 | + eu.flag = eu.scene.cpp_world.debug_sphere(eu.walk_target_x, eu.walk_target_y, 0.2, 0.2, 0xFF8080) |
| 82 | + eu.flag_timeout = 100500 |
| 83 | + |
| 84 | + obs, r, done, _ = env.step(a) |
| 85 | + img = env.render("rgb_array") |
| 86 | + control_me.imshow(img) |
| 87 | + control_me.each_frame() |
| 88 | + if control_me.still_open==False: break |
| 89 | + |
| 90 | +if __name__=="__main__": |
| 91 | + demo_run() |
0 commit comments