Skip to content

archittmittal/AI-Traffic-Flow-Optimizer-and-Emergency-Grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿšฆ TrafficAI โ€” Intelligent Traffic Management System

AI-Powered Smart City Traffic Command Center

Next.js React TailwindCSS Flask Socket.IO Chart.js Leaflet TypeScript Python License


A real-time AI-powered traffic management dashboard designed for city traffic authorities to monitor intersections, control signals, manage emergency corridors, and detect incidents โ€” all from a single command center interface.

๐Ÿ”ด Live Demo | ๐ŸŽฅ Demo Video

๐Ÿš€ Getting Started ยท ๐Ÿ“– Features ยท ๐Ÿ—๏ธ Architecture ยท ๐Ÿ“ก API Reference ยท ๐ŸŽฏ Pages


๐Ÿ“Œ Problem Statement

Urban traffic congestion leads to โ‚น1.5 lakh crore annual economic losses in India. Current traffic management relies on:

  • โŒ Fixed-timer signals that ignore real-time conditions
  • โŒ Manual monitoring with limited camera coverage
  • โŒ Delayed emergency response due to congested routes
  • โŒ No predictive capabilities for congestion prevention

TrafficAI solves this with an AI-driven, real-time adaptive system that brings intelligence to every intersection.


โœจ Features

๐Ÿ—บ๏ธ Real-Time Monitoring

  • Live city map with color-coded intersections
  • ๐ŸŸข Green = Low Traffic
  • ๐ŸŸก Yellow = Moderate Traffic
  • ๐Ÿ”ด Red = Heavy Congestion
  • Vehicle detection with AI bounding boxes
  • Lane-wise density heatmaps

๐Ÿšฆ AI Signal Control

  • AI-recommended signal timing (RL-based)
  • Manual override controls (Force Green / Pause / Reset)
  • Live countdown timers
  • Signal efficiency scoring per intersection

๐Ÿš‘ Emergency Management

  • Real-time emergency vehicle tracking
  • Green corridor activation/deactivation
  • Route visualization on map
  • ETA calculation and display

โš ๏ธ Incident Detection

  • Automatic accident detection alerts
  • Road blockage identification
  • Congestion spike detection
  • AI-suggested alternate routes

๐Ÿ“Š Analytics & Prediction

  • Congestion trends by hour/zone
  • Signal efficiency analytics
  • Emergency response time tracking
  • AI-powered traffic prediction (6hr horizon)

๐Ÿ”Œ Real-Time Architecture

  • WebSocket-based live updates
  • REST API backend
  • Background traffic simulation engine
  • Multi-channel event system

๐Ÿ—๏ธ System Architecture

High-Level System Flow

graph TD
    A["๐Ÿ“น Traffic Signal Cameras"] --> B["๐Ÿง  Edge AI Processing Node"]

    B --> C["๐Ÿ” Vehicle Detection<br/>(Deep Learning - YOLOv8)"]
    B --> D["๐Ÿ“Š Grid-Based Traffic<br/>Density Analysis"]

    C --> E["โš™๏ธ Traffic Data Processing"]
    D --> E

    E --> F["๐Ÿง  Intersection Signal Intelligence"]

    F --> G["๐Ÿค– Reinforcement Learning<br/>Signal Controller"]
    F --> H["๐ŸŒ Zone Traffic Coordinator"]

    G --> I["๐Ÿšฆ Traffic Signal Hardware"]
    H --> J["๐Ÿ™๏ธ City Traffic Brain"]

    J --> K["๐Ÿ“ˆ Traffic Prediction Engine"]
    J --> L["๐Ÿš‘ Emergency Response System"]
    J --> M["๐Ÿ’ฅ Accident Detection System"]
    J --> N["๐Ÿ“ฑ Citizen Traffic App"]

    L --> O["๐Ÿš‘ Emergency Vehicle App"]
    L --> P["๐ŸŸข Green Corridor<br/>Signal Control"]
    L --> Q["๐Ÿ“ก Cell Tower<br/>Broadcast Alerts"]

    Q --> R["๐Ÿ“ฑ Citizen Mobile Phones"]

    M --> S["๐Ÿ”€ Traffic Rerouting Engine"]
    S --> T["๐Ÿ”ด Dynamic Red Direction<br/>Indicators"]

    N --> U["๐Ÿ“ Live Traffic Updates"]
    N --> V["๐Ÿ†˜ Accident Reporting"]
    N --> W["๐Ÿ—บ๏ธ Route Optimization"]

    style A fill:#1a1a2e,stroke:#06b6d4,color:#e2e8f0
    style B fill:#1a1a2e,stroke:#06b6d4,color:#e2e8f0
    style C fill:#1a1a2e,stroke:#10b981,color:#e2e8f0
    style D fill:#1a1a2e,stroke:#10b981,color:#e2e8f0
    style E fill:#1a1a2e,stroke:#a855f7,color:#e2e8f0
    style F fill:#1a1a2e,stroke:#f59e0b,color:#e2e8f0
    style G fill:#1a1a2e,stroke:#f59e0b,color:#e2e8f0
    style H fill:#1a1a2e,stroke:#f59e0b,color:#e2e8f0
    style I fill:#1a1a2e,stroke:#ef4444,color:#e2e8f0
    style J fill:#0f172a,stroke:#06b6d4,color:#06b6d4,stroke-width:3px
    style K fill:#1a1a2e,stroke:#a855f7,color:#e2e8f0
    style L fill:#1a1a2e,stroke:#ef4444,color:#e2e8f0
    style M fill:#1a1a2e,stroke:#ef4444,color:#e2e8f0
    style N fill:#1a1a2e,stroke:#06b6d4,color:#e2e8f0
    style O fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style P fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style Q fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style R fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style S fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style T fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style U fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style V fill:#1e293b,stroke:#94a3b8,color:#94a3b8
    style W fill:#1e293b,stroke:#94a3b8,color:#94a3b8
Loading

Application Architecture

graph LR
    subgraph Frontend["๐Ÿ–ฅ๏ธ Frontend (Next.js + React)"]
        direction TB
        UI["Dashboard UI"]
        MAPS["Leaflet Maps"]
        CHARTS["Chart.js Analytics"]
        SOCKET_C["Socket.IO Client"]
        API_C["REST API Client"]
    end

    subgraph Backend["โš™๏ธ Backend (Flask + Python)"]
        direction TB
        REST["REST API Layer"]
        SOCKET_S["Socket.IO Server"]
        SIM["Traffic Simulation Engine"]
        DATA["Data Store (In-Memory)"]
    end

    subgraph AI["๐Ÿค– AI/ML Pipeline (Future)"]
        direction TB
        YOLO["YOLOv8 Detection"]
        RL["RL Signal Controller"]
        PRED["Prediction Engine"]
    end

    API_C <-->|"HTTP REST"| REST
    SOCKET_C <-->|"WebSocket"| SOCKET_S
    REST <--> DATA
    SOCKET_S <--> SIM
    SIM <--> DATA
    AI -.->|"Model Inference"| REST

    style Frontend fill:#0f172a,stroke:#06b6d4,color:#e2e8f0,stroke-width:2px
    style Backend fill:#0f172a,stroke:#10b981,color:#e2e8f0,stroke-width:2px
    style AI fill:#0f172a,stroke:#f59e0b,color:#e2e8f0,stroke-width:2px,stroke-dasharray: 5 5
Loading

Real-Time Data Flow

