You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Debian11下如何优化PyQt5 QPixmap提升实时图像显示效率

QThread中QLabel实时显示视频CPU占用过高问题

运行环境:

  • 系统:Debian
  • Python版本:3.9.2
  • PyQt5版本:5.15.2

之前关于“使用PyQt5的QPainter在paintEvent中实现图像显示无需信号传输”的说法并不准确。目前我将QLabel实时显示视频图像的逻辑放在QThread中运行,但这种方式导致CPU占用极高,经测试发现QPixmap.fromImage代码运行耗时较长。

相关代码:

class CommunicationWorker(QObject):
    start_communication_signal = pyqtSignal()

    def __init__(self, *args):
        super(CommunicationWorker, self).__init__()
        self.res_queue = args[0]
        self.pixmap = QPixmap()
        self.qp_cache = {}
        self.graphicsView = QGraphicsView()
        self.frame_list = [None, None, None, None]
        self.label_list = args[1]

        self.running = False
        self.start_communication_signal.connect(self.run_thread)

    @staticmethod
    def play_hologram_frame(frame, show_label):
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        video_img = array2qimage(image)
        video_img = QPixmap.fromImage(video_img)
        show_label.setPixmap(video_img)

    @pyqtSlot()
    def run_thread(self):
        self.running = True
        # hlg, front, back, enlarge = None, None, None, None
        while self.running:
            try:
                # Check for results
                for i in range(len(self.res_queue)):
                    temp = read_buffer(self.res_queue[i], i)
                    if temp is not None:
                        # self.play_hologram_frame(temp, self.label_list[i])
                        if i == 0:
                            self.frame_list[0] = cv2.resize(temp, (600, 720))
                        elif i == 1:
                            self.frame_list[1] = cv2.resize(temp, (600, 500))
                        elif i == 2:
                            self.frame_list[2] = cv2.resize(temp, (600, 500))
                        elif i == 3:
                            self.frame_list[3] = cv2.resize(temp, (1210, 720))
                        self.play_hologram_frame(self.frame_list[i], self.label_list[i])

            except Exception as e:
                print(e)
                print(traceback.print_exc())

    def stop_thread(self):
        self.running = False

内容的提问来源于stack exchange,提问作者biao_biao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 06:01:22