tommulder's picture
Deploy gesture detection & validation API
95db528
raw
history blame
5.34 kB
"""
Configuration settings for the validation module.
This module provides centralized configuration for the validation service,
including model paths, processing parameters, and validation thresholds.
"""
import os
from typing import Optional
class ValidationConfig:
"""
Configuration settings for the validation service.
This class provides centralized configuration management for all
validation-related settings, with sensible defaults and environment
variable overrides.
"""
def __init__(self):
"""Initialize configuration with default values and environment overrides."""
# Model paths
self.hand_detector_path = os.getenv(
"HAND_DETECTOR_PATH",
"models/hand_detector.onnx"
)
self.gesture_classifier_path = os.getenv(
"GESTURE_CLASSIFIER_PATH",
"models/crops_classifier.onnx"
)
# Processing parameters
self.frame_skip = int(os.getenv("FRAME_SKIP", "1"))
self.min_gesture_duration = int(os.getenv("MIN_GESTURE_DURATION", "5"))
self.confidence_threshold = float(os.getenv("CONFIDENCE_THRESHOLD", "0.7"))
# Validation parameters
self.default_error_margin = float(os.getenv("DEFAULT_ERROR_MARGIN", "0.33"))
self.require_all_gestures = os.getenv("REQUIRE_ALL_GESTURES", "true").lower() == "true"
self.confidence_threshold = float(os.getenv("CONFIDENCE_THRESHOLD", "0.7"))
self.min_gesture_duration = int(os.getenv("MIN_GESTURE_DURATION", "5"))
# Facial validation parameters
self.similarity_threshold = float(os.getenv("SIMILARITY_THRESHOLD", "0.7"))
self.frame_sample_rate = int(os.getenv("FRAME_SAMPLE_RATE", "10"))
# File size limits (in bytes)
self.max_photo_size = int(os.getenv("MAX_PHOTO_SIZE", str(50 * 1024 * 1024))) # 50MB
self.max_video_size = int(os.getenv("MAX_VIDEO_SIZE", str(200 * 1024 * 1024))) # 200MB
# Performance settings
self.max_processing_time = int(os.getenv("MAX_PROCESSING_TIME", "60")) # seconds
self.enable_detailed_logging = os.getenv("ENABLE_DETAILED_LOGGING", "false").lower() == "true"
# Security settings
self.allowed_image_types = os.getenv(
"ALLOWED_IMAGE_TYPES",
"image/jpeg,image/png,image/webp,application/pdf"
).split(",")
self.allowed_video_types = os.getenv(
"ALLOWED_VIDEO_TYPES",
"video/mp4,video/avi,video/mov,video/webm"
).split(",")
@property
def model_paths(self) -> dict:
"""Get model paths as a dictionary."""
return {
"hand_detector": self.hand_detector_path,
"gesture_classifier": self.gesture_classifier_path
}
@property
def processing_params(self) -> dict:
"""Get processing parameters as a dictionary."""
return {
"frame_skip": self.frame_skip,
"min_gesture_duration": self.min_gesture_duration,
"confidence_threshold": self.confidence_threshold
}
@property
def validation_params(self) -> dict:
"""Get validation parameters as a dictionary."""
return {
"default_error_margin": self.default_error_margin,
"require_all_gestures": self.require_all_gestures
}
def validate_file_type(self, content_type: str, file_type: str = "image") -> bool:
"""
Validate if a file type is allowed.
Parameters
----------
content_type : str
MIME content type of the file
file_type : str, optional
Type of file ("image" or "video"), by default "image"
Returns
-------
bool
True if file type is allowed, False otherwise
"""
if file_type == "image":
allowed_types = self.allowed_image_types
elif file_type == "video":
allowed_types = self.allowed_video_types
else:
return False
return content_type in allowed_types
def validate_file_size(self, file_size: int, file_type: str = "image") -> bool:
"""
Validate if a file size is within limits.
Parameters
----------
file_size : int
Size of the file in bytes
file_type : str, optional
Type of file ("image" or "video"), by default "image"
Returns
-------
bool
True if file size is within limits, False otherwise
"""
if file_type == "image":
max_size = self.max_photo_size
elif file_type == "video":
max_size = self.max_video_size
else:
return False
return file_size <= max_size
# Global configuration instance
config = ValidationConfig()
def get_config() -> ValidationConfig:
"""
Get the global configuration instance.
Returns
-------
ValidationConfig
Global configuration instance
"""
return config
def reload_config() -> ValidationConfig:
"""
Reload configuration from environment variables.
Returns
-------
ValidationConfig
New configuration instance with updated values
"""
global config
config = ValidationConfig()
return config