Skip to content

Commit

Permalink
add ! validator for means
Browse files Browse the repository at this point in the history
  • Loading branch information
vitali-yanushchyk-valor committed May 30, 2024
1 parent e6e8dc1 commit 4301381
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/hope_dedup_engine/apps/faces/validators.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 5 additions & 13 deletions src/hope_dedup_engine/config/fragments/constance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -102,6 +95,10 @@
}

CONSTANCE_ADDITIONAL_FIELDS = {
"email": [
"django.forms.EmailField",
{},
],
"dnn_backend": [
"django.forms.ChoiceField",
{
Expand All @@ -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", {}],
}

0 comments on commit 4301381

Please sign in to comment.