栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

cv2嵌入PyQt显示图像与视频

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cv2嵌入PyQt显示图像与视频

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


class CV2_PYQT_Window(QDialog):
    def __init__(self,src,target="video",parent=None):
        super(CV2_PYQT_Window,self).__init__()
        self.default_set_height = 800
        if target == "video":
            self.read_video(src)
        else:
            self.read_src(src)

    def read_src(self,src):
    
        image = cv2.imread(src, cv2.IMREAD_UNCHANGED)
        img_height, img_width, channels = image.shape
        if img_height>800:
            image = cv2.reszie(image,(img_height*default_set_height/img_height ,self.default_set_height ))
        img_height, img_width, channels = image.shape
        bytesPerLine = channels * img_width
        QImg = QImage(image.data, img_width, img_height, bytesPerLine, QImage.Format_BGR888)
        label = QLabel()
        label.setPixmap(QtGui.QPixmap.fromImage(QImg))
        button_ok = QPushButton("ok")
        button_no = QPushButton("no")
        layout1 = QtWidgets.QHBoxLayout()
        layout1.addWidget(label)
        layout2 = QtWidgets.QHBoxLayout()
        layout2.addWidget(button_ok)
        layout2.addWidget(button_no)
        lay = QtWidgets.QVBoxLayout()
        lay.addLayout(layout1)
        lay.addLayout(layout2)
        self.setLayout(lay)
        self.show()
        
    def read_video(self,file):
        self.frame = 1
        self._file = file
        self.cap = cv2.VideoCapture(file) 

        self.success, image = self.cap.read()
        if not self.success:
            return
        cv2.putText(image, str(self.frame), (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255, 0), 3)
  
        img_height, img_width, channels = image.shape
        if img_height>800:
            image = cv2.resize(image,(int(img_width*self.default_set_height/img_height) ,self.default_set_height ))
            
        img_height, img_width, channels = image.shape
        bytesPerLine = channels * img_width
        QImg = QImage(image.data, img_width, img_height, bytesPerLine, QImage.Format_BGR888)
        self.label = QLabel()
        self.label.setPixmap(QtGui.QPixmap.fromImage(QImg))
        button_ok = QPushButton("ok")
        button_no = QPushButton("no")
        self.timer= QtCore.QTimer()
    
        layout1 = QtWidgets.QHBoxLayout()
        layout1.addWidget(self.label)
        layout2 = QtWidgets.QHBoxLayout()
        layout2.addWidget(button_ok)
        layout2.addWidget(button_no)
        lay = QtWidgets.QVBoxLayout()
        lay.addLayout(layout1)
        lay.addLayout(layout2)
        self.setLayout(lay)
        self.show()

        self.timer.timeout.connect(self.queryframe)
        button_ok.clicked.connect(self.video_start)
        button_no.clicked.connect(self.video_stop)
        
    def cv2_display(self,src):
        cv2.imshow('src',src)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    def video_start(self):
        if(not self.timer.isActive() and not self.success):
            self.cap = cv2.VideoCapture(self._file) 
            self.frame = 1
            self.timer.start(35)
        else:
            self.timer.start(35)

    def video_stop(self):
        self.timer.stop()


    def queryframe(self):
        '''
        循环捕获图片
        '''
        print(self.frame)
        self.frame+=1
        self.success, image = self.cap.read()
        cv2.putText(image, str(self.frame), (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 3)
        if not self.success:
            self.timer.stop()
            return 

        img_height, img_width, channels = image.shape
        if img_height>800:
            image = cv2.resize(image,(int(img_width*self.default_set_height/img_height) ,self.default_set_height ))

        img_height, img_width, channels = image.shape
        bytesPerLine = channels * img_width
        QImg = QImage(image.data, img_width, img_height, bytesPerLine, QImage.Format_BGR888)
        self.label.setPixmap(QPixmap.fromImage(QImg))
  


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    file = "../video/dance/dance.mp4"
    file1 = "01.png"
    mainWin = CV2_PYQT_Window(file1,target="image")
    mainWin.show()
    sys.exit( app.exec_() )
图片

视频


说明,点击ok开始播放,no暂停。结束后点击ok重新播放。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号