Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot record the video by Wrappers.Monitor #1925

Closed
JingruiYu opened this issue May 21, 2020 · 17 comments
Closed

Cannot record the video by Wrappers.Monitor #1925

JingruiYu opened this issue May 21, 2020 · 17 comments

Comments

@JingruiYu
Copy link

I am trying to record the performance by the wrappers.Monitor. However, only .json files were generated, not any video files. No error was reported. How can I get the video file?

system
ubuntu 16, version 0.17.2,

code
env = gym.make('CartPole-v0') env = gym.wrappers.Monitor(env, "recording",force=True)

Thank you very much!

@christopherhesse
Copy link
Contributor

I ran the following script and it worked fine, are you actually stepping the environment?

import gym
env = gym.make('CartPole-v0')
env = gym.wrappers.Monitor(env, "recording",force=True)
env.reset()
while True:
    obs, rew, done, info = env.step(env.action_space.sample())
    if done:
        break

@JianmingTONG
Copy link

Hi, I do get the video under "recording" directory. However, this video cannot be played successfully. Is there any solution to it?
I am using:
Ubuntu 18.04
gym 0.18.0

openaigym.video.0.28612.video000000.mp4

@cxk1998
Copy link

cxk1998 commented Jan 2, 2021

I have the same problem. I get a video but it's only 1kb.

@pavlexander
Copy link

pavlexander commented Jan 2, 2021

same issue... been trying to investigate it with no success...

First enable logging:

gym.logger.set_level(gym.logger.DEBUG)

Logs:

D:\dev\Python_global_vEnv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.3\plugins\python-ce\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 49874 --file D:/dev/PycharmProjects/LearningPy/all/gym_testing.py
pydev debugger: process 13860 is connecting

Connected to pydev debugger (build 202.7660.27)
INFO: Making new env: CartPole-v0
INFO: Clearing 4 monitor files from previous run (because force=True was provided)
INFO: Starting new video recorder writing to D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
DEBUG: Starting ffmpeg with "ffmpeg -nostats -loglevel error -y -f rawvideo -s:v 600x400 -pix_fmt rgb24 -framerate 50 -i - -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -vcodec libx264 -pix_fmt yuv420p -r 50 D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4"
[-0.04456399 0.04653909 0.01326909 -0.02099827]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[-0.04363321 0.24146826 0.01284913 -0.30946528]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[-0.03880385 0.04616562 0.00665982 -0.01275795]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[-0.03788053 0.24119143 0.00640466 -0.30333221]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[-3.30567063e-02 4.36221519e-01 3.38020378e-04 -5.93988387e-01]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[-0.02433228 0.63133874 -0.01154175 -0.88656482]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[-0.0117055 0.43637535 -0.02927304 -0.59753241]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[-0.00297799 0.63189443 -0.04122369 -0.89929043]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.00965989 0.43735467 -0.0592095 -0.61984505]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.01840699 0.24310748 -0.0716064 -0.34638227]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.02326914 0.04907326 -0.07853405 -0.0771111 ]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.0242506 0.24522808 -0.08007627 -0.39350173]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.02915516 0.44138962 -0.0879463 -0.71031843]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.03798296 0.63761235 -0.10215267 -1.0293379 ]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.0507352 0.44398717 -0.12273943 -0.77039486]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.05961495 0.25074891 -0.13814733 -0.51871293]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.06462993 0.4475178 -0.14852159 -0.85154054]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.07358028 0.64431858 -0.1655524 -1.18699681]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.08646665 0.45168402 -0.18929233 -0.95044605]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
[ 0.09550033 0.64877987 -0.20830125 -1.29612913]
DEBUG: Capturing video frame: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
Episode finished after 20 timesteps
DEBUG: Closing video encoder: path=D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.video.0.13860.video000000.mp4
DEBUG: Writing training manifest file to D:\dev\PycharmProjects\LearningPy\all\vid\openaigym.manifest.0.13860.manifest.json
INFO: Finished writing results. You can upload them to the scoreboard via gym.upload('D:\dev\PycharmProjects\LearningPy\all\vid')

Process finished with exit code -1

So 20 timesteps were supposed to be written. In the destination folder we have a bunch of files. The video file is only 1kb and is unplayable.

image

The code used:

env = gym.make('CartPole-v0')
env = gym.wrappers.Monitor(env, "./vid", force=True)
env.seed(0)
for i_episode in range(1):
    observation = env.reset()
    for t in range(100):
        env.render()
        print(observation)
        action = env.action_space.sample()  # take a random action
        observation, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break
env.close()

I have been able to successfully use the ffmpeg python wrapper.. so this must not be an issue. The ffmpeg is in PATH and is locatable by the python virtual environment..

try:
    probe = ffmpeg.probe('some non gym video path here')
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    width = int(video_stream['width'])
    height = int(video_stream['height'])

    print(width)
    print(height)
except ffmpeg.Error as e:
    if e.stdout:
        print(e.stdout.decode('utf8'))
    if e.stderr:
        print(e.stderr.decode('utf8'))

exit(0)

works OK. So the issue must be somewhere in gym wrapper or the arguments it is using to encode the video..

  • win 10
  • Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
  • ffmpeg-4.3.1-2021-01-01-full_build
  • gym 0.18.0

@pavlexander
Copy link

pavlexander commented Jan 2, 2021

Actually nevermind.. I found the issue. It's a bug in the code.

To fix the issue temporary (until devs fix it in public repo) you have to edit the video_recorder.py and remove some tabs:

image

The condition to write frames to the video file is never executed in the current gym library version.
Anyone reading this topic feel free to create a pull request. Seems like the question has been stale for almost a year so devs might not be supporting the gym anymore.. However, you can try :)

mattmolinare added a commit to mattmolinare/gym that referenced this issue Jan 8, 2021
@i-am-neet
Copy link

Actually nevermind.. I found the issue. It's a bug in the code.

To fix the issue temporary (until devs fix it in public repo) you have to edit the video_recorder.py and remove some tabs:

image

The condition to write frames to the video file is never executed in the current gym library version.
Anyone reading this topic feel free to create a pull request. Seems like the question has been stale for almost a year so devs might not be supporting the gym anymore.. However, you can try :)

Hi, I tried to remove one indent by following your suggestion, as figure as below.
Screenshot from 2021-01-19 13-22-00

The size of video file seems normally.
But I still can not play the video with vlc player :((

Any suggestion?

@i-am-neet
Copy link

Actually nevermind.. I found the issue. It's a bug in the code.
To fix the issue temporary (until devs fix it in public repo) you have to edit the video_recorder.py and remove some tabs:
image
The condition to write frames to the video file is never executed in the current gym library version.
Anyone reading this topic feel free to create a pull request. Seems like the question has been stale for almost a year so devs might not be supporting the gym anymore.. However, you can try :)

Hi, I tried to remove one indent by following your suggestion, as figure as below.
Screenshot from 2021-01-19 13-22-00

The size of video file seems normally.
But I still can not play the video with vlc player :((

Any suggestion?

I notice that remove indent in function capture_frame is right track.
But need to use video_recorder directly instead of Monitor

import gym
from gym.wrappers.monitoring import video_recorder
env = gym.make('CartPole-v0')
vid = video_recorder.VideoRecorder(env,path="./recording/vid.mp4")
for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        vid.capture_frame()
        print(observation)
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break
env.close()

Any suggestion is welcome :)

@mwilbz
Copy link

mwilbz commented Mar 14, 2021

FWIW downgrading to gym==0.17.3 was a workaround for me. Thanks @pavlexander !

@vakker
Copy link
Contributor

vakker commented Mar 17, 2021

This was fixed and merged into the master branch in #2139

@rcshubhadeep
Copy link

Is the latest fix available in pip? I tried with 0.18 and I could not make it work so I downgraded to 0.17.3 as mentioned in @mwilbz comment and it worked.

@aterenin
Copy link

aterenin commented Apr 2, 2021

Confirmed same issue on latest version in pip. Manually removing the indent fixes the issue.

@aterenin
Copy link

aterenin commented Apr 2, 2021

If anyone else is using VideoRecorder as part of a larger logging framework, the following code patches capture_frame at runtime.

frame = env.render("rgb_array")
self.video_recorder.encoder = gym.wrappers.monitoring.video_recorder.ImageEncoder(self.video_recorder.path, frame.shape, self.video_recorder.frames_per_sec, self.video_recorder.output_frames_per_sec)
def capture_frame(self, frame):
    if not isinstance(frame, (np.ndarray, np.generic)):
        raise gym.error.InvalidFrame('Wrong type {} for {} (must be np.ndarray or np.generic)'.format(type(frame), frame))
    if frame.shape != self.frame_shape:
        raise gym.error.InvalidFrame("Your frame has shape {}, but the VideoRecorder is configured for shape {}.".format(frame.shape, self.frame_shape))
    if frame.dtype != np.uint8:
        raise gym.error.InvalidFrame("Your frame has data type {}, but we require uint8 (i.e. RGB values from 0-255).".format(frame.dtype))
    self.proc.stdin.write(frame.tobytes())
self.video_recorder.encoder.capture_frame = types.MethodType(capture_frame, self.video_recorder.encoder)

Looks like this is already fixed in an above-linked PR, so hopefully won't be long before this dirty hack is removed.

@UntotaufUrlaub
Copy link

Is the fix allready available by pip?
(editing the file manually worked for me)

@shapeng1998
Copy link

Is the fix allready available by pip?
(editing the file manually worked for me)

I have the same problem, the source code works, but the package on pypi seems have not updated yet.
QQ20210512-110051@2x

@bhatiaabhinav
Copy link

I just install gym 0.18.3 via pip. The issue is resolved.

@jkterry1
Copy link
Collaborator

jkterry1 commented Aug 6, 2021

Fix is in master

@jkterry1 jkterry1 closed this as completed Aug 6, 2021
@binakn
Copy link

binakn commented Jun 21, 2023

I just had a similar issue today: for me .json files were generated, but videos were not created / saved.
In my case this was caused by the metadata of the environment, because render.modes was empty after initialization. This turned of the video recorder upon initialization of the VideoRecorder object and hence didn't capture any video.

Setting them manually, such that it would include rgb_array fixed my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests