TensorFlow MTCNN facenet face recognition

MTCNN for detecting a picture of a human face, facenet who used to recognize faces Yes.

First, the whole idea

Create an identified target sample data sets.

Here we use Liu and Jay as the recognized object, therefore, to select a sample Jay Liu and base, both data sets extracted

1.1 MTCNN test sample using a human face, and the identification is L (Liu) or z (Jay)

1.2 were calculated using both facenet their embedding and stored. I.e., the sample data set

2 complete face recognition

2.1 use MTCNN test sample face 

Embedding calculated using facenet 2.2

2.3 calculated using the Euclidean distance, respectively, and their distance determination is l and z embedding Liu or other person or Jay

Second, create a sample data set

2.1 preparation

Create a dataset / img folder creation l and z are arranged in folders sample image   

copy facenet.py to the current folder

copy mtcnn align the folder to the current folder  

copy other people trained face recognition model models folder to the current folder

Wherein 2.2 using face recognition mtcnn and the recognized result is stored in the dataset / emb_face folders

def  create_face(img_dir,out_face_dir):
	# 获取 img 及其 label
	img_list,name_list = file_processing.gen_files_labels(img_dir,postfix='jpg')	
	
	#创建MTCNN网络
	face_detect = face_recognition.Facedetection()     #初始化 Facedetection()
	
	for img_path,name in zip(img_list,name_list):
		# 读取图片
		img = image_processing.read_image(img_path,resize_height=0,resize_width=0,normalization=False)
		# 调用MTCNN来进行人脸检测
		bounding_box,points = face_detect.detect_face(img)
		bounding_box = bounding_box[:,0:4].astype(int)
		bounding_box=bounding_box[0,:]
		
		# 截取人脸图片
		face_image = image_processing.crop_image(img,bounding_box)
		# 截取人脸输出的路径
		out_path = os.path.join(out_face_dir,name)
		# 对人脸进行缩放  因为facenet要求人脸输入为160*160
		face_image = image_processing.resize_image(face_image,resize_height,resize_width)
		
		if not os.path.exists(out_path):
			os.mkdir(out_path)
			
		basename = os.path.basename(img_path) #获取文件名
		out_path=os.path.join(out_path,basename) #保存人脸的绝对路径
		image_processing.save_image(out_path,face_image)

2.3 embedding the samples is calculated

def create_embedding(model_path,emb_face_dir,out_emb_path,out_filename):
	# 初始化facenetEmbedding
	face_net = face_recognition.facenetEmbedding(model_path)
	# 获取人脸img及其label
	img_list,name_list = file_processing.gen_files_labels(emb_face_dir,postfix='jpg')
	# 得到缩放的图片  并且归一化
	img = image_processing.get_images(img_list,resize_height,resize_width,whiten=True)
	
	compare_emb = face_net.get_embedding(img)
	np.save(out_emb_path,compare_emb)
	file_processing.write_data(out_filename,img_list,model='w')

Wherein, when said calculations need to use face_detect.detect_face (img) face_recognition.facenetEmbedding two functions, respectively, these two functions is the function of embedding function calculation and detection face, which is defined below

# -*-coding: utf-8 -*-

import facenet
import tensorflow as tf
import align.detect_face as detect_face


class facenetEmbedding:
	def __init__(self, model_path):
		self.sess = tf.InteractiveSession()
		self.sess.run(tf.global_variables_initializer())
		
		facenet.load_model(model_path)
		
		self.images_placeholder 	 = tf.get_default_graph().get_tensor_by_name("input:0")
		self.tf_embeddings 			 = tf.get_default_graph().get_tensor_by_name("embeddings:0")
		self.phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
		
	def get_embedding(self, images):
		feed_dict = {self.images_placeholder:images,  self.phase_train_placeholder:False}
		embedding = self.sess.run(self.tf_embeddings,feed_dict=feed_dict)
		return embedding
	
	def free(self):
		self.sess.close()
		

class Facedetection:
	def __init__(self):
		self.minsize = 20
		self.threshold = [0.6,0.7,0.7]
		self.factor = 0.709
		
		with tf.Graph().as_default():
			sess = tf.Session()
			with sess.as_default():
				self.pnet,self.rnet,self.onet = detect_face.create_mtcnn(sess,None)
				
	def detect_face(self,image):
		bounding_boxes,points = detect_face.detect_face(image, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor)

		return bounding_boxes,points
		
		
def detection_face(img):
	self.minsize = 20
	self.threshold = [0.6,0.7,0.7]
	self.factor = 0.709
	
	with tf.Graph().as_default():
		sess = tf.Session()
		with sess.as_default():
			 pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
	bounding_boxes, points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
	return bounding_boxes,points

		

By defining two class to encapsulate mtcnn detect and facenet embedding.

As for image processing functions, including save, open, zoom and other functions, where not too much to explain.

After this step, stored in the dataset / emb_face folder Liu Xiang and Jay face, in the dataset / emb / faceEmbedding.npy kept the embedding of the sample, kept the names of two people sign in the dataset / emb / name.txt in .

Third, the face recognition process.

A photo detector to be stored in the dataset / test_images.

2 Load sample data.

def load_dataset(dataset_path,filename):
	compare_emb = np.load(dataset_path)
	names_list  = file_processing.read_data(filename)
	return compare_emb,names_list

3 Open the image to be detected, identify the face

file_path = os.path.join(image_path, file_names)
	
image = image_processing.read_image(file_path)
		
bounding_box,points = face_detect.detect_face(image)
		
bounding_box = bounding_box[:,0:4].astype(int)

embedding 4 and samples are compared

face_images = image_processing.get_crop_images(image,bounding_box,resize_height,resize_width,whiten=True)
		
pred_emb = face_net.get_embedding(face_images)
		
pred_name = compare_embedding(pred_emb,dataset_emb,names_list)

 Wherein compare_embedding represents the calculated distance between the two, returns the name of a person is detected

def compare_embedding(pred_emb,dataset_emb,names_list):
	pred_num = len(pred_emb)
	dataset_num = len(dataset_emb)
	pred_name = []
	
	for i in range(pred_num):
		dist_list = []
		for j in range(dataset_num):
			dist = np.sqrt(np.sum(np.square(np.subtract(pred_emb[i,:],dataset_emb[j,:]))))
			dist_list.append(dist)
			
		min_value = min(dist_list)
		
		#print(min_value)
		
		if (min_value>0.85):
			pred_name.append('unkonwn')
		else:
			name = names_list[dist_list.index(min_value)]
			name = name.split(os.sep)[-2] 
			pred_name.append(name)	
	#print(pred_name)
	return pred_name

5 frame mark

bgr_image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)	
			
people_num = bounding_box.shape[0]
		
print("people num = %d"%bounding_box.shape[0])	
		
image_processing.cv_show_image_text("face_recognition", bgr_image,bounding_box, pred_name)

         

   

=======================================================================

Recently opened a public number, the article is a chapter of the update,

Public No Name: Fun electronic world

Ladies and gentlemen have any questions that can be directly above questions, I will answer one by one.

Followed by non-otaku sun, step by step into the electronic world.

Reply concern after data download Keywords can get free vast amounts of video learning materials to download ~ ~!

Shared learning videos, sharing information is constantly updated.

Share Machine Learning / tensorflow video learning materials:

=======================================================================

Source Download Link: https://download.csdn.net/download/yunge812/11373693

 

Published 70 original articles · won praise 73 · views 130 000 +

Guess you like

Origin blog.csdn.net/yunge812/article/details/86211824