Spaces:
Sleeping
Sleeping
File size: 6,256 Bytes
b5d3a91 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | # built-in dependencies
import traceback
from typing import Optional, Union, Dict, Any, Tuple, List
# 3rd party dependencies
from numpy.typing import NDArray
# project dependencies
from deepface import DeepFace
from deepface.commons.logger import Logger
logger = Logger()
# pylint: disable=broad-except, too-many-positional-arguments
def represent(
img_path: Union[str, NDArray[Any]],
model_name: str,
detector_backend: str,
enforce_detection: bool,
align: bool,
anti_spoofing: bool,
max_faces: Optional[int] = None,
) -> Tuple[Dict[str, Any], int]:
try:
result = {}
embedding_objs = DeepFace.represent(
img_path=img_path,
model_name=model_name,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
anti_spoofing=anti_spoofing,
max_faces=max_faces,
)
result["results"] = embedding_objs
return result, 200
except Exception as err:
tb_str = traceback.format_exc()
logger.error(str(err))
logger.error(tb_str)
return {"error": f"Exception while representing: {str(err)} - {tb_str}"}, 400
def verify(
img1_path: Union[str, NDArray[Any]],
img2_path: Union[str, NDArray[Any]],
model_name: str,
detector_backend: str,
distance_metric: str,
enforce_detection: bool,
align: bool,
anti_spoofing: bool,
) -> Tuple[Dict[str, Any], int]:
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,
anti_spoofing=anti_spoofing,
)
return obj, 200
except Exception as err:
tb_str = traceback.format_exc()
logger.error(str(err))
logger.error(tb_str)
return {"error": f"Exception while verifying: {str(err)} - {tb_str}"}, 400
def analyze(
img_path: Union[str, NDArray[Any]],
actions: List[str],
detector_backend: str,
enforce_detection: bool,
align: bool,
anti_spoofing: bool,
) -> Tuple[Dict[str, Any], int]:
try:
result = {}
demographies = DeepFace.analyze(
img_path=img_path,
actions=actions,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
silent=True,
anti_spoofing=anti_spoofing,
)
result["results"] = demographies
return result, 200
except Exception as err:
tb_str = traceback.format_exc()
logger.error(str(err))
logger.error(tb_str)
return {"error": f"Exception while analyzing: {str(err)} - {tb_str}"}, 400
def register(
img: Union[str, NDArray[Any]],
model_name: str,
detector_backend: str,
enforce_detection: bool,
align: bool,
l2_normalize: bool,
expand_percentage: int,
normalization: str,
anti_spoofing: bool,
img_name: Optional[str],
database_type: str,
connection_details: str,
) -> Tuple[Dict[str, Any], int]:
try:
return (
DeepFace.register(
img=img,
img_name=img_name,
model_name=model_name,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
l2_normalize=l2_normalize,
expand_percentage=expand_percentage,
normalization=normalization,
anti_spoofing=anti_spoofing,
database_type=database_type,
connection_details=connection_details,
),
200,
)
except Exception as err:
tb_str = traceback.format_exc()
logger.error(str(err))
logger.error(tb_str)
return {"error": f"Exception while registering: {str(err)} - {tb_str}"}, 400
def search(
img: Union[str, NDArray[Any]],
model_name: str,
detector_backend: str,
distance_metric: str,
enforce_detection: bool,
align: bool,
l2_normalize: bool,
expand_percentage: int,
normalization: str,
anti_spoofing: bool,
similarity_search: bool,
k: Optional[int],
database_type: str,
connection_details: str,
search_method: str,
) -> Tuple[Dict[str, Any], int]:
try:
result = {}
dfs = DeepFace.search(
img=img,
model_name=model_name,
detector_backend=detector_backend,
distance_metric=distance_metric,
enforce_detection=enforce_detection,
align=align,
l2_normalize=l2_normalize,
expand_percentage=expand_percentage,
normalization=normalization,
anti_spoofing=anti_spoofing,
similarity_search=similarity_search,
k=k,
database_type=database_type,
connection_details=connection_details,
search_method=search_method,
)
result["results"] = [df.to_dict(orient="records") for df in dfs]
return result, 200
except Exception as err:
tb_str = traceback.format_exc()
logger.error(str(err))
logger.error(tb_str)
return {"error": f"Exception while searching: {str(err)} - {tb_str}"}, 400
def build_index(
model_name: str,
detector_backend: str,
align: bool,
l2_normalize: bool,
database_type: str,
connection_details: str,
) -> Tuple[Dict[str, Any], int]:
try:
DeepFace.build_index(
model_name=model_name,
detector_backend=detector_backend,
align=align,
l2_normalize=l2_normalize,
database_type=database_type,
connection_details=connection_details,
)
return {"message": "Index built successfully"}, 200
except Exception as err:
tb_str = traceback.format_exc()
logger.error(str(err))
logger.error(tb_str)
return {"error": f"Exception while building index: {str(err)} - {tb_str}"}, 400
|