python + pyqt5 视频播放UI界面

需要装的包有pyqt5和opencv,我的python版本是3.7 。

最近在做一个人头检测的项目,识别的效果还算可以了,组长说要做一个demo出来,用来展示我们的成果……然后我就用python+qt做了一个超级简陋的界面出来了。

为了避免大家走弯路先跟大家说一下,qt 直接贴图片在label上的话很简单,几行转换语句就行了。但是视频是不能这样的,一开始我想当然的以为将视频读取帧,然后将每一帧图片贴在label上,发现。。他只会等你的视频播完只显示最后一帧图片,其他时刻一片空白。后来百度才发现是要像单片机一样设一个定时器给他,定时跳到某个函数一帧一帧读取图片然后显示在label上。

下面是代码,在类初始化处定义定时器,显示图片的label,打开文件夹open,关闭视频stop,检测detect,在打开文件夹选好视频的时候开启定时器展示图片帧,按下stop的时候停止定时器,终止视频。

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

import cv2
from PyQt5.QtCore import QTimer


class Video(QWidget):
    
    def __init__(self):
        super(Video, self).__init__()
        self.frame = []  # 存图片
        self.detectFlag = False  # 检测flag
        self.cap = []
        self.timer_camera = QTimer()     #定义定时器
        
        # 外框
        self.resize(900, 650)
        self.setWindowTitle("Head Detection")
        # 图片label
        self.label = QLabel(self)
        self.label.setText("Waiting for video...")
        self.label.setFixedSize(800, 450)  # width height
        self.label.move(50, 100)
        self.label.setStyleSheet("QLabel{background:pink;}"
                                 "QLabel{color:rgb(100,100,100);font-size:15px;font-weight:bold;font-family:宋体;}"
                                 )
        # 显示人数label
        self.label_num = QLabel(self)
        self.label_num.setText("Waiting for detectiong...")
        self.label_num.setFixedSize(430, 40)  # width height
        self.label_num.move(200, 20)
        self.label_num.setStyleSheet("QLabel{background:yellow;}")
        # 开启视频按键
        self.btn = QPushButton(self)
        self.btn.setText("Open")
        self.btn.move(150, 570)
        self.btn.clicked.connect(self.slotStart)
        # 检测按键
        self.btn_detect = QPushButton(self)
        self.btn_detect.setText("Detect")
        self.btn_detect.move(400, 570)
        self.btn_detect.setStyleSheet("QPushButton{background:red;}")  # 没检测红色,检测绿色
        self.btn_detect.clicked.connect(self.detection)
        # 关闭视频按钮
        self.btn_stop = QPushButton(self)
        self.btn_stop.setText("Stop")
        self.btn_stop.move(700, 570)
        self.btn_stop.clicked.connect(self.slotStop)

    def slotStart(self):
        """ Slot function to start the progamme
            """
        videoName, _ = QFileDialog.getOpenFileName(self, "Open", "", "*.avi;;*.mp4;;All Files(*)")
        if videoName != "":  # “”为用户取消
            self.cap = cv2.VideoCapture(videoName)
            self.timer_camera.start(100)
            self.timer_camera.timeout.connect(self.openFrame)

    def slotStop(self):
        """ Slot function to stop the programme
            """
        if self.cap != []:
            self.cap.release()
            self.timer_camera.stop()   # 停止计时器
            self.label.setText("This video has been stopped.")
            self.label.setStyleSheet("QLabel{background:pink;}"
                                     "QLabel{color:rgb(100,100,100);font-size:15px;font-weight:bold;font-family:宋体;}"
                                     )
        else:
            self.label_num.setText("Push the left upper corner button to Quit.")
            Warming = QMessageBox.warning(self, "Warming", "Push the left upper corner button to Quit.",QMessageBox.Yes)

    def openFrame(self):
        """ Slot function to capture frame and process it
            """
        
        if(self.cap.isOpened()):
            ret, self.frame = self.cap.read()
            if ret:
                frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
                if self.detectFlag == True:
                    
                    # 检测代码self.frame
                    self.label_num.setText("There are " + str(5) + " people.")

                height, width, bytesPerComponent = frame.shape
                bytesPerLine = bytesPerComponent * width
                q_image = QImage(frame.data,  width, height, bytesPerLine,
                                 QImage.Format_RGB888).scaled(self.label.width(), self.label.height())
                self.label.setPixmap(QPixmap.fromImage(q_image))
            else:
                self.cap.release()
                self.timer_camera.stop()   # 停止计时器

    def detection(self):
        self.detectFlag = not self.detectFlag # 取反
        if self.detectFlag == True:
            self.btn_detect.setStyleSheet("QPushButton{background:green;}")
        else:
            self.btn_detect.setStyleSheet("QPushButton{background:red;}")
    #        self.label_num.setText("There are 5 people.")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my = Video()
    my.show()
    sys.exit(app.exec_())

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页