Добрый день,
Я обучаю агента по усиленному обучению в Google Colab. Я хочу записать вывод моего агента, играющего в игру, на диск Google, чтобы я мог его скачать. Меня не интересует просто просмотр видео в Colab, я хочу написать его в mp4. Сложность в том, что для Lunar Lander Environment Space State - это не изображение игры, а вектор длины 8, описывающий состояние. В других средах я мог бы сделать видео, используя переменные состояния / next_state, так как они были изображениями пространства состояний.
Код ниже - то, что я использую. Это работает на моей машине, но не на Colab. импорт спортзал импорт numpy import 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 Воспроизведение видео в Google Colab Как визуализировать тренажерный зал OpenAI в Google Colab? https://medium.com/@kaleajit27/reinforcement-learning-on-google-colab-9cb2e1ef51e https://star -ai.github.io / Rendering-OpenAi-Gym-in-Colab Laboratory / https://www.reddit.com/r/MachineLearning/comments/a1bihm/p_how_to_render_openai_gym_in_google_colaboratory/
В некоторых из моих попыток мне удалось сохранитьвидео на мой диск, но его размер составляет 1 КБ, поэтому он не работает.
Спасибо