#!/usr/bin/env python3 """ Simple startup script for the Crowd Detection Backend """ import subprocess import sys import time import requests def check_dependencies(): """Check if required packages are installed""" required_packages = [ 'fastapi', 'uvicorn', 'websockets', 'opencv-python', 'ultralytics', 'numpy', 'scipy', 'pillow', 'python-multipart', 'aiofiles' ] missing_packages = [] for package in required_packages: try: __import__(package.replace('-', '_')) print(f"โœ… {package}") except ImportError: missing_packages.append(package) print(f"โŒ {package}") if missing_packages: print(f"\n๐Ÿšจ Missing packages: {', '.join(missing_packages)}") print("Installing missing packages...") try: subprocess.run([sys.executable, "-m", "pip", "install"] + missing_packages, check=True) print("โœ… All packages installed successfully!") except subprocess.CalledProcessError: print("โŒ Failed to install packages. Please install manually:") print(f"pip install {' '.join(missing_packages)}") return False return True def start_server(): """Start the FastAPI server""" print("\n๐Ÿš€ Starting Crowd Detection Backend...") try: # Start the server process = subprocess.Popen([ sys.executable, "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", # changed "--workers", "1" # better than --reload in container ]) print("โœ… Server started successfully!") print("๐ŸŒ Backend URL: http://localhost:7860") print("๐Ÿ“š API Docs: http://localhost:7860/docs") print("๐Ÿ” Health Check: http://localhost:7860/health") print("\n๐Ÿ’ก To test the API:") print(" curl http://localhost:7860/health") print(" curl http://localhost:7860/") print("\nโน๏ธ Press Ctrl+C to stop the server") # Wait for the process to complete process.wait() except KeyboardInterrupt: print("\n๐Ÿ›‘ Shutting down server...") if process: process.terminate() process.wait() print("โœ… Server stopped") except Exception as e: print(f"โŒ Failed to start server: {e}") return False return True def test_endpoints(): """Test basic endpoints""" print("\n๐Ÿงช Testing endpoints...") try: # Test health endpoint response = requests.get("http://localhost:7860/health", timeout=5) if response.status_code == 200: print("โœ… Health endpoint working") data = response.json() print(f" Status: {data.get('status')}") print(f" Zones: {data.get('zones_count')}") print(f" Cameras: {data.get('cameras_count')}") else: print(f"โŒ Health endpoint failed: {response.status_code}") # Test zones endpoint response = requests.get("http://localhost:7860/zones/heatmap", timeout=5) if response.status_code == 200: print("โœ… Zones endpoint working") zones = response.json() print(f" Found {len(zones)} zones") else: print(f"โŒ Zones endpoint failed: {response.status_code}") except requests.exceptions.ConnectionError: print("โŒ Cannot connect to server. Is it running?") except Exception as e: print(f"โŒ Test failed: {e}") if __name__ == "__main__": print("๐Ÿ”ง Crowd Detection Backend Startup") print("=" * 50) # Check dependencies if not check_dependencies(): print("โŒ Dependency check failed. Exiting.") sys.exit(1) # Start server if start_server(): print("โœ… Backend startup completed successfully!") else: print("โŒ Backend startup failed!") sys.exit(1)