用PyQt5进行视频录制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用PyQt5进行视频录制相关的知识,希望对你有一定的参考价值。

给出下面的PyQt代码,我就可以完美地捕获网络摄像头的流媒体视频。

现在,我想修改一下代码。因此,一个名为''记录''按钮,一旦按下捕获流媒体视频并保存视频。 我怎么能这样做呢?

我想录制视频,用于训练人脸识别。

class MainWindow(QWidget):
    # class constructor
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        # icon
        self.setWindowIcon(QtGui.QIcon('icon.png'))
        # create a timer
        self.timer = QTimer()
        # set timer timeout callback function

        self.timer.timeout.connect(self.viewCam)
        # set control_bt callback clicked  function
        self.ui.control_bt.clicked.connect(self.controlTimer)

    # view camera
    def viewCam(self):
        # read image in BGR format
        ret, image = self.cap.read()
        # convert image to RGB format
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        # get image infos
        height, width, channel = image.shape
        step = channel * width
        # create QImage from image
        # cv2.imshow('dna', image)
        qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
        # show image in img_label
        self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))


    # start/stop timer
    def controlTimer(self):
        # if timer is stopped
        if not self.timer.isActive():
            # create video capture
            self.cap = cv2.VideoCapture(0)
            # start timer
            self.timer.start(20)
            # update control_bt text
            self.ui.control_bt.setText("Stop")
        # if timer is started
        else:
            # stop timer
            self.timer.stop()
            # release video capture
            self.cap.release()
            # update control_bt text
            self.ui.control_bt.setText("Start")
            self.ui.image_label.setText("Camera")



if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create and show mainWindow
    mainWindow = MainWindow()
    mainWindow.show()

    sys.exit(app.exec_())

答案

答:我想修改代码,让一个名为''Record''的按钮加入,一旦按下就能捕捉到流媒体。

class MainWindow(QWidget):
    # class constructor
    def __init__(self):
        # call QWidget constructor
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        # icon
        self.setWindowIcon(QIcon('icon.png'))
        # create a timer
        self.viewTimer = QTimer()
        self.saveTimer = QTimer()
        # set timer timeout callback function
        self.viewTimer.timeout.connect(self.viewCam)
        self.saveTimer.timeout.connect(self.saveCam)
        # set control_bt callback clicked  function
        self.ui.control_bt.clicked.connect(self.controlView)
        self.ui.save_bt.clicked.connect(self.controlSave)
        # set video output
        self.out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 24, (640, 480))
        # waring message box
        self.msg = QMessageBox()
        self.msg.setWindowIcon(QIcon('warning.png'))


    # view camera
    def viewCam(self):
        if not self.saveTimer.isActive():
            # read image in BGR format
            ret, image = self.cap.read()
            # get image infos
            height, width, channel = image.shape
            step = channel * width
            # create QImage from image
            qImg = QImage(image.data, width, height, step, QImage.Format_BGR888)
            # show image in img_label
            self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))


    # start/stop timer
    def controlSave(self):
        # if camera timer active
        if self.viewTimer.isActive():
            self.msg.setWindowTitle("Warning")
            self.msg.setText("Please stop the camera")
            self.msg.exec_()
        # if timer is stopped
        if not self.saveTimer.isActive():
            if not self.viewTimer.isActive():
                # create video capture
                self.cap = cv2.VideoCapture(0)
                # start timer
                self.saveTimer.start(30)
                # update save_bt text
                self.ui.save_bt.setText("Stop")
        # if timer is started
        else:
            # stop timer
            self.saveTimer.stop()
            # release video capture
            self.cap.release()
            # update save_bt text
            self.ui.save_bt.setText("Save")
            self.ui.image_label.setText("Camera")

    def controlView(self):
        # if timer is stopped
        if self.saveTimer.isActive():
            self.msg.setWindowTitle("Warning")
            self.msg.setText("Please stop recording")
            self.msg.exec_()
        if not self.viewTimer.isActive():
            if not self.saveTimer.isActive():
                # create video capture
                self.cap = cv2.VideoCapture(0)
                # start timer
                self.viewTimer.start(30)
                # update control_bt text
                self.ui.control_bt.setText("Stop")
        # if timer is started
        else:
            # stop timer
            self.viewTimer.stop()
            # release video capture
            self.cap.release()
            # update control_bt text
            self.ui.control_bt.setText("Start")
            self.ui.image_label.setText("Camera")

    def saveCam(self):
        if not self.viewTimer.isActive():
            ret, image = self.cap.read()
            # get image infos
            height, width, channel = image.shape
            step = channel * width
            # write video
            self.out.write(image)
            # create QImage from image
            qImg = QImage(image.data, width, height, step, QImage.Format_BGR888)
            # show image in img_label
            self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))

以上是关于用PyQt5进行视频录制的主要内容,如果未能解决你的问题,请参考以下文章

在ubuntu下怎么用php代码自动调用ffmpeg命令来进行视频采集录制,抽帧!!是要调用shell脚本吗??急!!

Win7怎么录制电脑屏幕视频

iOS 录制视频MOV格式转MP4

C503怎么删除录制的视频文件?

用Python来做一个屏幕录制工具

Python使用ffmpeg合成视频音频