some changes on api services

- exception handling added in service
- new folder structure for api
This commit is contained in:
Sefik Ilkin Serengil 2024-02-04 10:38:34 +00:00
parent fe977673be
commit 53890974ea
6 changed files with 68 additions and 49 deletions

0
api/src/__init__.py Normal file
View File

View File

@ -1,6 +1,6 @@
# 3rd parth dependencies # 3rd parth dependencies
from flask import Flask from flask import Flask
from routes import blueprint from modules.core.routes import blueprint
def create_app(): def create_app():

View File

View File

@ -1,5 +1,8 @@
from flask import Blueprint, request from flask import Blueprint, request
import service from modules.core import service
from deepface.commons.logger import Logger
logger = Logger(module="api/src/routes.py")
blueprint = Blueprint("routes", __name__) blueprint = Blueprint("routes", __name__)
@ -16,7 +19,7 @@ def represent():
if input_args is None: if input_args is None:
return {"message": "empty input set passed"} return {"message": "empty input set passed"}
img_path = input_args.get("img") img_path = input_args.get("img") or input_args.get("img_path")
if img_path is None: if img_path is None:
return {"message": "you must pass img_path input"} return {"message": "you must pass img_path input"}
@ -33,6 +36,8 @@ def represent():
align=align, align=align,
) )
logger.debug(obj)
return obj return obj
@ -43,8 +48,8 @@ def verify():
if input_args is None: if input_args is None:
return {"message": "empty input set passed"} return {"message": "empty input set passed"}
img1_path = input_args.get("img1_path") img1_path = input_args.get("img1") or input_args.get("img1_path")
img2_path = input_args.get("img2_path") img2_path = input_args.get("img2") or input_args.get("img2_path")
if img1_path is None: if img1_path is None:
return {"message": "you must pass img1_path input"} return {"message": "you must pass img1_path input"}
@ -68,7 +73,7 @@ def verify():
enforce_detection=enforce_detection, enforce_detection=enforce_detection,
) )
verification["verified"] = str(verification["verified"]) logger.debug(verification)
return verification return verification
@ -80,7 +85,7 @@ def analyze():
if input_args is None: if input_args is None:
return {"message": "empty input set passed"} return {"message": "empty input set passed"}
img_path = input_args.get("img_path") img_path = input_args.get("img") or input_args.get("img_path")
if img_path is None: if img_path is None:
return {"message": "you must pass img_path input"} return {"message": "you must pass img_path input"}
@ -97,4 +102,6 @@ def analyze():
align=align, align=align,
) )
logger.debug(demographies)
return demographies return demographies

View File

@ -0,0 +1,54 @@
from deepface import DeepFace
# pylint: disable=broad-except
def represent(img_path, model_name, detector_backend, enforce_detection, align):
try:
result = {}
embedding_objs = DeepFace.represent(
img_path=img_path,
model_name=model_name,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
)
result["results"] = embedding_objs
return result
except Exception as err:
return {"error": f"Exception while representing: {str(err)}"}, 400
def verify(
img1_path, img2_path, model_name, detector_backend, distance_metric, enforce_detection, align
):
try:
obj = DeepFace.verify(
img1_path=img1_path,
img2_path=img2_path,
model_name=model_name,
detector_backend=detector_backend,
distance_metric=distance_metric,
align=align,
enforce_detection=enforce_detection,
)
return obj
except Exception as err:
return {"error": f"Exception while verifying: {str(err)}"}, 400
def analyze(img_path, actions, detector_backend, enforce_detection, align):
try:
result = {}
demographies = DeepFace.analyze(
img_path=img_path,
actions=actions,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
silent=True,
)
result["results"] = demographies
return result
except Exception as err:
return {"error": f"Exception while analyzing: {str(err)}"}, 400

View File

@ -1,42 +0,0 @@
from deepface import DeepFace
def represent(img_path, model_name, detector_backend, enforce_detection, align):
result = {}
embedding_objs = DeepFace.represent(
img_path=img_path,
model_name=model_name,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
)
result["results"] = embedding_objs
return result
def verify(
img1_path, img2_path, model_name, detector_backend, distance_metric, enforce_detection, align
):
obj = DeepFace.verify(
img1_path=img1_path,
img2_path=img2_path,
model_name=model_name,
detector_backend=detector_backend,
distance_metric=distance_metric,
align=align,
enforce_detection=enforce_detection,
)
return obj
def analyze(img_path, actions, detector_backend, enforce_detection, align):
result = {}
demographies = DeepFace.analyze(
img_path=img_path,
actions=actions,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
)
result["results"] = demographies
return result