【问题标题】:How to use optimize processors performance in OpenCV-Python?如何在 OpenCV-Python 中使用优化处理器性能?
【发布时间】: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


【解决方案1】:

不确定这是否有助于多处理方面,但您可以通过两种方式优化您的 cv2.resize 调用:

  • 简单:将插值 arg 更改为 INTER_LINEAR 并进行 2 倍的迭代缩小 *。这将产生大致相同的质量,但速度更快。
  • 更难:您可以在 GPU 上调整大小,因为它是调整大小的非常合适的设备

* 当然,循环中的最后一步应该使用小于 2 的因子,以确保结果具有您想要的大小。

【讨论】:

  • 嗨@Stenfan,我将 INTER_AREA 更改为 INTER_LINEAR。以及如何在 GPU 上调整大小,这听起来很奇怪。你能告诉我更多信息吗?非常感谢!
  • 关于在 GPU 上调整大小:最简单的方法是将 OpenGL 用于 GPGPU。基本上:将纹理四边形渲染到 FBO,并缩小纹理。
猜你喜欢
  • 2015-09-01
  • 2016-06-26
  • 2017-04-07
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 2021-03-04
  • 2023-01-28
  • 2019-06-05
相关资源
最近更新 更多