如何将picamera视频帧转换为openCV对象

时间:2017-02-01 16:33:11

标签: 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
        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帧? 我还不熟悉图像处理...... 感谢您提前提供任何帮助!

1 个答案:

答案 0 :(得分:2)

请参阅PiRGBAnalysis类的文档。