题意:OpenAI Gym:如何在 CartPole-v0 中获取像素?
问题背景:
I would like to access the raw pixels in the OpenAI gym CartPole-v0
environment without opening a render window. How do I do this?
我想在 OpenAI Gym 的 CartPole-v0 环境中访问原始像素,而不打开渲染窗口。我该如何实现?
Example code: 示例代码
import gym
env = gym.make("CartPole-v0")
env.reset()
img = env.render(mode='rgb_array', close=True) # Returns None
print(img)
img = env.render(mode='rgb_array', close=False)
# Opens annoying window, but gives me the array that I want
print(img.shape)
PS. I am having a hard time finding good documentation for OpenAI gym. Is it just me, or does it simply not exist?
附注:我很难找到 OpenAI Gym 的好文档,是我个人的问题,还是文档真的不存在?
Edit: I don't need to ever open the render video.
编辑:我完全不需要打开渲染视频。
问题解决:
I was curious about same so I started looking into the source code and this is what I found.
我对此也感到好奇,所以我开始查看源代码,以下是我发现的内容。
Open AI uses pyglet for displaying the window and animations.
OpenAI 使用 Pyglet 来显示窗口和动画。
For showing the animation everything is drawn on to window and then rendered.
为了显示动画,所有内容都会绘制到窗口上,然后进行渲染。
And then pyglet stores what is being displayed on to a buffer.
然后 Pyglet 将显示的内容存储到一个缓冲区中。
Dummy version of how code is written in open AI
在 OpenAI 中编写代码的简化版本
import pyglet
from pyglet.gl import *
import numpy as np
display = pyglet.canvas.get_display()
screen = display.get_screens()
config = screen[0].get_best_config()
pyglet.window.Window(width=500, height=500, display=display, config=config)
# draw what ever you want
#get image from the buffer
buffer = pyglet.image.get_buffer_manager().get_color_buffer()
image_data=buffer.get_image_data()
arr = np.frombuffer(image_data.get_data(),dtype=np.uint8)
print(arr)
print(arr.shape)
output: [0 0 0 ... 0 0 0]
(1000000,)
so basically every image we get is from buffer of what is being displayed on the window. So if we don't draw anything on window we get no image so that window is required to get the image. so you need to find a way such that windows is not displayed but its values are stored in buffer. I know its not what you wanted but I hope it might lead you to a solution.
所以基本上我们获取的每一张图像都是从显示在窗口上的内容的缓冲区中得到的。因此,如果我们不在窗口上绘制任何东西,就不会得到图像,所以要获取图像,窗口是必需的。因此,你需要找到一种方法,使窗口不显示但其值仍存储在缓冲区中。我知道这不是你想要的答案,但希望它能引导你找到解决方案。