深度学习实践-使用faceNet 进行人脸识别

捕风

1.下载facenet模型

facenet需要经过大量的运算,所以直接使用别人训练过的权重
链接:https://pan.baidu.com/s/1CGyz...
提取码:pew0

2. 使用的工具

1.    facenet 预训练模型,用于获取人脸特征
2.    mtcnn 用于人脸检测并返回对应参数

3. 代码实现

1. 加载模型

from keras.models import load_model
from mtcnn import MTCNN
# 加载mtcnn
dector = MTCNN()
# 加载facenet模型
model = load_model('facenet_keras.h5')

2.根据图片检测人脸

def extract_face(detector, file_name, required_size=(160, 160)):
    # detector:mtcnn 对象,file_name:图片路径
    # 加载图片
    image = Image.open(file_name)
    # 将图片转换彩色
    image = image.convert('RGB')
    # 将图片转换为 array
    pixels = asarray(image)
    # 使用 mtcnn 检测人脸
    results = detector.detect_faces(pixels)
    # 返回人脸位置信息
    x1, y1, width, height = results[0]['box']
    x1, y1 = abs(x1), abs(y1)
    x2, y2 = x1 + width, y1 + height
    # 从图片剪裁人脸
    face = pixels[y1:y2, x1:x2]
    # 将剪裁的人脸矩阵化
    image = Image.fromarray(face)
    # 调整检测人脸图片大小
    image = image.resize(required_size)
    # 调整矩阵形状
    face_array = asarray(image).reshape(1, 160, 160, 3)
    return face_array

3.使用facenet 提取人脸特征

def pre_process(x):
    if x.ndim == 4:
        axis = (1, 2, 3)
        size = x[0].size
    elif x.ndim == 3:
        axis = (0, 1, 2)
        size = x.size
    else:
        raise ValueError('Dimension should be 3 or 4')

    mean = np.mean(x, axis=axis, keepdims=True)
    std = np.std(x, axis=axis, keepdims=True)
    std_adj = np.maximum(std, 1.0 / np.sqrt(size))
    y = (x - mean) / std_adj
    return y
def get_embedding(model, img):
    face_img = pre_process(img)
    pre = model.predict(face_img)
    pre = l2_normalize(np.concatenate(pre))
    pre = np.reshape(pre, [128])
    return pre

4.人脸特征对比

当从输入的两张图片中获取到人脸特征后,需要将两张图片的特征进行对比,详细可以参考吴恩达老师的深度学习视频

def face_distance(face_encodings, face_to_compare):
    if len(face_encodings) == 0:
        return np.empty((0))
    return np.linalg.norm(face_encodings - face_to_compare, axis=1)

4.完整代码

import os

from keras.models import load_model
from numpy import expand_dims
import numpy as np
from face_detector import extract_face
from datetime import datetime
from mtcnn import MTCNN
import utils
from PIL import Image
from numpy import asarray


def pre_process(x):
    if x.ndim == 4:
        axis = (1, 2, 3)
        size = x[0].size
    elif x.ndim == 3:
        axis = (0, 1, 2)
        size = x.size
    else:
        raise ValueError('Dimension  3 or 4')

    mean = np.mean(x, axis=axis, keepdims=True)
    std = np.std(x, axis=axis, keepdims=True)
    std_adj = np.maximum(std, 1.0 / np.sqrt(size))
    y = (x - mean) / std_adj
    return y


def l2_normalize(x, axis=-1, epsilon=1e-10):
    output = x / np.sqrt(np.maximum(np.sum(np.square(x), axis=axis, keepdims=True), epsilon))
    return output


def get_embedding(model, img):
    face_img = pre_process(img)
    pre = model.predict(face_img)
    pre = l2_normalize(np.concatenate(pre))
    pre = np.reshape(pre, [128])
    return pre


def extract_face(detector, file_name, required_size=(160, 160)):
    # 加载图片
    image = Image.open(file_name)
    # 将图片转换彩色
    image = image.convert('RGB')
    # 将图片转换为 array
    pixels = asarray(image)
    # 使用 mtcnn 检测人脸
    results = detector.detect_faces(pixels)
    # 返回人脸位置信息
    x1, y1, width, height = results[0]['box']
    x1, y1 = abs(x1), abs(y1)
    x2, y2 = x1 + width, y1 + height
    # 从图片剪裁人脸
    face = pixels[y1:y2, x1:x2]
    # 将剪裁的人脸矩阵化
    image = Image.fromarray(face)
    # 调整检测人脸图片大小
    image = image.resize(required_size)
    # 调整矩阵形状
    face_array = asarray(image).reshape(1, 160, 160, 3)
    return face_array

def face_distance(face_encodings, face_to_compare):
    if len(face_encodings) == 0:
        return np.empty((0))
    return np.linalg.norm(face_encodings - face_to_compare, axis=1)


def verify_demo():
    model = load_model('facenet_keras.h5')
    dector = MTCNN()
    
    org_list = []
    # 图片路径,修改成自己的图片路径
    image1_path = 'images\camera_0.jpg'
    pixels = extract_face(dector, image1_path)
    embedding1 = get_embedding(model, pixels)
    org_list.append(embedding1)
    # 图片路径,修改成自己的图片路径
    image2_path = 'vertify\younes.jpg'
    pixels1 = extract_face(dector, image2_path)
    embedding2 = get_embedding(model, pixels1)

    dist = face_distance(org_list, embedding2)
    print(dist)
    if dist < 0.6:
        print("欢迎 " + str('ounes') + "回家!")
    else:
        print("经验证,您与" + str('ounes') + "不符!")


if __name__ == '__main__':
    verify_demo()
    # check_who_is()

参考:
吴恩达课后编程作业】Course 4 -卷积神经网络 - 第四周作业
如何在 Keras 中使用 FaceNet 开发人脸识别系统

阅读 207
26 声望
4 粉丝
0 条评论
26 声望
4 粉丝
文章目录
宣传栏