salvirezwan's picture
Add HF Spaces deployment
17c8768
|
Raw
History Blame Contribute Delete
10.1 kB
---
title: Traffic Video Analytics
emoji: πŸš—
colorFrom: blue
colorTo: gray
sdk: docker
app_port: 7860
short_description: Real-time vehicle detection, tracking, and traffic analytics
---
# Traffic Video Analytics Project
Real-time vehicle detection, multi-object tracking, and traffic analytics β€” end-to-end from raw video to a live React dashboard.
**Stack:** YOLOv8s Β· ByteTrack Β· ONNX Runtime GPU Β· FastAPI Β· WebSockets Β· React Β· Recharts Β· Tailwind CSS Β· SQLite Β· Docker
---
## Features
- **Vehicle detection** β€” YOLOv8s fine-tuned on traffic surveillance footage (UA-DETRAC dataset), exported to ONNX for fast GPU inference
- **Multi-object tracking** β€” ByteTrack via the `supervision` library; each vehicle gets a stable track ID across frames
- **Counting lines** β€” configurable virtual tripwires; vehicles are counted the moment their center crosses a line
- **Speed estimation** β€” pixel displacement Γ— camera calibration constant β†’ km/h per track
- **Class breakdown** β€” real-time split across car, bus, motorcycle, and truck
- **Anomaly detection** β€” rolling 60-frame window; surge alert fires when count exceeds 2Οƒ from the mean; stopped-vehicle alert for stationary tracks
- **Drift monitoring** β€” rolling confidence baseline; alerts when model performance degrades during a live run
- **Live dashboard** β€” video feed, KPI cards, vehicle count chart, speed distribution, class breakdown, alerts panel, track overlay canvas
- **Demo mode** β€” fully synthetic traffic scene (no ONNX model, no camera, no GPU required)
- **REST + WebSocket API** β€” annotated JPEG frames on `/ws/video`, JSON metrics on `/ws/metrics`, historical data via `/analytics/*`
- **Dockerised** β€” single `docker compose up` brings up the full stack (GPU optional)
---
## Demo
Demo mode generates a synthetic 2-lane traffic scene entirely in software using OpenCV and NumPy. Vehicles spawn with realistic class distribution, travel at 35–78 km/h, and produce real `Detection` objects identical to ONNX model output β€” no weights file or camera needed.
> Start the project and click **Demo** in the Pipeline Control bar.
---
## Architecture
```
Video Source (file / webcam / RTSP / demo)
β”‚
β–Ό
OpenCV Frame Capture core/video_source.py
β”‚
β–Ό
YOLOv8s β€” ONNX Runtime GPU core/detector.py
β”‚
β–Ό
ByteTrack (supervision) core/tracker.py
β”‚
β–Ό
Analytics Engine core/analytics.py
β”œβ”€β”€ multi-line vehicle counting
β”œβ”€β”€ speed estimation (px/frame Γ— calibration β†’ km/h)
β”œβ”€β”€ class breakdown
β”œβ”€β”€ anomaly detection (surge + stopped vehicle)
└── drift monitoring (confidence rolling baseline)
β”‚
β–Ό
FastAPI Backend api/
β”œβ”€β”€ WebSocket /ws/video β€” binary JPEG frames
β”œβ”€β”€ WebSocket /ws/metrics β€” JSON MetricsMessage per frame
└── REST /analytics/* β€” historical aggregates
β”‚
β–Ό
React Dashboard dashboard/
β”œβ”€β”€ VideoFeed canvas
β”œβ”€β”€ KPI cards (StatsCards)
β”œβ”€β”€ VehicleCountChart
β”œβ”€β”€ SpeedDistribution
β”œβ”€β”€ ClassBreakdown
β”œβ”€β”€ AlertsPanel
└── TrackOverlay canvas
β”‚
β–Ό
SQLite (hourly aggregates) + in-memory ring buffer (last 5 min)
```
---
## Quick Start β€” Demo Mode (no model required)
### Docker (recommended)
```bash
git clone https://github.com/salvirezwan/Traffic-Video-Analytics-Project.git
cd Traffic-Video-Analytics-Project
docker compose up --build
```
Open **http://localhost** in your browser, then click **Demo** in the Pipeline Control bar.
> The `deploy.resources` GPU block in `docker-compose.yml` is silently ignored if `nvidia-container-toolkit` is not installed β€” CPU inference and demo mode work fine without it.
### Local development
**Prerequisites:** Python 3.10+, Node 20+
```bash
# 1. Backend
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env # edit as needed
uvicorn api.main:app --reload --host 0.0.0.0 --port 8000
# 2. Frontend (separate terminal)
cd dashboard
npm install
npm run dev
```
Open **http://localhost:3000**, click **Demo**.
---
## Full Setup β€” Live Video with GPU Inference
### 1. Requirements
- NVIDIA GPU with CUDA 12.x support
- CUDA Toolkit 12.6 + cuDNN 9.x installed
- `onnxruntime-gpu` (included in `requirements.txt`)
### 2. Obtain weights
Train on Colab (see [Training](#training)) or download a pre-exported file and place it at:
```
models/weights/yolov8s_traffic.onnx
```
### 3. Configure
```bash
cp .env.example .env
```
Key variables:
| Variable | Default | Description |
|---|---|---|
| `MODEL_PATH` | `models/weights/yolov8s_traffic.onnx` | Path to ONNX weights |
| `VIDEO_SOURCE` | `data/sample_videos/traffic.mp4` | File path, `0` for webcam, `rtsp://…` |
| `CONFIDENCE_THRESHOLD` | `0.4` | Detection confidence (0–1) |
| `IOU_THRESHOLD` | `0.5` | NMS IoU threshold |
| `METERS_PER_PIXEL` | `0.05` | Speed calibration constant |
| `JPEG_QUALITY` | `80` | Video stream compression (1–100) |
### 4. Run
```bash
# Docker (with GPU)
docker compose up --build
# Local
uvicorn api.main:app --reload --host 0.0.0.0 --port 8000
# Frontend: cd dashboard && npm run dev
```
### 5. Use the dashboard
1. Set **Source** (file path, `0` for webcam, or RTSP URL) in the Pipeline Control bar.
2. Adjust **Confidence** threshold as needed (lower = more detections, more false positives).
3. Optionally expand **Lines** to define counting tripwires β€” enter `(x1, y1) β†’ (x2, y2)` in source frame pixels.
4. Click **Start**.
---
## Counting Lines
Lines are configured per-run via the dashboard UI or directly via the API. Each line has a name and two endpoints in the **source frame's pixel coordinate system**.
A vehicle is counted when its center point crosses from one side of the line to the other.
```json
POST /pipeline/start
{
"source": "data/sample_videos/traffic.mp4",
"confidence_threshold": 0.35,
"counting_lines": [
{ "name": "north", "x1": 0, "y1": 200, "x2": 1280, "y2": 200 },
{ "name": "south", "x1": 0, "y1": 500, "x2": 1280, "y2": 500 }
]
}
```
Live counts per line are broadcast on `/ws/metrics` β†’ `count_per_line` and rendered on the Track Overlay canvas.
---
## API Reference
Interactive docs available at **http://localhost:8000/docs**
| Method | Path | Description |
|---|---|---|
| `POST` | `/pipeline/start` | Start (or restart) the pipeline |
| `POST` | `/pipeline/stop` | Stop the running pipeline |
| `GET` | `/pipeline/status` | Current pipeline state |
| `WS` | `/ws/video` | Binary JPEG frame stream |
| `WS` | `/ws/metrics` | JSON `MetricsMessage` per frame |
| `GET` | `/analytics/recent` | Last N minutes from ring buffer |
| `GET` | `/analytics/hourly` | Hourly aggregates from SQLite |
| `GET` | `/health` | Health check |
---
## Training
All training happens on Google Colab (free T4 GPU). The local machine is inference-only.
```
notebooks/
β”œβ”€β”€ 01_data_prep.ipynb # Download UA-DETRAC, convert to YOLO format
└── 02_training.ipynb # Train YOLOv8s, evaluate, export to ONNX
```
**Workflow:**
1. Open `notebooks/02_training.ipynb` in Colab.
2. Run all cells β€” trains YOLOv8s on UA-DETRAC, saves `.pt` + `.onnx` to Google Drive.
3. Download `yolov8s_traffic.onnx` to `models/weights/`.
4. Restart the API β€” it loads the model automatically on startup.
**Hardware used:**
- Training: Google Colab T4 (15 GB VRAM), `batch_size=16`
- Inference: NVIDIA RTX 3050 Ti (4 GB VRAM) β€” YOLOv8s ONNX fits comfortably
---
## Project Structure
```
traffic-video-analytics-project/
β”œβ”€β”€ core/ # CV + analytics engine
β”‚ β”œβ”€β”€ detector.py # ONNX/YOLOv8 inference wrapper
β”‚ β”œβ”€β”€ tracker.py # ByteTrack via supervision
β”‚ β”œβ”€β”€ video_source.py # OpenCV frame capture abstraction
β”‚ β”œβ”€β”€ demo_generator.py # Synthetic traffic scene (no model needed)
β”‚ β”œβ”€β”€ pipeline.py # Orchestrator: detect β†’ track β†’ analyse
β”‚ └── analytics.py # Counting, speed, anomalies, drift monitor
β”œβ”€β”€ api/ # FastAPI backend
β”‚ β”œβ”€β”€ main.py
β”‚ β”œβ”€β”€ pipeline_manager.py # Singleton: owns Pipeline, fans out to WS clients
β”‚ β”œβ”€β”€ database.py # SQLite + in-memory ring buffer
β”‚ β”œβ”€β”€ schemas.py # Pydantic v2 models
β”‚ β”œβ”€β”€ routes/
β”‚ β”‚ β”œβ”€β”€ stream.py # WebSocket video + metrics, pipeline control
β”‚ β”‚ β”œβ”€β”€ analytics.py # Historical data REST endpoints
β”‚ β”‚ └── health.py
β”‚ └── Dockerfile
β”œβ”€β”€ dashboard/ # React + Vite frontend
β”‚ β”œβ”€β”€ src/
β”‚ β”‚ β”œβ”€β”€ components/ # VideoFeed, StatsCards, charts, AlertsPanel, …
β”‚ β”‚ └── hooks/
β”‚ β”‚ └── useWebSocket.js
β”‚ β”œβ”€β”€ nginx.conf # Reverse proxy for Docker deployment
β”‚ └── Dockerfile
β”œβ”€β”€ models/
β”‚ β”œβ”€β”€ weights/ # .onnx / .pt files (gitignored)
β”‚ └── export/ # ONNX export scripts
β”œβ”€β”€ notebooks/ # Colab training notebooks
β”œβ”€β”€ tests/ # pytest test suite
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ requirements.txt
└── .env.example
```
---
## Tech Stack
| Layer | Technology |
|---|---|
| Detection model | YOLOv8s (Ultralytics) fine-tuned on UA-DETRAC β†’ ONNX |
| Inference runtime | ONNX Runtime with CUDAExecutionProvider |
| Tracking | ByteTrack via `supervision` |
| Video I/O | OpenCV |
| Backend | FastAPI + uvicorn + WebSockets |
| Data models | Pydantic v2 |
| Persistence | SQLite via `aiosqlite` + in-memory ring buffer |
| Frontend | React 18 + Vite + Tailwind CSS + Recharts |
| Container | Docker + Docker Compose |
| Training | Google Colab T4 GPU |
---
## Development
```bash
# Backend tests
pytest tests/ -v
# Frontend lint
cd dashboard && npm run lint
```