PyQt5缩放视频窗口后界面卡死无响应,Python停止工作

ZFlymee 发布于 2019/05/08 21:45
阅读 2K+
收藏 0

正在直播: LF AI 基金会主办首届AICON 2022>>>

使用PyQt5编写了一个UI,用QLabel显示视频,对界面进行缩放,Python进程就停止,把视频换成图片就不会,请问怎么进行调试?

源码:

import threading
import cv2
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QImage, QPixmap


class Ui_childwind(object):
    def setupUi(self, childwind):
        childwind.setObjectName("childwind")
        childwind.setWindowModality(QtCore.Qt.NonModal)
        childwind.resize(640, 480)
        self.gridLayout = QtWidgets.QGridLayout(childwind)
        self.gridLayout.setObjectName("gridLayout")
        self.childisplaylabel = QtWidgets.QLabel(childwind)
        self.childisplaylabel.setText("")
        self.childisplaylabel.setScaledContents(True)
        self.childisplaylabel.setObjectName("childisplaylabel")
        self.gridLayout.addWidget(self.childisplaylabel, 0, 0, 1, 1)

        self.retranslateUi(childwind)
        QtCore.QMetaObject.connectSlotsByName(childwind)

    def retranslateUi(self, childwind):
        _translate = QtCore.QCoreApplication.translate
        childwind.setWindowTitle(_translate("childwind", "可视化界面"))


class Display:
    def __init__(self, ui, mainWnd):
        self.ui = ui
        self.mainWnd = mainWnd

        self.cap = cv2.VideoCapture(0)
        th = threading.Thread(target=self.Display)
        th.start()

    def Display(self):
        while self.cap.isOpened():
            success, frame = self.cap.read()
            if not success:
                self.cap.release()
                cv2.destroyAllWindows()
                break

            img = QImage(frame.data, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
            self.ui.childisplaylabel.setPixmap(QPixmap.fromImage(img))


if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    app = QApplication(sys.argv)
    Dialog = QWidget()
    ui = Ui_childwind()
    ui.setupUi(Dialog)
    display = Display(ui, Dialog)
    Dialog.show()
    sys.exit(app.exec_())

加载中
0
lieyi
lieyi

这个问题是否解决?我也遇到相同问题

0
victorfengming
victorfengming

这个问题需要使用多线程吧,我也遇到过

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部