Spaces:
Build error
Build error
File size: 4,103 Bytes
773b38b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #!/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) |