QLabel和画布QGraphicsView都可以实现全屏显示视频流,这次先说下QLabel,这个控件在全屏显示的时候会遇到一个问题,就是控件大小以及窗口模式的设置(参考:linux qt应用程序全屏,QT在ubuntu下实现界面全屏,侧边栏隐藏,上边栏隐藏【实例】..._落木君的博客-CSDN博客https://blog.csdn.net/weixin_32816821/article/details/116685490)
下面给出代码示例:
1.窗体部分(DisplayUI.py)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QPixmap, QPainter
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem
from PyQt5.QtWidgets import QDesktopWidget
import numpy as np
from concurrent.futures import ThreadPoolExecutor
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.Main = MainWindow
MainWindow.setObjectName("MainWindow")
#获取整个屏幕的大小
self.w = QApplication.desktop().width()
self.h = QApplication.desktop().height()
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.DispalyLabel = QtWidgets.QLabel(self.centralwidget)
self.DispalyLabel.setGeometry(QtCore.QRect(0, 0, 640, 480))
self.DispalyLabel.setText("")
self.DispalyLabel.setObjectName("label")
self.DispalyLabel.setScaledContents(True)
#设置为顶级窗口
self.DispalyLabel.setWindowFlags(QtCore.Qt.Window|QtCore.Qt.FramelessWindowHint)
self.DispalyLabel.showFullScreen()
self.vbox = QtWidgets.QVBoxLayout(self.centralwidget)
self.vbox.addWidget(self.DispalyLabel)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#MainWindow.showFullScreen()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
2.视频流显示部分(videoDisplay.py)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, sys
import cv2
import time
import json
import numpy as np
import threading
from PyQt5 import QtCore
from PyQt5.QtCore import QFile, QTimer
from PyQt5.QtWidgets import QFileDialog, QMessageBox
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView
from PyQt5.QtGui import QImage, QPixmap
from concurrent.futures import ThreadPoolExecutor
grun = True
stop = False
class DisplayMain:
def __init__(self, ui, mainWnd):
self.ui = ui
self.mainWnd = mainWnd
self.isCamera = True
self.isCamera = True
self.cap = None
self.frame_height = None
self.frame_width = None
self.fps = None
self.size = None
self.fourcc = None
self.Open()
# 创建一个关闭事件并设为未触发
self.stopEvent = threading.Event()
self.stopEvent.clear()
def Open(self):
self.cap = cv2.VideoCapture(0)
self.frame_height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
self.frame_width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
self.fps = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
#利用新的线程开启视频流
th1 = ThreadPoolExecutor(1)
th1.submit(self.Display)
def Display(self):
while self.cap.isOpened():
success, frame = self.cap.read()
# RGB转BGR
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
#中间可以放一些 *** 作视频的代码,比如放大、变换颜色或者放深度学习中提取目标的代码
frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = QImage(frame1, *(self.ui.w, self.ui.h), QImage.Format_RGB888).rgbSwapped()
img_temp = image.copy()
self.ui.DispalyLabel.setPixmap(QPixmap.fromImage(img_temp))
# 判断关闭事件是否已触发
if True == self.stopEvent.is_set():
# 关闭事件置为未触发,清空显示label
self.stopEvent.clear()
self.ui.DispalyLabel.clear()
self.ui.Close.setEnabled(False)
self.ui.Open.setEnabled(True)
self.cap.release()
break
3.程序入口(main.py,运行这个函数启动程序)
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import sys
import DisplayUI
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QMessageBox
from VideoDisplay import DisplayMain
def mainUse():
app = QApplication(sys.argv)
mainWnd = QMainWindow()
ui = DisplayUI.Ui_MainWindow()
ui.setupUi(mainWnd)
mainWnd.setWindowTitle('视频流显示')
icon = QtGui.QIcon()
#这句是图标
#icon.addPixmap(QtGui.QPixmap('./pic/xxx.ico'), QtGui.QIcon.Normal, QtGui.QIcon.Off)
mainWnd.setWindowIcon(icon)
DisplayMain(ui, mainWnd)
mainWnd.show()
sys.exit(app.exec_())
if __name__ == '__main__':
mainUse()
提示:全屏以后要退出可以按alt + F4关闭界面
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)