From 459b40d0e8793ec282773aa18a7046bc843438e2 Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Thu, 16 Apr 2020 10:27:50 +0300 Subject: [PATCH 1/9] Update DeepFace.py --- deepface/DeepFace.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index dfff493..adb3bf2 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -20,6 +20,51 @@ from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace from deepface.extendedmodels import Age, Gender, Race, Emotion from deepface.commons import functions, realtime, distance as dst + +def analyze_init(models = []): + #--------------------------------- + + #if a specific target is not passed, then find them all + if len(models) == 0: + models = ['emotion', 'age', 'gender', 'race'] + + print("Models to initialize: ", models) + + #--------------------------------- + + if 'emotion' in models: + emotion_model = Emotion.loadModel() + + if 'age' in models: + age_model = Age.loadModel() + + if 'gender' in models: + gender_model = Gender.loadModel() + + if 'race' in models: + race_model = Race.loadModel() + + +def verify_init(model_name = 'VGG-Face'): + if model_name == 'VGG-Face': + print("Loading %s model" % model_name) + model = VGGFace.loadModel() + + elif model_name == 'OpenFace': + print("Loading %s model" % model_name) + model = OpenFace.loadModel() + + elif model_name == 'Facenet': + print("Loading %s model" % model_name) + model = Facenet.loadModel() + + elif model_name == 'DeepFace': + print("Loading %s model" % model_name) + model = FbDeepFace.loadModel() + else: + raise ValueError("Invalid model_name passed - ", model_name) + + def verify(img1_path, img2_path='' , model_name ='VGG-Face', distance_metric = 'cosine', model = None): From 8cc09f4314d8e4dbcc567c9d7a6c8392afabf68f Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Thu, 16 Apr 2020 10:29:34 +0300 Subject: [PATCH 2/9] Update DeepFace.py --- deepface/DeepFace.py | 44 -------------------------------------------- 1 file changed, 44 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index adb3bf2..16c9b27 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -21,50 +21,6 @@ from deepface.extendedmodels import Age, Gender, Race, Emotion from deepface.commons import functions, realtime, distance as dst -def analyze_init(models = []): - #--------------------------------- - - #if a specific target is not passed, then find them all - if len(models) == 0: - models = ['emotion', 'age', 'gender', 'race'] - - print("Models to initialize: ", models) - - #--------------------------------- - - if 'emotion' in models: - emotion_model = Emotion.loadModel() - - if 'age' in models: - age_model = Age.loadModel() - - if 'gender' in models: - gender_model = Gender.loadModel() - - if 'race' in models: - race_model = Race.loadModel() - - -def verify_init(model_name = 'VGG-Face'): - if model_name == 'VGG-Face': - print("Loading %s model" % model_name) - model = VGGFace.loadModel() - - elif model_name == 'OpenFace': - print("Loading %s model" % model_name) - model = OpenFace.loadModel() - - elif model_name == 'Facenet': - print("Loading %s model" % model_name) - model = Facenet.loadModel() - - elif model_name == 'DeepFace': - print("Loading %s model" % model_name) - model = FbDeepFace.loadModel() - else: - raise ValueError("Invalid model_name passed - ", model_name) - - def verify(img1_path, img2_path='' , model_name ='VGG-Face', distance_metric = 'cosine', model = None): From 93c94621611ca870a114579e09d4b4b95bee4132 Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Thu, 16 Apr 2020 10:37:23 +0300 Subject: [PATCH 3/9] Load models to memory 2 new functions: verify_init(model_name) analyze_init(models) The functions created in order to pre load models to memory for shorter analzye / verify first time Ex: Real time analyze waiting for first face to show without analyze_init() first analyze takes 1.5 sec with analyze_init() first analyze takes 0.3 sec --- deepface/DeepFace.py | 188 ++++++++++++++++++++++++++----------------- 1 file changed, 116 insertions(+), 72 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 16c9b27..e5dc176 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -21,73 +21,117 @@ from deepface.extendedmodels import Age, Gender, Race, Emotion from deepface.commons import functions, realtime, distance as dst +def analyze_init(models = []): + #--------------------------------- + + #if a specific target is not passed, then find them all + if len(models) == 0: + models = ['emotion', 'age', 'gender', 'race'] + + print("Models to initialize: ", models) + + #--------------------------------- + + if 'emotion' in models: + emotion_model = Emotion.loadModel() + + if 'age' in models: + age_model = Age.loadModel() + + if 'gender' in models: + gender_model = Gender.loadModel() + + if 'race' in models: + race_model = Race.loadModel() + + +def verify_init(model_name = 'VGG-Face'): + if model_name == 'VGG-Face': + print("Loading %s model" % model_name) + model = VGGFace.loadModel() + + elif model_name == 'OpenFace': + print("Loading %s model" % model_name) + model = OpenFace.loadModel() + + elif model_name == 'Facenet': + print("Loading %s model" % model_name) + model = Facenet.loadModel() + + elif model_name == 'DeepFace': + print("Loading %s model" % model_name) + model = FbDeepFace.loadModel() + else: + raise ValueError("Invalid model_name passed - ", model_name) + + def verify(img1_path, img2_path='' , model_name ='VGG-Face', distance_metric = 'cosine', model = None): - + tic = time.time() - + if type(img1_path) == list: bulkProcess = True img_list = img1_path.copy() else: bulkProcess = False img_list = [[img1_path, img2_path]] - + #------------------------------ - + if model == None: if model_name == 'VGG-Face': print("Using VGG-Face model backend and", distance_metric,"distance.") model = VGGFace.loadModel() - + elif model_name == 'OpenFace': print("Using OpenFace model backend", distance_metric,"distance.") model = OpenFace.loadModel() - + elif model_name == 'Facenet': print("Using Facenet model backend", distance_metric,"distance.") model = Facenet.loadModel() - + elif model_name == 'DeepFace': print("Using FB DeepFace model backend", distance_metric,"distance.") model = FbDeepFace.loadModel() - + else: raise ValueError("Invalid model_name passed - ", model_name) else: #model != None print("Already built model is passed") - + #------------------------------ #face recognition models have different size of inputs input_shape = model.layers[0].input_shape[1:3] #------------------------------ - + #tuned thresholds for model and metric pair threshold = functions.findThreshold(model_name, distance_metric) - + #------------------------------ resp_objects = [] for instance in img_list: if type(instance) == list and len(instance) >= 2: img1_path = instance[0] img2_path = instance[1] - + #---------------------- #crop and align faces - + img1 = functions.detectFace(img1_path, input_shape) img2 = functions.detectFace(img2_path, input_shape) - + #---------------------- #find embeddings img1_representation = model.predict(img1)[0,:] img2_representation = model.predict(img2)[0,:] - + #---------------------- #find distances between embeddings - + if distance_metric == 'cosine': distance = dst.findCosineDistance(img1_representation, img2_representation) elif distance_metric == 'euclidean': @@ -96,18 +140,18 @@ def verify(img1_path, img2_path='' distance = dst.findEuclideanDistance(dst.l2_normalize(img1_representation), dst.l2_normalize(img2_representation)) else: raise ValueError("Invalid distance_metric passed - ", distance_metric) - + #---------------------- #decision - + if distance <= threshold: identified = "true" else: identified = "false" - + #---------------------- #response object - + resp_obj = "{" resp_obj += "\"verified\": "+identified resp_obj += ", \"distance\": "+str(distance) @@ -115,30 +159,30 @@ def verify(img1_path, img2_path='' resp_obj += ", \"model\": \""+model_name+"\"" resp_obj += ", \"similarity_metric\": \""+distance_metric+"\"" resp_obj += "}" - + resp_obj = json.loads(resp_obj) #string to json - + if bulkProcess == True: resp_objects.append(resp_obj) else: K.clear_session() return resp_obj #---------------------- - + else: raise ValueError("Invalid arguments passed to verify function: ", instance) - + #------------------------- - + toc = time.time() - + #print("identification lasts ",toc-tic," seconds") - + if bulkProcess == True: resp_obj = "{" - + for i in range(0, len(resp_objects)): - resp_item = json.dumps(resp_objects[i]) + resp_item = json.dumps(resp_objects[i]) if i > 0: resp_obj += ", " @@ -157,129 +201,129 @@ def analyze(img_path, actions= []): else: img_paths = [img_path] bulkProcess = False - + #--------------------------------- - + #if a specific target is not passed, then find them all if len(actions) == 0: actions= ['emotion', 'age', 'gender', 'race'] - + print("Actions to do: ", actions) - + #--------------------------------- - + if 'emotion' in actions: emotion_model = Emotion.loadModel() - + if 'age' in actions: age_model = Age.loadModel() - + if 'gender' in actions: gender_model = Gender.loadModel() - + if 'race' in actions: race_model = Race.loadModel() #--------------------------------- - + resp_objects = [] for img_path in img_paths: - + resp_obj = "{" - + #TO-DO: do this in parallel - + pbar = tqdm(range(0,len(actions)), desc='Finding actions') - + action_idx = 0 #for action in actions: for index in pbar: action = actions[index] pbar.set_description("Action: %s" % (action)) - + if action_idx > 0: resp_obj += ", " - + if action == 'emotion': emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral'] img = functions.detectFace(img_path, (48, 48), True) - + emotion_predictions = emotion_model.predict(img)[0,:] - + sum_of_predictions = emotion_predictions.sum() - + emotion_obj = "\"emotion\": {" for i in range(0, len(emotion_labels)): emotion_label = emotion_labels[i] emotion_prediction = 100 * emotion_predictions[i] / sum_of_predictions - + if i > 0: emotion_obj += ", " - + emotion_obj += "\"%s\": %s" % (emotion_label, emotion_prediction) - + emotion_obj += "}" - + emotion_obj += ", \"dominant_emotion\": \"%s\"" % (emotion_labels[np.argmax(emotion_predictions)]) - + resp_obj += emotion_obj - + elif action == 'age': img = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images #print("age prediction") age_predictions = age_model.predict(img)[0,:] apparent_age = Age.findApparentAge(age_predictions) - + resp_obj += "\"age\": %s" % (apparent_age) - + elif action == 'gender': img = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images #print("gender prediction") - + gender_prediction = gender_model.predict(img)[0,:] - + if np.argmax(gender_prediction) == 0: gender = "Woman" elif np.argmax(gender_prediction) == 1: gender = "Man" - + resp_obj += "\"gender\": \"%s\"" % (gender) - + elif action == 'race': img = functions.detectFace(img_path, (224, 224), False) #just emotion model expects grayscale images race_predictions = race_model.predict(img)[0,:] race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic'] - + sum_of_predictions = race_predictions.sum() - + race_obj = "\"race\": {" for i in range(0, len(race_labels)): race_label = race_labels[i] race_prediction = 100 * race_predictions[i] / sum_of_predictions - + if i > 0: race_obj += ", " - + race_obj += "\"%s\": %s" % (race_label, race_prediction) - + race_obj += "}" race_obj += ", \"dominant_race\": \"%s\"" % (race_labels[np.argmax(race_predictions)]) - + resp_obj += race_obj - + action_idx = action_idx + 1 - + resp_obj += "}" - + resp_obj = json.loads(resp_obj) - + if bulkProcess == True: resp_objects.append(resp_obj) else: return resp_obj - + if bulkProcess == True: resp_obj = "{" - + for i in range(0, len(resp_objects)): - resp_item = json.dumps(resp_objects[i]) + resp_item = json.dumps(resp_objects[i]) if i > 0: resp_obj += ", " From 59d5b5f27e2ea1968704b63bb7e743b2eeb6a2fe Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Thu, 16 Apr 2020 10:38:08 +0300 Subject: [PATCH 4/9] Add uria-franko to Contributors list --- Contributors.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Contributors.md diff --git a/Contributors.md b/Contributors.md new file mode 100644 index 0000000..ef52008 --- /dev/null +++ b/Contributors.md @@ -0,0 +1 @@ +uria-franko \ No newline at end of file From d9c7ad6b7ab5c75d67de6f2ffecbbccb04f269ef Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Thu, 16 Apr 2020 10:41:51 +0300 Subject: [PATCH 5/9] Update Contributors.md --- Contributors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Contributors.md b/Contributors.md index ef52008..45ff2d6 100644 --- a/Contributors.md +++ b/Contributors.md @@ -1 +1 @@ -uria-franko \ No newline at end of file +uriafranko \ No newline at end of file From 1fcbd36464f390c82fffd8463f2a9998df5da646 Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Thu, 16 Apr 2020 11:32:56 +0300 Subject: [PATCH 6/9] Return Built Models --- deepface/DeepFace.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index e5dc176..e4f1f94 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -23,7 +23,7 @@ from deepface.commons import functions, realtime, distance as dst def analyze_init(models = []): #--------------------------------- - + built_models = {} #if a specific target is not passed, then find them all if len(models) == 0: models = ['emotion', 'age', 'gender', 'race'] @@ -33,16 +33,17 @@ def analyze_init(models = []): #--------------------------------- if 'emotion' in models: - emotion_model = Emotion.loadModel() + built_models['emotion'] = Emotion.loadModel() if 'age' in models: - age_model = Age.loadModel() + built_models['age'] = Age.loadModel() if 'gender' in models: - gender_model = Gender.loadModel() + built_models['gender'] = Gender.loadModel() if 'race' in models: - race_model = Race.loadModel() + built_models['race'] = Race.loadModel() + return built_models def verify_init(model_name = 'VGG-Face'): @@ -63,6 +64,7 @@ def verify_init(model_name = 'VGG-Face'): model = FbDeepFace.loadModel() else: raise ValueError("Invalid model_name passed - ", model_name) + return model def verify(img1_path, img2_path='' @@ -193,7 +195,8 @@ def verify(img1_path, img2_path='' return resp_obj #return resp_objects -def analyze(img_path, actions= []): + +def analyze(img_path, actions= [], models= {}): if type(img_path) == list: img_paths = img_path.copy() @@ -213,16 +216,28 @@ def analyze(img_path, actions= []): #--------------------------------- if 'emotion' in actions: - emotion_model = Emotion.loadModel() + if 'emotion' in models: + emotion_model = models['emotion'] + else: + emotion_model = Emotion.loadModel() if 'age' in actions: - age_model = Age.loadModel() + if 'age' in models: + age_model = models['age'] + else: + age_model = Age.loadModel() if 'gender' in actions: - gender_model = Gender.loadModel() + if 'gender' in models: + gender_model = models['gender'] + else: + gender_model = Gender.loadModel() if 'race' in actions: - race_model = Race.loadModel() + if 'race' in models: + race_model = models['race'] + else: + race_model = Race.loadModel() #--------------------------------- resp_objects = [] From b124c7bd391f492f07df3fd4a5f13888be3f8426 Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Fri, 17 Apr 2020 14:49:10 +0300 Subject: [PATCH 7/9] Analyze Pass Models --- deepface/DeepFace.py | 49 +++----------------------------------------- 1 file changed, 3 insertions(+), 46 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index e4f1f94..c71d7d9 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -21,52 +21,6 @@ from deepface.extendedmodels import Age, Gender, Race, Emotion from deepface.commons import functions, realtime, distance as dst -def analyze_init(models = []): - #--------------------------------- - built_models = {} - #if a specific target is not passed, then find them all - if len(models) == 0: - models = ['emotion', 'age', 'gender', 'race'] - - print("Models to initialize: ", models) - - #--------------------------------- - - if 'emotion' in models: - built_models['emotion'] = Emotion.loadModel() - - if 'age' in models: - built_models['age'] = Age.loadModel() - - if 'gender' in models: - built_models['gender'] = Gender.loadModel() - - if 'race' in models: - built_models['race'] = Race.loadModel() - return built_models - - -def verify_init(model_name = 'VGG-Face'): - if model_name == 'VGG-Face': - print("Loading %s model" % model_name) - model = VGGFace.loadModel() - - elif model_name == 'OpenFace': - print("Loading %s model" % model_name) - model = OpenFace.loadModel() - - elif model_name == 'Facenet': - print("Loading %s model" % model_name) - model = Facenet.loadModel() - - elif model_name == 'DeepFace': - print("Loading %s model" % model_name) - model = FbDeepFace.loadModel() - else: - raise ValueError("Invalid model_name passed - ", model_name) - return model - - def verify(img1_path, img2_path='' , model_name ='VGG-Face', distance_metric = 'cosine', model = None): @@ -349,13 +303,16 @@ def analyze(img_path, actions= [], models= {}): return resp_obj #return resp_objects + def detectFace(img_path): img = functions.detectFace(img_path)[0] #detectFace returns (1, 224, 224, 3) return img[:, :, ::-1] #bgr to rgb + 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.initializeFolder() From 378417f7691a5240434389728a73ff66719274cd Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Fri, 17 Apr 2020 14:57:53 +0300 Subject: [PATCH 8/9] Revert "Analyze Pass Models" This reverts commit b124c7bd391f492f07df3fd4a5f13888be3f8426. --- deepface/DeepFace.py | 49 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index c71d7d9..e4f1f94 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -21,6 +21,52 @@ from deepface.extendedmodels import Age, Gender, Race, Emotion from deepface.commons import functions, realtime, distance as dst +def analyze_init(models = []): + #--------------------------------- + built_models = {} + #if a specific target is not passed, then find them all + if len(models) == 0: + models = ['emotion', 'age', 'gender', 'race'] + + print("Models to initialize: ", models) + + #--------------------------------- + + if 'emotion' in models: + built_models['emotion'] = Emotion.loadModel() + + if 'age' in models: + built_models['age'] = Age.loadModel() + + if 'gender' in models: + built_models['gender'] = Gender.loadModel() + + if 'race' in models: + built_models['race'] = Race.loadModel() + return built_models + + +def verify_init(model_name = 'VGG-Face'): + if model_name == 'VGG-Face': + print("Loading %s model" % model_name) + model = VGGFace.loadModel() + + elif model_name == 'OpenFace': + print("Loading %s model" % model_name) + model = OpenFace.loadModel() + + elif model_name == 'Facenet': + print("Loading %s model" % model_name) + model = Facenet.loadModel() + + elif model_name == 'DeepFace': + print("Loading %s model" % model_name) + model = FbDeepFace.loadModel() + else: + raise ValueError("Invalid model_name passed - ", model_name) + return model + + def verify(img1_path, img2_path='' , model_name ='VGG-Face', distance_metric = 'cosine', model = None): @@ -303,16 +349,13 @@ def analyze(img_path, actions= [], models= {}): return resp_obj #return resp_objects - def detectFace(img_path): img = functions.detectFace(img_path)[0] #detectFace returns (1, 224, 224, 3) return img[:, :, ::-1] #bgr to rgb - 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.initializeFolder() From 987c5782a0b8503070a3d61603f18789b00762ad Mon Sep 17 00:00:00 2001 From: Uria Franko Date: Fri, 17 Apr 2020 14:59:01 +0300 Subject: [PATCH 9/9] Revert "Revert "Analyze Pass Models"" This reverts commit 378417f7691a5240434389728a73ff66719274cd. --- deepface/DeepFace.py | 49 +++----------------------------------------- 1 file changed, 3 insertions(+), 46 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index e4f1f94..c71d7d9 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -21,52 +21,6 @@ from deepface.extendedmodels import Age, Gender, Race, Emotion from deepface.commons import functions, realtime, distance as dst -def analyze_init(models = []): - #--------------------------------- - built_models = {} - #if a specific target is not passed, then find them all - if len(models) == 0: - models = ['emotion', 'age', 'gender', 'race'] - - print("Models to initialize: ", models) - - #--------------------------------- - - if 'emotion' in models: - built_models['emotion'] = Emotion.loadModel() - - if 'age' in models: - built_models['age'] = Age.loadModel() - - if 'gender' in models: - built_models['gender'] = Gender.loadModel() - - if 'race' in models: - built_models['race'] = Race.loadModel() - return built_models - - -def verify_init(model_name = 'VGG-Face'): - if model_name == 'VGG-Face': - print("Loading %s model" % model_name) - model = VGGFace.loadModel() - - elif model_name == 'OpenFace': - print("Loading %s model" % model_name) - model = OpenFace.loadModel() - - elif model_name == 'Facenet': - print("Loading %s model" % model_name) - model = Facenet.loadModel() - - elif model_name == 'DeepFace': - print("Loading %s model" % model_name) - model = FbDeepFace.loadModel() - else: - raise ValueError("Invalid model_name passed - ", model_name) - return model - - def verify(img1_path, img2_path='' , model_name ='VGG-Face', distance_metric = 'cosine', model = None): @@ -349,13 +303,16 @@ def analyze(img_path, actions= [], models= {}): return resp_obj #return resp_objects + def detectFace(img_path): img = functions.detectFace(img_path)[0] #detectFace returns (1, 224, 224, 3) return img[:, :, ::-1] #bgr to rgb + 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.initializeFolder()