represent function

This commit is contained in:
Sefik Ilkin Serengil 2021-04-10 12:30:24 +03:00
parent d51e628fb1
commit d43aacb87e
2 changed files with 290 additions and 242 deletions

View File

@ -48,8 +48,7 @@ def build_model(model_name):
else: else:
raise ValueError('Invalid model_name passed - {}'.format(model_name)) raise ValueError('Invalid model_name passed - {}'.format(model_name))
def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric = 'cosine', def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'mtcnn'):
model = None, enforce_detection = True, detector_backend = 'mtcnn'):
""" """
This function verifies an image pair is same person or different persons. This function verifies an image pair is same person or different persons.
@ -255,9 +254,7 @@ def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric =
return resp_obj return resp_obj
def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] , models = {}, enforce_detection = True, detector_backend = 'mtcnn'):
, models = {}, enforce_detection = True
, detector_backend = 'mtcnn'):
""" """
This function analyzes facial attributes including age, gender, emotion and race This function analyzes facial attributes including age, gender, emotion and race
@ -726,9 +723,48 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
return None return None
def stream(db_path = '', model_name ='VGG-Face', distance_metric = 'cosine' def represent(img_path, model_name = 'VGG-Face', distance_metric = 'euclidean', model = None, enforce_detection = True, detector_backend = 'mtcnn'):
, enable_face_analysis = True
, source = 0, time_threshold = 5, frame_threshold = 5): """
This function represents facial images as vectors.
Parameters:
img_path: exact image path, numpy array or based64 encoded images could be passed.
model_name (string): VGG-Face, Facenet, OpenFace, DeepFace, DeepID, Dlib, ArcFace.
distance_metric (string): cosine, euclidean, euclidean_l2
model: Built deepface model. A face recognition model is built every call of verify function. You can pass pre-built face recognition model optionally if you will call verify function several times. Consider to pass model if you are going to call represent function in a for loop.
model = DeepFace.build_model('VGG-Face')
enforce_detection (boolean): If any face could not be detected in an image, then verify function will return exception. Set this to False not to have this exception. This might be convenient for low resolution images.
detector_backend (string): set face detector backend as mtcnn, opencv, ssd or dlib
Returns:
Represent function returns a multidimensional vector. The number of dimensions is changing based on the reference model. E.g. FaceNet returns 128 dimensional vector; VGG-Face returns 2622 dimensional vector.
"""
if model is None:
model = build_model(model_name)
#---------------------------------
#decide input shape
input_shape = input_shape_x, input_shape_y= functions.find_input_shape(model)
img = functions.preprocess_face(img = img_path
, target_size=(input_shape_y, input_shape_x)
, enforce_detection = enforce_detection
, detector_backend = detector_backend)
embedding = model.predict(img)[0].tolist()
return embedding
def stream(db_path = '', model_name ='VGG-Face', distance_metric = 'cosine', enable_face_analysis = True, source = 0, time_threshold = 5, frame_threshold = 5):
""" """
This function applies real time face recognition and facial attribute analysis This function applies real time face recognition and facial attribute analysis

View File

@ -26,6 +26,18 @@ from deepface.extendedmodels import Age, Gender, Race, Emotion
#----------------------------------------- #-----------------------------------------
img_path = "dataset/img1.jpg"
embedding = DeepFace.represent(img_path)
print("Function returned ", len(embedding), "dimensional vector")
model_name = "VGG-Face"
model = DeepFace.build_model(model_name)
print(model_name," is built")
embedding = DeepFace.represent(img_path, model = model)
print("Represent function returned ", len(embedding), "dimensional vector")
#-----------------------------------------
dataset = [ dataset = [
['dataset/img1.jpg', 'dataset/img2.jpg', True], ['dataset/img1.jpg', 'dataset/img2.jpg', True],
['dataset/img1.jpg', 'dataset/img6.jpg', True] ['dataset/img1.jpg', 'dataset/img6.jpg', True]
@ -64,9 +76,9 @@ print("-----------------------------------------")
print("Pre-built model for single find function test") print("Pre-built model for single find function test")
model_name = "VGG-Face" #model_name = "VGG-Face"
model = DeepFace.build_model(model_name) #model = DeepFace.build_model(model_name)
print(model_name," is built") #print(model_name," is built")
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset" df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
, model_name = model_name, model = model , model_name = model_name, model = model