当前位置:   article > 正文

Python、QT的人脸识别项目_cv::facedetectoryn::create

cv::facedetectoryn::create

项目效果图

在这里插入图片描述

人脸识别.py

核心代码

import cv2 
from csv import QUOTE_MINIMAL
from turtle import onclick
from PySide2 import QtWidgets, QtGui
from FaceRecognitionMain_ui import Ui_MainWindow

# 加载模型
fd_model_path = "face_recognizer_fast/yunet.onnx"
fr_model_path = "face_recognizer_fast/face_recognizer_fast.onnx"

address_image_1 = "address_image_1"
address_image_2 = "address_image_2"

class MyWidget(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.btn_add1.clicked.connect(self.on_clicked_add_1)
        self.ui.btn_add2.clicked.connect(self.on_clicked_add_2)
        self.ui.btn_distinguish_cos.clicked.connect(self.on_clicked_distinguish_cos)
        self.ui.btn_distinguish_l2.clicked.connect(self.on_clicked_distinguish_l2)
        
    def get_face_feature(address_image):
        src = cv2.imread(address_image)
        # print(src)
        # 创建人脸检测对象  模型  配制  尺寸
        faceDetector = cv2.FaceDetectorYN.create(fd_model_path, "", (src.shape[1], src.shape[0]))
        faces = faceDetector.detect(src) 
        print(faces)
        # 判断是否有人脸
        if faces[1] is None:
            print('------ 未发现人脸!')
            exit(0)
        faceRecognizer = cv2.FaceRecognizerSF.create(fr_model_path, "")
        align_face = faceRecognizer.alignCrop(src, faces[1][0])
        face_feature = faceRecognizer.feature(align_face)
        return face_feature

    def on_clicked_distinguish_cos(self):
        print("第一张图片的地址:",address_image_1)
        print("第二张图片的地址:",address_image_2)

        faceRecognizer = cv2.FaceRecognizerSF.create(fr_model_path, "")
        face_feature_1 = MyWidget.get_face_feature(address_image_1)
        face_feature_2 = MyWidget.get_face_feature(address_image_2)
        # 人脸匹配
        score = faceRecognizer.match(face_feature_1, face_feature_2)
        print("score = ", score)
        self.ui.label_4.setText(str(score))

        if score > 0.363:
            self.ui.label_result.setText("是同一个人!")
        else:
            self.ui.label_result.setText("不是同一个人!")

    def on_clicked_distinguish_l2(self):
        faceRecognizer = cv2.FaceRecognizerSF.create(fr_model_path, "")
        face_feature_1 = MyWidget.get_face_feature(address_image_1)
        face_feature_2 = MyWidget.get_face_feature(address_image_2)
        # L2匹配方式
        score = faceRecognizer.match(face_feature_1, face_feature_2,cv2.FaceRecognizerSF_FR_NORM_L2)
        print("score = ", score)
        self.ui.label_4.setText(str(score))
        if score < 1.128:
            self.ui.label_result.setText("是同一个人!")
        else:
            self.ui.label_result.setText("不是同一个人!")

    def on_clicked_add_1(self):
        filename = QtWidgets.QFileDialog.getOpenFileName(self)
        print("filename = ", filename)
        pix = QtGui.QPixmap()
        pix.load(filename[0])
        global address_image_1 
        address_image_1 = filename[0]
        self.ui.label_image_1.setPixmap(pix)

    def on_clicked_add_2(self):
        filename = QtWidgets.QFileDialog.getOpenFileName(self)
        print("filename = ", filename)
        pix = QtGui.QPixmap()
        pix.load(filename[0])
        global address_image_2
        address_image_2 = filename[0]
        self.ui.label_image_2.setPixmap(pix)

def main():
    app = QtWidgets.QApplication([])
    w = MyWidget()
    w.show()
    app.exec_()

if __name__ == "__main__":
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

FaceRecognitionMain_ui.py

== 这个文件是你通过Python自带的QT设计工具设计了一个后缀为.ui的文件后,通过编译自动生成的文件 ==

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

################################################################################
## Form generated from reading UI file 'FaceRecognitionMain.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.label_image_1 = QLabel(self.centralwidget)
        self.label_image_1.setObjectName(u"label_image_1")
        self.label_image_1.setGeometry(QRect(70, 50, 250, 250))
        self.label_image_1.setFrameShape(QFrame.Box)
        self.label_image_2 = QLabel(self.centralwidget)
        self.label_image_2.setObjectName(u"label_image_2")
        self.label_image_2.setGeometry(QRect(510, 50, 250, 250))
        self.label_image_2.setFrameShape(QFrame.Box)
        self.btn_add1 = QPushButton(self.centralwidget)
        self.btn_add1.setObjectName(u"btn_add1")
        self.btn_add1.setGeometry(QRect(130, 330, 121, 51))
        font = QFont()
        font.setPointSize(15)
        self.btn_add1.setFont(font)
        self.btn_add2 = QPushButton(self.centralwidget)
        self.btn_add2.setObjectName(u"btn_add2")
        self.btn_add2.setGeometry(QRect(580, 330, 131, 51))
        self.btn_add2.setFont(font)
        self.label_3 = QLabel(self.centralwidget)
        self.label_3.setObjectName(u"label_3")
        self.label_3.setGeometry(QRect(90, 440, 90, 40))
        self.label_3.setFont(font)
        self.btn_distinguish_cos = QPushButton(self.centralwidget)
        self.btn_distinguish_cos.setObjectName(u"btn_distinguish_cos")
        self.btn_distinguish_cos.setGeometry(QRect(350, 260, 121, 51))
        self.btn_distinguish_cos.setFont(font)
        self.label_result = QLabel(self.centralwidget)
        self.label_result.setObjectName(u"label_result")
        self.label_result.setGeometry(QRect(280, 490, 221, 51))
        self.label_result.setFont(font)
        self.label_result.setFrameShape(QFrame.NoFrame)
        self.label_4 = QLabel(self.centralwidget)
        self.label_4.setObjectName(u"label_4")
        self.label_4.setGeometry(QRect(180, 440, 531, 40))
        self.label_4.setFont(font)
        self.btn_distinguish_l2 = QPushButton(self.centralwidget)
        self.btn_distinguish_l2.setObjectName(u"btn_distinguish_l2")
        self.btn_distinguish_l2.setGeometry(QRect(350, 330, 121, 51))
        self.btn_distinguish_l2.setFont(font)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
        self.label_image_1.setText("")
        self.label_image_2.setText("")
        self.btn_add1.setText(QCoreApplication.translate("MainWindow", u"\u6dfb\u52a0\u56fe\u7247", None))
        self.btn_add2.setText(QCoreApplication.translate("MainWindow", u"\u6dfb\u52a0\u56fe\u7247", None))
        self.label_3.setText(QCoreApplication.translate("MainWindow", u"\u76f8\u4f3c\u5ea6:", None))
        self.btn_distinguish_cos.setText(QCoreApplication.translate("MainWindow", u"\u4f59\u5f26\u5339\u914d", None))
        self.label_result.setText("")
        self.label_4.setText("")
        self.btn_distinguish_l2.setText(QCoreApplication.translate("MainWindow", u"L2\u5339\u914d", None))
    # retranslateUi


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/116189?site
推荐阅读
相关标签
  

闽ICP备14008679号