如何录制和保存健身房环境视频

问题描述 投票:0回答:2

我已经训练了一个 DQN 代理,我想使用gym.wrappers.RecordVideo 录制并保存单集的视频。到目前为止,这是我的代码:

from gym.wrappers import RecordVideo
env = gym.make("AlienDeterministic-v4", render_mode="human")
env = preprocess_env(env)  # method with some other wrappers
env = RecordVideo(env, 'video', episode_trigger=lambda x: x == 2)

for episode in range(4):
        state = env.reset()[0]
        done = False
        while not done:
            action = self.select_smart_action(state)
            next_state, reward, done, _, _ = env.step(action)
            state = next_state
            env.render()

    env.close()

到目前为止,我的代码创建了一个名为“video”的目录,但它没有创建或保存任何视频。我怎样才能做到这一点?

python reinforcement-learning openai-gym dqn
2个回答
1
投票

根据源代码,您可能需要在第一步之前调用

start_video_recorder()
方法。

总而言之:

from gym.wrappers import RecordVideo
env = gym.make("AlienDeterministic-v4", render_mode="human")
env = preprocess_env(env)  # method with some other wrappers
env = RecordVideo(env, 'video', episode_trigger=lambda x: x == 2)
env.start_video_recorder()

for episode in range(4):
        state = env.reset()[0]
        done = False
        while not done:
            action = self.select_smart_action(state)
            next_state, reward, done, _, _ = env.step(action)
            state = next_state
            env.render()

    env.close()

0
投票

我刚刚遇到了同样的问题,因为文档有点缺乏。

无论如何,您忘记将

render_mode
设置为
rgb_mode
停止录制。我用
###

标记了相关代码

import gymnasium as gym

###
# create a temporary variable with our env, which will use rgb_array as render mode. This mode is supported by the RecordVideo-Wrapper
tmp_env = gym.make("ALE/Pong-v5", render_mode="rgb_array")

# wrap the env in the record video
env = gym.wrappers.RecordVideo(env=tmp_env, video_folder="/home/viewviewview/WS2023/deeplearning", name_prefix="test-video", episode_trigger=lambda x: x % 2 == 0)

# env reset for a fresh start
observation, info = env.reset()

###
# Start the recorder
env.start_video_recorder()


# AI logic
for _ in range(1000):
    action = env.action_space.sample()  # agent policy that uses the observation and info
    observation, reward, terminated, truncated, info = env.step(action)
    env.render()

    if terminated or truncated:
        observation, info = env.reset()

####
# Don't forget to close the video recorder before the env!
env.close_video_recorder()

# Close the environment
env.close()

© www.soinside.com 2019 - 2024. All rights reserved.