2023-06-25 05:18:09 +02:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import cv2
|
2023-08-06 04:45:13 +02:00
|
|
|
import numpy as np
|
|
|
|
from insightface.model_zoo import ArcFaceONNX, RetinaFace
|
|
|
|
from insightface.utils.face_align import norm_crop
|
2024-01-13 07:00:09 +02:00
|
|
|
from numpy.typing import NDArray
|
2023-06-25 05:18:09 +02:00
|
|
|
|
2023-11-12 03:04:49 +02:00
|
|
|
from app.config import clean_name
|
2024-01-13 07:00:09 +02:00
|
|
|
from app.schemas import Face, ModelType, is_ndarray
|
2023-10-31 12:02:04 +02:00
|
|
|
|
2023-06-25 05:18:09 +02:00
|
|
|
from .base import InferenceModel
|
|
|
|
|
|
|
|
|
|
|
|
class FaceRecognizer(InferenceModel):
|
|
|
|
_model_type = ModelType.FACIAL_RECOGNITION
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
model_name: str,
|
2023-08-29 15:58:00 +02:00
|
|
|
min_score: float = 0.7,
|
2023-06-28 01:21:33 +02:00
|
|
|
cache_dir: Path | str | None = None,
|
|
|
|
**model_kwargs: Any,
|
2023-06-27 23:01:24 +02:00
|
|
|
) -> None:
|
2023-08-30 10:16:00 +02:00
|
|
|
self.min_score = model_kwargs.pop("minScore", min_score)
|
2023-11-12 03:04:49 +02:00
|
|
|
super().__init__(clean_name(model_name), cache_dir, **model_kwargs)
|
2023-08-06 04:45:13 +02:00
|
|
|
|
2023-09-09 11:02:44 +02:00
|
|
|
def _load(self) -> None:
|
2024-01-11 19:26:46 +02:00
|
|
|
self.det_model = RetinaFace(session=self._make_session(self.det_file))
|
|
|
|
self.rec_model = ArcFaceONNX(self.rec_file.as_posix(), session=self._make_session(self.rec_file))
|
2023-08-06 04:45:13 +02:00
|
|
|
|
|
|
|
self.det_model.prepare(
|
2023-08-25 06:28:51 +02:00
|
|
|
ctx_id=0,
|
2023-06-25 05:18:09 +02:00
|
|
|
det_thresh=self.min_score,
|
2023-08-06 04:45:13 +02:00
|
|
|
input_size=(640, 640),
|
2023-06-25 05:18:09 +02:00
|
|
|
)
|
2023-08-25 06:28:51 +02:00
|
|
|
self.rec_model.prepare(ctx_id=0)
|
2023-06-25 05:18:09 +02:00
|
|
|
|
2024-01-13 07:00:09 +02:00
|
|
|
def _predict(self, image: NDArray[np.uint8] | bytes) -> list[Face]:
|
2023-08-29 15:58:00 +02:00
|
|
|
if isinstance(image, bytes):
|
2024-01-13 07:00:09 +02:00
|
|
|
decoded_image = cv2.imdecode(np.frombuffer(image, np.uint8), cv2.IMREAD_COLOR)
|
|
|
|
else:
|
|
|
|
decoded_image = image
|
|
|
|
assert is_ndarray(decoded_image, np.uint8)
|
|
|
|
bboxes, kpss = self.det_model.detect(decoded_image)
|
2023-08-06 04:45:13 +02:00
|
|
|
if bboxes.size == 0:
|
|
|
|
return []
|
2024-01-13 07:00:09 +02:00
|
|
|
assert is_ndarray(kpss, np.float32)
|
2023-06-25 05:18:09 +02:00
|
|
|
|
2023-08-06 04:45:13 +02:00
|
|
|
scores = bboxes[:, 4].tolist()
|
|
|
|
bboxes = bboxes[:, :4].round().tolist()
|
2023-06-25 05:18:09 +02:00
|
|
|
|
2023-08-06 04:45:13 +02:00
|
|
|
results = []
|
2024-01-13 07:00:09 +02:00
|
|
|
height, width, _ = decoded_image.shape
|
2023-08-06 04:45:13 +02:00
|
|
|
for (x1, y1, x2, y2), score, kps in zip(bboxes, scores, kpss):
|
2024-01-13 07:00:09 +02:00
|
|
|
cropped_img = norm_crop(decoded_image, kps)
|
|
|
|
embedding: NDArray[np.float32] = self.rec_model.get_feat(cropped_img)[0]
|
2023-11-13 18:18:46 +02:00
|
|
|
face: Face = {
|
|
|
|
"imageWidth": width,
|
|
|
|
"imageHeight": height,
|
|
|
|
"boundingBox": {
|
|
|
|
"x1": x1,
|
|
|
|
"y1": y1,
|
|
|
|
"x2": x2,
|
|
|
|
"y2": y2,
|
|
|
|
},
|
|
|
|
"score": score,
|
|
|
|
"embedding": embedding,
|
|
|
|
}
|
|
|
|
results.append(face)
|
2023-06-25 05:18:09 +02:00
|
|
|
return results
|
2023-08-06 04:45:13 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def cached(self) -> bool:
|
2023-11-12 03:04:49 +02:00
|
|
|
return self.det_file.is_file() and self.rec_file.is_file()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def det_file(self) -> Path:
|
|
|
|
return self.cache_dir / "detection" / "model.onnx"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rec_file(self) -> Path:
|
|
|
|
return self.cache_dir / "recognition" / "model.onnx"
|
2023-08-29 15:58:00 +02:00
|
|
|
|
|
|
|
def configure(self, **model_kwargs: Any) -> None:
|
2023-08-30 10:16:00 +02:00
|
|
|
self.det_model.det_thresh = model_kwargs.pop("minScore", self.det_model.det_thresh)
|