--- title: Crowd Detection API emoji: ๐Ÿ‘ฅ colorFrom: blue colorTo: red sdk: docker pinned: false license: mit --- # ๐Ÿ‘ฅ Crowd Detection & Disaster Management API A real-time crowd monitoring system with anomaly detection, emergency alerts, and WebSocket broadcasting capabilities, deployed on Hugging Face Spaces. ## ๐Ÿš€ Features - **Real-time People Counting** using YOLOv8 - **Crowd Density Heatmaps** for visualization - **Anomaly Detection** (stampede, fallen person detection) - **Emergency Alert System** with WebSocket broadcasting - **Zone-based Monitoring** with capacity management - **RTSP Stream Processing** for live camera feeds - **Video File Analysis** for uploaded content - **RESTful API** with interactive documentation ## ๐ŸŽฏ Quick Demo ### Upload an Image 1. Go to the [API Documentation](/docs) 2. Try the `POST /process/image` endpoint 3. Upload any image with people 4. Get instant people count and annotated result! ### Test the API ```bash # Health Check curl https://YOUR-USERNAME-crowd-detection-api.hf.space/health # Get Demo Zones curl https://YOUR-USERNAME-crowd-detection-api.hf.space/zones/heatmap ``` ## ๐Ÿ“Š API Endpoints ### Core Features - `POST /process/image` - Analyze uploaded images for people counting - `GET /zones/heatmap` - Get zones with crowd density data - `GET /health` - API health status - `GET /` - API information and quick start guide ### Advanced Features - `POST /monitor/rtsp` - Start monitoring RTSP streams - `POST /process/video` - Process uploaded video files - `POST /emergency` - Send emergency alerts - `GET /crowd-flow` - Get crowd flow analytics ### WebSocket Endpoints - `ws://YOUR-SPACE-URL/ws/alerts` - Real-time alerts - `ws://YOUR-SPACE-URL/ws/frames/{camera_id}` - Live video frames - `ws://YOUR-SPACE-URL/ws/live-map` - Live map updates ## ๐Ÿ› ๏ธ Technology Stack - **Backend**: FastAPI + Python 3.9 - **AI/ML**: YOLOv8 (Ultralytics), PyTorch, OpenCV - **Data Processing**: NumPy, SciPy - **Deployment**: Docker on Hugging Face Spaces - **Real-time**: WebSockets for live updates ## ๐ŸŽฎ Usage Examples ### JavaScript (Browser) ```javascript // Test people counting const formData = new FormData(); formData.append('file', imageFile); fetch('/process/image', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { console.log('People count:', data.people_count); // Display annotated image document.getElementById('result').src = data.annotated_image; }); // WebSocket alerts const ws = new WebSocket('wss://YOUR-SPACE-URL/ws/alerts'); ws.onmessage = (event) => { const alert = JSON.parse(event.data); console.log('Alert received:', alert); }; ``` ### Python ```python import requests import websockets import asyncio # Upload image for analysis with open('crowd_image.jpg', 'rb') as f: response = requests.post( 'https://YOUR-SPACE-URL/process/image', files={'file': f} ) result = response.json() print(f"People detected: {result['people_count']}") # WebSocket connection async def listen_alerts(): uri = "wss://YOUR-SPACE-URL/ws/alerts" async with websockets.connect(uri) as websocket: async for message in websocket: data = json.loads(message) print(f"Alert: {data}") ``` ### cURL ```bash # Process image curl -X POST "https://YOUR-SPACE-URL/process/image" \ -F "file=@crowd_photo.jpg" # Start RTSP monitoring curl -X POST "https://YOUR-SPACE-URL/monitor/rtsp" \ -d "camera_id=cam1&rtsp_url=rtsp://example.com/stream&zone_id=zone1" # Send emergency alert curl -X POST "https://YOUR-SPACE-URL/emergency" \ -d "emergency_type=MEDICAL&message=Medical emergency&location=Gate 1" ``` ## ๐Ÿ—๏ธ Architecture ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Input Layer โ”‚ => โ”‚ AI Processingโ”‚ => โ”‚ Output Layer โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ โ€ข Image Upload โ”‚ โ”‚ โ€ข YOLOv8 โ”‚ โ”‚ โ€ข People Count โ”‚ โ”‚ โ€ข Video Stream โ”‚ โ”‚ โ€ข OpenCV โ”‚ โ”‚ โ€ข Heatmaps โ”‚ โ”‚ โ€ข RTSP Feed โ”‚ โ”‚ โ€ข Anomaly โ”‚ โ”‚ โ€ข Alerts โ”‚ โ”‚ โ€ข WebSocket โ”‚ โ”‚ Detection โ”‚ โ”‚ โ€ข WebSocket โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ## ๐Ÿšฆ System Status - โœ… **AI Models**: YOLOv8s loaded and ready - โœ… **Image Processing**: Real-time people detection - โœ… **WebSocket**: Live alerts and updates - โœ… **API Documentation**: Interactive Swagger UI - โšก **Performance**: Optimized for Hugging Face Spaces ## ๐Ÿ“ˆ Performance - **Image Processing**: ~1-3 seconds per image - **People Detection Accuracy**: >90% (YOLOv8s) - **Supported Formats**: JPG, PNG, MP4, AVI, RTSP - **Concurrent Users**: Scales with Hugging Face Spaces - **Model Size**: ~20MB (YOLOv8s) ## ๐Ÿ”’ Privacy & Security - **No Data Storage**: Images processed in memory only - **Temporary Files**: Automatically cleaned up - **No Logging**: Personal data not logged - **CORS Enabled**: Secure browser access - **Rate Limiting**: Built-in request throttling ## ๐ŸŒŸ Use Cases ### Public Safety - **Crowd Management**: Monitor capacity at events - **Emergency Response**: Detect anomalies and alert teams - **Traffic Analysis**: Count people flow in areas ### Smart Cities - **Urban Planning**: Analyze pedestrian patterns - **Public Transport**: Monitor station capacity - **Event Management**: Real-time crowd control ### Business Intelligence - **Retail Analytics**: Customer flow analysis - **Venue Management**: Occupancy monitoring - **Security Systems**: Automated surveillance