Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(face): add alerts #105

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions alice/face/face_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Any, Dict, Union
from typing import Any, Dict, List, Union, cast

from meiga import Error
from pydantic import BaseModel, Field
Expand Down Expand Up @@ -50,12 +50,18 @@ def number_of_faces_response(response_encoded: bytes) -> int:
return number_of_faces


def alerts_response(response_encoded: bytes) -> list[str]:
alerts = cast(List[str], json.loads(response_encoded.decode()))
return alerts


response_factory = {
"liveness_score": liveness_response,
"metadata": metadata_response,
"face_avatar": bytes_response,
"face_selfie": bytes_response,
"face_profile": bytes_response,
"alerts": alerts_response,
"face_bounding_box": face_bounding_box_response,
"number_of_faces": number_of_faces_response,
}
Expand Down Expand Up @@ -94,14 +100,23 @@ class SelfieResult(BaseModel):
metadata: Union[Dict[str, Any], None] = Field(
None, description="Dictionary with some optional metadata"
)
alerts: Union[List[str], None] = Field(
default_factory=list, description="List of selfie alerts"
)

def __repr__(self) -> str:
face_profile = (
f"face_profile(length)={len(self.face_profile)}"
if self.face_profile
else "face_profile=None"
)
return f"[SelfieResult {face_profile} liveness_score={self.liveness_score} number_of_faces={self.number_of_faces} metadata={self.metadata}]"
return (
f"[SelfieResult {face_profile} "
f"liveness_score={self.liveness_score} "
f"number_of_faces={self.number_of_faces} "
f"alerts={self.alerts} "
f"metadata={self.metadata}]"
)

def __str__(self) -> str:
return self.__repr__()
Expand All @@ -112,12 +127,13 @@ def from_response(response: Response) -> "SelfieResult":
face_profile = multipart_response_dict.get("face_profile")
liveness_score = multipart_response_dict.get("liveness_score")
number_of_faces = multipart_response_dict.get("number_of_faces")
alerts = multipart_response_dict.get("alerts")
metadata = multipart_response_dict.get("metadata")

return SelfieResult(
face_profile=face_profile,
liveness_score=liveness_score,
number_of_faces=number_of_faces,
alerts=alerts,
metadata=metadata,
)

Expand Down
2 changes: 2 additions & 0 deletions examples/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

def face_example(api_key: str, verbose: Optional[bool] = False) -> None:
config = Config(api_key=api_key, verbose=verbose)

face = Face.from_config(config)

selfie_media_data = given_any_selfie_image_media_data()
document_front_media_data = given_any_document_front_media_data()

selfie_result = face.selfie(selfie_media_data).unwrap_or_raise()
print(selfie_result)

selfie_result.save_face_profile("face_profile_v5.bin")

Expand Down
Loading