簡體   English   中英

如何在 OpenCV-Python 中使用優化處理器性能?

[英]How to use optimize processors performance in OpenCV-Python?

我是 OpenCV-Python 多處理的初學者。 我使用的是 Raspberry Pi 2 B 型(四核 x 1GHz)。 我正在嘗試用一個非常簡單的例子來優化 FPS。

這是我的代碼:

網絡攝像頭.py

import cv2
from threading import Thread
from multiprocessing import Process, Queue

class Webcam:
    def __init__(self):
        self.video_capture = cv2.VideoCapture('/dev/video2')
        self.current_frame = self.video_capture.read()[1]
        self.current_frame = cv2.resize(self.current_frame,(1280,720), cv2.INTER_AREA)

def _update_frame(self):
    self.current_frame = self.video_capture.read()[1]

def _resize(self):
    self.current_frame = cv2.resize(self.current_frame,(1280,720), cv2.INTER_AREA)

def resizeThread(self):
    p2 = Process(target=self._resize, args=())
    p2.start()

def readThread(self):
    p1 = Process(target=self._update_frame, args=())
    p1.start()

def get_frame(self):
    return self.current_frame

主文件

from webcam import Webcam
import cv2
import time
webcam = Webcam()
webcam.readThread()
webcam.resizeThread()
while True:
    startF = time.time()
    webcam._update_frame()
    webcam._resize()
    image = webcam.get_frame()
    print  (image.shape)
    cv2.imshow("Frame", image)
    endF = time.time()
    print ("FPS: {:.2f}".format(1/(endF-startF)))
    cv2.waitKey(1)

我只有大約 10 FPS 的 FPS。

FPS: 11.16
(720, 1280, 3)
FPS: 10.91
(720, 1280, 3)
FPS: 10.99
(720, 1280, 3)
FPS: 10.01
(720, 1280, 3)
FPS: 9.98
(720, 1280, 3)
FPS: 9.97

那么如何優化多處理以提高 FPS? 我是否正確使用了多處理?

非常感謝你幫助我

東安

不確定這對多處理方面是否有幫助,但您可以通過兩種方式優化 cv2.resize 調用:

  • 簡單:將插值 arg 更改為 INTER_LINEAR 並按因子 2 *進行迭代縮小。 這將產生大致相同的質量,但速度更快。
  • 更難:您可以在 GPU 上調整大小,因為它是調整大小的非常合適的設備

*當然,循環的最后一步應該使用一個小於 2 的因子,以確保結果具有您想要的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM