mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
lightweight api
This commit is contained in:
parent
dcd883f223
commit
0fc1672c89
153
api/api.py
153
api/api.py
@ -35,71 +35,6 @@ app = Flask(__name__)
|
|||||||
|
|
||||||
#------------------------------
|
#------------------------------
|
||||||
|
|
||||||
tic = time.time()
|
|
||||||
|
|
||||||
print("Loading Face Recognition Models...")
|
|
||||||
|
|
||||||
pbar = tqdm(range(0, 6), desc='Loading Face Recognition Models...')
|
|
||||||
|
|
||||||
for index in pbar:
|
|
||||||
|
|
||||||
if index == 0:
|
|
||||||
pbar.set_description("Loading VGG-Face")
|
|
||||||
vggface_model = DeepFace.build_model("VGG-Face")
|
|
||||||
elif index == 1:
|
|
||||||
pbar.set_description("Loading OpenFace")
|
|
||||||
openface_model = DeepFace.build_model("OpenFace")
|
|
||||||
elif index == 2:
|
|
||||||
pbar.set_description("Loading Google FaceNet")
|
|
||||||
facenet_model = DeepFace.build_model("Facenet")
|
|
||||||
elif index == 3:
|
|
||||||
pbar.set_description("Loading Facebook DeepFace")
|
|
||||||
deepface_model = DeepFace.build_model("DeepFace")
|
|
||||||
elif index == 4:
|
|
||||||
pbar.set_description("Loading DeepID DeepFace")
|
|
||||||
deepid_model = DeepFace.build_model("DeepID")
|
|
||||||
elif index == 5:
|
|
||||||
pbar.set_description("Loading ArcFace DeepFace")
|
|
||||||
arcface_model = DeepFace.build_model("ArcFace")
|
|
||||||
|
|
||||||
toc = time.time()
|
|
||||||
|
|
||||||
print("Face recognition models are built in ", toc-tic," seconds")
|
|
||||||
|
|
||||||
#------------------------------
|
|
||||||
|
|
||||||
tic = time.time()
|
|
||||||
|
|
||||||
print("Loading Facial Attribute Analysis Models...")
|
|
||||||
|
|
||||||
pbar = tqdm(range(0,4), desc='Loading Facial Attribute Analysis Models...')
|
|
||||||
|
|
||||||
for index in pbar:
|
|
||||||
if index == 0:
|
|
||||||
pbar.set_description("Loading emotion analysis model")
|
|
||||||
emotion_model = DeepFace.build_model('Emotion')
|
|
||||||
elif index == 1:
|
|
||||||
pbar.set_description("Loading age prediction model")
|
|
||||||
age_model = DeepFace.build_model('Age')
|
|
||||||
elif index == 2:
|
|
||||||
pbar.set_description("Loading gender prediction model")
|
|
||||||
gender_model = DeepFace.build_model('Gender')
|
|
||||||
elif index == 3:
|
|
||||||
pbar.set_description("Loading race prediction model")
|
|
||||||
race_model = DeepFace.build_model('Race')
|
|
||||||
|
|
||||||
toc = time.time()
|
|
||||||
|
|
||||||
facial_attribute_models = {}
|
|
||||||
facial_attribute_models["emotion"] = emotion_model
|
|
||||||
facial_attribute_models["age"] = age_model
|
|
||||||
facial_attribute_models["gender"] = gender_model
|
|
||||||
facial_attribute_models["race"] = race_model
|
|
||||||
|
|
||||||
print("Facial attribute analysis models are built in ", toc-tic," seconds")
|
|
||||||
|
|
||||||
#------------------------------
|
|
||||||
|
|
||||||
if tf_version == 1:
|
if tf_version == 1:
|
||||||
graph = tf.get_default_graph()
|
graph = tf.get_default_graph()
|
||||||
|
|
||||||
@ -153,15 +88,26 @@ def analyzeWrapper(req, trx_id = 0):
|
|||||||
|
|
||||||
#---------------------------
|
#---------------------------
|
||||||
|
|
||||||
|
detector_backend = 'opencv'
|
||||||
|
|
||||||
actions= ['emotion', 'age', 'gender', 'race']
|
actions= ['emotion', 'age', 'gender', 'race']
|
||||||
|
|
||||||
if "actions" in list(req.keys()):
|
if "actions" in list(req.keys()):
|
||||||
actions = req["actions"]
|
actions = req["actions"]
|
||||||
|
|
||||||
|
if "detector_backend" in list(req.keys()):
|
||||||
|
detector_backend = req["detector_backend"]
|
||||||
|
|
||||||
#---------------------------
|
#---------------------------
|
||||||
|
|
||||||
#resp_obj = DeepFace.analyze(instances, actions=actions)
|
try:
|
||||||
resp_obj = DeepFace.analyze(instances, actions=actions, models=facial_attribute_models)
|
resp_obj = DeepFace.analyze(instances, actions = actions)
|
||||||
|
except Exception as err:
|
||||||
|
print("Exception: ", str(err))
|
||||||
|
return jsonify({'success': False, 'error': str(err)}), 205
|
||||||
|
|
||||||
|
#---------------
|
||||||
|
#print(resp_obj)
|
||||||
return resp_obj
|
return resp_obj
|
||||||
|
|
||||||
@app.route('/verify', methods=['POST'])
|
@app.route('/verify', methods=['POST'])
|
||||||
@ -194,11 +140,13 @@ def verifyWrapper(req, trx_id = 0):
|
|||||||
|
|
||||||
resp_obj = jsonify({'success': False})
|
resp_obj = jsonify({'success': False})
|
||||||
|
|
||||||
model_name = "VGG-Face"; distance_metric = "cosine"
|
model_name = "VGG-Face"; distance_metric = "cosine"; detector_backend = "opencv"
|
||||||
if "model_name" in list(req.keys()):
|
if "model_name" in list(req.keys()):
|
||||||
model_name = req["model_name"]
|
model_name = req["model_name"]
|
||||||
if "distance_metric" in list(req.keys()):
|
if "distance_metric" in list(req.keys()):
|
||||||
distance_metric = req["distance_metric"]
|
distance_metric = req["distance_metric"]
|
||||||
|
if "detector_backend" in list(req.keys()):
|
||||||
|
detector_backend = req["detector_backend"]
|
||||||
|
|
||||||
#----------------------
|
#----------------------
|
||||||
|
|
||||||
@ -233,31 +181,19 @@ def verifyWrapper(req, trx_id = 0):
|
|||||||
|
|
||||||
#--------------------------
|
#--------------------------
|
||||||
|
|
||||||
if model_name == "VGG-Face":
|
try:
|
||||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = vggface_model)
|
resp_obj = DeepFace.verify(instances
|
||||||
elif model_name == "Facenet":
|
, model_name = model_name
|
||||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = facenet_model)
|
, distance_metric = distance_metric
|
||||||
elif model_name == "OpenFace":
|
, detector_backend = detector_backend
|
||||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = openface_model)
|
)
|
||||||
elif model_name == "DeepFace":
|
|
||||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = deepface_model)
|
|
||||||
elif model_name == "DeepID":
|
|
||||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = deepid_model)
|
|
||||||
elif model_name == "ArcFace":
|
|
||||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = arcface_model)
|
|
||||||
elif model_name == "Ensemble":
|
|
||||||
models = {}
|
|
||||||
models["VGG-Face"] = vggface_model
|
|
||||||
models["Facenet"] = facenet_model
|
|
||||||
models["OpenFace"] = openface_model
|
|
||||||
models["DeepFace"] = deepface_model
|
|
||||||
resp_obj = DeepFace.verify(instances, model_name = model_name, model = models)
|
|
||||||
|
|
||||||
for key in resp_obj: #issue 198.
|
if model_name == "Ensemble": #issue 198.
|
||||||
resp_obj[key]['verified'] = bool(resp_obj[key]['verified'])
|
for key in resp_obj: #issue 198.
|
||||||
|
resp_obj[key]['verified'] = bool(resp_obj[key]['verified'])
|
||||||
|
|
||||||
else:
|
except Exception as err:
|
||||||
resp_obj = jsonify({'success': False, 'error': 'You must pass a valid model name. You passed %s' % (model_name)}), 205
|
resp_obj = jsonify({'success': False, 'error': str(err)}), 205
|
||||||
|
|
||||||
return resp_obj
|
return resp_obj
|
||||||
|
|
||||||
@ -294,11 +230,14 @@ def representWrapper(req, trx_id = 0):
|
|||||||
#-------------------------------------
|
#-------------------------------------
|
||||||
#find out model
|
#find out model
|
||||||
|
|
||||||
model_name = "VGG-Face"; distance_metric = "cosine"
|
model_name = "VGG-Face"; distance_metric = "cosine"; detector_backend = 'opencv'
|
||||||
|
|
||||||
if "model_name" in list(req.keys()):
|
if "model_name" in list(req.keys()):
|
||||||
model_name = req["model_name"]
|
model_name = req["model_name"]
|
||||||
|
|
||||||
|
if "detector_backend" in list(req.keys()):
|
||||||
|
detector_backend = req["detector_backend"]
|
||||||
|
|
||||||
#-------------------------------------
|
#-------------------------------------
|
||||||
#retrieve images from request
|
#retrieve images from request
|
||||||
|
|
||||||
@ -316,27 +255,25 @@ def representWrapper(req, trx_id = 0):
|
|||||||
return jsonify({'success': False, 'error': 'you must pass img as base64 encoded string'}), 205
|
return jsonify({'success': False, 'error': 'you must pass img as base64 encoded string'}), 205
|
||||||
|
|
||||||
#-------------------------------------
|
#-------------------------------------
|
||||||
#cal represent function from the interface
|
#call represent function from the interface
|
||||||
|
|
||||||
embedding = []
|
try:
|
||||||
if model_name == "VGG-Face":
|
|
||||||
embedding = DeepFace.represent(img, model_name = model_name, model = vggface_model)
|
embedding = DeepFace.represent(img
|
||||||
elif model_name == "Facenet":
|
, model_name = model_name
|
||||||
embedding = DeepFace.represent(img, model_name = model_name, model = facenet_model)
|
, detector_backend = detector_backend
|
||||||
elif model_name == "OpenFace":
|
)
|
||||||
embedding = DeepFace.represent(img, model_name = model_name, model = openface_model)
|
|
||||||
elif model_name == "DeepFace":
|
except Exception as err:
|
||||||
embedding = DeepFace.represent(img, model_name = model_name, model = deepface_model)
|
print("Exception: ",str(err))
|
||||||
elif model_name == "DeepID":
|
resp_obj = jsonify({'success': False, 'error': str(err)}), 205
|
||||||
embedding = DeepFace.represent(img, model_name = model_name, model = deepid_model)
|
|
||||||
elif model_name == "ArcFace":
|
#-------------------------------------
|
||||||
embedding = DeepFace.represent(img, model_name = model_name, model = arcface_model)
|
|
||||||
else:
|
|
||||||
resp_obj = jsonify({'success': False, 'error': 'You must pass a valid model name. You passed %s' % (model_name)}), 205
|
|
||||||
|
|
||||||
#print("embedding is ", len(embedding)," dimensional vector")
|
#print("embedding is ", len(embedding)," dimensional vector")
|
||||||
resp_obj = {}
|
resp_obj = {}
|
||||||
resp_obj["embedding"] = embedding
|
resp_obj["embedding"] = embedding
|
||||||
|
|
||||||
#-------------------------------------
|
#-------------------------------------
|
||||||
|
|
||||||
return resp_obj
|
return resp_obj
|
||||||
|
@ -360,13 +360,15 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] , models =
|
|||||||
|
|
||||||
disable_option = False if len(actions) > 1 else True
|
disable_option = False if len(actions) > 1 else True
|
||||||
|
|
||||||
pbar = tqdm(range(0,len(actions)), desc='Finding actions', disable = disable_option)
|
pbar = tqdm(range(0, len(actions)), desc='Finding actions', disable = disable_option)
|
||||||
|
|
||||||
img_224 = None # Set to prevent re-detection
|
img_224 = None # Set to prevent re-detection
|
||||||
|
|
||||||
region = [] # x, y, w, h of the detected face region
|
region = [] # x, y, w, h of the detected face region
|
||||||
region_labels = ['x', 'y', 'w', 'h']
|
region_labels = ['x', 'y', 'w', 'h']
|
||||||
|
|
||||||
|
is_region_set = False
|
||||||
|
|
||||||
#facial attribute analysis
|
#facial attribute analysis
|
||||||
for index in pbar:
|
for index in pbar:
|
||||||
action = actions[index]
|
action = actions[index]
|
||||||
@ -376,10 +378,11 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] , models =
|
|||||||
emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
|
emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
|
||||||
img, region = functions.preprocess_face(img = img_path, target_size = (48, 48), grayscale = True, enforce_detection = enforce_detection, detector_backend = detector_backend, return_region = True)
|
img, region = functions.preprocess_face(img = img_path, target_size = (48, 48), grayscale = True, enforce_detection = enforce_detection, detector_backend = detector_backend, return_region = True)
|
||||||
|
|
||||||
resp_obj["region"] = {}
|
if is_region_set != True:
|
||||||
|
resp_obj["region"] = {}
|
||||||
for i, parameter in enumerate(region_labels):
|
is_region_set = True
|
||||||
resp_obj["region"][parameter] = region[i]
|
for i, parameter in enumerate(region_labels):
|
||||||
|
resp_obj["region"][parameter] = int(region[i]) #int cast is for the exception - object of type 'float32' is not JSON serializable
|
||||||
|
|
||||||
emotion_predictions = models['emotion'].predict(img)[0,:]
|
emotion_predictions = models['emotion'].predict(img)[0,:]
|
||||||
|
|
||||||
@ -398,24 +401,27 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] , models =
|
|||||||
if img_224 is None:
|
if img_224 is None:
|
||||||
img_224, region = functions.preprocess_face(img = img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection, detector_backend = detector_backend, return_region = True)
|
img_224, region = functions.preprocess_face(img = img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection, detector_backend = detector_backend, return_region = True)
|
||||||
|
|
||||||
resp_obj["region"] = {}
|
if is_region_set != True:
|
||||||
|
resp_obj["region"] = {}
|
||||||
for i, parameter in enumerate(region_labels):
|
is_region_set = True
|
||||||
resp_obj["region"][parameter] = region[i]
|
for i, parameter in enumerate(region_labels):
|
||||||
|
resp_obj["region"][parameter] = int(region[i]) # #int cast is for the exception - object of type 'float32' is not JSON serializable
|
||||||
|
|
||||||
age_predictions = models['age'].predict(img_224)[0,:]
|
age_predictions = models['age'].predict(img_224)[0,:]
|
||||||
apparent_age = Age.findApparentAge(age_predictions)
|
apparent_age = Age.findApparentAge(age_predictions)
|
||||||
|
|
||||||
resp_obj["age"] = int(apparent_age)
|
resp_obj["age"] = int(apparent_age)
|
||||||
|
#int cast is for the exception - object of type 'float32' is not JSON serializable
|
||||||
|
|
||||||
elif action == 'gender':
|
elif action == 'gender':
|
||||||
if img_224 is None:
|
if img_224 is None:
|
||||||
img_224, region = functions.preprocess_face(img = img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection, detector_backend = detector_backend, return_region = True)
|
img_224, region = functions.preprocess_face(img = img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection, detector_backend = detector_backend, return_region = True)
|
||||||
|
|
||||||
resp_obj["region"] = {}
|
if is_region_set != True:
|
||||||
|
resp_obj["region"] = {}
|
||||||
for i, parameter in enumerate(region_labels):
|
is_region_set = True
|
||||||
resp_obj["region"][parameter] = region[i]
|
for i, parameter in enumerate(region_labels):
|
||||||
|
resp_obj["region"][parameter] = int(region[i]) ##int cast is for the exception - object of type 'float32' is not JSON serializable
|
||||||
|
|
||||||
gender_prediction = models['gender'].predict(img_224)[0,:]
|
gender_prediction = models['gender'].predict(img_224)[0,:]
|
||||||
|
|
||||||
@ -432,10 +438,11 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] , models =
|
|||||||
race_predictions = models['race'].predict(img_224)[0,:]
|
race_predictions = models['race'].predict(img_224)[0,:]
|
||||||
race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic']
|
race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic']
|
||||||
|
|
||||||
resp_obj["region"] = {}
|
if is_region_set != True:
|
||||||
|
resp_obj["region"] = {}
|
||||||
for i, parameter in enumerate(region_labels):
|
is_region_set = True
|
||||||
resp_obj["region"][parameter] = region[i]
|
for i, parameter in enumerate(region_labels):
|
||||||
|
resp_obj["region"][parameter] = int(region[i]) ##int cast is for the exception - object of type 'float32' is not JSON serializable
|
||||||
|
|
||||||
sum_of_predictions = race_predictions.sum()
|
sum_of_predictions = race_predictions.sum()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user