Skip to content

Instantly share code, notes, and snippets.

@jaume-ferrarons
Last active April 20, 2020 09:24
Show Gist options
  • Save jaume-ferrarons/0a2bde9f9a2468657aebf358e99c5ceb to your computer and use it in GitHub Desktop.
Save jaume-ferrarons/0a2bde9f9a2468657aebf358e99c5ceb to your computer and use it in GitHub Desktop.
colab-gym-recording
import gym
from gym import logger as gymlogger
from gym.wrappers import Monitor
import glob
import io
import base64
from IPython.display import HTML, clear_output
from IPython import display as ipythondisplay
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1400, 900))
display.start()
# Adapted from: https://colab.research.google.com/drive/1flu31ulJlgiRL1dnN2ir8wGh9p7Zij2t
"""
Utility functions to enable video recording of gym environment and displaying it
To enable video, just do "env = wrap_env(env)""
"""
def show_video():
mp4list = glob.glob('video/*.mp4')
if len(mp4list) > 0:
mp4 = mp4list[0]
video = io.open(mp4, 'r+b').read()
encoded = base64.b64encode(video)
ipythondisplay.display(HTML(data='''<video alt="test"
controls style="height: 400px;">
<source src="data:video/mp4;base64,{0}" type="video/mp4" />
</video>'''.format(encoded.decode('ascii'))))
else:
print("Could not find video")
def video_wrap_env(env):
env = Monitor(env, './video', force=True)
return env
pip install gym pyvirtualdisplay box2d box2d-kengz > /dev/null 2>&1
apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment