PyQt4设计UI界面并添加简单功能【python+pyqt4+opencv】

时间:2021-08-26 23:01:16

学习python的过程中,了解到PyQt,加上先前用过OpenCV,自然就有了用PyQt设计UI界面,调用OpenCV函数实现功能的想法。

步骤:

1.  Qt designer 快速实现UI界面,并转换成 .py文件。

  PyQt4设计UI界面并添加简单功能【python+pyqt4+opencv】

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'client.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(683, 417)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(150, 70, 81, 31))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayoutWidget = QtGui.QWidget(Dialog)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(100, 110, 181, 151))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.pushButton_3 = QtGui.QPushButton(self.gridLayoutWidget)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.gridLayout.addWidget(self.pushButton_3, 1, 0, 1, 1)
        self.pushButton_2 = QtGui.QPushButton(self.gridLayoutWidget)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.gridLayout.addWidget(self.pushButton_2, 0, 0, 1, 1)
        self.pushButton_4 = QtGui.QPushButton(self.gridLayoutWidget)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.gridLayout.addWidget(self.pushButton_4, 2, 0, 1, 1)
        self.pushButton_5 = QtGui.QPushButton(Dialog)
        self.pushButton_5.setGeometry(QtCore.QRect(540, 310, 81, 31))
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        self.lcdNumber = QtGui.QLCDNumber(Dialog)
        self.lcdNumber.setGeometry(QtCore.QRect(40, 360, 64, 23))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("新宋体"))
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.lcdNumber.setFont(font)
        self.lcdNumber.setObjectName(_fromUtf8("lcdNumber"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL(_fromUtf8("clicked()")), Dialog.close)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Client", None))
        self.pushButton.setText(_translate("Dialog", "功能选择", None))
        self.pushButton_3.setText(_translate("Dialog", "Face Detection", None))
        self.pushButton_2.setText(_translate("Dialog", "Show Picture", None))
        self.pushButton_4.setText(_translate("Dialog", "Eye Detetction", None))
        self.pushButton_5.setText(_translate("Dialog", "退出", None))


2.  OpenCV实现功能。(这里以显示图片 ,人脸检测 和眼睛检测为例)

显示图片:

import cv2

filename = './painting.jpg'


def showPicture():
    img = cv2.imread(filename)
    cv2.namedWindow('Show Pictures')
    cv2.imshow('Show Pictures', img)
    if cv2.waitKey() == 27 & 0xff == ord("q"):
        cv2.destroyAllWindows()


if __name__ == '__main__':
   showPicture()

人脸检测:

import cv2


def faceDetect():
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    camera = cv2.VideoCapture(0)
    while (True):
        ret, frame = camera.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        cv2.imshow('VideoFaceDetect', frame)
        k = cv2.waitKey(1)
        if k == ord("q"):
                break  camera.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    faceDetect()

人眼检测:

import cv2


def eyeDetect():
    eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
    camera = cv2.VideoCapture(0)
    while (True):
        ret, frame = camera.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        eyes = eye_cascade.detectMultiScale(gray, 1.3, 5, 0, (40, 40))
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
        cv2.imshow('VideoFaceDetect', frame)
        k = cv2.waitKey(1)
        if k == ord("q"):
                break  camera.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    eyeDetect()

3.  二者结合

# -*- coding: utf-8 -*-

from showpicture import *
from facedetect import *
from eyedetect import *
from client import *
import sys

class MyClient(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        _timer = QtCore.QTimer(self)
        _timer.timeout.connect(self._showtime)
        _timer.start()
        self._showtime()

        QtCore.QObject.connect(self.ui.pushButton_2, QtCore.SIGNAL("clicked()"), self._showpicture)
        QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL('clicked()'), self._facedetect)
        QtCore.QObject.connect(self.ui.pushButton_4, QtCore.SIGNAL('clicked()'), self._eyedetect)

    def _showtime(self):
        _time = QtCore.QTime.currentTime()
        _text = _time.toString("hh:mm")
        self.ui.lcdNumber.display(_text)

    def _showpicture(self):
        showPicture()

    def _facedetect(self):
        faceDetect()

    def _eyedetect(self):
        eyeDetect()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    my = MyClient()
    my.show()
    sys.exit(app.exec_())


这个project小且简单,代码也不够规范,主要是将自己的思路实现