【專案】Python人臉識別(GUI介面)—— 基於pyopencv

專注的阿熊發表於2022-10-26

from PyQt5 import QtCore, QtGui, QtWidgets

import cv2

class Ui_MainWindow(QtWidgets.QMainWindow):

     def __init__(self):

         super(Ui_MainWindow,self).__init__()

     def setupUi(self, MainWindow):

         MainWindow.setObjectName("MainWindow")

         MainWindow.resize(565, 331)

         self.centralwidget = QtWidgets.QWidget(MainWindow)

         self.centralwidget.setObjectName("centralwidget")

         self.pushButton = QtWidgets.QPushButton(self.centralwidget)

         self.pushButton.setGeometry(QtCore.QRect(70, 190, 111, 61))

         self.pushButton.setObjectName("pushButton")

         self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)

         self.pushButton_2.setGeometry(QtCore.QRect(310, 190, 121, 61))

         self.pushButton_2.setObjectName("pushButton_2")

         self.label = QtWidgets.QLabel(self.centralwidget)

         self.label.setGeometry(QtCore.QRect(180, 60, 161, 81))

         self.label.setObjectName("label")

         MainWindow.setCentralWidget(self.centralwidget)

         self.menubar = QtWidgets.QMenuBar(MainWindow)

         self.menubar.setGeometry(QtCore.QRect(0, 0, 565, 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.pushButton.setText(_translate("MainWindow", " 採集人臉資訊 "))

         self.pushButton_2.setText(_translate("MainWindow", " 開始識別 "))

         # self.label.setText(_translate("MainWindow", " 結果: "))

         self.pushButton.clicked.connect(self.b)

         self.pushButton_2.clicked.connect(self.final)

     def b(self):

         print(' 正在呼叫攝像頭! ')

         print(" 輸入 'esc' 為退出!!! ")

         faceCascade = 跟單網gendan5.comcv2.CascadeClassifier('haarcascade_frontalface_default.xml')

         cap = cv2.VideoCapture(0)

         cap.set(3, 640)  # set Width

         cap.set(4, 480)  # set Height

         print(" 請輸入字母 's' 儲存資訊!! ")

         while True:

             ret, img = cap.read()

             # 將彩色圖轉為灰度圖

             gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

             faces = faceCascade.detectMultiScale(

                 gray,

                 scaleFactor=1.2,

                 minNeighbors=5

                 ,

                 minSize=(20, 20)

             )

             for (x, y, w, h) in faces:

                 cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

                 roi_gray = gray[y:y + h, x:x + w]

                 roi_color = img[y:y + h, x:x + w]

             cv2.imshow('video', img)

             k = cv2.waitKey(30) & 0xff

             if k == ord('s'):

                 n = input(' 請輸入編號: ')

                 cv2.imwrite('./data/jm/' + n + '.jpg', roi_gray)

             if k == 27:

                 break

         cap.release()

         cv2.destroyAllWindows()

         print(" 正在訓練!!! ")

         self.train()

     def final(self):

         import face_zhineng.final_face

         face_zhineng.final_face.a()

     def train(self):

         import cv2

         import numpy as np

         import face_zhineng.training

         # 圖片路徑

         path = './data/jm/'

         faces, ids = face_zhineng.training.getImageAndLabels(path)

         # 獲取訓練物件

         recognizer = cv2.face.LBPHFaceRecognizer_create()

         recognizer.train(faces, np.array(ids))

         # 儲存檔案

         recognizer.write('trainer/trainer3.yml')

         print(" 訓練完畢!!! ")

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_())


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2920374/,如需轉載,請註明出處,否則將追究法律責任。

相關文章