美好的一天,
我正在Google Colab上培训强化学习代理。我想将代理玩游戏的输出写到google drive上,以便我可以下载它。我不在乎仅在Colab中观看视频,而是想将其写入mp4。棘手的部分是,对于月球着陆器环境,状态空间不是游戏的图像表示,而是描述状态的长度为8的向量。在其他环境中,我可以使用state / next_state变量制作视频,因为它们是状态空间的图像。
以下代码是我正在使用的代码。它可以在我的机器上运行,但不能在Colab上运行。 进口体育馆 导入numpy 导入cv2
#------------------------------------------------------------------------------
def im2vid(images):
"""
Take a generator object and converts the images to a video.
"""
# Arguments
ext = 'png'
output = 'video.mp4'
fps = 10
# Determine the width and height from the first image
height, width, channels = (168, 168, 3)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case
out = cv2.VideoWriter(output, fourcc, fps, (width, height))
#write the video
for frame in images:
out.write(frame) # Write out frame to video
# Release everything if job is finished
out.release()
cv2.destroyAllWindows()
#------------------------------------------------------------------------------
def run_basic_agent(env):
"""
Run an agent.
This executes actions in the environemnt so that the agent can be recorded.
Yields series of frames of the recorded agent.
"""
max_steps = 100
obs = env.reset()
counter = 0
while True:
action = env.action_space.sample()
next_obs, reward, done, info = env.step(action)
yield env.render(mode = 'rgb_array')
if done or counter == max_steps:
break
obs = next_obs
counter += 1
env.close()
#------------------------------------------------------------------------------
env = gym.make('LunarLander-v2')
im2vid(run_basic_agent(env))
我尝试了各种方法,但是没有成功。我更喜欢使用cv2 writer的解决方案。
我尝试过:
https://dzone.com/articles/how-to-use-google-colaboratory-for-video-processin Playing videos on Google Colab How to render OpenAI gym in google Colab? https://medium.com/@kaleajit27/reinforcement-learning-on-google-colab-9cb2e1ef51e https://star-ai.github.io/Rendering-OpenAi-Gym-in-Colaboratory/ https://www.reddit.com/r/MachineLearning/comments/a1bihm/p_how_to_render_openai_gym_in_google_colaboratory/
在某些尝试中,我设法将视频保存到驱动器中,但是大小为1kb,因此无法正常工作。
谢谢