Skip to content

UniFace: A Unified Face Analysis Library for Python | Detection, alignment, landmarks, face-mesh, recognition, parsing, gaze, attributes and anti-spoofing under one API.

feat: Add new face recognition models and update existing code

yakhyod1830c705898
diff --git a/uniface/constants.py b/uniface/constants.py
index 715f33f..e6b174a 100644
--- a/uniface/constants.py
+++ b/uniface/constants.py
@@ -6,16 +6,37 @@ from enum import Enum
from typing import Dict
# fmt: off
-class FaceEncoderWeights(str, Enum):
+class SphereFaceWeights(str, Enum):
+ """
+ Trained on MS1M V2 dataset with 5.8 million images of 85k identities.
+ https://github.com/yakhyo/face-recognition
+ """
SPHERE20 = "sphere20"
SPHERE36 = "sphere36"
+
+class MobileFaceWeights(str, Enum):
+ """
+ Trained on MS1M V2 dataset with 5.8 million images of 85k identities.
+ https://github.com/yakhyo/face-recognition
+ """
MNET_025 = "mobilenetv1_025"
MNET_V2 = "mobilenetv2"
MNET_V3_SMALL = "mobilenetv3_small"
MNET_V3_LARGE = "mobilenetv3_large"
+class ArcFaceWeights(str, Enum):
+ """
+ Pretrained weights from ArcFace model (insightface).
+ https://github.com/deepinsight/insightface
+ """
+ MNET = "arcface_mnet"
+ RESNET = "arcface_resnet"
class RetinaFaceWeights(str, Enum):
+ """
+ Trained on WIDER FACE dataset.
+ https://github.com/yakhyo/retinaface-pytorch
+ """
MNET_025 = "retinaface_mnet025"
MNET_050 = "retinaface_mnet050"
MNET_V1 = "retinaface_mnet_v1"
diff --git a/uniface/recognition/__init__.py b/uniface/recognition/__init__.py
index e69de29..011662b 100644
--- a/uniface/recognition/__init__.py
+++ b/uniface/recognition/__init__.py
@@ -0,0 +1 @@
+from .models import SphereFace, MobileFace, ArcFace
diff --git a/uniface/recognition/encoder.py b/uniface/recognition/base.py
similarity index 87%
rename from uniface/recognition/encoder.py
rename to uniface/recognition/base.py
index 32c7d3b..8371e9b 100644
--- a/uniface/recognition/encoder.py
+++ b/uniface/recognition/base.py
@@ -7,23 +7,26 @@ import cv2
import numpy as np
import onnxruntime as ort
-from typing import Tuple, List, Optional, Literal
+from typing import Tuple, Optional
+
from uniface.face_utils import compute_similarity, face_alignment
from uniface.model_store import verify_model_weights
-from uniface.constants import FaceEncoderWeights
+from uniface.constants import SphereFaceWeights, MobileFaceWeights
from uniface.logger import Logger
-class FaceEncoder:
+__all__ = ["BaseFaceEncoder"]
+
+
+class BaseFaceEncoder:
"""
- Face recognition model using ONNX Runtime for inference and OpenCV for image preprocessing,
- utilizing an external face alignment function.
+ Unified Face Encoder supporting multiple model families (e.g., SphereFace, MobileFace).
"""
def __init__(
self,
- model_path: Optional[FaceEncoderWeights] = FaceEncoderWeights.MNET_V2,
+ model_path: Optional[SphereFaceWeights | MobileFaceWeights] = MobileFaceWeights.MNET_V2
) -> None:
"""
Initializes the FaceEncoder model for inference.
@@ -108,7 +111,7 @@ class FaceEncoder:
Returns:
np.ndarray: 512-dimensional face embedding.
"""
- aligned_face = face_alignment(image, landmarks) # Use your function for alignment
- blob = self.preprocess(image) # Convert to blob
+ aligned_face, _ = face_alignment(image, landmarks) # Use your function for alignment
+ blob = self.preprocess(aligned_face) # Convert to blob
embedding = self.session.run(self.output_names, {self.input_name: blob})[0]
return embedding # Return the 512-D feature vector
diff --git a/uniface/recognition/models.py b/uniface/recognition/models.py
new file mode 100644
index 0000000..ebaf9ee
--- /dev/null
+++ b/uniface/recognition/models.py
@@ -0,0 +1,26 @@
+# Copyright 2025 Yakhyokhuja Valikhujaev
+# Author: Yakhyokhuja Valikhujaev
+# GitHub: https://github.com/yakhyo
+
+from typing import Optional
+
+from .base import BaseFaceEncoder
+from uniface.constants import SphereFaceWeights, MobileFaceWeights, ArcFaceWeights
+
+
+__all__ = ["SphereFace", "MobileFace", "ArcFace"]
+
+
+class SphereFace(BaseFaceEncoder):
+ def __init__(self, model_path: Optional[SphereFaceWeights] = SphereFaceWeights.SPHERE20) -> None:
+ super().__init__(model_path=model_path)
+
+
+class MobileFace(BaseFaceEncoder):
+ def __init__(self, model_path: Optional[MobileFaceWeights] = MobileFaceWeights.MNET_V2) -> None:
+ super().__init__(model_path=model_path)
+
+
+class ArcFace(BaseFaceEncoder):
+ def __init__(self, model_path: Optional[ArcFaceWeights] = ArcFaceWeights.MNET) -> None:
+ super().__init__(model_path=model_path)