enforce face detection option

This commit is contained in:
Şefik Serangil 2020-05-04 10:54:28 +03:00
parent d9c6a4eaa4
commit 74d8a64d57
2 changed files with 19 additions and 16 deletions

View File

@ -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,12 +316,11 @@ 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)
#---------------------------
def allocateMemory():
print("Analyzing your system...")
functions.allocateMemory()
functions.initializeFolder()

View File

@ -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():