PyQt的视频完美,可实现实时、本地、暂停、继续、停止等功能,且无卡退、程序崩溃等情况

使用pyqt来读取本地视频和实时视频

1.首先使用创建ui文件,样式如下

组成:
lable:显示视频
Open:打开文件或者摄像头并且显示,Close为取消
lable:显示文件
GrouBox:为了保证多个QRadioButton只选中一个,(注:我继承的的是c#还要java的思想,这里边到底需不需要这个控件我也没试)

在这里插入图片描述

先保存为DisplayUI.ui文件然后再转成 DisplayUI.py文件
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'DisplayUI.ui'
#
# Created by: PyQt5 UI code generator 5.14.2
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.DispalyLabel = QtWidgets.QLabel(self.centralwidget)
        self.DispalyLabel.setGeometry(QtCore.QRect(0, 0, 681, 451))
        self.DispalyLabel.setText("")
        self.DispalyLabel.setObjectName("DispalyLabel")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(60, 460, 120, 80))
        self.groupBox.setObjectName("groupBox")
        self.radioButtonCam = QtWidgets.QRadioButton(self.groupBox)
        self.radioButtonCam.setGeometry(QtCore.QRect(10, 20, 89, 17))
        self.radioButtonCam.setObjectName("radioButtonCam")
        self.radioButtonFile = QtWidgets.QRadioButton(self.groupBox)
        self.radioButtonFile.setGeometry(QtCore.QRect(10, 40, 89, 17))
        self.radioButtonFile.setCheckable(True)
        self.radioButtonFile.setChecked(True)
        self.radioButtonFile.setObjectName("radioButtonFile")
        self.Open = QtWidgets.QPushButton(self.centralwidget)
        self.Open.setGeometry(QtCore.QRect(220, 490, 75, 21))
        self.Open.setObjectName("Open")
        self.Close = QtWidgets.QPushButton(self.centralwidget)
        self.Close.setGeometry(QtCore.QRect(380, 490, 75, 23))
        self.Close.setObjectName("Close")

        self.First= QtWidgets.QPushButton(self.centralwidget)
        self.First.setGeometry(QtCore.QRect(500, 490, 75, 23))
        self.First.setObjectName("First")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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.groupBox.setTitle(_translate("MainWindow", "GroupBox"))
        self.radioButtonCam.setText(_translate("MainWindow", "摄像模式"))
        self.radioButtonFile.setText(_translate("MainWindow", "本地文件模式"))
        self.Open.setText(_translate("MainWindow", "Open"))
        self.Close.setText(_translate("MainWindow", "Close"))
        self.First.setText(_translate("MainWindow", "暂停||继续"))


2.功能事件

写一个VideoDisplay.py文件,用来生成各个控件的事件和槽,,以及创建线程,
注明:内部代码逻辑已经改好,,实现了暂停和继续的功能,还有就是解决了视频播放完毕程序各个按钮事件失灵的问题,以及再开启摄像头的模式下再点击本地文件模式会崩溃的问题。。好了,大概就这么多。下面上萤火(硬货)。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/4/27 19:40
# @Author  : 沐白
# @Site    :
# @File    : VideoDisplay.py
# @Software: PyCharm
import cv2
import threading
from PyQt5.QtCore import QFile
from PyQt5.QtWidgets import QFileDialog, QMessageBox
from PyQt5.QtGui import QImage, QPixmap
import time
import os

