🚦 FlowSync AI

Smart Adaptive Traffic Signal Management System

"Every red light is a decision. We made that decision intelligent."

Built at UHack 4.0 — replacing dumb timers with a fully AI-driven perception-to-signal pipeline.


The Problem

In India, an ambulance arrives 8 minutes late on average. Fixed-timer traffic signals waste over 500 crore person-hours annually — not because the infrastructure doesn't exist, but because the intelligence doesn't.

A green light running for 90 seconds with 2 cars while 15 vehicles queue on the perpendicular road is not a traffic system. It's a timer with a paint job.

FlowSync AI replaces that timer with a real-time perception-decision-control loop.


System Architecture

┌─────────────────────────────────────────────────────────────────────┐
│                        FlowSync AI Pipeline                         │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│   Camera / SUMO Simulation                                          │
│          │                                                          │
│          ▼                                                          │
│   ┌─────────────────┐                                               │
│   │   YOLOv8 Vision │  ← dual-model: general + emergency           │
│   │  (yolo_detector)│    area-based density (not headcount)        │
│   └────────┬────────┘                                               │
│            │  traffic_data.json  (every 10 frames)                 │
│            ▼                                                        │
│   ┌─────────────────┐                                               │
│   │  main.py bridge │  ← format conversion + RL dispatch           │
│   └────────┬────────┘                                               │
│            │                                                        │
│            ▼                                                        │
│   ┌─────────────────┐                                               │
│   │  rl_model.py    │  ← Multi-Agent PPO (4 agents)                │
│   │  PPO Inference  │    emergency override hardcoded               │
│   └────────┬────────┘                                               │
│            │                                                        │
│            ▼                                                        │
│   ┌─────────────────┐    ┌──────────────────────────────┐          │
│   │  Flask Dashboard│    │   SUMO TraCI Signal Control   │          │
│   │  (port 5000)    │    │   traci.trafficlight.setPhase │          │
│   └─────────────────┘    └──────────────────────────────┘          │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

Key Innovations

1. Area-Based Density (Not Headcount)

Standard systems count vehicles. We measure physical road space occupied using IRC (Indian Roads Congress) standard vehicle dimensions.

Vehicle Real Area PCU Weight
Motorcycle / Bike 1.6 m² 0.75
Auto-Rickshaw 4.5 m² 0.75
Car 9.0 m² 1.0
Truck 22.5 m² 3.0
Bus 30.0 m² 3.5
Ambulance 10.5 m² 5.0 (priority)

Why it matters: 10 motorcycles and 10 cars both "count" as 10. But 10 cars occupy 5.6× more road space. FlowSync AI knows the difference.


2. Multi-Agent PPO Reinforcement Learning

Four independent PPO agents — one per intersection — coordinate via neighbour pressure messaging to create emergent green waves.

State Vector (10-dim per agent):
  [q_N, q_S, q_E, q_W,     # normalized queue per direction
   phase,                    # current signal phase
   phase_timer,              # time in current phase
   ambulance_flag,           # emergency override signal
   nb_pressure_0,            # neighbour 1 queue pressure
   nb_pressure_1,            # neighbour 2 queue pressure
   nb_pressure_2]            # neighbour 3 queue pressure

Action Space: Discrete(2)
  0 → NS-GREEN
  1 → EW-GREEN

Reward Function:
  r = -(total_queue / MAX_Q)
    - 0.3 if wrong phase for dominant direction
    + 0.5 if ambulance override active
    + 0.2 if local queue cleared while neighbours are busy

3. Dual-Model Emergency Detection

Two YOLOv8 models run in parallel on every frame:

  • Model 1 (yolov8n.pt) — General traffic: car, truck, bus, motorcycle, person
  • Model 2 (best.pt) — Emergency vehicles: Indian ambulance dataset, fine-tuned with transfer learning, 78.2% mAP@50

Emergency confirmation requires 3 consecutive frames to eliminate false positives. When confirmed, the RL pipeline is bypassed entirely — emergency lane gets immediate green. Safety is not left to a probabilistic model.


4. SUMO Traffic Simulation (TraCI Integration)

Before connecting to a physical camera, the full system was validated inside SUMO (Simulation of Urban MObility) — a 3×3 grid network with probabilistic traffic flows.

# TraCI real-time data extraction
vehicles    = traci.lane.getLastStepVehicleNumber("A1B1_0")
waiting     = traci.lane.getWaitingTime("A1B1_0")
queue       = traci.lane.getLastStepHaltingNumber("A1B1_0")
avg_speed   = traci.lane.getLastStepMeanSpeed("A1B1_0")
veh_type    = traci.vehicle.getTypeID(veh_id)  # for PCU scoring

The simulation uses unequal probabilistic flows (0.6 / 0.3 / 0.2 / 0.1) to generate realistic traffic imbalance — the exact scenario where adaptive signals outperform fixed timers.


Project Structure

