no need to use initialize_detector function anymore

This commit is contained in:
Sefik Ilkin Serengil 2021-06-24 00:35:47 +03:00
parent e0809bf490
commit 8458e107c7
3 changed files with 9 additions and 28 deletions

View File

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

View File

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

View File

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