自己写的代码懒得解释了,看的懂就看,代码是没有任何问题的,只有创建一个个人文件夹,在个人文件夹里面放一张照片,就可以进行实时人脸识别了。我会在最后附上github或者gitee地址,gitee和github的readme里面我给了facenet模型下载地址,facenet是用了构建mtcnn的模型,构建mtcnn后在运用mtcnn得到人脸的128特征向量,在判断欧式距离看是不是同一个人,好了我就解释这么多了。。
先运行untitled里面的代码
from future import absolute_import
from future import division
from future import print_function
import cv2
import csv
from os.path import join as pjoin
import matplotlib.pyplot as plt
import sklearn
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.externals import joblib
from scipy import misc
import tensorflow as tf
import numpy as np
import sys
import os
import copy
import argparse
import facenet
import align.detect_face
minsize = 20 # minimum size of face
threshold = [ 0.6, 0.7, 0.7 ] # three steps’s threshold
factor = 0.709 # scale factor
创建mtcnn网络,并加载参数
print(‘Creating networks and loading parameters’)
with tf.Graph().as_default():
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
with sess.as_default():
pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
def load_and_align_data(image, image_size, margin, gpu_memory_fraction):
# 读取图片
img = image
# 获取图片的shape
img_size = np.asarray(img.shape)[0:2]
# 返回边界框数组 (参数分别是输入图片 脸部最小尺寸 三个网络 阈值 factor不清楚)
bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
# 如果检测出图片中不存在人脸 则直接返回,return 0(表示不存在人脸,跳过此图)
if len(bounding_boxes) < 1:
return 0,0,0
else:
crop=[]
det=bounding_boxes
det[:,0]=np.maximum(det[:,0], 0)
det[:,1]=np.maximum(det[:,1], 0)
det[:,2]=np.minimum(det[:,2], img_size[1])
det[:,3]=np.minimum(det[:,3], img_size[0])
# det[:,0]=np.maximum(det[:,0]-margin/2, 0)
# det[:,1]=np.maximum(det[:,1]-margin/2, 0)
# det[:,2]=np.minimum(det[:,2]+margin/2, img_size[1])
# det[:,3]=np.minimum(det[:,3]+margin/2, img_size[0])
det=det.astype(int)
for