From 74d8a64d5787d7dcd88d05e317ef94637501d236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eefik=20Serangil?= Date: Mon, 4 May 2020 10:54:28 +0300 Subject: [PATCH] enforce face detection option --- deepface/DeepFace.py | 25 ++++++++++++------------- deepface/commons/functions.py | 10 +++++++--- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 503775a..4b88a1a 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -21,7 +21,7 @@ from deepface.extendedmodels import Age, Gender, Race, Emotion from deepface.commons import functions, realtime, distance as dst def verify(img1_path, img2_path='' - , model_name ='VGG-Face', distance_metric = 'cosine', model = None): + , model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True): tic = time.time() @@ -75,8 +75,8 @@ def verify(img1_path, img2_path='' #---------------------- #crop and align faces - img1 = functions.detectFace(img1_path, input_shape) - img2 = functions.detectFace(img2_path, input_shape) + img1 = functions.detectFace(img1_path, input_shape, enforce_detection = enforce_detection) + img2 = functions.detectFace(img2_path, input_shape, enforce_detection = enforce_detection) #---------------------- #find embeddings @@ -149,7 +149,7 @@ def verify(img1_path, img2_path='' #return resp_objects -def analyze(img_path, actions = [], models = {}): +def analyze(img_path, actions = [], models = {}, enforce_detection = True): if type(img_path) == list: img_paths = img_path.copy() @@ -218,7 +218,7 @@ def analyze(img_path, actions = [], models = {}): if action == 'emotion': emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral'] - img = functions.detectFace(img_path, (48, 48), True) + img = functions.detectFace(img_path, target_size = (48, 48), grayscale = True, enforce_detection = enforce_detection) emotion_predictions = emotion_model.predict(img)[0,:] @@ -241,7 +241,7 @@ def analyze(img_path, actions = [], models = {}): elif action == 'age': if img_224 is None: - img_224 = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images + img_224 = functions.detectFace(img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection) #just emotion model expects grayscale images #print("age prediction") age_predictions = age_model.predict(img_224)[0,:] apparent_age = Age.findApparentAge(age_predictions) @@ -250,7 +250,7 @@ def analyze(img_path, actions = [], models = {}): elif action == 'gender': if img_224 is None: - img_224 = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images + img_224 = functions.detectFace(img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection) #just emotion model expects grayscale images #print("gender prediction") gender_prediction = gender_model.predict(img_224)[0,:] @@ -264,7 +264,7 @@ def analyze(img_path, actions = [], models = {}): elif action == 'race': if img_224 is None: - img_224 = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images + img_224 = functions.detectFace(img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection) #just emotion model expects grayscale images race_predictions = race_model.predict(img_224)[0,:] race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic'] @@ -316,13 +316,12 @@ def detectFace(img_path): return img[:, :, ::-1] #bgr to rgb -def stream(db_path, model_name ='VGG-Face', distance_metric = 'cosine', enable_face_analysis = True): +def stream(db_path = '', model_name ='VGG-Face', distance_metric = 'cosine', enable_face_analysis = True): realtime.analysis(db_path, model_name, distance_metric, enable_face_analysis) - -#--------------------------- - -functions.allocateMemory() +def allocateMemory(): + print("Analyzing your system...") + functions.allocateMemory() functions.initializeFolder() diff --git a/deepface/commons/functions.py b/deepface/commons/functions.py index 9ac3fc1..a0ba457 100644 --- a/deepface/commons/functions.py +++ b/deepface/commons/functions.py @@ -135,7 +135,9 @@ def get_opencv_path(): return path+"/data/" -def detectFace(img, target_size=(224, 224), grayscale = False): +def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection = True): + + img_path = "" #----------------------- @@ -166,6 +168,8 @@ def detectFace(img, target_size=(224, 224), grayscale = False): elif exact_image != True: #image path passed as input + img_path = ""+img + if os.path.isfile(img) != True: raise ValueError("Confirm that ",img," exists") @@ -283,7 +287,7 @@ def detectFace(img, target_size=(224, 224), grayscale = False): else: - if exact_image == True: + if (exact_image == True) or (enforce_detection != True): if grayscale == True: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) @@ -294,7 +298,7 @@ def detectFace(img, target_size=(224, 224), grayscale = False): img_pixels /= 255 return img_pixels else: - raise ValueError("Face could not be detected in ", img,". Please confirm that the picture is a face photo.") + raise ValueError("Face could not be detected in ", img_path,". Please confirm that the picture is a face photo.") def allocateMemory():