sequenceDiagram
    participant C as ๐Ÿ“น Camera/Sensor
    participant E as ๐Ÿง  Edge AI Node
    participant B as โš™๏ธ Flask Backend
    participant S as ๐Ÿ”Œ Socket.IO
    participant F as ๐Ÿ–ฅ๏ธ Frontend Dashboard

    C->>E: Raw video feed
    E->>E: YOLOv8 vehicle detection
    E->>B: Processed traffic data
    B->>B: Update intersection state
    B->>S: Emit traffic_update event
    S-->>F: Real-time push update
    F->>F: Re-render map markers & stats

    Note over B,S: Every 5 seconds
    B->>B: Signal countdown tick
    B->>S: Emit signal_update_batch
    S-->>F: Update signal displays

    Note over F: User Action
    F->>B: POST /api/signals/:id/control
    B->>B: Force green / Pause / Reset
    B->>S: Emit signal_update
    S-->>F: Instant UI update
Loading

๐ŸŽฏ Dashboard Pages

1๏ธโƒฃ Dashboard Overview โ€” /

The command center view โ€” A bird's-eye view of the entire city traffic network

Component Description
๐Ÿ“Š KPI Widgets Active Intersections, Avg Congestion Score, Emergency Corridors, Active Incidents
๐Ÿ—บ๏ธ City Map Leaflet map with color-coded intersection markers (green/yellow/red)
๐Ÿ“ˆ Charts Traffic Density over Time, Average Wait Time, Signal Efficiency

2๏ธโƒฃ Intersection Monitoring โ€” /intersections

Deep-dive into any intersection โ€” Three-panel real-time monitoring layout

Panel Content
๐ŸŽฅ Left โ€” Camera Feed Simulated live feed with AI vehicle detection bounding boxes
๐Ÿ—บ๏ธ Center โ€” Map Zoomed intersection map with signal phase diagram
๐Ÿ“Š Right โ€” Statistics Lane-wise vehicle counts, density bars, queue lengths, signal countdown

3๏ธโƒฃ Signal Control โ€” /signals

AI-assisted traffic signal management with manual override

Feature Details
๐Ÿšฆ Traffic Lights Animated red/yellow/green with glow effects
๐Ÿค– AI Timing Recommended green/yellow/red durations from RL model
๐ŸŽฎ Controls Force Green, Pause Signal, Reset to Auto buttons
โฑ๏ธ Countdown Live countdown timer with animated transitions

4๏ธโƒฃ Emergency Corridor โ€” /emergency

Emergency vehicle tracking and green corridor activation

Feature Details
๐Ÿ—บ๏ธ Route Map Emergency vehicle positions + highlighted route paths
๐Ÿ“‹ Vehicle List ID, type (๐Ÿš‘๐Ÿš’๐Ÿš“), status, clickable selection
โ„น๏ธ Info Panel Current location, destination, ETA
๐ŸŸข Corridor Toggle Activate/Deactivate emergency corridor buttons

5๏ธโƒฃ Incident Monitoring โ€” /incidents

Real-time incident detection with smart rerouting

Feature Details
๐Ÿ—บ๏ธ Incident Map Markers for accidents (โš ๏ธ), blockages (๐Ÿšง), congestion spikes (โšก)
๐Ÿ” Filters Filter by type: All / Accident / Blockage / Congestion
๐Ÿ“‹ Detail Cards Type, location, severity badge, status, description
๐Ÿ”€ Alt Routes AI-suggested alternate routes displayed on map

6๏ธโƒฃ Analytics โ€” /analytics

Comprehensive traffic analytics and predictive insights

Feature Details
๐Ÿ“Š 6 Charts Congestion by Hour, Signal Efficiency, Emergency Response, Traffic Prediction, Density by Zone, Wait Time
๐ŸŽ›๏ธ Filters Date range, Zone selector, Intersection dropdown
๐Ÿ“‹ Performance Table All intersections with congestion %, vehicles, wait time, signal phase
๐Ÿ“ˆ KPI Cards Avg Congestion, Signal Efficiency, Response Time, Incidents Today

๐Ÿ“ก API Reference

Base URL: http://localhost:5001/api

๐Ÿ”— Traffic & Intersections
Method Endpoint Description
GET /intersections List all intersections with real-time data
GET /intersections/:id Get single intersection details
GET /intersections/stats Aggregate stats (avg congestion, vehicle count)

