python 封装dlib模型进行人脸识别系统的登录认证

1、直接上干货

#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import dlib
import numpy as np
class faceDiscernModel:
    def __init__(self):
        # 加载预训练人脸检测CNN模型
        self.cnn_face_model = "./model/mmod_human_face_detector.dat"
        self.cnn_face_detector = dlib.cnn_face_detection_model_v1(self.cnn_face_model)

        # 加载人脸特征检测模型
        self.predictor_path = "./model/shape_predictor_5_face_landmarks.dat"
        self.predictor = dlib.shape_predictor(self.predictor_path)

        # 加载人脸特征提取模型
        self.featureModel = "./model/dlib_face_recognition_resnet_model_v1.dat"
        self.feater = dlib.face_recognition_model_v1(self.featureModel)

    def get_faces_feature(self,imgPath):

        # 读取人脸图片
        img = dlib.load_rgb_image(imgPath)

        feat = []

        # 检测每个人脸的边界框
        dets = self.cnn_face_detector(img, 1)

        # len(dets) 是检测到的人脸数量
        for i, d in enumerate(dets):
            # print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(
            #     i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence))

            # 检测 box i 内的人脸关键点
            shape = self.predictor(img, d.rect)

            # 计算特征向量
            face_descriptor = self.feater.compute_face_descriptor(img, shape)

            feat.append(face_descriptor)

        return feat

    def Euclidean_distance_test(self,feature_1,feature_2):
        return np.sqrt(np.sum((np.array(feature_1)-np.array(feature_2))**2))

    def face_compare(self,imgPath_1,imgPath_2,assess=0.6):
        feature_1 = self.get_faces_feature(imgPath_1)
        feature_2 = self.get_faces_feature(imgPath_2)
        score = self.Euclidean_distance_test(feature_1=feature_1,feature_2=feature_2)
        if score > assess:
            return False
        elif 0< score < assess:
            return True
        else:
            return False


if __name__ == '__main__':

    imgPath_1 = "./faceImage/test.jpg"
    imgPath_2 = "./faceImage/test1.jpg"
    faceDiscern = faceDiscernModel()
    st = time.time()
    result = faceDiscern.face_compare(imgPath_1,imgPath_2)

    print(time.time()-st)
    print(result)

2、模型下载地址

  http://dlib.net/files/

转载于:https://www.cnblogs.com/wuzaipei/p/11031760.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值