From 8458e107c73a6487f88485a2567598db6ad5fb5c Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Thu, 24 Jun 2021 00:35:47 +0300 Subject: [PATCH] no need to use initialize_detector function anymore --- deepface/DeepFace.py | 14 ++++---------- deepface/commons/functions.py | 12 ++++-------- tests/unit_tests.py | 11 +---------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index d69e07b..6723185 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -48,7 +48,8 @@ def build_model(model_name): 'Emotion': Emotion.loadModel, 'Age': Age.loadModel, 'Gender': Gender.loadModel, - 'Race': Race.loadModel + 'Race': Race.loadModel, + 'Ensemble': Boosting.loadModel } if not "model_obj" in globals() or model_label != model_name: @@ -105,7 +106,6 @@ def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric = tic = time.time() img_list, bulkProcess = functions.initialize_input(img1_path, img2_path) - functions.initialize_detector(detector_backend = detector_backend) resp_objects = [] @@ -123,7 +123,7 @@ def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric = if model == None: if model_name == 'Ensemble': - models = Boosting.loadModel() + models = build_model(model_name) else: model = build_model(model_name) models = {} @@ -310,7 +310,6 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] , models = """ img_paths, bulkProcess = functions.initialize_input(img_path) - functions.initialize_detector(detector_backend = detector_backend) #--------------------------------- @@ -494,7 +493,6 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', tic = time.time() img_paths, bulkProcess = functions.initialize_input(img_path) - functions.initialize_detector(detector_backend = detector_backend) #------------------------------- @@ -504,7 +502,7 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', if model_name == 'Ensemble': print("Ensemble learning enabled") - models = Boosting.loadModel() + models = build_model(model_name) else: #model is not ensemble model = build_model(model_name) @@ -735,8 +733,6 @@ def represent(img_path, model_name = 'VGG-Face', model = None, enforce_detection if model is None: model = build_model(model_name) - functions.initialize_detector(detector_backend = detector_backend) - #--------------------------------- #decide input shape @@ -782,8 +778,6 @@ def stream(db_path = '', model_name ='VGG-Face', distance_metric = 'cosine', ena if frame_threshold < 1: raise ValueError("frame_threshold must be greater than the value 1 but you passed "+str(frame_threshold)) - functions.initialize_detector(detector_backend = 'opencv') #stream uses opencv by default! - realtime.analysis(db_path, model_name, distance_metric, enable_face_analysis , source = source, time_threshold = time_threshold, frame_threshold = frame_threshold) diff --git a/deepface/commons/functions.py b/deepface/commons/functions.py index 39eab5d..4ed5de3 100644 --- a/deepface/commons/functions.py +++ b/deepface/commons/functions.py @@ -41,11 +41,6 @@ def initialize_input(img1_path, img2_path = None): return img_list, bulkProcess -def initialize_detector(detector_backend): - - global face_detector - face_detector = FaceDetector.build_model(detector_backend) - def initializeFolder(): home = str(Path.home()) @@ -91,9 +86,10 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det img_region = [0, 0, img.shape[0], img.shape[1]] - #if functions.preproces_face is called directly, then face_detector global variable might not been initialized. - if not "face_detector" in globals(): - initialize_detector(detector_backend = detector_backend) + #detector stored in a global variable in FaceDetector object. + #this call should be completed very fast because it will return found in memory + #it will not build face detector model in each call (consider for loops) + face_detector = FaceDetector.build_model(detector_backend) detected_face, img_region = FaceDetector.detect_face(face_detector, detector_backend, img, align) diff --git a/tests/unit_tests.py b/tests/unit_tests.py index 176154a..bd6c699 100644 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -279,16 +279,7 @@ print("Pre-trained ensemble method - find") from deepface import DeepFace from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace -model = {} -model["VGG-Face"] = VGGFace.loadModel() -print("VGG loaded") -model["Facenet"] = Facenet.loadModel() -print("Facenet loaded") -model["OpenFace"] = OpenFace.loadModel() -print("OpenFace loaded") -model["DeepFace"] = FbDeepFace.loadModel() -print("DeepFace loaded") - +model = DeepFace.build_model("Ensemble") df = DeepFace.find("dataset/img1.jpg", db_path = "dataset", model_name = 'Ensemble', model = model, enforce_detection=False) print(df)