利用pyqt5+opencv水位监测界面设计

界面UI设计

'''
coding: utf-8
date: 2018-9-14
author: Angus Cai
'''
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QFont

class Ui_TabWidget(object):

    def setupUi(self, TabWidget):
        TabWidget.setObjectName("TabWidget") #创建的是"TabWidget"
        TabWidget.resize(950, 600)
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab") #"第一个子窗口"
        self.pushButton = QtWidgets.QPushButton(self.tab)
        self.pushButton.setGeometry(QtCore.QRect(10, 30, 75, 25))
        self.pushButton.setObjectName("pushButton")

        self.pushButton_3 = QtWidgets.QPushButton(self.tab)
        self.pushButton_3.setGeometry(QtCore.QRect(10, 75, 75, 25))
        self.pushButton_3.setObjectName("pushButton_3")

        self.pushButton_5 = QtWidgets.QPushButton(self.tab)
        self.pushButton_5.setGeometry(QtCore.QRect(10,120, 75, 25))
        self.pushButton_5.setObjectName("pushButton_5")
        self.label = QtWidgets.QLabel(self.tab)
        self.label.setGeometry(QtCore.QRect(110, 30, 600, 450))
        self.label.setText("")
        self.label.setObjectName("label")

        self.label_3 = QtWidgets.QLabel(self.tab)
        self.label_3.setGeometry(QtCore.QRect(210, 500, 75, 25))
        self.label_3.setText("水位预测值:")
        self.label_3.setObjectName("label_3")

        self.label_5 = QtWidgets.QLabel(self.tab)
        self.label_5.setGeometry(QtCore.QRect(300, 500, 75, 25))
        self.label_5.setFont(QFont("Roman times",21))
        self.label_5.setObjectName("label_5")
        TabWidget.addTab(self.tab, "")

        self.tab1 = QtWidgets.QWidget() # "第二个子窗口"
        self.tab1.setObjectName("tab1")
        self.pushButton_2 = QtWidgets.QPushButton(self.tab1)
        self.pushButton_2.setGeometry(QtCore.QRect(10, 30, 75, 25))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_4 = QtWidgets.QPushButton(self.tab1)
        self.pushButton_4.setGeometry(QtCore.QRect(10, 75, 75, 25))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_6 = QtWidgets.QPushButton(self.tab1)
        self.pushButton_6.setGeometry(QtCore.QRect(10,170, 75, 25))
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(self.tab1)
        self.pushButton_7.setGeometry(QtCore.QRect(10,120, 75, 25))
        self.pushButton_7.setObjectName("pushButton_7")
        # self.pushButton_8 = QtWidgets.QPushButton(self.tab1)
        # self.pushButton_8.setGeometry(QtCore.QRect(10,220, 75, 25))
        # self.pushButton_8.setObjectName("pushButton_8")

        self.label_2 = QtWidgets.QLabel(self.tab1)
        self.label_2.setGeometry(QtCore.QRect(110, 30, 800, 450))
        self.label_2.setText("")
        self.label_2.setObjectName("label_2")

        self.label_4 = QtWidgets.QLabel(self.tab1)
        self.label_4.setGeometry(QtCore.QRect(210, 500, 75, 23))
        self.label_4.setText("水位预测值:")
        self.label_4.setObjectName("label_4")
        self.label_6 = QtWidgets.QLabel(self.tab1)
        self.label_6.setGeometry(QtCore.QRect(300, 500, 75, 23))
        self.label_6.setFont(QFont("Roman times",21))
        self.label_6.setObjectName("label_6")
        TabWidget.addTab(self.tab1, "")
        # self.tab_2 = QtWidgets.QWidget() #"第三个子窗口"
        # self.tab_2.setObjectName("tab_2")
        # TabWidget.addTab(self.tab_2, "")

        self.retranslateUi(TabWidget)
        TabWidget.setCurrentIndex(0)
        self.pushButton.clicked.connect(TabWidget.imageprocessing) #将按键与事件相连
        self.pushButton_2.clicked.connect(TabWidget.videoprocessing) 
        self.pushButton_3.clicked.connect(TabWidget.imagePredict)
        self.pushButton_4.clicked.connect(TabWidget.videoPredict)
        self.pushButton_5.clicked.connect(TabWidget.onButtonExit)
        self.pushButton_6.clicked.connect(TabWidget.onButtonExit)
        self.pushButton_7.clicked.connect(TabWidget.onButtonPause)
        # self.pushButton_8.clicked.connect(TabWidget.onButtonTerminate)
        QtCore.QMetaObject.connectSlotsByName(TabWidget)

    def retranslateUi(self, TabWidget):
        _translate = QtCore.QCoreApplication.translate
        TabWidget.setWindowTitle(_translate("TabWidget", "Water Level Detector"))
        self.pushButton.setText(_translate("TabWidget", "打开图片"))
        self.pushButton_2.setText(_translate("TabWidget", "打开视频"))
        self.pushButton_3.setText(_translate("TabWidget", "开始预测"))
        self.pushButton_4.setText(_translate("TabWidget", "开始预测"))
        self.pushButton_5.setText(_translate("TabWidget", "退出"))
        self.pushButton_6.setText(_translate("TabWidget", "退出"))
        self.pushButton_7.setText(_translate("TabWidget", "暂停/播放"))
        # self.pushButton_8.setText(_translate("TabWidget", "结束播放"))
        # self.pushButton_5.setFont(QFont("Roman times",21))
        TabWidget.setTabText(TabWidget.indexOf(self.tab), _translate("TabWidget", "Tab 1"))
        TabWidget.setTabText(TabWidget.indexOf(self.tab1), _translate("TabWidget", "Tab 2"))
        # TabWidget.setTabText(TabWidget.indexOf(self.tab_2), _translate("TabWidget", "页"))

