crowd-detection-api / start_backend.py
ShaunMendes001's picture
Add application file
773b38b
#!/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)