Example Response โ€” /api/intersections/stats

{
  "totalActive": 12,
  "avgCongestionScore": 56,
  "heavyCount": 4,
  "moderateCount": 4,
  "lowCount": 4,
  "totalVehicles": 2557
}
๐Ÿšฆ Signal Control
Method Endpoint Description
GET /signals List all signals
GET /signals/:id Get signal details
POST /signals/:id/control Manual control (force_green / pause / reset)

POST Body:

{ "action": "force_green" }
โš ๏ธ Incidents
Method Endpoint Description
GET /incidents List incidents (filterable: ?type=accident&severity=high)
GET /incidents/:id Get incident with alternate routes
GET /incidents/summary Count by type and status
๐Ÿš‘ Emergency
Method Endpoint Description
GET /emergency/vehicles List all emergency vehicles
GET /emergency/vehicles/:id Get vehicle details with route
POST /emergency/vehicles/:id/corridor Activate/deactivate corridor
๐Ÿ“Š Analytics
Method Endpoint Description
GET /analytics/traffic-density Traffic density by zone over time
GET /analytics/congestion-by-hour Hourly congestion averages
GET /analytics/signal-efficiency Per-intersection signal efficiency
GET /analytics/emergency-response Weekly response time trends
GET /analytics/prediction 6-hour traffic prediction
GET /analytics/summary KPI summary stats
๐Ÿ”Œ Socket.IO Events
Event Direction Description
traffic_update Server โ†’ Client Real-time intersection data (every 5s)
signal_update_batch Server โ†’ Client All signal countdown updates
signal_update Server โ†’ Client Individual signal state change
corridor_update Server โ†’ Client Emergency corridor toggle
incident_alert Server โ†’ Client New incident detected

๐Ÿ“ Project Structure

INDIA_INNOVATES/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ backend/                        # Python Flask Backend
โ”‚   โ”œโ”€โ”€ app.py                         # Main server (REST + Socket.IO + Simulation)
โ”‚   โ””โ”€โ”€ requirements.txt               # Python dependencies
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ src/                            # Next.js Frontend
โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ app/                        # Pages (App Router)
โ”‚   โ”‚   โ”œโ”€โ”€ layout.tsx                 # Root layout (Sidebar + TopBar)
โ”‚   โ”‚   โ”œโ”€โ”€ globals.css                # Dark cyberpunk theme
โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx                   # Dashboard Overview
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ intersections/          # Intersection Monitoring
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ signals/                # Signal Control
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ emergency/              # Emergency Corridor
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ incidents/              # Incident Monitoring
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“‚ analytics/              # Analytics & Reports
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ components/                 # Reusable UI Components
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ layout/                 # Sidebar, TopBar
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ maps/                   # MapView (Leaflet)
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ traffic/                # StatsCard, CameraFeed
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ signals/                # SignalControlPanel
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ alerts/                 # IncidentAlertPanel
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“‚ charts/                 # AnalyticsChart (Chart.js)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ services/                   # API & Socket.IO Client
โ”‚   โ”‚   โ”œโ”€โ”€ api.ts                     # REST API service layer
โ”‚   โ”‚   โ””โ”€โ”€ socket.ts                  # Socket.IO real-time client
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ ๐Ÿ“‚ lib/                        # Utilities & Mock Data
โ”‚       โ””โ”€โ”€ mockData.ts                # Hardcoded demo data
โ”‚
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tailwind.config.ts
โ”œโ”€โ”€ tsconfig.json
โ””โ”€โ”€ README.md

๐Ÿš€ Getting Started

Prerequisites

Tool Version
Node.js >= 18.x
Python >= 3.9
npm >= 9.x

Installation

# 1. Clone the repository
git clone https://github.com/your-username/INDIA_INNOVATES.git
cd INDIA_INNOVATES

# 2. Install frontend dependencies
npm install

