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.

make emotion module optional to avoid pytorch dependency

yakhyo0aea17d14de5
diff --git a/uniface/__init__.py b/uniface/__init__.py
index 351da5d..b632757 100644
--- a/uniface/__init__.py
+++ b/uniface/__init__.py
@@ -21,7 +21,11 @@ from uniface.log import Logger, enable_logging
from uniface.model_store import verify_model_weights
from uniface.visualization import draw_detections
-from .attribute import AgeGender, Emotion
+from .attribute import AgeGender
+try:
+ from .attribute import Emotion
+except ImportError:
+ Emotion = None # PyTorch not installed
from .detection import SCRFD, RetinaFace, create_detector, detect_faces, list_available_detectors
from .landmark import Landmark106, create_landmarker
from .recognition import ArcFace, MobileFace, SphereFace, create_recognizer
diff --git a/uniface/attribute/__init__.py b/uniface/attribute/__init__.py
index eda6dbd..62bb82d 100644
--- a/uniface/attribute/__init__.py
+++ b/uniface/attribute/__init__.py
@@ -6,10 +6,17 @@ from typing import Dict, Any, List, Union
import numpy as np
from uniface.attribute.age_gender import AgeGender
-from uniface.attribute.emotion import Emotion
from uniface.attribute.base import Attribute
from uniface.constants import AgeGenderWeights, DDAMFNWeights
+# Emotion requires PyTorch - make it optional
+try:
+ from uniface.attribute.emotion import Emotion
+ _EMOTION_AVAILABLE = True
+except ImportError:
+ Emotion = None
+ _EMOTION_AVAILABLE = False
+
# Public API for the attribute module
__all__ = [
"AgeGender",
@@ -21,9 +28,12 @@ __all__ = [
# A mapping from model enums to their corresponding attribute classes
_ATTRIBUTE_MODELS = {
**{model: AgeGender for model in AgeGenderWeights},
- **{model: Emotion for model in DDAMFNWeights}
}
+# Add Emotion models only if PyTorch is available
+if _EMOTION_AVAILABLE:
+ _ATTRIBUTE_MODELS.update({model: Emotion for model in DDAMFNWeights})
+
def create_attribute_predictor(
model_name: Union[AgeGenderWeights, DDAMFNWeights],