【问题标题】:I try to get 2 frames from picamera. Why it works very slowly?我尝试从 picamera 获取 2 帧。为什么它工作得非常慢?
【发布时间】:2018-02-02 12:51:59
【问题描述】:

picamera.PiCamera() 线程我尝试获取cls.frameCV2 = snap.array 用于在opencv 中处理,另一个cls.frame = stream.read() 用于网络流。两者都有效,但速度很慢。

有什么问题?如果只使用frameCV2,它的工作速度非常快,或者如果我只使用stream.read,它也可以。

这是我的代码 sn-p:

import time
import io
import threading
import cv2
import picamera
from picamera.array import PiRGBArray

class Camera(object):
    thread = None  # background thread that reads frames from camera
    frame = None  # current frame is stored here by background thread
    frameCV2 = None # same, but as a numpy array
    last_access = 0  # time of last client access to the camera

    def initialize(self):
        if Camera.thread is None:
            # start background frame thread
            Camera.thread = threading.Thread(target=self._thread)
            Camera.thread.start()

            # wait until frames start to be available
            while self.frame is None:
                time.sleep(0)

    def get_frame(self):
        Camera.last_access = time.time()
        self.initialize()
        return self.frame

    def get_frame_for_internal_proc(self): # store frame for cv2 я добавил
        Camera.last_access = time.time()
        self.initialize()
        return self.frameCV2

    @classmethod
    def _thread(cls):
        with picamera.PiCamera() as camera:
            # camera setup
            camera.resolution = (800, 600)
            camera.hflip = True
            camera.vflip = True

            # let camera warm up
            camera.start_preview()
            time.sleep(2)

            rawCapture = PiRGBArray(camera) # я добавил

            stream = io.BytesIO()
            for snap in camera.capture_continuous(rawCapture, 'bgr',
                                                 use_video_port=True):
                cls.frameCV2 = snap.array # frame for cv2

                # store frame for web
                camera.capture(stream, format='jpeg')
                stream.seek(0)
                cls.frame = stream.read()

                # reset stream for next frame
                stream.seek(0)
                stream.truncate()

                rawCapture.truncate(0)

                # if there hasn't been any clients asking for frames in
                # the last 10 seconds stop the thread
                if time.time() - cls.last_access > 10:
                    break
        cls.thread = None

【问题讨论】:

    标签: python python-3.x opencv raspberry-pi3 opencv3.0


    【解决方案1】:

    所以,我没有找到让它工作得更快的方法。低速的主要原因是在camera.capture_continuous圈内加倍捕获。

    无论如何,我使用 1 个线程解决了这个问题,用于生成 frameframeCV2,只是将第二个线程转换为第一个线程。我只是将self.frame = cv2.imencode('.jpg', self.frameCV2)[1].tobytes() 放在get_frame() 方法中,该方法将numpy 矩阵转换为格式,适合网络流媒体。

    现在效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2014-10-20
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      相关资源
      最近更新 更多