我正在遵循此topic尝试从相机获取缓冲区数据并将其另存为numpy数组。在主题之后,我重用了这段代码
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)
但是我收到此错误
文件“ main.py”,第137行,位于初始化中 图片= np.frombuffer(data,np.uint8) AttributeError:'panda3d.core.ConstPointerToArray_unsigned_char'对象没有属性' buffer '
有什么建议吗?
答案 0 :(得分:0)
通过将原始代码更改为以下代码来解决:
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)
在重塑numpy数组(因此从底部开始的第三行)后,无论出于何种原因,图像最终都会被翻转。当您将此片段作为接受密钥或其他内容运行时,您应该看到相机看到的相同图像。希望这可以帮助遇到相同问题的人。