class Display:
    def __init__(self, ui, mainWnd):
        self.ui = ui
        self.mainWnd = mainWnd
        # 默认视频源为相机
        self.ui.radioButtonCam.setChecked(True)
        self.isCamera = True
        self.a = 0
        self.b=0
        # 信号槽设置
        ui.Open.clicked.connect(self.Open)
        ui.Close.clicked.connect(self.Close)
        ui.radioButtonCam.clicked.connect(self.radioButtonCam)
        ui.radioButtonFile.clicked.connect(self.radioButtonFile)
        ui.First.clicked.connect(self.suspend_continue)

        # 创建一个关闭事件并设为未触发
        self.continueEvent1 = threading.Event()
        self.continueEvent1.clear()

        self.stopEvent = threading.Event()
        self.stopEvent.clear()

    def radioButtonCam(self):
        if self.a==0:
            self.isCamera = True

    def suspend_continue(self):
        self.continueEvent1.set()
    def radioButtonFile(self):
        if self.a==0:
            self.isCamera = False

    # def pause(self):
    #     self.__flag.clear()  # 设置为False, 让线程阻塞
    #     print("pause")
    #
    # def resume(self):
    #     self.__flag.set()  # 设置为True, 让线程停止阻塞
    #     print("resume")

    def Open(self):
        self.fileName=""
        if not self.isCamera:
            self.fileName, self.fileType = QFileDialog.getOpenFileName(self.mainWnd, 'Choose file', '',"MP4Files(*.mp4);;AVI Files(*.avi)" )
            self.cap = cv2.VideoCapture(self.fileName)
            self.frameRate = self.cap.get(cv2.CAP_PROP_FPS)
        else:
            # 下面两种rtsp格式都是支持的
            #  cap = cv2.VideoCapture("rtsp://admin:[email protected]/main/Channels/1")
             self.a=1
             self.cap = cv2.VideoCapture(0)

        # 创建视频显示线程
        if  (self.fileName != "") or(self.a==1) :
            th = threading.Thread(target=self.Display)
            th.start()


    def Close(self):
        # 关闭事件设为触发,关闭视频播放
        self.a = 0
        self.stopEvent.set()


    def Display(self):
        self.ui.Open.setEnabled(False)
        self.ui.Close.setEnabled(True)
        self.ui.First.setEnabled(True)

        while self.cap.isOpened() and True:
            success, frame = self.cap.read()
            # RGB转BGR
            print(success)
            if success==False:
                print("play finished")  # 判断本地文件播放完毕
                break
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

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

            if self.isCamera:
                cv2.waitKey(1)
            else:
                cv2.waitKey(int(1000 / self.frameRate))
            # 判断关闭事件是否已触发

            if True==self.continueEvent1.is_set():
                self.continueEvent1.clear()
                self.b=1
                while self.b==1:
                    if True == self.continueEvent1.is_set():
                        self.continueEvent1.clear()
                        self.b=0
            if True == self.stopEvent.is_set():
                # 关闭事件置为未触发,清空显示label
                break
        self.cap.release()
        self.stopEvent.clear()
        self.ui.DispalyLabel.clear()
        self.ui.Close.setEnabled(False)
        self.ui.Open.setEnabled(True)
        self.ui.DispalyLabel.setText("1212")

3.生成主函数

至于有人问为什么不把这三个文件写在一起???
答案是:能写在一起,但是你想想一个文件划分为每个功能不更好用吗,最重要的不会吧自己绕迷糊,

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/4/26 19:36
# @Author  : 沐白
# @Site    : 
# @File    : Main.py
# @Software: PyCharm


# import sys
# from PyQt5.QtWidgets import QApplication, QMainWindow
# import asa
#
#
#
#
# if __name__ == '__main__':
#     app = QApplication(sys.argv)
#     MainWindow = QMainWindow()
#     ui = asa.Ui_MainWindow()
#     ui.setupUi(MainWindow)
#     MainWindow.show()
#     sys.exit(app.exec_())


import sys
import DisplayUI
from PyQt5.QtWidgets import QApplication, QMainWindow
from VideoDisplay import Display

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWnd = QMainWindow()
    ui = DisplayUI.Ui_MainWindow()
    # 可以理解成将创建的 ui 绑定到新建的 mainWnd 上
    ui.setupUi(mainWnd)
    display = Display(ui, mainWnd)
    mainWnd.show()

    sys.exit(app.exec_())

最后呢希望大家收藏走起一波,后续会有pyqt,tensorflow,爬虫,python基础的各个模块,,
更重要的是我都改bug改到0:34了,,大家给按排一波》》》

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章