-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_recorder.py
43 lines (39 loc) · 1.49 KB
/
text_recorder.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
# Using string_recorder https://github.com/kiyukuta/string_recorder.git for text-based environments
import subprocess
import gym
from gym.wrappers.monitoring.video_recorder import VideoRecorder
import string_recorder
def record(env, policy, video_dir):
rec = string_recorder.StringRecorder()
timestep_limit = 30
video_recorder = VideoRecorder(env, video_dir, enabled=True)
is_done = False
state = env.reset()
while not is_done: # Loop until game ends
env.render()
# subprocess.call('clear', shell=False)
video_recorder.capture_frame()
action = policy[state]
state, reward, is_done, info = env.step(action)
video_recorder.close()
video_recorder.enabled = False
env.close()
rec.make_gif_from_gym_record(video_dir)
if __name__ == "__main__":
env = gym.make("Taxi-v3")
rec = string_recorder.StringRecorder()
timestep_limit = 30
video_dir = "./video/taxi2.json"
video_recorder = VideoRecorder(env, video_dir, enabled=True)
is_done = False
state = env.reset()
while not is_done: # Loop until game ends
env.render()
# subprocess.call('clear', shell=False)
video_recorder.capture_frame()
action = env.action_space.sample()
state, reward, is_done, info = env.step(action)
video_recorder.close()
video_recorder.enabled = False
env.close()
rec.make_gif_from_gym_record(video_dir)