我尝试制作一个带有视频的GUI应用程序(来自机械压缩测试的视频),应用一些图像处理并实时给出绘图结果和机械系数(泊松系数)的值。 我已经设法在PYQT5中使用上传和启动按钮以及视频处理的结果。我现在正在尝试做的事情并没有管理,就是同时绘制计算结果。
我的架构代码如下:
以上是我的代码uiQT和MainQT,如果有人愿意帮助我。 我尝试过几个教程,(here,here,and here),但似乎没有任何效果。
uiQT.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from stylesheet import colors, dimmension, ressources
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
Mainx=dimmension["MainWinSize"][0]
Mainy=dimmension["MainWinSize"][1]
left = (int(QtWidgets.QDesktopWidget().screenGeometry().width()) - Mainx) /2
top = (int(QtWidgets.QDesktopWidget().screenGeometry().height()) - Mainy) /2
MainWindow.setGeometry(left, top, Mainx, Mainy)
#Main Centralwidget
MainWindow.setStyleSheet(colors["MainBackGr"])
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setStyleSheet(colors["EmptyBackGr"])
self.centralwidget.setObjectName("centralwidget")
#button to upload video
self.browse_button = QtWidgets.QPushButton(self.centralwidget)
self.browse_button.setIcon(QtGui.QIcon(ressources["UploadBtnNormal"]))
self.browse_button.setIconSize(QtCore.QSize(dimmension["UploadBtnSize"][0],dimmension["UploadBtnSize"][1]),)
self.browse_button.setGeometry(QtCore.QRect(dimmension["UploadBtnposition"][0],dimmension["UploadBtnposition"][1],dimmension["UploadBtnposition"][2],dimmension["UploadBtnposition"][3]))
self.browse_button.setObjectName("browse_button")
#button to launch processing
self.go_button = QtWidgets.QPushButton(self.centralwidget)
self.go_button.setIcon(QtGui.QIcon(ressources["GoBtninitial"]))
self.go_button.setIconSize(QtCore.QSize(dimmension["UploadBtnSize"][0],dimmension["UploadBtnSize"][1]),)
self.go_button.setGeometry(QtCore.QRect(QtCore.QRect(dimmension["LaunchBtnNormalPosition"][0],dimmension["LaunchBtnNormalPosition"][1],dimmension["LaunchBtnNormalPosition"][2],dimmension["LaunchBtnNormalPosition"][3])))
self.go_button.setStyleSheet(colors["EmptyBackGr"])
self.go_button.setObjectName("GO_button")
#Area that contains video rendering
self.render_video = QtWidgets.QLabel(self.centralwidget)
self.render_video.setGeometry(QtCore.QRect(dimmension["render_video"][0],dimmension["render_video"][1],dimmension["render_video"][2],dimmension["render_video"][3] ) )
self.render_video.setStyleSheet(colors["VideoBackGr"])
self.render_video.setText("")
self.render_video.setScaledContents(True)
self.render_video.setObjectName("render_video")
MainWindow.setCentralWidget(self.centralwidget)
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", "Coefficient_poisson"))
self.go_button.setText(_translate("MainWindow", "GO_button"))
mainQT.py
import sys
import cv2
import os
from stylesheet import colors, dimmension, ressources
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QFileDialog
from PyQt5.QtGui import QPixmap,QImage
from uiQT import *
from uiQT import Ui_MainWindow
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
playvid = True
class form(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.browse_button.clicked.connect(self.SelectFile)
self.ui.go_button.clicked.connect(self.start_processing)
self.show()
def SelectFile(self):
self.fname = QFileDialog.getOpenFileName(None, 'Open Video file',"*.avi;*mp4", os.getenv('HOME'))
self.fname=str(self.fname[0])
print(self.fname)
#check file extension
if (os.path.splitext(self.fname)[1] == ".avi") or (os.path.splitext(self.fname)[1] == ".mp4"):
print("file valid")
self.ui.go_button.setIcon(QtGui.QIcon(ressources["GoBtnready"]))
self.ui.browse_button.setIcon(QtGui.QIcon(ressources["UploadBtnOk"]))
else:
QMessageBox.critical(self, "ERREUR", "Le format n'est pas valide, choisissez une vidéo")
def start_processing(self):
self.c = cv2.VideoCapture(self.fname)
if playvid == True:
print("PLAYVIDTRUE")
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.load_vid)
self.timer.start(100)
self.timerId=self.timer.timerId()
else: print("PLAYVID FALSE")
def load_vid(self):
print (self.c.grab())
print(self.c.get(cv2.CAP_PROP_POS_FRAMES),self.c.get(cv2.CAP_PROP_FRAME_COUNT))
while True:
if self.c.grab():
print(self.c.get(cv2.CAP_PROP_POS_FRAMES),self.c.get(cv2.CAP_PROP_FRAME_COUNT))
ret, frame = self.c.read()
height, width, bytesPerComponent = frame.shape
bytesPerLine = 3 * width
cv2.cvtColor(frame, cv2.COLOR_BGR2RGB, frame)
QImg = QImage(frame.data, width, height, bytesPerLine,QImage.Format_RGB888)
pixmap = QPixmap.fromImage(QImg)
self.ui.render_video.setPixmap(pixmap)
cv2.namedWindow("video",CV_GUI_NORMAL)
cv2.resizeWindow('video', 1,1)
cv2.imshow('video', frame)
#get value of frames
global currentframe
currentframe=self.c.get(cv2.CAP_PROP_POS_FRAMES)
totalFrames= self.c.get(cv2.CAP_PROP_FRAME_COUNT)
print(currentframe,totalFrames,"\n")
if currentframe+5>totalFrames:
print("last frame achieve")
global playvid
playvid= False
print(playvid)
cv2.destroyAllWindows()
self.c.release()
# quit timer when last frames (+/- 5 five frames,depends on frame rate and timer value)
if self.timer.isActive:
self.timer.disconnect()
self.timer.stop()
if __name__ == '__main__':
app = QApplication(sys.argv)
fm = form()
sys.exit(app.exec_())
GUI呈现
当前呈现如下
如何在我的GUI中启用动态绘图? (理想情况下,与我的CV2处理同时)没有修改我的实际代码,尽可能简单,QPIXmap QVboxlayout? 非常感谢