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 开发人脸识别零碎