Farama-Foundation / D4RL

A collection of reference environments for offline reinforcement learning
Apache License 2.0
1.35k stars 285 forks source link

[Bug Report] AttributeError: 'PenEnvV0' object has no attribute 'recording' #206

Closed mertalbaba closed 1 year ago

mertalbaba commented 1 year ago

If you are submitting a bug report, please fill in the following details and use the tag [bug].

Describe the bug For Adroit environments, I can't record videos using gym.wrappers.RecordVideo utility since it throws AttributeError: 'PenEnvV0' object has no attribute 'recording'

Code example

import gym
import d4rl
import model

env = gym.make('pen-human-v1')
env = gym.wrappers.RecordVideo(env, 'video')

s = env.reset()

for i in range(1000):
    a = model.select_action(s)
    s, r, _, _ = env.step(a)

env.close()

System Info Describe the characteristic of your environment:

Additional context Add any other context about the problem here.

Checklist

mertalbaba commented 1 year ago

Solved the problem, solution is using mujoco-py for video generation. I attached a minimal working code below for anyone experiencing a similar problem

import mujoco_py
import gym
import d4rl

env = gym.make('pen-human-v1')
s = env.reset()

frames = []
viewer = mujoco_py.MjRenderContextOffscreen(env.sim, device_id=-1)

for i in range(1000):
    viewer.render(width=640, height=480)
    frame = np.asarray(viewer.read_pixels(640, 480, depth=False)[::-1, :, :], dtype=np.uint8)
    frames.append(frame)

    a = model.select_action(s)
    s, r, _, _ = env.step(a)

env.close()
frames_np = np.array(frames)
imageio.mimwrite('video.mp4', frames_np, fps=30)