This repository contains the vision-service component of an AI-powered urban safety platform. It ingests a video stream, runs three parallel AI branches (anomaly risk, incident classification, and crowd detection), fuses the results, and writes incident alerts with geolocation to Supabase.
Cities need faster, proactive safety monitoring. The goal is to automatically detect incidents from video, associate them with locations, dispatch the nearest drone (simulated), and show live movement on a map. This service handles the first step: AI detection and alert generation from video feeds.
- Computer vision and deep learning (video preprocessing, detection, classification)
- Backend APIs (FastAPI, async processing, data storage)
- Geospatial concepts (lat/lng mapping, distance, routing)
- Frontend dashboards (maps, real-time updates, UI/UX)
- DevOps basics (model artifacts, deployment, environment config)
- Video decoding and frame sampling (OpenCV)
- Anomaly detection using autoencoders (PyTorch)
- Video classification (VideoMAE via Hugging Face)
- Object detection for crowd estimation (YOLO via Ultralytics)
- Result fusion and threshold calibration
- Map visualization (e.g., Mapbox, Leaflet, or Google Maps)
- Real-time updates (WebSocket, polling, or SSE)
- Incident list with severity and status
- Drone markers with live position, ETA, and route
- Video preview widgets (source feed and response drone)
Flow:
- Download video from
stream_url. - Decode once and build shared frame sets.
- Run three branches in parallel:
- Risk scoring (autoencoder reconstruction error)
- Incident classification (VideoMAE)
- Crowd detection (YOLO person detection + clustering)
- Fuse results into a single incident type + risk score.
- Insert incident into Supabase with lat/lng.
main.py- FastAPI app with/analyzeendpoint, Supabase insert.analysis_service.py- Orchestrates download, frame prep, parallel branches, and fusion.risk_scoring_service.py- Autoencoder-based anomaly scoring.incident_classification_service.py- VideoMAE-based incident classification with rejection rules.crowd_detection_service.py- YOLO-based crowd scoring.fusion_service.py- Combines branch outputs into final incident decision.anomaly_engine/- Autoencoder model and utilities.yolo11m.pt,yolo11n.pt- YOLO model weights.
Request body:
{
"session_id": "sess-123",
"camera_id": "cam-01",
"stream_url": "https://.../video.mp4",
"lat": 18.5204,
"lng": 73.8567
}Response body:
{
"id": 1,
"camera_id": "cam-01",
"incident_type": "vehicle_collision",
"risk_score": 0.72,
"confidence": 0.81,
"decision_source": "agreed",
"crowd_score": 0.0,
"lat": 18.5204,
"lng": 73.8567,
"snapshot_url": null
}Required:
SUPABASE_URLSUPABASE_SERVICE_KEY
Optional (tuning):
ALERT_THRESHOLD(default 0.08)MAX_CONCURRENT_DOWNLOADS,MAX_CONCURRENT_RISK,MAX_CONCURRENT_CLASSIFIER,MAX_CONCURRENT_CROWDDOWNLOAD_TIMEOUT_SECONDS,REQUEST_TIMEOUT_SECONDSVIDEO_CLS_MODEL_NAME,VIDEO_CLS_TOP_K,VIDEO_CLS_DEVICEYOLO_CROWD_MODEL,YOLO_PERSON_CONF
- Install dependencies (example):
fastapi,uvicorn,torch,opencv-python,numpy,requests,python-dotenv,transformers,ultralytics,supabase
- Set environment variables in
.env. - Run:
uvicorn main:app --host 0.0.0.0 --port 8000
- This service expects a direct video URL for
stream_url. - The autoencoder model weights are stored at
anomaly_engine/trained_model.pth. - The fusion logic ensures classifier outputs are only trusted when backed by risk or confidence thresholds.
This service is part of a larger hackathon project for an AI-powered urban safety platform. The complete system includes simulated drone dispatch and a command dashboard, which live outside this repository.