人脸识别python pyqt5_python+opencv+dlib+pyqt5人脸识别实践

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

import sys, os, numpy, cv2, dlib

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):

defsetupUi(self, Dialog):

Dialog.setObjectName("Dialog")

Dialog.resize(699, 300)

self.toolButton = QtWidgets.QToolButton(Dialog)

self.toolButton.setGeometry(QtCore.QRect(390, 10, 31, 21))

self.toolButton.setObjectName("toolButton")

self.pushButton = QtWidgets.QPushButton(Dialog)

self.pushButton.setGeometry(QtCore.QRect(460, 10, 75, 23))

self.pushButton.setObjectName("pushButton")

self.lineEdit = QtWidgets.QLineEdit(Dialog)

self.lineEdit.setGeometry(QtCore.QRect(130, 10, 251, 20))

self.lineEdit.setObjectName("lineEdit")

self.graphicsView = QtWidgets.QGraphicsView(Dialog)

self.graphicsView.setGeometry(QtCore.QRect(290, 41, 251, 241))

self.graphicsView.setObjectName("graphicsView")

self.label = QtWidgets.QLabel(Dialog)

self.label.setGeometry(QtCore.QRect(290, 41, 251, 241))

self.label.setObjectName("label")

self.label.setScaledContents(True) #label自适应图片大小

self.graphicsView_2 = QtWidgets.QGraphicsView(Dialog)

self.graphicsView_2.setGeometry(QtCore.QRect(10, 40, 256, 241))

self.graphicsView_2.setObjectName("graphicsView_2")

self.label_2 = QtWidgets.QLabel(Dialog)

self.label_2.setGeometry(QtCore.QRect(13, 41, 251, 241))

self.label_2.setObjectName("label_2")

self.label_2.setScaledContents(True) #label自适应图片大小

self.label_3 = QtWidgets.QLabel(Dialog)

self.label_3.setGeometry(QtCore.QRect(10, 10, 111, 21))

font = QtGui.QFont()

font.setFamily("Agency FB")

font.setPointSize(11)

self.label_3.setFont(font)

self.label_3.setObjectName("label_3")

self.label_4 = QtWidgets.QLabel(Dialog)

self.label_4.setGeometry(QtCore.QRect(550, 210, 81, 21))

font = QtGui.QFont()

font.setFamily("Agency FB")

font.setPointSize(11)

self.label_4.setFont(font)

self.label_4.setObjectName("label_4")

self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)

self.lineEdit_2.setGeometry(QtCore.QRect(550, 240, 141, 20))

font = QtGui.QFont()

font.setFamily("Agency FB")

font.setPointSize(11)

self.lineEdit_2.setFont(font)

self.lineEdit_2.setObjectName("lineEdit_2")

self.retranslateUi(Dialog)

QtCore.QMetaObject.connectSlotsByName(Dialog)

defretranslateUi(self, Dialog):

_translate = QtCore.QCoreApplication.translate

Dialog.setWindowTitle(_translate("Dialog", "龟速人脸识别demo"))

self.toolButton.setText(_translate("Dialog", "..."))

self.pushButton.setText(_translate("Dialog", "开始识别"))

self.lineEdit.setText(_translate("Dialog", "E:/"))

self.label.setText(_translate("Dialog", "识别结果"))

self.label_2.setText(_translate("Dialog", "待测人脸"))

self.label_3.setText(_translate("Dialog", "待测人照片路径:"))

self.label_4.setText(_translate("Dialog", "识别结果:"))

class Myshow(QtWidgets.QWidget, Ui_Dialog):

def__init__(self):

super(Myshow, self).__init__()

self.setupUi(self)

self.pushButton.clicked.connect(self.Recognition)

self.toolButton.clicked.connect(self.ChoosePath)

self.predictor_path = 'shape_predictor_68_face_landmarks.dat'

self.face_rc_model_path = 'dlib_face_recognition_resnet_model_v1.dat'

self.face_folder_path = 'E:\candidate_face'

self.name_list = os.listdir(self.face_folder_path)

self.descriptors = numpy.load('vectors.npy')

# dlib方法检测人脸

# self.detector = dlib.get_frontal_face_detector()

# opencv方法检测人脸

self.face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface_improved.xml')

self.feature_point = dlib.shape_predictor(self.predictor_path)

self.feature_model = dlib.face_recognition_model_v1(self.face_rc_model_path)

self.test_path = 'E:/'

defChoosePath(self):

file_name = QtWidgets.QFileDialog.getOpenFileName(self, "open file dialog", self.test_path, "图片(*.jpg)")

print(file_name[0])

self.test_path = file_name[0]

self.lineEdit.setText(self.test_path)

self.label_2.setPixmap(QtGui.QPixmap(self.test_path)) #显示待测人脸图

# 清空不相关内容

self.label.clear()

self.lineEdit_2.clear()

defRecognition(self):

test_img = cv2.imread(self.test_path)

# dlib方法检测人脸

# dets = self.detector(test_img, 1)

# for k, d in enumerate(dets):

# shape = self.feature_point(test_img, d)

# test_feature = self.feature_model.compute_face_descriptor(test_img, shape)

# test_feature = numpy.array(test_feature)

# opencv方法检测人脸

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

dets = self.face_cascade.detectMultiScale(gray, 1.1, 6)

mark = 0

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

mark = 1

d = dlib.rectangle(numpy.long(x),numpy.long(y),numpy.long(x+w),numpy.long(y+h))

shape = self.feature_point(test_img, d)

test_feature = self.feature_model.compute_face_descriptor(test_img, shape)

test_feature = numpy.array(test_feature)

if mark == 1:

dist = []

count = 0

for i in self.descriptors:

dist_ = numpy.linalg.norm(i - test_feature)

print('%s : %f' % (self.name_list[count], dist_))

dist.append(dist_)

count += 1

min_dist = numpy.argmin(dist)

print('%s' % self.name_list[min_dist][:-4])

show_img_path = os.path.join(self.face_folder_path, self.name_list[min_dist])

self.label.setPixmap(QtGui.QPixmap(show_img_path)) #显示人脸识别结果图

self.lineEdit_2.setText(self.name_list[min_dist][:-4])

else :

self.lineEdit_2.setText('haven\'t find any people')

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)

w = Myshow()

w.show()

sys.exit(app.exec_())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值