关闭后再打开一个新的cv2.VideoCapture

时间:2019-09-01 19:47:30

标签: python opencv pyqt pyqt5 qthread

这不是任何问题的重复。我有1个代码,但有2个不同的问题。

我有2个功能。一个用于从网络摄像头捕获视频,另一个用于从该视频捕获帧。在第一个函数中,我使用cap = cv2.VideoCapture(0),然后尝试使用'input.avi'参数创建一个新的cv2.VideoCapture(cap2 = cv2.VideoCapture('input.avi' ))在cap.release()命令之后。 但是我认为它可以释放(因为灯熄灭),但是无法重新打开它。 我能做什么? 第一个方法在Thread类中,运行。 第二个是MainWindows的分析功能。

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

    def run(self):
        while True:
            ret, frame = self.cap.read()
            if ret:
                # self.out.write(frame)
                copy = frame.copy()
                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, copy)
        # 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.start)
        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.stop)
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(20, 490, 261, 31))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.clicked.connect(self.analyze)
        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)

        self.stream = True
        self.record = False
        self.out = None
        self.images = []

    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"))
        self.pushButton_3.setText(_translate("MainWindow", "analyze"))

    def setImage(self, image, ndarr):
        self.saveVideo()
        if self.stream:
            self.label.setPixmap(QPixmap.fromImage(image))
        if self.out is not None:
            self.out.write(ndarr)

    def start(self):
        self.out = cv2.VideoWriter('input.avi', cv2.VideoWriter_fourcc(
            'M', 'J', 'P', 'G'), 30, (640, 480))

    def stop(self):
        self.out = None

    def analyze(self):
        self.th.cap.release()
        if not os.path.exists('input.avi'):
            return
        self.images = []
        capt = cv2.VideoCapture('input.avi')
        aout = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(
            'M', 'J', 'P', 'G'), 2, (640, 480))

        ret, frame = capt.read()
        print(ret, frame)


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_())

0 个答案:

没有答案