简体   繁体   中英

How to use optimize processors performance in OpenCV-Python?

I'm a beginner in multiprocessing on OpenCV-Python. I'm using Raspberry Pi 2 Model B (Quad-core x 1GHz). And I'm trying to optimize FPS with a very simple example.

This is my code:

webcam.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

main.py

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)

I only got FPS around 10 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

So how can I optimize multiprocessing to increase FPS? Did I use multiprocessing correctly?

Thank you so much for helping me,

Toan

Not sure this will help with the multiprocessing aspect, but you can optimize your cv2.resize call in two ways:

  • Easy: Change the interpolation arg to INTER_LINEAR and do iterative downscaling by a factor of 2 * . This will produce about the same quality, but faster.
  • Harder: You could do the resizing on the GPU, as it's a very appropriate device for resizing

* Of course, the last step in the loop should use a factor that is less than two, to make sure the result has your desired size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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