question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Questions on using DeepFace.find() but with a custom face-detection

See original GitHub issue

For my use case (a live-cam-analysis-setting) I have a face detection model already, but still need a good face-recognition model and I’m using DeepFace.find(): DeepFace.find(face_image, db_path = "./train_cut_faces/", model_name = model_name, model=model, enforce_detection = False, silent =True) as “model” [“VGG-Face”, “Facenet”, “Facenet512”, “OpenFace”, “DeepFace”, “DeepID”, “ArcFace”, “Dlib”, “SFace”] are used (I want to test them all for my usecase) and have some questions:

1.) When using enforce_detection = False, do the images in db_path should only contain the face? (I guess this must be true) 2.) The creation of my face-database is done in a different script (lets call it training.py), so, do I need to somehow (pre-)process the faces (for example with cv2.resize(face_image, (224, 224)) or normalize/standardize the faces in a certain way? 3.) If 2.) is not needed, should the stored faces have a minimum/maximum pixel-size? The face detection algorithm used can detect faces up to a few meters from the camera and some faces have like 20x40 pixels 4.) When DeepFace.find() is called the first time it creates a representations_deeface.pkl file with the embeddings of the faces in db_path (as far as I understand) and reuses the .pkl if it was already created. In my usecase the user can add persons/faces and the name to the database at will, is it possible to somehow “update” the .pkl? 5.) What is in your opinion the “best” “middle-ground” model for a live-analysis setting in terms of accuracy and speed? Due to this setting at least a few FPS is desirable. 6.) Does the choice of the distance_metric function has a significant impact on the recognized faces?

Thank you for your time!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Terminazorcommented, Aug 4, 2022

@serengil Regarding 4): I have looked through the code and found the part where the embeddings are calculated and the .pkl file is stored. I put it all into a function and it seems to do the job. The only thing I added was the removal of the .pkl file right before saving it again with the new embeddings:

def update_database(db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'opencv', align = True, prog_bar = True, normalization = 'base', silent=False):

	if os.path.isdir(db_path) == True:

		# Load or build model
		if model == None:

			if model_name == 'Ensemble':
				if not silent: print("Ensemble learning enabled")
				models = Boosting.loadModel()

			else: #model is not ensemble
				model = build_model(model_name)
				models = {}
				models[model_name] = model

		else: #model != None
			if not silent: print("Already built model is passed")

			if model_name == 'Ensemble':
				Boosting.validate_model(model)
				models = model.copy()
			else:
				models = {}
				models[model_name] = model

		#---------------------------------------

		if model_name == 'Ensemble':
			model_names = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace']
			metric_names = ['cosine', 'euclidean', 'euclidean_l2']
		elif model_name != 'Ensemble':
			model_names = []; metric_names = []
			model_names.append(model_name)
			metric_names.append(distance_metric)

		#---------------------------------------

		file_name = "representations_%s.pkl" % (model_name)
		file_name = file_name.replace("-", "_").lower()

		#find representations for db images
		employees = []
		representations = []
		pbar = tqdm(range(0,len(employees)), desc='Finding representations', disable = prog_bar)

		#for employee in employees:
		for index in pbar:
			employee = employees[index]

			instance = []
			instance.append(employee)

			for j in model_names:
				custom_model = models[j]

				representation = represent(img_path = employee
					, model_name = model_name, model = custom_model
					, enforce_detection = enforce_detection, detector_backend = detector_backend
					, align = align
					, normalization = normalization
					)

				instance.append(representation)

			#-------------------------------

			representations.append(instance)
			
		if path.exists(db_path+'/'+file_name):
			os.remove(db_path+'/'+file_name)

		f = open(db_path+'/'+file_name, "wb")
		pickle.dump(representations, f)
		f.close()

		if not silent: print("Representations stored in ",db_path,"/",file_name," file. This file has been updated!")
	
	else:
		raise ValueError("Passed db_path does not exist!")
0reactions
Risingabhicommented, Aug 8, 2022

1- pass img_path to represent function directly

embeddings = represent(img_path = "C:/Users/Risin/Desktop/Students/Student/1002/1012/AdarshKumar_72_1.jpg"
, model_name='VGG-Face', enforce_detection= False, detector_backend='mtcnn')

2- do not use normalization

3- why cosine is weird? if it is less than 0.40, then they are same person. (Ref: https://github.com/serengil/deepface/blob/master/deepface/commons/distance.py)

working now

Refer to point 3 by you: when i run this, at times i get different person, as Identified. I am extracting face images by using MTCNN() and then calculating its vector (VGG-Face) . check the verificiation result of first 5 images.

Read more comments on GitHub >

github_iconTop Results From Across the Web

serengil/deepface: A Lightweight Face Recognition ... - GitHub
Deepface is a lightweight face recognition and facial attribute analysis (age, gender, emotion and race) framework for python. It is a hybrid face ......
Read more >
How to Perform Face Detection with Deep Learning
In this tutorial, you will discover how to perform face detection in Python using classical and deep learning models.
Read more >
Deep face recognition with Keras, Dlib and OpenCV
Detect, transform, and crop faces on input images. This ensures that faces are aligned before feeding them into the CNN. This preprocessing step ......
Read more >
Face recognition with OpenCV, Python, and deep learning
Learn how to perform face recognition using OpenCV, Python, and dlib by applying deep learning for highly accurate facial recognition.
Read more >
Implementing Face Recognition Using Deep Learning and ...
Deep Learning - Convolutional Neural Network (CNN) · Using VGGFace for Face Recognition · Using Transfer Learning to Recognize Custom Faces.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found