import torch import os import numpy as np # ANSI color codes class Colors: RESET = "\033[0m" BOLD = "\033[1m" GREEN = "\033[92m" CYAN = "\033[96m" YELLOW = "\033[93m" def get_yolov5(confident_val): """Initialize YOLOv5 model with optimized thread settings""" # Force CPU mode os.environ['CUDA_VISIBLE_DEVICES'] = '' # Use multiple threads for better throughput (auto-scale based on CPU) num_threads = min(os.cpu_count() or 2, 4) # Cap at 4 threads torch.set_num_threads(num_threads) torch.set_num_interop_threads(num_threads) print(f"{Colors.CYAN}[MODEL]{Colors.RESET} 🔄 Loading YOLOv5 with {Colors.BOLD}{num_threads} threads{Colors.RESET}") print(f"{Colors.CYAN}[MODEL]{Colors.RESET} 📊 Confidence threshold: {confident_val}") # Load model with optimization flags model = torch.hub.load('./yolov5', 'custom', path='./countmodel.pt', source='local', force_reload=False) # Configure model settings model.conf = confident_val # Confidence threshold model.max_det = 4000 # Maximum detections model.cpu() # Ensure CPU mode # Enable inference optimizations model.eval() # Set to evaluation mode # Enable inference mode for better performance torch.inference_mode() print(f"{Colors.GREEN}[MODEL]{Colors.RESET} ✅ Model loaded successfully!") print(f"{Colors.CYAN}[MODEL]{Colors.RESET} 📈 Max detections: {model.max_det}") return model