""" DetectifAI API Startup Script Quick script to launch the DetectifAI API server with proper environment setup and preliminary checks for the surveillance system. """ import sys import os import subprocess import time import logging def check_python_environment(): """Check if required Python packages are available""" print("šŸ Checking Python environment...") required_packages = [ 'flask', 'flask_cors', 'opencv-python', 'numpy', 'ultralytics', 'pillow', 'matplotlib' ] missing_packages = [] for package in required_packages: try: if package == 'opencv-python': import cv2 elif package == 'flask_cors': from flask_cors import CORS elif package == 'ultralytics': from ultralytics import YOLO elif package == 'pillow': from PIL import Image else: __import__(package) print(f" āœ… {package}") except ImportError: missing_packages.append(package) print(f" āŒ {package}") if missing_packages: print(f"\nāš ļø Missing packages: {', '.join(missing_packages)}") print("šŸ’” Install with: pip install " + " ".join(missing_packages)) return False return True def check_model_files(): """Check if YOLO model files are available""" print("\nšŸ¤– Checking AI model files...") model_files = [ 'models/fire_YOLO11.pt', 'models/weapon_YOLO11.pt' ] missing_models = [] found_models = [] for model_file in model_files: if os.path.exists(model_file): size_mb = os.path.getsize(model_file) / (1024 * 1024) print(f" āœ… {model_file} ({size_mb:.1f} MB)") found_models.append(model_file) else: missing_models.append(model_file) print(f" āŒ {model_file}") if missing_models: print(f"\nāš ļø Missing model files: {', '.join(missing_models)}") if found_models: print(f"āœ… Found {len(found_models)} model(s): {', '.join([os.path.basename(f) for f in found_models])}") print("šŸ’” DetectifAI will work with available models") else: print("šŸ’” DetectifAI will work with reduced functionality") return len(found_models) > 0 return True def check_test_videos(): """Check if test videos are available""" print("\nšŸŽ¬ Checking test videos...") test_videos = ['rob.mp4', 'fire.avi'] available_videos = [] for video in test_videos: if os.path.exists(video): size_mb = os.path.getsize(video) / (1024 * 1024) print(f" āœ… {video} ({size_mb:.1f} MB)") available_videos.append(video) else: print(f" āŒ {video}") print(f"\nšŸ“Š {len(available_videos)}/{len(test_videos)} test videos available") return available_videos def setup_directories(): """Create necessary directories""" print("\nšŸ“ Setting up directories...") directories = [ 'uploads', 'video_processing_outputs', 'logs', 'core', 'docs', 'models' ] for directory in directories: if not os.path.exists(directory): os.makedirs(directory) print(f" āœ… Created {directory}/") else: print(f" āœ… {directory}/ exists") def start_detectifai_api(): """Start the DetectifAI API server""" print("\nšŸš€ Starting DetectifAI API server...") print("=" * 50) try: # Change to backend directory if needed if not os.path.exists('app.py'): print("āŒ app.py not found in current directory") print("šŸ’” Make sure you're in the backend directory") return False # Start the API server print("🌐 API will be available at: http://localhost:5000") print("šŸ“‹ API endpoints:") print(" • Health: GET /api/health") print(" • Upload: POST /api/upload") print(" • Status: GET /api/status/") print(" • Results: GET /api/results/") print(" • Demo: GET /api/detectifai/demo") print(" • DetectifAI Events: GET /api/detectifai/events/") print(" • Keyframes: GET /api/keyframes/") print("") print("šŸ”§ To test the API, run: python test_detectifai_integration.py") print("🌐 For frontend integration, ensure CORS is enabled") print("") print("Press Ctrl+C to stop the server") print("=" * 50) # Run the API server subprocess.run([sys.executable, 'app.py']) except KeyboardInterrupt: print("\n\nšŸ›‘ DetectifAI API server stopped") return True except Exception as e: print(f"\nāŒ Error starting API server: {e}") return False def main(): """Main startup function""" print("šŸ”§ DetectifAI API Startup") print("========================") # System checks env_ok = check_python_environment() models_ok = check_model_files() videos = check_test_videos() # Setup setup_directories() # Summary print("\nšŸ“‹ System Status Summary:") print(f" šŸ Python Environment: {'āœ…' if env_ok else 'āš ļø'}") print(f" šŸ¤– AI Models: {'āœ…' if models_ok else 'āš ļø'}") print(f" šŸŽ¬ Test Videos: {len(videos)} available") if not env_ok: print("\nāŒ Cannot start API - missing required Python packages") return False print(f"\nšŸŽÆ DetectifAI System Ready") if videos: print(f"šŸ’” Demo videos available: {', '.join(videos)}") # Ask user if they want to continue try: response = input("\nšŸš€ Start DetectifAI API server? (y/n): ").lower().strip() if response in ['y', 'yes', '']: return start_detectifai_api() else: print("šŸ‘‹ Startup cancelled") return True except KeyboardInterrupt: print("\nšŸ‘‹ Startup cancelled") return True if __name__ == "__main__": success = main() sys.exit(0 if success else 1)