我正在尝试在 Python3 OpenCV 中将大小为 640x480 的 RAW YUYY422 帧转换为 RGB。我查看了 OpenCV Docs 和谷歌各种解决方案,但似乎无法让它工作。
我不断收到的错误是:
Before 614400 <class 'bytes'>
After 614400 <class 'numpy.ndarray'>
(614400,)
Traceback (most recent call last):
File "./capture.py", line 111, in <module>
cv2.cvtColor(dataBuffer, dataBuffer, cv2.COLOR_YUV2RGB_UYVY);
TypeError: only size-1 arrays can be converted to Python scalars
这是我要转换的代码:
buf = v4l2_buffer()
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
buf.memory = V4L2_MEMORY_MMAP
fcntl.ioctl(vd, VIDIOC_DQBUF, buf) # get image from the driver queue
mm = buffers[buf.index]
dataBuffer = mm.read()
print("Before",len(dataBuffer),type(dataBuffer))
dataBuffer = np.fromstring(dataBuffer, np.uint8)
print("After",len(dataBuffer),type(dataBuffer))
#dataBuffer = dataBuffer.reshape( 640, 480 )
print(dataBuffer.shape)
cv2.cvtColor(dataBuffer, dataBuffer, cv2.COLOR_YUV2RGB_UYVY);
dbusClient.run(dataBuffer)
mm.seek(0)
fcntl.ioctl(vd, VIDIOC_QBUF, buf) # requeue the buffer
我怎样才能完成这种转换?谢谢。
更新:根据评论:
for _ in range(50): # capture 50 frames
buf = v4l2_buffer()
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
buf.memory = V4L2_MEMORY_MMAP
fcntl.ioctl(vd, VIDIOC_DQBUF, buf) # get image from the driver queue
mm = buffers[buf.index]
dataBuffer = mm.read()
dataBuffer = np.fromstring(dataBuffer, dtype=np.uint8)
dataBuffer2 = cv2.cvtColor(dataBuffer, cv2.COLOR_BGR2YUV)
dbusClient.run(dataBuffer)
mm.seek(0)
fcntl.ioctl(vd, VIDIOC_QBUF, buf) # requeue the buffer
给出错误:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /io/opencv/modules/imgproc/src/color.cpp, line 11144
Traceback (most recent call last):
File "./capture.py", line 106, in <module>
dataBuffer2 = cv2.cvtColor(dataBuffer, cv2.COLOR_BGR2YUV)
cv2.error: /io/opencv/modules/imgproc/src/color.cpp:11144: error: (-215) scn == 3 || scn == 4 in function cvtColor