From 9816b8f2bdf9648838a770a3f719e48459c7bd21 Mon Sep 17 00:00:00 2001 From: Arsalan Rabdanov Date: Mon, 2 Sep 2024 13:36:19 +0300 Subject: [PATCH] added extract service route --- .gitignore | 3 ++- deepface/api/src/modules/core/routes.py | 23 +++++++++++++++++++++++ deepface/api/src/modules/core/service.py | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b8359b9..5dedb26 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ tests/*.csv benchmarks/results benchmarks/outputs benchmarks/dataset -benchmarks/lfwe \ No newline at end of file +benchmarks/lfwe +venv \ No newline at end of file diff --git a/deepface/api/src/modules/core/routes.py b/deepface/api/src/modules/core/routes.py index 98f624a..24c9ddf 100644 --- a/deepface/api/src/modules/core/routes.py +++ b/deepface/api/src/modules/core/routes.py @@ -12,6 +12,29 @@ blueprint = Blueprint("routes", __name__) def home(): return f"

Welcome to DeepFace API v{DeepFace.__version__}!

" +@blueprint.route("/represent", methods=["POST"]) +def extract(): + input_args = request.get_json() + + if input_args is None: + return {"message": "empty input set passed"} + + img_path = input_args.get("img") or input_args.get("img_path") + if img_path is None: + return {"message": "you must pass img_path input"} + + obj = service.extract_faces( + img_path=img_path, + detector_backend=input_args.get("detector_backend", "opencv"), + enforce_detection=input_args.get("enforce_detection", True), + align=input_args.get("align", True), + anti_spoofing=input_args.get("anti_spoofing", False), + ) + + logger.debug(obj) + + return obj + @blueprint.route("/represent", methods=["POST"]) def represent(): diff --git a/deepface/api/src/modules/core/service.py b/deepface/api/src/modules/core/service.py index 4188229..ef0464a 100644 --- a/deepface/api/src/modules/core/service.py +++ b/deepface/api/src/modules/core/service.py @@ -31,6 +31,27 @@ def represent( tb_str = traceback.format_exc() return {"error": f"Exception while representing: {str(err)} - {tb_str}"}, 400 +def extract_faces( + img_path: str, + detector_backend: str, + enforce_detection: bool, + align: bool, + anti_spoofing: bool, +): + try: + result = {} + faces_objs = DeepFace.extract_faces( + img_path=img_path, + detector_backend=detector_backend, + enforce_detection=enforce_detection, + align=align, + anti_spoofing=anti_spoofing, + ) + result["results"] = faces_objs + return result + except Exception as err: + tb_str = traceback.format_exc() + return {"error": f"Exception while extracting faces: {str(err)} - {tb_str}"}, 400 def verify( img1_path: str,