简体   繁体   English

低 FPS 使用 OpenCv 和 PiCamera (python)

[英]Low FPS using OpenCv with PiCamera (python)

I am trying to interface my OpenCV program with my Raspberry Pi PiCamera.我正在尝试将我的 OpenCV 程序与我的 Raspberry Pi PiCamera 接口。 Every time I use OpenCV to capture video, it drastically drops the FPS.每次我使用 OpenCV 捕捉视频时,它都会大幅降低 FPS。 When I capture video using PiCamera's Library, everything is fine and smooth.当我使用 PiCamera 的库捕捉视频时,一切都很好而且很流畅。

  1. Why is this happening?为什么会这样?
  2. Is there a way to fix it?有没有办法解决它?

This is my code:这是我的代码:

import time
import RPi.GPIO as GPIO
from PCA9685 import PCA9685
import numpy as np
import cv2

try:


    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FPS, 90)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 700)

    while(True):
        ret, frame = cap.read()

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

# When everything is done, release the capture


except:
    pwm.exit_PCA9685()
    print ("\nProgram end")
    exit()
cap.release()
cv2.destroyAllWindows()

This is the error I'm getting:这是我得到的错误:

在此处输入图像描述

  1. First of all, those are warnings not errors.首先,这些是警告而不是错误。

  2. Reduce the video dimension.减少视频维度。 Specify the dimension.指定尺寸。

  3. cv2.VideoCapture has some problems as it buffers the frames, and the frames are queued so if you're doing some processing and the speed is less than the bandwidth of VideoCapture the video will be slowed down. cv2.VideoCapture有一些问题,因为它缓冲帧,并且帧排队,所以如果你正在做一些处理并且速度小于VideoCapture的带宽,视频将会变慢。

So, here is a bufferless VideoCapture .所以,这是一个无缓冲的VideoCapture

video_capture_Q_buf.py video_capture_Q_buf.py

import cv2, queue as Queue, threading, time


is_frame = True
# bufferless VideoCapture


class VideoCaptureQ:

    def __init__(self, name):
        self.cap = cv2.VideoCapture(name)
        self.q = Queue.Queue()
        t = threading.Thread(target=self._reader)
        t.daemon = True
        t.start()

    # read frames as soon as they are available, keeping only most recent one
    def _reader(self):
        while True:
            ret, frame = self.cap.read()
            if not ret:
                global is_frame
                is_frame = False
                break
            if not self.q.empty():
                try:
                    self.q.get_nowait()   # discard previous (unprocessed) frame
                except Queue.Empty:
                    pass
            self.q.put(frame)

    def read(self):
        return self.q.get()

Using it:使用它:

test.py测试.py

import video_capture_Q_buf as vid_cap_q # import as alias
from video_capture_Q_buf import VideoCaptureQ # class import
import time

cap = VideoCaptureQ(vid_path)

while True:

    t1 = time.time()

    if vid_cap_q.is_frame == False:
        print('no more frames left')
        break

    try:
        ori_frame = cap.read()
        # do your stuff
    except Exception as e:
        print(e)
        break
    t2 = time.time()
    print(f'FPS: {1/(t2-t1)}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM