【发布时间】:2020-02-28 08:50:12
【问题描述】:
我是 OpenCV-Python 多处理方面的初学者。我正在使用 Raspberry Pi 2 Model 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
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?我是否正确使用了多处理?
非常感谢你帮助我,
东安
【问题讨论】:
-
我认为那里有很多问题。 1)您在
webcam.py中的缩进是错误的。 2)您的进程称为线程,这没有帮助。 3)您正在计时单个帧而不是平均超过 100 帧或更多,因此您会得到很大的变化。 4)据我所知,您的 “进程” 读取一帧并退出 - 这是最大的问题。 -
这个想法是使用线程在一个线程中抓取帧,然后在另一个线程中显示/处理帧。 FPS 性能提升将来自 I/O 延迟减少。如果您的应用程序由于 I/O 延迟而出现瓶颈,这些链接将有所帮助
-
嗨@MarkSetchell,您可以按照您的意见编辑我的代码吗?提前致谢
-
好吧,你不能说多处理比多线程更好,反之亦然。他们是两个不同的东西。由于 CPU 限制(例如计算或执行大量算术运算),多处理将为您带来性能提升。相比之下,多线程可以在您不执行 CPU 受限的操作(例如 I/O 操作)时提供帮助。我认为你需要衡量你的表现并确定你的瓶颈来自哪里,然后再决定使用哪一个
标签: python opencv optimization multiprocessing frame-rate