我是OpenCV-Python上多处理的初学者。我正在使用Raspberry Pi 2 Model B(四核x 1GHz)。我正在尝试通过一个非常简单的示例来优化FPS。
这是我的代码:
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)
我只有大约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?我是否正确使用了多重处理?
非常感谢您对我的帮助,
Toan
不确定这在多处理方面是否有帮助,但是您可以通过两种方式优化cv2.resize调用:
*当然,循环的最后一步应使用小于2的因数,以确保结果具有所需的大小。