Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5线程退出时关闭GUI_Python_Pyqt_Pyqt5_Opencv Python - Fatal编程技术网

Python PyQt5线程退出时关闭GUI

Python PyQt5线程退出时关闭GUI,python,pyqt,pyqt5,opencv-python,Python,Pyqt,Pyqt5,Opencv Python,我正在尝试从opencv视频流运行pyqt5应用程序中的视频,在那里我可以使用2个按钮启动和停止视频 这个问题与我们的关系密切。问题是,如果我想取消视频,主窗口也会关闭。然而,我不确定为什么会发生这种情况 编辑:此问题出现在Windows 10上,而线程在Mac上运行时没有任何问题 from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * import sys import cv2

我正在尝试从opencv视频流运行pyqt5应用程序中的视频,在那里我可以使用2个按钮启动和停止视频

这个问题与我们的关系密切。问题是,如果我想取消视频,主窗口也会关闭。然而,我不确定为什么会发生这种情况

编辑:此问题出现在Windows 10上,而线程在Mac上运行时没有任何问题

from PyQt5.QtGui import * 
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import cv2 

class MainWindow(QWidget):
    def __init__(self) -> None:
        super(MainWindow, self).__init__()
        self.VBL = QVBoxLayout()
        self.FeedLabel = QLabel()
        self.VBL.addWidget(self.FeedLabel)
        self.playBTN = QPushButton('Play')
        self.playBTN.clicked.connect(self.play)
        self.VBL.addWidget(self.playBTN)
        self.cancelBTN.clicked.connect(self.Cancel)
        self.VBL.addWidget(self.cancelBTN)
        self.setLayout(self.VBL)
    
    def ImageUpdateSlot(self, Image):
        Image = Image.scaled(640, 480, Qt.KeepAspectRatio)
        self.FeedLabel.setPixmap(QPixmap.fromImage(Image))
    
    def Cancel(self):
        self.worker1.stop()
    
    def play(self):
        self.worker1 = CompleteVideoRunner("Path/to/some/video")
        self.worker1.start()
        self.worker1.sigImage.connect(self.ImageUpdateSlot)
        self.cancelBTN.clicked.connect(self.Cancel)

class VideoSignal(QObject):
    Image = pyqtSignal(QImage)
    imageCount = pyqtSignal(int)

class CompleteVideoRunner(QThread):
    #signal = VideoSignal() 
    sigImage = pyqtSignal(QImage)
    sigCount =pyqtSignal(int)
    def __init__(self, path, curFrame = 0):
        super(CompleteVideoRunner, self).__init__()
        self.cap = cv2.VideoCapture(path)
        #self.signal = VideoSignal()
        self.curFrame = curFrame
        self.is_killed = False

    def run(self):
        self.is_killed = False
        j = self.curFrame
        self.cap.set(1, j)
        while (self.cap.isOpened() and not self.is_killed):
            rep, frame = self.cap.read()
            self.curFrame = j
            if not rep:
                break
            height, width, channel = frame.shape
            bytesPerLine = 3 * width
            jpg = frame.tobytes()
            jpg = QByteArray(jpg)
            QImg= QImage(frame.data, width, height, bytesPerLine,
                         QImage.Format_BGR888)
            self.sigImage.emit(QImg)
            self.sigCount.emit(j)
            j+=1
    

    def stop(self):
        self.is_killed = True
        print('finished thread')
        self.quit()

if __name__ == "__main__":
    App = QApplication(sys.argv)
    Root = MainWindow()
    Root.show()
    sys.exit(App.exec())

为了解决这个问题,它像线程一样工作。重要的一步是在发出信号之前在线程的run()函数中调整QImage的大小,而不是在主线程中

接下来的问题是:为什么我需要在发出信号之前重新缩放QImage

再生能力考虑以下代码:

from PyQt5.QtGui import * 
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import cv2 

class MainWindow(QWidget):
    def __init__(self) -> None:
        super(MainWindow, self).__init__()
        self.VBL = QVBoxLayout()
        self.FeedLabel = QLabel()
        self.VBL.addWidget(self.FeedLabel)
        self.playBTN = QPushButton('Play')
        self.playBTN.clicked.connect(self.play)
        self.VBL.addWidget(self.playBTN)
        self.cancelBTN.clicked.connect(self.Cancel)
        self.VBL.addWidget(self.cancelBTN)
        self.setLayout(self.VBL)
    
    def ImageUpdateSlot(self, Image):
        self.FeedLabel.setPixmap(QPixmap.fromImage(Image))
    
    def Cancel(self):
        self.worker1.stop()
    
    def play(self):
        self.worker1 = CompleteVideoRunner("Path/to/some/video")
        self.worker1.start()
        self.worker1.sigImage.connect(self.ImageUpdateSlot)
        self.cancelBTN.clicked.connect(self.Cancel)

class VideoSignal(QObject):
    Image = pyqtSignal(QImage)
    imageCount = pyqtSignal(int)

class CompleteVideoRunner(QThread):
    #signal = VideoSignal() 
    sigImage = pyqtSignal(QImage)
    sigCount =pyqtSignal(int)
    def __init__(self, path, curFrame = 0):
        super(CompleteVideoRunner, self).__init__()
        self.cap = cv2.VideoCapture(path)
        #self.signal = VideoSignal()
        self.curFrame = curFrame
        self.is_killed = False

    def run(self):
        self.is_killed = False
        j = self.curFrame
        self.cap.set(1, j)
        while (self.cap.isOpened() and not self.is_killed):
            rep, frame = self.cap.read()
            self.curFrame = j
            if not rep:
                break
            height, width, channel = frame.shape
            bytesPerLine = 3 * width
            QImg= QImage(frame.data, width, height, bytesPerLine,
                         QImage.Format_BGR888)
            QImg = QImg.scaled(640, 480, Qt.KeepAspectRatio)
            self.sigImage.emit(QImg)
            self.sigCount.emit(j)
            j+=1
    

    def stop(self):
        self.is_killed = True
        print('finished thread')
        self.quit()

if __name__ == "__main__":
    App = QApplication(sys.argv)
    Root = MainWindow()
    Root.show()
    sys.exit(App.exec())