Getting camera image directly

How do you get get the camera image directly into a numpy array rather than having a window displaying it?

You can use offscreen window mode to avoid having a window show up.

You can attach a texture to the window using base.win.addRenderTexture, and then you can use RTMCopyRam mode (which copies the result of the render to RAM every frame) or RTMTriggeredCopyRam (which will copy it every time after you call base.win.triggerCopy().)

Then, you can extract the RAM image of this texture using tex.getRamImage() or tex.getRamImageAs("RGBA") in order to obtain it as something you can then convert easily into a numpy array.

Let us know if you need specific help with any of the steps!

1 Like

Hey edb!

Could you please elaborate your answer a bit? May be with a code sample…

I have a similar problem as the topic creator: Currently I have a Panda3D-window which displays models that I load. And now I would like to get the window content at every frame/scene as a matrix of pixel values for further analysis.

Here is the code, that I use:

    self.texture = Texture()
    self.win.addRenderTexture(tex=self.texture,
                              mode=GraphicsOutput.RTM_copy_ram,
                              bitplane=GraphicsOutput.RTP_color)
    imgs = self.texture.getRamImageAs('RGBA')

which produces the following error:

:gobj(error): Couldn’t find an uncompressed RAM image!

If I use getRamImage() instead of getRamImageAs() the imgs is just zero.

Thanks in advance!

You first need to render a frame with the texture attached before data will appear in the texture. You can do that using base.win.graphicsEngine.renderFrame().

1 Like