mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
common input initializer for img pairs as arrays
This commit is contained in:
parent
edae2a799c
commit
3057d82e0f
@ -45,15 +45,7 @@ def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric =
|
|||||||
|
|
||||||
tic = time.time()
|
tic = time.time()
|
||||||
|
|
||||||
if type(img1_path) == list:
|
img_list, bulkProcess = initialize_input(img1_path, img2_path)
|
||||||
bulkProcess = True
|
|
||||||
img_list = img1_path.copy()
|
|
||||||
else:
|
|
||||||
bulkProcess = False
|
|
||||||
img_list = [[img1_path, img2_path]]
|
|
||||||
|
|
||||||
#------------------------------
|
|
||||||
|
|
||||||
functions.initialize_detector(detector_backend = detector_backend)
|
functions.initialize_detector(detector_backend = detector_backend)
|
||||||
|
|
||||||
resp_objects = []
|
resp_objects = []
|
||||||
@ -212,8 +204,8 @@ def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric =
|
|||||||
if model == None:
|
if model == None:
|
||||||
model = build_model(model_name)
|
model = build_model(model_name)
|
||||||
|
|
||||||
else: #model != None
|
"""else: #model != None
|
||||||
print("Already built model is passed")
|
print("Already built model is passed")"""
|
||||||
|
|
||||||
#------------------------------
|
#------------------------------
|
||||||
#face recognition models have different size of inputs
|
#face recognition models have different size of inputs
|
||||||
@ -326,15 +318,7 @@ def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric =
|
|||||||
def analyze(img_path, actions = [], models = {}, enforce_detection = True
|
def analyze(img_path, actions = [], models = {}, enforce_detection = True
|
||||||
, detector_backend = 'mtcnn'):
|
, detector_backend = 'mtcnn'):
|
||||||
|
|
||||||
if type(img_path) == list:
|
img_paths, bulkProcess = initialize_input(img_path)
|
||||||
img_paths = img_path.copy()
|
|
||||||
bulkProcess = True
|
|
||||||
else:
|
|
||||||
img_paths = [img_path]
|
|
||||||
bulkProcess = False
|
|
||||||
|
|
||||||
#---------------------------------
|
|
||||||
|
|
||||||
functions.initialize_detector(detector_backend = detector_backend)
|
functions.initialize_detector(detector_backend = detector_backend)
|
||||||
|
|
||||||
#---------------------------------
|
#---------------------------------
|
||||||
@ -474,21 +458,16 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True
|
|||||||
|
|
||||||
def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'mtcnn'):
|
def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'mtcnn'):
|
||||||
|
|
||||||
model_names = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace']
|
|
||||||
metric_names = ['cosine', 'euclidean', 'euclidean_l2']
|
|
||||||
|
|
||||||
tic = time.time()
|
tic = time.time()
|
||||||
|
|
||||||
if type(img_path) == list:
|
img_paths, bulkProcess = initialize_input(img_path)
|
||||||
bulkProcess = True
|
functions.initialize_detector(detector_backend = detector_backend)
|
||||||
img_paths = img_path.copy()
|
|
||||||
else:
|
|
||||||
bulkProcess = False
|
|
||||||
img_paths = [img_path]
|
|
||||||
|
|
||||||
#-------------------------------
|
#-------------------------------
|
||||||
|
|
||||||
functions.initialize_detector(detector_backend = detector_backend)
|
#model metric pairs for ensemble
|
||||||
|
model_names = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace']
|
||||||
|
metric_names = ['cosine', 'euclidean', 'euclidean_l2']
|
||||||
|
|
||||||
#-------------------------------
|
#-------------------------------
|
||||||
|
|
||||||
@ -811,6 +790,32 @@ def detectFace(img_path, detector_backend = 'mtcnn'):
|
|||||||
|
|
||||||
img = functions.preprocess_face(img = img_path, detector_backend = detector_backend)[0] #preprocess_face returns (1, 224, 224, 3)
|
img = functions.preprocess_face(img = img_path, detector_backend = detector_backend)[0] #preprocess_face returns (1, 224, 224, 3)
|
||||||
return img[:, :, ::-1] #bgr to rgb
|
return img[:, :, ::-1] #bgr to rgb
|
||||||
|
|
||||||
|
def initialize_input(img1_path, img2_path = None):
|
||||||
|
|
||||||
|
"""
|
||||||
|
verify, analyze and find functions build complex machine learning models in every call.
|
||||||
|
To avoid memory problems, you can pass image pairs as array.
|
||||||
|
|
||||||
|
This function manages this usage is enabled or not
|
||||||
|
|
||||||
|
E.g.
|
||||||
|
result = DeepFace.verify("img1.jpg", "img2.jpg")
|
||||||
|
results = DeepFace.verify([['img1.jpg', 'img2.jpg'], ['img1.jpg', 'img3.jpg']])
|
||||||
|
"""
|
||||||
|
|
||||||
|
if type(img1_path) == list:
|
||||||
|
bulkProcess = True
|
||||||
|
img_list = img1_path.copy()
|
||||||
|
else:
|
||||||
|
bulkProcess = False
|
||||||
|
if img2_path != None:
|
||||||
|
img_list = [[img1_path, img2_path]]
|
||||||
|
else:
|
||||||
|
img_list = [img1_path]
|
||||||
|
|
||||||
|
return img_list, bulkProcess
|
||||||
|
|
||||||
#---------------------------
|
#---------------------------
|
||||||
#main
|
#main
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ dataset = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
"""
|
||||||
print("Face detectors test")
|
print("Face detectors test")
|
||||||
|
|
||||||
print("opencv detector")
|
print("opencv detector")
|
||||||
@ -94,7 +94,7 @@ print("Race: ", demography["dominant_race"])
|
|||||||
print("Emotion: ", demography["dominant_emotion"])
|
print("Emotion: ", demography["dominant_emotion"])
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
"""
|
||||||
print("Face recognition tests")
|
print("Face recognition tests")
|
||||||
|
|
||||||
dataset = [
|
dataset = [
|
||||||
@ -118,13 +118,17 @@ metrics = ['cosine', 'euclidean', 'euclidean_l2']
|
|||||||
passed_tests = 0; test_cases = 0
|
passed_tests = 0; test_cases = 0
|
||||||
|
|
||||||
for model in models:
|
for model in models:
|
||||||
|
prebuilt_model = DeepFace.build_model(model)
|
||||||
|
print(model," is built")
|
||||||
for metric in metrics:
|
for metric in metrics:
|
||||||
for instance in dataset:
|
for instance in dataset:
|
||||||
img1 = instance[0]
|
img1 = instance[0]
|
||||||
img2 = instance[1]
|
img2 = instance[1]
|
||||||
result = instance[2]
|
result = instance[2]
|
||||||
|
|
||||||
resp_obj = DeepFace.verify(img1, img2, model_name = model, distance_metric = metric)
|
resp_obj = DeepFace.verify(img1, img2
|
||||||
|
, model_name = model, model = prebuilt_model
|
||||||
|
, distance_metric = metric)
|
||||||
|
|
||||||
prediction = resp_obj["verified"]
|
prediction = resp_obj["verified"]
|
||||||
distance = round(resp_obj["distance"], 2)
|
distance = round(resp_obj["distance"], 2)
|
||||||
@ -142,7 +146,7 @@ for model in models:
|
|||||||
|
|
||||||
test_cases = test_cases + 1
|
test_cases = test_cases + 1
|
||||||
|
|
||||||
print(img1.split("/")[-1], "and", img2.split("/")[-1],"are", classified_label, "as same person based on", model,"model and",metric,"distance. Distance:",distance,", Threshold:", required_threshold,"(",test_result_label,")")
|
print(img1.split("/")[-1], "-", img2.split("/")[-1], classified_label, "as same person based on", model,"and",metric,". Distance:",distance,", Threshold:", required_threshold,"(",test_result_label,")")
|
||||||
|
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user