Saving camera buffer to numpy array

Hi all, I was following this topic to try to get the buffer data from a camera and save it as a numpy array. Following the topic I reused this code

base.graphicsEngine.renderFrame()
dr = base.camNode.getDisplayRegion(0)
tex = dr.getScreenshot()
data = tex.getRamImage()
image = np.frombuffer(data,np.uint8)
image.shape = (tex.getYSize(),tex.getXSize(),tex.getNumComponents())
print(image)

however I get this error

File “main.py”, line 137, in init
image = np.frombuffer(data,np.uint8)
AttributeError: ‘panda3d.core.ConstPointerToArray_unsigned_char’ object has no attribute ‘buffer

Any advice?

Solved it by changing the original code to the following:

    base.graphicsEngine.renderFrame()
    dr = base.camNode.getDisplayRegion(0)
    tex = dr.getScreenshot()
    data = tex.getRamImage()
    v = memoryview(data).tolist()
    img = np.array(v,dtype=np.uint8)
    img = img.reshape((tex.getYSize(),tex.getXSize(),4))
    img = img[::-1]
    cv2.imshow('img',img)
    cv2.waitKey(0)

The image ends up being flipped for whatever reason after reshaping the numpy array hence the 3rd line from the bottom. You should see an identical image of whatever your camera sees when you run this snippet as an accept key or something. Hope this helps someone having the same issues.

Now I just need to figure out how to do the same thing for the depth buffer in the perspective of the camera…

1 Like