| """
|
| Model Configuration
|
| Centralizes all model paths and loading configurations
|
| """
|
|
|
| from pathlib import Path
|
|
|
|
|
| PROJECT_ROOT = Path(__file__).parent.parent
|
|
|
|
|
| MODELS_DIR = PROJECT_ROOT / "ai_models"
|
|
|
|
|
| MODEL_PATHS = {
|
| 'violence': {
|
| 'model': MODELS_DIR / "violence_detection" / "violence_model.h5",
|
| 'labels': MODELS_DIR / "violence_detection" / "lb.pickle",
|
| 'description': 'Violence Detection Model'
|
| },
|
| 'yolo': {
|
| 'candidates': [
|
| MODELS_DIR / "object_detection" / "yolov8n.pt",
|
| MODELS_DIR / "object_detection" / "yolov8s.pt",
|
| ],
|
| 'description': 'YOLO Object Detection Model'
|
| },
|
| 'weapon': {
|
| 'gun_model_candidates': [
|
| MODELS_DIR / "weapon_detection" / "GunDetector.pt",
|
| MODELS_DIR / "weapon_detection" / "best.pt",
|
| ],
|
| 'person_model_candidates': [
|
| MODELS_DIR / "object_detection" / "yolov8n.pt",
|
| MODELS_DIR / "object_detection" / "yolov8s.pt",
|
| ],
|
| 'description': 'Weapon & Person Detection Model'
|
| },
|
| 'pose': {
|
| 'candidates': [
|
| MODELS_DIR / "pose_detection" / "yolo11n-pose.pt",
|
| ],
|
| 'description': 'Pose Detection Model'
|
| },
|
| 'anomaly': {
|
| 'candidates': [
|
| MODELS_DIR / "anomaly_detection" / "best.bin",
|
| ],
|
| 'description': 'Anomaly Detection Model'
|
| },
|
| 'analysis': {
|
| 'candidates': [
|
| MODELS_DIR / "analysis_models" / "fight_detection_model.h5",
|
| MODELS_DIR / "analysis_models" / "CustomCNN150.h5",
|
| MODELS_DIR / "analysis_models" / "CustomCNN100.h5",
|
| MODELS_DIR / "analysis_models" / "CustomCNN.h5",
|
| ],
|
| 'description': 'Advanced Analysis Model (Fight/Behavior Detection)'
|
| }
|
| }
|
|
|
|
|
| DETECTION_THRESHOLDS = {
|
| 'yolo': 0.25,
|
| 'violence': 0.6,
|
| 'weapon': 0.5,
|
| 'anomaly': 0.5,
|
| }
|
|
|
|
|
| PROCESSING_PARAMS = {
|
| 'frame_skip': 10,
|
| 'alert_threshold': 3,
|
| 'max_upload_size': 500 * 1024 * 1024,
|
| }
|
|
|
|
|
| def get_model_path(model_name: str, model_type: str = None):
|
| """
|
| Get model path by name
|
|
|
| Args:
|
| model_name: 'violence', 'yolo', 'weapon', 'pose', 'anomaly'
|
| model_type: specific model variant (if applicable) - for weapon, use 'gun' or 'person'
|
|
|
| Returns:
|
| Path to model file or None if not found
|
| """
|
| if model_name not in MODEL_PATHS:
|
| return None
|
|
|
| config = MODEL_PATHS[model_name]
|
|
|
|
|
| if 'model' in config:
|
| return config['model'] if config['model'].exists() else None
|
|
|
|
|
| if model_name == 'weapon':
|
| if model_type == 'gun' and 'gun_model_candidates' in config:
|
| for candidate in config['gun_model_candidates']:
|
| if candidate.exists():
|
| return candidate
|
| elif model_type == 'person' and 'person_model_candidates' in config:
|
| for candidate in config['person_model_candidates']:
|
| if candidate.exists():
|
| return candidate
|
| elif not model_type:
|
|
|
| for candidate in config.get('gun_model_candidates', []):
|
| if candidate.exists():
|
| return candidate
|
| return None
|
|
|
|
|
| if 'candidates' in config:
|
| for candidate in config['candidates']:
|
| if candidate.exists():
|
| return candidate
|
|
|
| return None
|
|
|
|
|
| def get_all_available_models():
|
| """
|
| Get list of all available models
|
|
|
| Returns:
|
| dict: {model_name: is_available}
|
| """
|
| available = {}
|
| for model_name in MODEL_PATHS.keys():
|
| path = get_model_path(model_name)
|
| available[model_name] = path is not None
|
|
|
| return available
|
|
|