PyQt读取视频并播放

12 篇文章 0 订阅
import os
import sys

from PyQt5 import QtGui,QtWidgets,QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.uic import loadUi

import cv2

class my_software(QMainWindow):
    def __init__(self,parent = None):
        super(my_software,self).__init__(parent)
        loadUi('test.ui',self)

        self.frame = []
        # self.detectFlag = False
        self.cap = []
        self.timer_camera = QTimer()

        video = "test.mp4"
        self.cap = cv2.VideoCapture(video)
        self.Initial_GUI()

    def Initial_GUI(self):
        self.setWindowTitle("VideoDisplay")

        self.lb_Screen.setFixedSize(self.lb_Screen.width(),self.lb_Screen.height())
        
        self.btn_Start.clicked.connect(self.Btn_Start)
        self.btn_Stop.clicked.connect(self.Btn_Stop)

    #窗口关闭时间
    def Btn_Close(self,event):
        event.accept()
        self.cap.release()

    def Btn_Start(self):
        #定时器开启,每隔一段时间,读取一帧
        self.timer_camera.start(100)
        self.timer_camera.timeout.connect(self.OpenFrame)

    def Btn_Stop(self):
        # self.cap.release()
        self.timer_camera.stop()

    def OpenFrame(self):
        ret,frame = self.cap.read()

        if ret:
            #Process
            # cv2.putText(frame,"good",(50,100),2,cv2.FONT_HERSHEY_COMPLEX,(0,0,255),3)

            self.Display_Image(frame)
        else:
            self.cap.release()
            self.timer_camera.stop()

    def Display_Image(self,image):
        if(len(image.shape) == 3):
            image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
            Q_img = QImage(image.data,
                        image.shape[1],
                        image.shape[0],
                        QImage.Format_RGB888)
        elif(len(image.shape) == 1):
            Q_img = QImage(image.data,
                        image.shape[1],
                        image.shape[0],
                        QImage.Format_Indexed8)
        else:
            Q_img = QImage(image.data,
                        image.shape[1],
                        image.shape[0],
                        QImage.Format_RGB888)
            
        self.lb_Screen.setPixmap(QtGui.QPixmap(Q_img))
        self.lb_Screen.setScaledContents(True)

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

    my = my_software()
    my.show()

    sys.exit(app.exec_())

 

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值