Spaces:
Sleeping
Sleeping
| 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 | |
| ``` | |