Search code examples
pythonmp4reinforcement-learning

Convert render to small video in Reinforcement learning


Below is the sample code for simulation of atari games:

import numpy as np
import matplotlib.pyplot as plt
import gym
import time
env =gym.make('BreakoutNoFrameskip-v4')
print("Observation Space :",env.observation_space)
print("Action Space :",env.action_space)
env.reset()
for i in range(1000):
    action =env.action_space.sample()
    obs,reward,done,info =env.step(action)
    env.render()
    time.sleep(0.01)
    if done :
        env.reset()
env.close()
plt.show()

The question:

  • Is it possible to create a simple video from the render? So my question is whether it is possible to convert render to mp4 format?

Solution

  • This saves a video of every video_every'th episode to the folder "video" while maintaining rendering to screen functionality from your post.

    import gym
    import time
    env = gym.make('BreakoutNoFrameskip-v4')
    
    video_every = 1
    env = gym.wrappers.Monitor(env, "./video", video_callable=lambda episode_id: (episode_id%video_every)==0, force=True)
    
    print("Observation Space :",env.observation_space)
    print("Action Space :",env.action_space)
    env.reset()
    for i in range(1000):
        action =env.action_space.sample()
        obs,reward,done,info =env.step(action)
        env.render()
        time.sleep(0.01)
        if done :
            env.reset()
    env.close()