# 3. Install backend dependencies
cd backend
pip3 install -r requirements.txt
cd ..

Running the Application

# Terminal 1 โ€” Frontend (http://localhost:3000)
npm run dev

# Terminal 2 โ€” Backend (http://localhost:5001)
cd backend
python3 app.py

๐Ÿ’ก Open http://localhost:3000 in your browser to access the dashboard.

Build for Production

npm run build
npm start

๐Ÿ› ๏ธ Tech Stack Deep Dive

graph TB
    subgraph Presentation["๐ŸŽจ Presentation Layer"]
        NEXT["Next.js 16 (App Router)"]
        REACT["React 19"]
        TW["Tailwind CSS 4"]
        FM["Framer Motion"]
    end

    subgraph Visualization["๐Ÿ“Š Visualization Layer"]
        LEAF["Leaflet + CartoDB Dark Tiles"]
        CHART["Chart.js + react-chartjs-2"]
        HERO["Heroicons"]
    end

    subgraph Communication["๐Ÿ”Œ Communication Layer"]
        REST_C["REST API (fetch)"]
        WS_C["Socket.IO Client"]
    end

    subgraph Server["โš™๏ธ Server Layer"]
        FLASK["Flask 3.0"]
        CORS["Flask-CORS"]
        SIO["Flask-SocketIO"]
        SIM2["Background Simulator Thread"]
    end

    subgraph Future["๐Ÿ”ฎ Future: AI/ML Layer"]
        YOLO2["YOLOv8 Object Detection"]
        RL2["Deep RL Signal Optimization"]
        LSTM["LSTM Traffic Prediction"]
        CV["OpenCV Video Processing"]
    end

    Presentation --> Visualization
    Presentation --> Communication
    Communication --> Server
    Future -.-> Server

    style Presentation fill:#0f172a,stroke:#06b6d4,color:#e2e8f0,stroke-width:2px
    style Visualization fill:#0f172a,stroke:#a855f7,color:#e2e8f0,stroke-width:2px
    style Communication fill:#0f172a,stroke:#10b981,color:#e2e8f0,stroke-width:2px
    style Server fill:#0f172a,stroke:#f59e0b,color:#e2e8f0,stroke-width:2px
    style Future fill:#0f172a,stroke:#ef4444,color:#e2e8f0,stroke-width:2px,stroke-dasharray: 5 5
Loading

๐Ÿค– AI/ML Components (Planned)

Component Technology Purpose
Vehicle Detection YOLOv8 + OpenCV Real-time vehicle counting and classification from camera feeds
Signal Optimization Deep Reinforcement Learning (DQN) Adaptive signal timing that learns from traffic patterns
Traffic Prediction LSTM Neural Network 6-hour congestion forecasting for proactive management
Accident Detection CNN-based Anomaly Detection Automatic accident identification from camera feeds
Density Analysis Grid-based Heatmap Algorithm Lane-wise traffic density calculation

๐ŸŽจ Design Philosophy

The UI is designed as a professional traffic command center with:

Aspect Implementation
๐ŸŒ‘ Dark Theme Navy/slate background (#0a0e1a) reducing eye strain for 24/7 operators
โœจ Neon Accents Cyan, green, amber, red โ€” following traffic signal conventions
๐Ÿ”ฎ Glassmorphism Frosted glass panels with backdrop-blur for depth
๐Ÿ’ก Glowing Indicators CSS box-shadow glow effects on traffic signals and status dots
๐ŸŽฌ Micro-animations Framer Motion transitions, pulsing dots, scanner lines
๐Ÿ“ฑ Responsive Adapts from 1080p monitors to ultrawide command center displays

| Archit Mittal | Full-Stack Developer |

๐Ÿ“„ License

This project is licensed under the MIT License โ€” see the LICENSE file for details.


Built with โค๏ธ for India Innovates

๐Ÿšฆ Making India's roads smarter, safer, and more efficient

Releases

No releases published

Packages

 
 
 

Contributors