FlowsSync_SIM/
│
├── yolo_detector.py        # Vision layer — dual YOLO, area density, emergency
├── vehicle_counter.py      # SUMO TraCI interface — PCU scoring, metrics
├── rl_model.py             # PPO inference module — emergency override, fallback
├── main.py                 # Integration bridge — YOLO → RL → decision loop
├── dashboard.py            # Flask live dashboard — density, history, signals
│
├── ma_logs/
│   ├── agent_NW.zip        # Trained PPO agent — NW intersection
│   ├── agent_NE.zip        # Trained PPO agent — NE intersection
│   ├── agent_SW.zip        # Trained PPO agent — SW intersection
│   └── agent_SE.zip        # Trained PPO agent — SE intersection
│
├── network.net.xml         # SUMO 3×3 grid road network
├── routes.rou.xml          # Probabilistic vehicle flows (end=300s)
├── run.sumocfg             # SUMO simulation config
│
├── best.pt                 # Fine-tuned emergency vehicle model
├── yolov8n.pt              # General traffic detection model
│
└── traffic_data.json       # Live state bridge (YOLO → RL)

Setup & Installation

Prerequisites

# Python 3.11 (required — SB3 not yet compatible with 3.12+)
python --version  # must be 3.11.x

# SUMO 1.26+ (for simulation module)
sumo --version

# CUDA-enabled PyTorch (RTX recommended)
nvidia-smi

Install Dependencies

# Create virtual environment
python -m venv .venv
.venv\Scripts\activate  # Windows
source .venv/bin/activate  # Linux/Mac

# Core dependencies
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
pip install ultralytics opencv-python flask stable-baselines3

# SUMO Python bindings
pip install traci

# Verify GPU
python -c "import torch; print(torch.cuda.is_available())"
# Expected: True

Set SUMO Environment

# Windows PowerShell
$env:SUMO_HOME = "C:\Program Files (x86)\Eclipse\Sumo"

# Linux / Mac
export SUMO_HOME=/usr/share/sumo

Running the System

Full Pipeline (3 terminals)

# Terminal 1 — Vision layer
python yolo_detector.py

# Terminal 2 — RL decision loop
python main.py

# Terminal 3 — Live dashboard
python dashboard.py
# Opens http://127.0.0.1:5000 automatically

SUMO Simulation Only

# Run simulation with TraCI vehicle counting
python vehicle_counter.py
# Opens SUMO GUI with real-time lane density extraction

RL Inference Test

python rl_model.py
# Runs with test state vectors
# Expected output: Source: PPO (not rule-based)

Dashboard Features

Panel What It Shows
Lane Density Real-time area-based density bars per lane. Highest density lane highlighted in green — receives signal priority.
Signal & Emergency Current signal decision (NS-GREEN / EW-GREEN), RL source (PPO / emergency-override), ambulance alert with lane identification.
Density Over Time Historical density graph for all 4 lanes — last 200 decisions. Reveals traffic patterns over time.
RL Decision History Timestamped log of every PPO decision — signal, source, emergency status, lane. Full audit trail.

Model Training

YOLO Emergency Model

from ultralytics import YOLO

model = YOLO("yolov8n.pt")  # pretrained backbone
model.train(
    data="dataset.yaml",    # Indian ambulance dataset (Roboflow)
    epochs=50,
    imgsz=640,
    device="cuda",
    batch=16,
    augment=True,
    lr0=0.001,
    mosaic=1.0,
    freeze=10,              # transfer learning — backbone frozen
)
# Results: mAP@50 = 78.2%, Precision = 78.7%, Recall = 72.6%

Multi-Agent PPO Training

Training is handled in MultiAgent_Traffic_Management.ipynb.

Architecture:  4× PPO (stable-baselines3)
Environment:   SingleIntersectionEnv (Gymnasium)
Observation:   10-dimensional per agent
Action:        Discrete(2) — NS or EW green
Reward:        -queue_pressure + ambulance_bonus + coordination_bonus
Coordination:  Neighbour pressure broadcasting (adjacency graph)
Training time: ~6 iterations on Colab T4 GPU
Saved models:  ./ma_logs/agent_{NW,NE,SW,SE}.zip

Limitations & Honest Assessment

This is a research prototype, not a production deployment. Known limitations:

Limitation Current State Planned Fix
Ambulance detection accuracy 78.2% mAP@50 Larger Indian-specific dataset, more epochs
Night / low-light detection Not tested IR camera support, night-augmented training
Fire brigade / police vehicle Partially detected Dedicated multi-class emergency dataset
Camera calibration Fixed lane zones Perspective transform + reference point calibration
Multi-intersection demo Single junction V2X integration, city-wide deployment
Occlusion handling Limited Tracking (DeepSORT), multi-camera fusion

Future Roadmap

Phase 2  →  GPS + acoustic siren fusion for early ambulance detection
Phase 3  →  Jetson Nano edge deployment (real CCTV integration)
Phase 4  →  City-wide signal coordination (V2X protocol)
Phase 5  →  Pedestrian priority + crossing time estimation
Phase 6  →  Incident detection (sudden queue spike + zero speed)
Phase 7  →  Time-of-day adaptive thresholds (peak hour learning)

Technical Stack

Component Technology
Computer Vision YOLOv8n (Ultralytics 8.4.22)
Reinforcement Learning PPO (Stable-Baselines3)
Simulation SUMO 1.26 + TraCI API
Backend Flask 3.x
Frontend Vanilla JS + Chart.js
Video Stream MJPEG over HTTP
GPU NVIDIA RTX 2050 (CUDA 12.5)
Training Google Colab T4

References


**Built with unreasonable commitment during UHack 4.0** *From pixels to saved lives — one intersection at a time.*

Built With

  • multi-agent-framewrok
  • python
  • reinforcement-learning
Share this project:

Updates