逻辑功能实现

'''
coding: utf-8
date: 2018-9-14
author: Angus Cai
'''
import sys, cv2, time, os

from x import Ui_TabWidget

from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.QtWidgets import QFileDialog,QTabWidget 

from PyQt5.QtCore import QTimer, QThread, pyqtSignal, Qt

from PyQt5.QtGui import QPixmap, QImage

from PyQt5.QtWidgets import QLabel,QWidget


class mywindow(QTabWidget,Ui_TabWidget): #这个窗口继承了用QtDesignner 绘制的窗口

    def __init__(self):
        super(mywindow,self).__init__()
        self.isAnother = False
        self.setupUi(self)

    def videoprocessing(self):
        print("播放中。。。")
        print("isAnother", self.isAnother)
        global videoName #在这里设置全局变量以便在线程中使用
        if self.isAnother == False:
            videoName,videoType= QFileDialog.getOpenFileName(self, "打开视频",""," *.mp4;;*.avi;;All Files (*)")
            self.th = Thread(parent=None)
            self.th.changePixmap.connect(self.setImage)
            self.th.start()
            self.isAnother = True
        elif self.isAnother == True: # 解决再次打开选择一个视频,会导致两个视频帧交叉播放
            self.th.terminate()
            videoName,videoType= QFileDialog.getOpenFileName(self, "打开视频",""," *.mp4;;*.avi;;All Files (*)")
            self.th = Thread(parent=None)
            self.th.changePixmap.connect(self.setImage)
            self.th.start()

    def setImage(self, image):
        self.label_2.setPixmap(QPixmap.fromImage(image))
    def imageprocessing(self):
        print("hehe")
        imgName,imgType= QFileDialog.getOpenFileName(self,
                                    "打开图片",
                                    "",
                                    #" *.jpg;;*.png;;*.jpeg;;*.bmp")
                                    " *.jpg;;*.png;;*.jpeg;;*.bmp;;All Files (*)")

        #利用qlabel显示图片
        print(str(imgName))        
        png = QtGui.QPixmap(imgName).scaledToHeight(self.label.height())#适应设计label时的大小
        self.label.setPixmap(png)

    def imagePredict(self):
        self.label_5.setText("128mm")
        print("Predicting..............")

    def videoPredict(self):
        self.label_6.setText("128mm")
        print("Predicting..............")

    def onButtonExit(self):
        app.quit()
        print("成功退出!!!!")

    def onButtonPause(self):
        self.th.pause()
    # def onButtonTerminate(self):
    #     self.th.stop()

class Thread(QThread):#采用线程来播放视频

    def __init__(self, parent=None):
        super(Thread, self).__init__(parent)
        self.isRunning = True

    changePixmap = pyqtSignal(QtGui.QImage)
    def run(self):
        cap = cv2.VideoCapture(videoName)
        fps = cap.get(5)
        playingSpeed = float(1/fps)
        print(fps)
        print(videoName)
        while (1):
            if(cap.isOpened()==True and self.isRunning == True):
                ret, frame = cap.read()
                if ret:
                    rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    convertToQtFormat = QtGui.QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QImage.Format_RGB888)#在这里可以对每帧图像进行处理,
                    p = convertToQtFormat.scaled(800, 450, Qt.KeepAspectRatio)
                    self.changePixmap.emit(p)
                    # time.sleep(0.03) #控制视频播放的速度
                    time.sleep(playingSpeed)
                else:
                    break
            else:
                continue
    #     cap.release()
    def pause(self):
        if(self.isRunning == True):
            self.isRunning = False
            print("暂停播放")
        elif(self.isRunning == False):
            self.isRunning = True
            print("继续播放")


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = mywindow()
    window.show()
    sys.exit(app.exec_())

界面展示

图片分析

图片分析

视频分析

1)视频播放采用opencv读取视频帧率,自然播放;2)可以暂停/播放;3)支持再次打开新的视频文件。

视频分析