Python 如何将picamera视频帧转换为openCV对象
我正在尝试用python raspberry摄像头模块和 然后将每个帧转换为openCV帧,但未成功:Python 如何将picamera视频帧转换为openCV对象,python,opencv,numpy,camera,raspberry-pi,Python,Opencv,Numpy,Camera,Raspberry Pi,我正在尝试用python raspberry摄像头模块和 然后将每个帧转换为openCV帧,但未成功: import time import picamera import cv2 import numpy as np class BroadcastOutput(object): def __init__(self, camera): return def write(self, b): #create numpy array from b
import time
import picamera
import cv2
import numpy as np
class BroadcastOutput(object):
def __init__(self, camera):
return
def write(self, b):
#create numpy array from b
data = np.fromstring(b, dtype=np.uint8)
#doesn't work with reshape either
#data = np.fromstring(b, dtype=np.uint8).reshape(320, 280, 3)
#enconde as image
image = cv2.imdecode(data, 1)
#test if is valid cv2 object -> fails
cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
def flush(self):
print('Waiting for background conversion process to exit')
#camera setup and start
with picamera.PiCamera() as camera:
camera.resolution = (320, 280)
camera.framerate = 24
time.sleep(2) # camera warm-up time
print('Initializing broadcast thread')
output = BroadcastOutput(camera)
print('Starting recording')
camera.start_recording(output, 'bgr')
try:
while True:
camera.wait_recording(1)
except KeyboardInterrupt:
pass
finally:
print('Stopping recording')
camera.stop_recording()
当我打印我的numpy数组时,它有内容,但是解码后的图像对象总是没有
所以我的问题是:如何正确使用b中提供的数据作为cv2帧?
我对图像处理还是新手。。。
提前谢谢你的帮助 请参阅该类的文档。请参阅该类的文档。从
b
中获取数据和图像
有什么意义,然后忽略它们并在cvolor
中使用b
?实际上,imdecode
似乎有点毫无意义,因为考虑到将数据重塑为3通道矩阵。整个片段看起来很奇怪…你完全正确。。。漫长的一天;)我试图澄清我的截图,并提供了一个完整且可用的示例当调用write
时b
的长度是多少?我不确定是否保证每帧都有一个write
。库中提供的方便包装器似乎在flush
中进行转换如果你看一下,假设你有完整的框架,那么np.fromstring
方法似乎是正确的。你是对的!写入回调没有收到完整帧@fireant为PirgAnalysis类提供了正确的提示,当您忽略它们并在cvtColor
中使用b
时,从b
获取数据和图像有什么意义?实际上,imdecode
似乎有点毫无意义,因为考虑到将数据重塑为3通道矩阵。整个片段看起来很奇怪…你完全正确。。。漫长的一天;)我试图澄清我的截图,并提供了一个完整且可用的示例当调用write
时b
的长度是多少?我不确定是否保证每帧都有一个write
。库中提供的方便包装器似乎在flush
中进行转换如果你看一下,假设你有完整的框架,那么np.fromstring
方法似乎是正确的。你是对的!写入回调没有收到完整帧@fireant为PirgAnalysis类提供了正确的提示,正如前面提到的,写回调没有收到完整的帧,但是PirgAnalysis是!谢谢你的提示,现在一切都正常了。请参阅,以获取classIt's great的工作示例。您发现了一个工作示例代码,并在此处共享了链接。如前所述,写入回调未接收完整帧,但PirgAnalysis正在接收!谢谢你的提示,现在一切都正常了。请参阅,以获取classIt的伟大的工作示例。您发现了一个工作示例代码,并在此处共享了链接。