mirror of
https://github.com/serengil/deepface.git
synced 2025-07-23 10:20:03 +00:00
api becomes compatible /w tf2
This commit is contained in:
parent
5b24de8de2
commit
3e93c47d09
201
api/api.py
201
api/api.py
@ -7,6 +7,7 @@ import time
|
||||
from tqdm import tqdm
|
||||
|
||||
import tensorflow as tf
|
||||
tf_version = int(tf.__version__.split(".")[0])
|
||||
|
||||
from deepface import DeepFace
|
||||
from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace, DeepID
|
||||
@ -87,7 +88,8 @@ print("Facial attribute analysis models are built in ", toc-tic," seconds")
|
||||
|
||||
#------------------------------
|
||||
|
||||
graph = tf.get_default_graph()
|
||||
if tf_version == 1:
|
||||
graph = tf.get_default_graph()
|
||||
|
||||
#------------------------------
|
||||
#Service API Interface
|
||||
@ -107,32 +109,13 @@ def analyze():
|
||||
|
||||
#---------------------------
|
||||
|
||||
resp_obj = jsonify({'success': False})
|
||||
with graph.as_default():
|
||||
instances = []
|
||||
if "img" in list(req.keys()):
|
||||
raw_content = req["img"] #list
|
||||
|
||||
for item in raw_content: #item is in type of dict
|
||||
instances.append(item)
|
||||
if tf_version == 1:
|
||||
with graph.as_default():
|
||||
resp_obj = analyzeWrapper(req, trx_id)
|
||||
elif tf_version == 2:
|
||||
resp_obj = analyzeWrapper(req, trx_id)
|
||||
|
||||
if len(instances) == 0:
|
||||
return jsonify({'success': False, 'error': 'you must pass at least one img object in your request'}), 205
|
||||
|
||||
print("Analyzing ", len(instances)," instances")
|
||||
|
||||
#---------------------------
|
||||
|
||||
actions= ['emotion', 'age', 'gender', 'race']
|
||||
if "actions" in list(req.keys()):
|
||||
actions = req["actions"]
|
||||
|
||||
#---------------------------
|
||||
|
||||
#resp_obj = DeepFace.analyze(instances, actions=actions)
|
||||
resp_obj = DeepFace.analyze(instances, actions=actions, models=facial_attribute_models)
|
||||
|
||||
#---------------------------
|
||||
#---------------------------
|
||||
|
||||
toc = time.time()
|
||||
|
||||
@ -141,8 +124,35 @@ def analyze():
|
||||
|
||||
return resp_obj, 200
|
||||
|
||||
@app.route('/verify', methods=['POST'])
|
||||
def analyzeWrapper(req, trx_id = 0):
|
||||
resp_obj = jsonify({'success': False})
|
||||
|
||||
instances = []
|
||||
if "img" in list(req.keys()):
|
||||
raw_content = req["img"] #list
|
||||
|
||||
for item in raw_content: #item is in type of dict
|
||||
instances.append(item)
|
||||
|
||||
if len(instances) == 0:
|
||||
return jsonify({'success': False, 'error': 'you must pass at least one img object in your request'}), 205
|
||||
|
||||
print("Analyzing ", len(instances)," instances")
|
||||
|
||||
#---------------------------
|
||||
|
||||
actions= ['emotion', 'age', 'gender', 'race']
|
||||
if "actions" in list(req.keys()):
|
||||
actions = req["actions"]
|
||||
|
||||
#---------------------------
|
||||
|
||||
#resp_obj = DeepFace.analyze(instances, actions=actions)
|
||||
resp_obj = DeepFace.analyze(instances, actions=actions, models=facial_attribute_models)
|
||||
|
||||
return resp_obj
|
||||
|
||||
@app.route('/verify', methods=['POST'])
|
||||
def verify():
|
||||
|
||||
global graph
|
||||
@ -153,70 +163,11 @@ def verify():
|
||||
|
||||
resp_obj = jsonify({'success': False})
|
||||
|
||||
with graph.as_default():
|
||||
|
||||
model_name = "VGG-Face"; distance_metric = "cosine"
|
||||
if "model_name" in list(req.keys()):
|
||||
model_name = req["model_name"]
|
||||
if "distance_metric" in list(req.keys()):
|
||||
distance_metric = req["distance_metric"]
|
||||
|
||||
#----------------------
|
||||
|
||||
instances = []
|
||||
if "img" in list(req.keys()):
|
||||
raw_content = req["img"] #list
|
||||
|
||||
for item in raw_content: #item is in type of dict
|
||||
instance = []
|
||||
img1 = item["img1"]; img2 = item["img2"]
|
||||
|
||||
validate_img1 = False
|
||||
if len(img1) > 11 and img1[0:11] == "data:image/":
|
||||
validate_img1 = True
|
||||
|
||||
validate_img2 = False
|
||||
if len(img2) > 11 and img2[0:11] == "data:image/":
|
||||
validate_img2 = True
|
||||
|
||||
if validate_img1 != True or validate_img2 != True:
|
||||
return jsonify({'success': False, 'error': 'you must pass both img1 and img2 as base64 encoded string'}), 205
|
||||
|
||||
instance.append(img1); instance.append(img2)
|
||||
instances.append(instance)
|
||||
|
||||
#--------------------------
|
||||
|
||||
if len(instances) == 0:
|
||||
return jsonify({'success': False, 'error': 'you must pass at least one img object in your request'}), 205
|
||||
|
||||
print("Input request of ", trx_id, " has ",len(instances)," pairs to verify")
|
||||
|
||||
#--------------------------
|
||||
|
||||
if model_name == "VGG-Face":
|
||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = vggface_model)
|
||||
elif model_name == "Facenet":
|
||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = facenet_model)
|
||||
elif model_name == "OpenFace":
|
||||
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 == "Dlib":
|
||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = dlib_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)
|
||||
|
||||
else:
|
||||
return jsonify({'success': False, 'error': 'You must pass a valid model name. Available models are VGG-Face, Facenet, OpenFace, DeepFace but you passed %s' % (model_name)}), 205
|
||||
if tf_version == 1:
|
||||
with graph.as_default():
|
||||
resp_obj = verifyWrapper(req, trx_id)
|
||||
elif tf_version == 2:
|
||||
resp_obj = verifyWrapper(req, trx_id)
|
||||
|
||||
#--------------------------
|
||||
|
||||
@ -227,6 +178,74 @@ def verify():
|
||||
|
||||
return resp_obj, 200
|
||||
|
||||
def verifyWrapper(req, trx_id = 0):
|
||||
|
||||
resp_obj = jsonify({'success': False})
|
||||
|
||||
model_name = "VGG-Face"; distance_metric = "cosine"
|
||||
if "model_name" in list(req.keys()):
|
||||
model_name = req["model_name"]
|
||||
if "distance_metric" in list(req.keys()):
|
||||
distance_metric = req["distance_metric"]
|
||||
|
||||
#----------------------
|
||||
|
||||
instances = []
|
||||
if "img" in list(req.keys()):
|
||||
raw_content = req["img"] #list
|
||||
|
||||
for item in raw_content: #item is in type of dict
|
||||
instance = []
|
||||
img1 = item["img1"]; img2 = item["img2"]
|
||||
|
||||
validate_img1 = False
|
||||
if len(img1) > 11 and img1[0:11] == "data:image/":
|
||||
validate_img1 = True
|
||||
|
||||
validate_img2 = False
|
||||
if len(img2) > 11 and img2[0:11] == "data:image/":
|
||||
validate_img2 = True
|
||||
|
||||
if validate_img1 != True or validate_img2 != True:
|
||||
return jsonify({'success': False, 'error': 'you must pass both img1 and img2 as base64 encoded string'}), 205
|
||||
|
||||
instance.append(img1); instance.append(img2)
|
||||
instances.append(instance)
|
||||
|
||||
#--------------------------
|
||||
|
||||
if len(instances) == 0:
|
||||
return jsonify({'success': False, 'error': 'you must pass at least one img object in your request'}), 205
|
||||
|
||||
print("Input request of ", trx_id, " has ",len(instances)," pairs to verify")
|
||||
|
||||
#--------------------------
|
||||
|
||||
if model_name == "VGG-Face":
|
||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = vggface_model)
|
||||
elif model_name == "Facenet":
|
||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = facenet_model)
|
||||
elif model_name == "OpenFace":
|
||||
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 == "Dlib":
|
||||
resp_obj = DeepFace.verify(instances, model_name = model_name, distance_metric = distance_metric, model = dlib_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)
|
||||
|
||||
else:
|
||||
resp_obj = jsonify({'success': False, 'error': 'You must pass a valid model name. Available models are VGG-Face, Facenet, OpenFace, DeepFace but you passed %s' % (model_name)}), 205
|
||||
|
||||
return resp_obj
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
|
Loading…
x
Reference in New Issue
Block a user