使用pyqt5展示基于YOLOv5的目标检测结果
本界面根据PyQt5 实现视频播放器_Taro的成长经历-CSDN博客_pyqt5播放视频提供的源代码改编制作。
videoDetect_pane.py:
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtMultimediaWidgets import QVideoWidget
from resource.videoDetect import Ui_MainWindow
from resource.myVideoWidget import myVideoWidget
import sys
from PyQt5 import QtCore,QtGui
class videoDetectionPane(Ui_MainWindow, QMainWindow):
returnHome_signal = pyqtSignal()
def __init__(self):
super(Ui_MainWindow, self).__init__()
self.setupUi(self)
self.setWindowTitle("基层公安绩效评价工具 视频检测")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("resource/images/police.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.setWindowIcon(icon)
self.setIconSize(QtCore.QSize(40, 40))
self.sld_video_pressed=False #判断当前进度条识别否被鼠标点击
self.videoFullScreen = False # 判断当前widget是否全屏
self.videoFullScreenWidget = myVideoWidget() # 创建一个全屏的widget
self.player1 = QMediaPlayer()
self.player2 = QMediaPlayer()
self.player1.setVideoOutput(self.wgt_video) # 视频播放输出的widget,就是上面定义的
self.player2.setVideoOutput(self.wgt_video_2)
self.btn_open.clicked.connect(self.openVideoFile) # 打开视频文件按钮
self.btn_play.clicked.connect(self.playVideo) # play
self.btn_stop.clicked.connect(self.pauseVideo) # pause
self.btn_cast.clicked.connect(self.castVideo) # 视频截图
self.player1.positionChanged.connect(self.changeSlide) # change Slide
self.player2.positionChanged.connect(self.changeSlide)
self.videoFullScreenWidget.doubleClickedItem.connect(self.videoDoubleClicked) #双击响应
self.wgt_video.doubleClickedItem.connect(self.videoDoubleClicked) #双击响应
self.sld_video.setTracking(False)
self.sld_video.sliderReleased.connect(self.releaseSlider)
self.sld_video.sliderPressed.connect(self.pressSlider)
self.sld_video.sliderMoved.connect(self.moveSlider) # 进度条拖拽跳转
self.sld_video.ClickedValue.connect(self.clickedSlider) # 进度条点击跳转
self.sld_audio.valueChanged.connect(self.volumeChange) # 控制声音播放
self.pushButton.clicked.connect(self.returnHome) # 返回首页
self.btn_cast.hide()
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("resource/images/播放1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_play.setIcon(icon1)
self.btn_play.setIconSize(QtCore.QSize(30, 30))
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("resource/images/选择.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_open.setIcon(icon2)
self.btn_open.setIconSize(QtCore.QSize(30, 30))
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("resource/images/返回首页.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon4)
self.pushButton.setIconSize(QtCore.QSize(30, 30))
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("resource/images/暂停.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_stop.setIcon(icon3)
self.btn_stop.setIconSize(QtCore.QSize(30, 30))
def castVideo(self):
screen = QGuiApplication.primaryScreen()
cast_jpg = './'+QDateTime.currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz")+'.jpg'
screen.grabWindow(self.wgt_video.winId()).save(cast_jpg)
def volumeChange(self, position):
volume= round(position/self.sld_audio.maximum()*100) # 音量
# print("vlume %f" %volume)
self.player1.setVolume(volume)
self.player2.setVolume(volume)
self.lab_audio.setText("volume:"+str(volume)+"%")
def clickedSlider(self, position):
if self.player1.duration() > 0: # 开始播放后才允许进行跳转
video_position = int((position / 100) * self.player1.duration())
self.player1.setPosition(video_position)
self.lab_video.setText("%.2f%%" % position)
else:
self.sld_video.setValue(0)
if self.player2.duration() > 0: # 开始播放后才允许进行跳转
video_position = int((position / 100) * self.player2.duration())
self.player2.setPosition(video_position)
self.lab_video.setText("%.2f%%" % position)
else:
self.sld_video.setValue(0)
def moveSlider(self, position):
self.sld_video_pressed = True
if self.player1.duration() > 0: # 开始播放后才允许进行跳转
video_position = int((position / 100) * self.player1.duration())
self.player1.setPosition(video_position)
self.lab_video.setText("%.2f%%" % position)
if self.player2.duration() > 0: # 开始播放后才允许进行跳转
video_position = int((position / 100) * self.player2.duration())
self.player2.setPosition(video_position)
self.lab_video.setText("%.2f%%" % position)
def pressSlider(self):
self.sld_video_pressed = True
print("pressed")
def releaseSlider(self):
self.sld_video_pressed = False
def changeSlide(self, position):
if not self.sld_video_pressed: # 进度条被鼠标点击时不更新
self.vidoeLength = self.player1.duration()+0.1
self.sld_video.setValue(round((position/self.vidoeLength)*100))
self.lab_video.setText("%.2f%%" % ((position/self.vidoeLength)*100))
def openVideoFile(self):
self.player1.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) # 选取视频文件
self.player1.play() # 播放视频
self.player2.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))
self.player2.play()
print(self.player1.availableMetaData())
print(self.player2.availableMetaData())
def playVideo(self):
self.player1.play()
self.player2.play()
def pauseVideo(self):
self.player1.pause()
self.player2.pause()
def videoDoubleClicked(self, text):
if self.player1.duration() > 0: # 开始播放后才允许进行全屏操作
if self.videoFullScreen:
self.player1.setVideoOutput(self.wgt_video)
self.player2.setVideoOutput(self.wgt_video_2)
self.videoFullScreenWidget.hide()
self.videoFullScreen = False
else:
self.videoFullScreenWidget.show()
self.player1.setVideoOutput(self.videoFullScreenWidget)
self.player2.setVideoOutput(self.videoFullScreenWidget)
self.videoFullScreenWidget.setFullScreen(1)
self.videoFullScreen = True
def returnHome(self):
self.returnHome_signal.emit()
if __name__ == '__main__':
app = QApplication(sys.argv)
vieo_gui = videoDetectionPane()
vieo_gui.show()
sys.exit(app.exec_())
videoDetect.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'videoDetect.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1082, 584)
MainWindow.setStyleSheet("#MainWindow{\n"
" background-color: rgb(0, 0, 127, 0.5);\n"
"}")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setStyleSheet("#centralwidget{\n"
"background-color: rgb(0, 0, 127, 0.3);\n"
"}")
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.lab_video = QtWidgets.QLabel(self.centralwidget)
self.lab_video.setMaximumSize(QtCore.QSize(50, 50))
self.lab_video.setStyleSheet("color: rgb(255, 255, 255);\n"
"font: 11pt \"Arial\";")
self.lab_video.setObjectName("lab_video")
self.gridLayout.addWidget(self.lab_video, 2, 2, 1, 1)
self.sld_video = myVideoSlider(self.centralwidget)
self.sld_video.setMinimumSize(QtCore.QSize(410, 0))
self.sld_video.setMaximumSize(QtCore.QSize(16777215, 20))
self.sld_video.setMaximum(100)
self.sld_video.setOrientation(QtCore.Qt.Horizontal)
self.sld_video.setObjectName("sld_video")
self.gridLayout.addWidget(self.sld_video, 2, 0, 1, 2)
self.wgt_video_2 = myVideoWidget(self.centralwidget)
self.wgt_video_2.setMinimumSize(QtCore.QSize(410, 200))
self.wgt_video_2.setMaximumSize(QtCore.QSize(16777215, 16777215))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
self.wgt_video_2.setPalette(palette)
self.wgt_video_2.setAutoFillBackground(True)
self.wgt_video_2.setObjectName("wgt_video_2")
self.gridLayout.addWidget(self.wgt_video_2, 1, 1, 1, 2)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setStyleSheet("font: 20pt \"隶书\";\n"
"color: rgb(255, 255, 255);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.wgt_video = myVideoWidget(self.centralwidget)
self.wgt_video.setMinimumSize(QtCore.QSize(410, 200))
self.wgt_video.setMaximumSize(QtCore.QSize(16777215, 16777215))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
self.wgt_video.setPalette(palette)
self.wgt_video.setAutoFillBackground(True)
self.wgt_video.setObjectName("wgt_video")
self.gridLayout.addWidget(self.wgt_video, 1, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setStyleSheet("font: 20pt \"隶书\";\n"
"color: rgb(255, 255, 255);")
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName("splitter")
self.btn_open = QtWidgets.QPushButton(self.splitter)
self.btn_open.setMinimumSize(QtCore.QSize(150, 50))
self.btn_open.setMaximumSize(QtCore.QSize(100, 25))
self.btn_open.setStyleSheet("QPushButton {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 38, 200, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:8px;\n"
"color:white;\n"
"spacing:20px;\n"
" font: 16pt \"楷体\";\n"
"}\n"
"QPushButton:hover {\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(147, 33, 250, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:pressed {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(250, 33, 80, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:disabled{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}")
self.btn_open.setObjectName("btn_open")
self.btn_play = QtWidgets.QPushButton(self.splitter)
self.btn_play.setMinimumSize(QtCore.QSize(150, 50))
self.btn_play.setMaximumSize(QtCore.QSize(100, 25))
self.btn_play.setStyleSheet("QPushButton {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 38, 200, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:8px;\n"
"color:white;\n"
"spacing:20px;\n"
" font: 16pt \"楷体\";\n"
"}\n"
"QPushButton:hover {\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(147, 33, 250, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:pressed {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(250, 33, 80, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:disabled{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}")
self.btn_play.setObjectName("btn_play")
self.btn_stop = QtWidgets.QPushButton(self.splitter)
self.btn_stop.setMinimumSize(QtCore.QSize(150, 50))
self.btn_stop.setMaximumSize(QtCore.QSize(100, 25))
self.btn_stop.setStyleSheet("QPushButton {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 38, 200, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:8px;\n"
"color:white;\n"
"spacing:20px;\n"
" font: 16pt \"楷体\";\n"
"}\n"
"QPushButton:hover {\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(147, 33, 250, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:pressed {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(250, 33, 80, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:disabled{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}")
self.btn_stop.setObjectName("btn_stop")
self.sld_audio = QtWidgets.QSlider(self.splitter)
self.sld_audio.setMinimumSize(QtCore.QSize(100, 50))
self.sld_audio.setMaximumSize(QtCore.QSize(150, 20))
self.sld_audio.setProperty("value", 99)
self.sld_audio.setOrientation(QtCore.Qt.Horizontal)
self.sld_audio.setObjectName("sld_audio")
self.lab_audio = QtWidgets.QLabel(self.splitter)
self.lab_audio.setStyleSheet("color: rgb(255, 255, 255);\n"
"font: 14pt \"Arial\";")
self.lab_audio.setAlignment(QtCore.Qt.AlignCenter)
self.lab_audio.setObjectName("lab_audio")
self.btn_cast = QtWidgets.QPushButton(self.splitter)
self.btn_cast.setMinimumSize(QtCore.QSize(150, 50))
self.btn_cast.setStyleSheet("QPushButton {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 38, 200, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:8px;\n"
"color:white;\n"
"spacing:20px;\n"
" font: 16pt \"楷体\";\n"
"}\n"
"QPushButton:hover {\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(147, 33, 250, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:pressed {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(250, 33, 80, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:disabled{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}")
self.btn_cast.setObjectName("btn_cast")
self.pushButton = QtWidgets.QPushButton(self.splitter)
self.pushButton.setMinimumSize(QtCore.QSize(50, 50))
self.pushButton.setMaximumSize(QtCore.QSize(50, 50))
self.pushButton.setStyleSheet("QPushButton {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 38, 200, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:8px;\n"
"color:white;\n"
"spacing:20px;\n"
" font: 16pt \"楷体\";\n"
"}\n"
"QPushButton:hover {\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(147, 33, 250, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:pressed {\n"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(250, 33, 80, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton:disabled{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}")
self.pushButton.setText("")
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.splitter, 3, 0, 1, 3)
self.gridLayout.setRowStretch(1, 10)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1082, 26))
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.lab_video.setText(_translate("MainWindow", "0%"))
self.label.setText(_translate("MainWindow", "检测前的视频"))
self.label_2.setText(_translate("MainWindow", "检测后的视频"))
self.btn_open.setText(_translate("MainWindow", "打开"))
self.btn_play.setText(_translate("MainWindow", "播放"))
self.btn_stop.setText(_translate("MainWindow", "暂停"))
self.lab_audio.setText(_translate("MainWindow", "volume:100%"))
self.btn_cast.setText(_translate("MainWindow", "截图"))
from resource.myVideoWidget import myVideoWidget
from resource.myvideoslider import myVideoSlider
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
myVideoWidget.py:
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import *
class myVideoWidget(QVideoWidget):
doubleClickedItem = pyqtSignal(str) # 创建双击信号
def __init__(self, parent=None):
super(QVideoWidget, self).__init__(parent)
def mouseDoubleClickEvent(self, QMouseEvent): #双击事件
self.doubleClickedItem.emit("double clicked")
myvideoslider.py:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QSlider
class myVideoSlider(QSlider):
ClickedValue = pyqtSignal(int)
def __init__(self, father):
super().__init__(Qt.Horizontal, father)
def mousePressEvent(self, QMouseEvent): #单击事件
super().mousePressEvent(QMouseEvent)
value = QMouseEvent.localPos().x()
# self.setValue(int(value)/9)
value = round(value/self.width()*self.maximum()) # 根据鼠标点击的位置和slider的长度算出百分比
self.ClickedValue.emit(value)