神经网络人脸识别

采用深度学习的模型实现人脸识别

测试图picture1
在这里插入图片描述
picture2
在这里插入图片描述

识别图test
在这里插入图片描述

模型:

shape_predictor_68_face_landmarks.dat
dlib_face_recognition_resnet_model_v1.dat模型

##代码


import numpy as np
import cv2
import dlib
import matplotlib.pyplot as plt


def show_image(image,title):
    img_RGB=image[:,:,::-1]
    plt.title(title)
    plt.imshow(img_RGB)
    plt.axis("off")

def compare_faces(face_encoding,test_encoding):
    return list(np.linalg.norm(np.array(face_encoding)-np.array(test_encoding),axis=1))


def encoder_face(image,detector,predictor,encoder,upsample=1,jet=1):
    faces=detector(image,upsample)
    faces_keypoints=[predictor(image,face) for face in faces]
    return [np.array(encoder.compute_face_descriptor(image,face_keypoint,jet))for face_keypoint in faces_keypoints]

def compare_faces_order(face_encoding,test_encoding,names):
    distance=list(np.linalg.norm(np.array(face_encoding)-np.array(test_encoding),axis=1))
    return zip(*sorted(zip(distance,names)))

def plot_rectangle(image,faces,name):
    for face in faces:
        cv2.putText(image, name, (face.left(), face.top()-30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
        cv2.rectangle(image,(face.left(),face.top()),(face.right(),face.bottom()),(255,0,0),4)
        return image


def main():
    img1=cv2.imread("picture1.jpg")
    img2=cv2.imread("picture2.jpg")

    test=cv2.imread("test.jpg")
    img1=img1[:,:,::-1]
    img2=img2[:,:,::-1]
    test=test[:,:,::-1]
    img_names=["picture1","picture2"]
    detector=dlib.get_frontal_face_detector()
    predictor=dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    encoder=dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")

    img1_128=encoder_face(img1,detector,predictor,encoder)[0]
    img2_128=encoder_face(img2,detector,predictor,encoder)[0]
    test_128=encoder_face(test,detector,predictor,encoder)[0]

    zong=[img1_128,img2_128]
    distance=compare_faces(zong,test_128)
    print(distance)

    distance,name=compare_faces_order(zong,test_128,img_names)
    print(name)

    img = cv2.imread("test.jpg")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    detector = dlib.get_frontal_face_detector()
    dets_result = detector(gray, 1)
    img_result = plot_rectangle(img.copy(), dets_result,name[0])

    plt.figure(figsize=(9, 6))

    show_image(img_result,"result")

    plt.show()


if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值