mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
load image either from json or form data
This commit is contained in:
parent
8b2475acb2
commit
60067e24a0
@ -1,31 +1,86 @@
|
|||||||
|
# built-in dependencies
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
# 3rd party dependencies
|
||||||
from flask import Blueprint, request
|
from flask import Blueprint, request
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# project dependencies
|
||||||
from deepface import DeepFace
|
from deepface import DeepFace
|
||||||
from deepface.api.src.modules.core import service
|
from deepface.api.src.modules.core import service
|
||||||
|
from deepface.commons import image_utils
|
||||||
from deepface.commons.logger import Logger
|
from deepface.commons.logger import Logger
|
||||||
|
|
||||||
logger = Logger()
|
logger = Logger()
|
||||||
|
|
||||||
blueprint = Blueprint("routes", __name__)
|
blueprint = Blueprint("routes", __name__)
|
||||||
|
|
||||||
|
# pylint: disable=no-else-return, broad-except
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route("/")
|
@blueprint.route("/")
|
||||||
def home():
|
def home():
|
||||||
return f"<h1>Welcome to DeepFace API v{DeepFace.__version__}!</h1>"
|
return f"<h1>Welcome to DeepFace API v{DeepFace.__version__}!</h1>"
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route("/represent", methods=["POST"])
|
def extract_image_from_request(img_key: str) -> Union[str, np.ndarray]:
|
||||||
def represent():
|
"""
|
||||||
input_args = request.get_json()
|
Extracts an image from the request either from json or a multipart/form-data file.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
img_key (str): The key used to retrieve the image data
|
||||||
|
from the request (e.g., 'img1').
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
img (str or np.ndarray): Given image detail (base64 encoded string, image path or url)
|
||||||
|
or the decoded image as a numpy array.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Check if the request is multipart/form-data (file input)
|
||||||
|
if request.files:
|
||||||
|
# request.files is instance of werkzeug.datastructures.ImmutableMultiDict
|
||||||
|
# file is instance of werkzeug.datastructures.FileStorage
|
||||||
|
file = request.files.get(img_key)
|
||||||
|
|
||||||
|
if file is None:
|
||||||
|
raise ValueError(f"Request form data doesn't have {img_key}")
|
||||||
|
|
||||||
|
if file.filename == "":
|
||||||
|
raise ValueError(f"No file uploaded for '{img_key}'")
|
||||||
|
|
||||||
|
img = image_utils.load_image_from_file_storage(file)
|
||||||
|
|
||||||
|
return img
|
||||||
|
# Check if the request is coming as base64, file path or url from json or form data
|
||||||
|
elif request.is_json or request.form:
|
||||||
|
input_args = request.get_json() or request.form.to_dict()
|
||||||
|
|
||||||
if input_args is None:
|
if input_args is None:
|
||||||
return {"message": "empty input set passed"}
|
raise ValueError("empty input set passed")
|
||||||
|
|
||||||
img_path = input_args.get("img") or input_args.get("img_path")
|
# this can be base64 encoded image, and image path or url
|
||||||
if img_path is None:
|
img = input_args.get(img_key)
|
||||||
return {"message": "you must pass img_path input"}
|
|
||||||
|
if not img:
|
||||||
|
raise ValueError(f"'{img_key}' not found in either json or form data request")
|
||||||
|
|
||||||
|
return img
|
||||||
|
|
||||||
|
# If neither JSON nor file input is present
|
||||||
|
raise ValueError(f"'{img_key}' not found in request in either json or form data")
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route("/represent", methods=["POST"])
|
||||||
|
def represent():
|
||||||
|
input_args = request.get_json() or request.form.to_dict()
|
||||||
|
|
||||||
|
try:
|
||||||
|
img = extract_image_from_request("img")
|
||||||
|
except Exception as err:
|
||||||
|
return {"exception": str(err)}, 400
|
||||||
|
|
||||||
obj = service.represent(
|
obj = service.represent(
|
||||||
img_path=img_path,
|
img_path=img,
|
||||||
model_name=input_args.get("model_name", "VGG-Face"),
|
model_name=input_args.get("model_name", "VGG-Face"),
|
||||||
detector_backend=input_args.get("detector_backend", "opencv"),
|
detector_backend=input_args.get("detector_backend", "opencv"),
|
||||||
enforce_detection=input_args.get("enforce_detection", True),
|
enforce_detection=input_args.get("enforce_detection", True),
|
||||||
@ -41,23 +96,21 @@ def represent():
|
|||||||
|
|
||||||
@blueprint.route("/verify", methods=["POST"])
|
@blueprint.route("/verify", methods=["POST"])
|
||||||
def verify():
|
def verify():
|
||||||
input_args = request.get_json()
|
input_args = request.get_json() or request.form.to_dict()
|
||||||
|
|
||||||
if input_args is None:
|
try:
|
||||||
return {"message": "empty input set passed"}
|
img1 = extract_image_from_request("img1")
|
||||||
|
except Exception as err:
|
||||||
|
return {"exception": str(err)}, 400
|
||||||
|
|
||||||
img1_path = input_args.get("img1") or input_args.get("img1_path")
|
try:
|
||||||
img2_path = input_args.get("img2") or input_args.get("img2_path")
|
img2 = extract_image_from_request("img2")
|
||||||
|
except Exception as err:
|
||||||
if img1_path is None:
|
return {"exception": str(err)}, 400
|
||||||
return {"message": "you must pass img1_path input"}
|
|
||||||
|
|
||||||
if img2_path is None:
|
|
||||||
return {"message": "you must pass img2_path input"}
|
|
||||||
|
|
||||||
verification = service.verify(
|
verification = service.verify(
|
||||||
img1_path=img1_path,
|
img1_path=img1,
|
||||||
img2_path=img2_path,
|
img2_path=img2,
|
||||||
model_name=input_args.get("model_name", "VGG-Face"),
|
model_name=input_args.get("model_name", "VGG-Face"),
|
||||||
detector_backend=input_args.get("detector_backend", "opencv"),
|
detector_backend=input_args.get("detector_backend", "opencv"),
|
||||||
distance_metric=input_args.get("distance_metric", "cosine"),
|
distance_metric=input_args.get("distance_metric", "cosine"),
|
||||||
@ -73,18 +126,31 @@ def verify():
|
|||||||
|
|
||||||
@blueprint.route("/analyze", methods=["POST"])
|
@blueprint.route("/analyze", methods=["POST"])
|
||||||
def analyze():
|
def analyze():
|
||||||
input_args = request.get_json()
|
input_args = request.get_json() or request.form.to_dict()
|
||||||
|
|
||||||
if input_args is None:
|
try:
|
||||||
return {"message": "empty input set passed"}
|
img = extract_image_from_request("img")
|
||||||
|
except Exception as err:
|
||||||
|
return {"exception": str(err)}, 400
|
||||||
|
|
||||||
img_path = input_args.get("img") or input_args.get("img_path")
|
actions = input_args.get("actions", ["age", "gender", "emotion", "race"])
|
||||||
if img_path is None:
|
# actions is the only argument instance of list or tuple
|
||||||
return {"message": "you must pass img_path input"}
|
# if request is form data, input args can either be text or file
|
||||||
|
if isinstance(actions, str):
|
||||||
|
actions = (
|
||||||
|
actions.replace("[", "")
|
||||||
|
.replace("]", "")
|
||||||
|
.replace("(", "")
|
||||||
|
.replace(")", "")
|
||||||
|
.replace('"', "")
|
||||||
|
.replace("'", "")
|
||||||
|
.replace(" ", "")
|
||||||
|
.split(",")
|
||||||
|
)
|
||||||
|
|
||||||
demographies = service.analyze(
|
demographies = service.analyze(
|
||||||
img_path=img_path,
|
img_path=img,
|
||||||
actions=input_args.get("actions", ["age", "gender", "emotion", "race"]),
|
actions=actions,
|
||||||
detector_backend=input_args.get("detector_backend", "opencv"),
|
detector_backend=input_args.get("detector_backend", "opencv"),
|
||||||
enforce_detection=input_args.get("enforce_detection", True),
|
enforce_detection=input_args.get("enforce_detection", True),
|
||||||
align=input_args.get("align", True),
|
align=input_args.get("align", True),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user