Controlling a QThread in pyqt5?

2020-04-20 07:10发布

问题:

I wrote a webcam app with pyqt5 and opencv, which saves live videos. screenshot : https://i.hizliresim.com/kM8GOD.png

I used a QThread for streaming without blocking main app. But the problem is, I can't control streaming&saving videos with my buttons. It start recording when I run main program but I want to make it with RUN button and save with STOP button.

I tried do it with recording parameter but it's weird a bit. Program starts recording initially and first time I pressed stop it saves video perfectly. But when I'm trying to save 2nd video with start button. It doesn't started streaming on label but when I'm closing the app, it's saving video.

class Thread(QThread):
    recording = True
    changePixmap = pyqtSignal(QImage)
    cap = cv2.VideoCapture(0)
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))

    out = cv2.VideoWriter('input.avi', cv2.VideoWriter_fourcc(
        'M', 'J', 'P', 'G'), 30, (frame_width, frame_height))

    def run(self):
        while self.recording:
            ret, frame = self.cap.read()
            if ret:
                self.out.write(frame)
                rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                h, w, ch = rgbImage.shape
                bytesPerLine = ch * w
                convertToQtFormat = QtGui.QImage(
                    rgbImage.data, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
                p = convertToQtFormat.scaled(
                    350, 350, PyQt5.QtCore.Qt.KeepAspectRatio)
                self.changePixmap.emit(p)
        # self.cap.release()
        # self.out.release()
        self.changePixmap.emit(p)


class Ui_MainWindow(QtWidgets.QWidget):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(901, 593)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 20, 350, 350))
        self.label.setObjectName("label")
        self.th = Thread(self)
        self.th.changePixmap.connect(self.setImage)
        self.th.start()
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(20, 320, 261, 31))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.func)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(470, 20, 261, 271))
        self.label_2.setText("")
        self.label_2.setObjectName("label_2")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(20, 390, 261, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.func2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 901, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "record"))
        self.pushButton_2.setText(_translate("MainWindow", "stop"))

    def setImage(self, image):
        self.label.setPixmap(QPixmap.fromImage(image))

    def func(self):
        self.th.recording = True

    def func2(self):
        self.th.recording = False


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())