diff --git a/src/hope_dedup_engine/apps/faces/utils/duplication_detector.py b/src/hope_dedup_engine/apps/faces/utils/duplication_detector.py index d7292267..5d9fad5b 100644 --- a/src/hope_dedup_engine/apps/faces/utils/duplication_detector.py +++ b/src/hope_dedup_engine/apps/faces/utils/duplication_detector.py @@ -43,7 +43,8 @@ def __init__(self, filename: str) -> None: self.filename: str = filename self.encodings_filename: str = f"{self.filename}.npy" self.scale_factor: float = config.SCALE_FACTOR - self.mean_values: Tuple[float, float, float] = tuple(map(float, config.MEAN_VALUES.split(","))) + # self.mean_values: Tuple[float, float, float] = tuple(map(float, config.MEAN_VALUES.split(", "))) + self.mean_values: Tuple[float, float, float] = config.MEAN_VALUES self.face_detection_confidence: float = config.FACE_DETECTION_CONFIDENCE self.face_detection_model: str = config.FACE_DETECTION_MODEL self.distance_threshold: float = config.DISTANCE_THRESHOLD diff --git a/src/hope_dedup_engine/apps/faces/validators.py b/src/hope_dedup_engine/apps/faces/validators.py new file mode 100644 index 00000000..893275ba --- /dev/null +++ b/src/hope_dedup_engine/apps/faces/validators.py @@ -0,0 +1,24 @@ +from django.forms import CharField, ValidationError + + +class MeanValuesTupleField(CharField): + def to_python(self, value): + try: + values = tuple(map(float, value.split(", "))) + if len(values) != 3: + raise ValueError("The tuple must have exactly three elements.") + if not all(-255 <= v <= 255 for v in values): + raise ValueError("Each value in the tuple must be between -255 and 255.") + return values + except Exception as e: + raise ValidationError( + """ + Enter a valid tuple of three float values separated by commas and spaces, e.g. '0.0, 0.0, 0.0'. + Each value must be between -255 and 255. + """ + ) from e + + def prepare_value(self, value): + if isinstance(value, tuple): + return ", ".join(map(str, value)) + return value diff --git a/src/hope_dedup_engine/config/fragments/constance.py b/src/hope_dedup_engine/config/fragments/constance.py index 0fa1a04d..b3852113 100644 --- a/src/hope_dedup_engine/config/fragments/constance.py +++ b/src/hope_dedup_engine/config/fragments/constance.py @@ -4,13 +4,6 @@ CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend" -CONSTANCE_ADDITIONAL_FIELDS = { - "email": [ - "django.forms.EmailField", - {}, - ], -} - CONSTANCE_CONFIG = { "NEW_USER_IS_STAFF": (False, "Set any new user as staff", bool), "NEW_USER_DEFAULT_GROUP": (DEFAULT_GROUP_NAME, "Group to assign to any new user", str), @@ -102,6 +95,10 @@ } CONSTANCE_ADDITIONAL_FIELDS = { + "email": [ + "django.forms.EmailField", + {}, + ], "dnn_backend": [ "django.forms.ChoiceField", { @@ -120,10 +117,5 @@ "choices": (("hog", "HOG"), ("cnn", "CNN")), }, ], - "tuple_field": [ - "django.forms.CharField", - { - "widget": "django.forms.TextInput", - }, - ], + "tuple_field": ["hope_dedup_engine.apps.faces.validators.MeanValuesTupleField", {}], }