This guide documents the complete API surface of CrowdShield AI (FastAPI Backend + AI Engine) and details how they interact to provide real-time crowd density estimations, privacy-preserving face blurring, automatic alerts, and safety analytics.
- Clone the repository and navigate to the project directory.
- Install dependencies for both backend and AI engine:
pip install -r backend/requirements.txt pip install -r ai-engine/requirements.txt
- Setup .env in both backend/ , ai-engine/ with appropriate environment variables (see .env.example files for reference).
- Change directory of backend/ and ai-engine/ in two different terminal windows to run them separately.
cd backend # In one terminal cd ai-engine # In another terminal cd frontend # In another terminal
- Activate the virtual environment in both terminals (backend/ and ai-engine/):
Windows -> venv\Scripts\activate Linux, Mac -> source /venv/bin/activate
- Start the FastAPI backend server:
python run.py
- In a separate terminal, start the AI Engine server:
python api_server.py
- Run the frontend in another terminal , access the ReactJS dashboard at
http://localhost:3000and log in with the default admin credentials:- Username:
admin - Password:
admin123
- Username:
- FastAPI Backend Server: Exposes database interfaces, authentication, user sessions, camera configurations, heatmap generation, alert lifecycles, and compliance logs.
- Port:
http://localhost:8000 - Swagger Docs: http://localhost:8000/docs
- Port:
- AI Engine Server: Exposes interfaces for deep learning inference (YOLOv8 person detection), centroid tracking, OpenCV face blurring, and video stream processing.
- Port:
http://localhost:8001 - Swagger Docs: http://localhost:8001/docs
- Port:
CrowdShield AI supports two secure authentication mechanisms:
- Session Cookie (
access_token): Used for the ReactJS Frontend UI. Swagger UI sets this automatically after logging in via/api/auth/login. - API Key Bearer Token: Used for background worker agents (like the AI Engine) to authenticate securely without holding an active browser session.
- Header:
Authorization: Bearer codorra_ai_engine_secret_2026
- Header:
The unified data flow operates in five logical steps:
sequenceDiagram
autonumber
actor Admin
participant Frontend as React Dashboard
participant Backend as FastAPI Backend (Port 8000)
participant AI as AI Engine (Port 8001)
Admin->>Backend: 1. Register & Login User
Admin->>Backend: 2. Register Camera (capacity=100)
Admin->>AI: 3. Trigger stream processing (RTSP or File)
loop Every Nth Frame
AI->>AI: 4. YOLO Detects people & blurs faces
AI->>Backend: 5. POST /api/detections/ (metrics)
Note over AI,Backend: If Density > 85%, Auto-generates Alert!
end
Frontend->>Backend: 6. Fetch live heatmap & active alerts
- Endpoint:
POST /api/auth/register - Payload:
{ "username": "operator_john", "email": "john@crowdshield.local", "password": "secure_password_123", "role": "admin" }
- Endpoint:
POST /api/auth/login - Payload:
{ "username": "operator_john", "password": "secure_password_123" }
- Endpoint:
POST /api/cameras/ - Headers:
Authorization: Bearer codorra_ai_engine_secret_2026(or Cookie) - Payload:
{ "name": "Metro Station A - Platform 1", "location": "Downtown Metro", "latitude": 27.7172, "longitude": 85.3240, "stream_url": "rtsp://camera.local/stream1", "capacity": 150, "status": "active", "description": "Main platform camera" } - Response:
{"success": true, "camera_id": "6a19834cf0f0ece547290126"}
- Endpoint:
GET /api/cameras
- Endpoint:
POST /api/detections/ - Headers:
Authorization: Bearer codorra_ai_engine_secret_2026 - Payload:
{ "camera_id": "6a19834cf0f0ece547290126", "people_count": 130, "density_percentage": 86.6, "density_level": "critical", "risk_score": 0.87, "frame_data": {} } - Behavior: Saves details to
density_logs. Becausedensity_levelis"critical", an active alert is generated in the database automatically.
- Endpoint:
GET /api/detections/live
- Endpoint:
GET /api/detections/{camera_id}/history?limit=50
- Endpoint:
GET /api/alerts/?status=active&severity=critical
- Endpoint:
POST /api/alerts/{alert_id}/acknowledge - Payload:
{ "note": "Dispatching station staff to platform 1." }
- Endpoint:
POST /api/alerts/{alert_id}/resolve - Payload:
{ "note": "Crowd size dispersed. Normal service resumed." }
- Endpoint:
GET /api/heatmap/data?period_hours=1 - Response: Lists geographical coordinates, people count, and intensities (0.0 to 1.0) for active map rendering.
- Endpoint:
GET /api/analytics/temporal?period_hours=24&granularity=hourly&camera_id=6a19834cf0f0ece547290126 - Response: High-resolution arrays for charts (avg/max density, count) grouped by hour.
- Endpoint:
GET /api/privacy/compliance/verify - Response: Verifies data classification compliance, anonymization logs, and retention status.
- Endpoint:
POST /api/privacy/data/deletion?reason=User requested opt-out
Exposes computer vision inference nodes to initialize pipelines, process mock media files, or trigger streaming ingestion.
- Endpoint:
POST /api/process-frame?camera_id=6a19834cf0f0ece547290126 - Content-Type:
multipart/form-data - Form Param:
file(Image upload) - Execution Flow:
- Downloads YOLO model (on first boot).
- Locates coordinates of people using YOLOv8 bounding boxes.
- Crops bounding boxes and applies a Gaussian blur to all faces (privacy mask).
- POSTs metrics directly to Backend
POST /api/detections/using API Key. - Returns anonymized frame results back to caller.
- Endpoint:
POST /api/process-video?camera_id=6a19834cf0f0ece547290126 - Content-Type:
multipart/form-data - Form Param:
file(Video upload) - Response: Starts background processing thread and returns ingestion job status.
- Endpoint:
POST /api/stream?camera_id=6a19834cf0f0ece547290126&stream_url=rtsp://localhost:8554/stream1 - Behavior: Spawns an asynchronous background stream processor that keeps reading camera feeds, processing frames continuously at interval intervals, and pushing metrics directly to backend telemetry APIs in real-time.