Skip to content

🚦 Next-generation AI Traffic Management System with real-time computer vision, reinforcement learning optimization, emergency vehicle detection, and immersive 3D visualization

Notifications You must be signed in to change notification settings

Ruchit-Gaurh/AI-Traffic-Management-System

Repository files navigation

🚦 AI Traffic Management System

Traffic AI Computer Vision Reinforcement Learning SUMO 3D Visualization

Next-generation traffic optimization using AI, computer vision, and immersive 3D visualization

🚀 Quick Start✨ Features🎮 3D Visualization📖 Documentation🤝 Contributing


🌟 Overview

Main Menu

This cutting-edge AI Traffic Management System revolutionizes urban traffic control by combining advanced AI with stunning 3D visualization:

  • 🤖 Reinforcement Learning (PPO) for intelligent traffic optimization
  • 👁️ Computer Vision (YOLOv8) for real-time multi-camera vehicle detection
  • 🚨 Emergency Vehicle Detection with automatic priority routing
  • 🎮 Multiple 3D Visualization Options including Unity integration
  • 🌐 SUMO Integration for realistic traffic simulation
  • 📊 Real-time Web Dashboard with live analytics and controls
  • 🧪 Comprehensive Testing Suite with simulated data modes

✨ Features

🎯 Core AI Capabilities

  • Intelligent Traffic Optimization: PPO-based reinforcement learning for adaptive signal control
  • Multi-Camera Vehicle Detection: Simultaneous processing of multiple intersection cameras
  • Emergency Vehicle Priority: Real-time detection with automatic signal preemption
  • Predictive Analytics: AI-driven traffic pattern analysis and optimization
  • Zone-Based Detection: Configurable polygon zones for precise vehicle counting

🎮 3D Visualization Suite

  • Integrated 3D System: Complete AI + 3D visualization in one interface
  • Unity Integration: Professional game engine support with WebSocket API
  • Web-Based 3D: Browser-compatible Three.js visualization with interactive controls
  • Real-time Rendering: Live vehicle movement, traffic lights, and AI decisions
  • Multiple Camera Angles: Interactive 3D scene exploration

🌐 Web Interface & APIs

  • Real-time Dashboard: Live traffic statistics with animated visualizations
  • RESTful API: Complete API for external integrations
  • WebSocket Support: Real-time data streaming for live applications
  • Mobile Responsive: Works seamlessly on desktop, tablet, and mobile
  • Performance Monitoring: Live system health and performance metrics

🔧 Advanced Technical Stack

  • AI Framework: Stable Baselines3 (PPO) + Gymnasium
  • Computer Vision: Ultralytics YOLOv8 + OpenCV
  • 3D Graphics: Three.js + WebGL + Unity integration
  • Simulation: SUMO (Simulation of Urban Mobility)
  • Web Backend: Flask + Socket.IO + RESTful APIs
  • Analytics: Matplotlib + Seaborn + Real-time charting
  • Testing: Comprehensive test suite with simulated data modes

🚀 Quick Start

Prerequisites

# Python 3.8+ required
python --version

# Install system dependencies
# Ubuntu/Debian:
sudo apt-get install sumo sumo-tools sumo-doc

# macOS:
brew install sumo

# Windows: Download from https://sumo.dlr.de/docs/Installing/

Installation

# Clone the repository
git clone https://github.com/Ruchit-Gaurh/ai-traffic-management-system.git
cd ai-traffic-management

# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

🎯 One-Command Launch

# Interactive launcher with all options
python launch.py

# Or launch specific components directly:
python launch.py --component dashboard    # Web dashboard
python launch.py --component live        # Live traffic analysis  
python launch.py --component 3d          # 3D visualization
python launch.py --component test        # Test mode (no video needed)

🎮 3D Visualization

Option 1: Integrated 3D System (Recommended)

python launch.py --component 3d
# Open: http://localhost:5004

Features: Complete AI + 3D visualization, interactive controls, real-time metrics

Option 2: Unity Integration

python launch.py --component unity
# API: http://localhost:5002/api/3d_data
# WebSocket: ws://localhost:5002

Features: Professional Unity integration, VR/AR ready, custom game environments

Option 3: Advanced Web 3D

python launch.py --component web3d
# Open: http://localhost:5003

Features: Advanced Three.js visualization, lighting effects, interactive camera

🧪 Testing & Development

Quick Test (No Video Required)

# Terminal 1: Start dashboard
python launch.py --component dashboard

# Terminal 2: Run with simulated data
python launch.py --component test

# Open: http://localhost:5001

Full System Test

# Terminal 1: Dashboard
python launch.py --component dashboard

# Terminal 2: Live analysis (requires video files)
python launch.py --component live

# Terminal 3: 3D visualization
python launch.py --component 3d

🎯 System Demonstrations

🎬 Live Multi-Camera Traffic Analysis

Traffic Analysis

  • Dual camera processing with zone-based vehicle detection
  • AI decision visualization showing KEEP/SWITCH recommendations
  • Real-time queue monitoring across 4 detection zones
  • Professional overlay with clean metrics display

🎮 Interactive 3D Visualization

  • Immersive 3D intersection with realistic vehicle movement
  • Interactive camera controls (rotate, zoom, pan)
  • Real-time traffic lights synchronized with AI decisions
  • Multiple visualization modes (Unity, Web3D, Integrated)

🚨 Emergency Vehicle Detection & Priority

Emergency Detection

  • YOLOv8-based detection of ambulances, fire trucks, police vehicles
  • Automatic signal preemption for emergency vehicle priority
  • Visual and audio alerts with customizable notification system
  • Response time tracking and performance analytics

📊 Real-time Web Dashboard

Dashboard

  • Live traffic statistics with animated charts and graphs
  • System health monitoring with real-time performance metrics
  • Interactive controls for system configuration and testing
  • Mobile-responsive design for monitoring on any device

📁 Project Architecture

ai-traffic-management/
├── 🚀 launch.py                   # Universal system launcher
├── 📋 requirements.txt            # Python dependencies
├── ⚙️ config.json                # System configuration
├── 🤖 project/
│   ├── 📊 models/                 # Pre-trained AI models
│   │   ├── ppo_traffic_model_v*.zip    # Reinforcement learning models
│   │   └── yolo_emergency_detector.pt  # Emergency vehicle detector
│   ├── 🎬 videos/                 # Input video files for analysis
│   ├── 📈 results/                # Performance analytics & data
│   │   ├── plot_results.py        # Advanced visualization suite
│   │   ├── baseline_results.csv   # Baseline performance data
│   │   └── results_v*.csv         # AI performance comparisons
│   ├── 💻 src/
│   │   ├── 👁️ vision/            # Computer vision pipeline
│   │   │   ├── run_live.py        # Main multi-camera analysis
│   │   │   ├── processor.py       # Core vision processing
│   │   │   ├── emergency_vehicle_detection.py
│   │   │   └── new_run_live.py    # Enhanced live analysis
│   │   ├── 🌐 api/               # Web services & dashboard
│   │   │   └── app.py            # Flask API + WebSocket server
│   │   ├── 🎮 3D Systems/         # Multiple 3D visualization options
│   │   │   ├── simple_3d_system.py      # Fast 3D visualization
│   │   │   ├── integrated_3d_system.py  # Full AI + 3D integration
│   │   │   ├── unity_3d_integration.py  # Unity engine support
│   │   │   └── web_3d_visualization.py  # Advanced web 3D
│   │   └── 🧠 ai_core/           # AI training & optimization
│   ├── 🛣️ sumo_files/            # SUMO simulation configuration
│   └── 📊 datasets/              # Training datasets
├── 📊 tensorboard_logs/           # AI training logs & metrics
├── 🧪 test_*.py                  # Testing & validation scripts
└── 📖 Documentation/
    ├── QUICK_START.md            # Getting started guide
    ├── 3D_SYSTEM_GUIDE.md       # 3D visualization guide
    └── IMPROVEMENTS_SUMMARY.md   # Latest updates & fixes

🔄 Data Flow Architecture

Video Feeds → Computer Vision → AI Decision Engine → SUMO Simulation
     ↓              ↓                    ↓              ↓
Zone Detection → Vehicle Counting → Traffic Optimization → 3D Visualization
     ↓              ↓                    ↓              ↓
Emergency Alert → Priority Routing → Signal Control → Web Dashboard

🎮 Comprehensive Usage Guide

🎯 Multi-Component System Operation

Recommended Workflow

# 1. Start the web dashboard (always first)
python launch.py --component dashboard

# 2. Choose your analysis mode:
# Option A: Live video analysis (requires video files)
python launch.py --component live

# Option B: Test mode (simulated data, no video needed)  
python launch.py --component test

# 3. Add 3D visualization (optional)
python launch.py --component 3d

# 4. Open browser: http://localhost:5001

🎬 Live Traffic Analysis Controls

  • 's' key: Save current performance data and screenshots
  • 'q' key: Quit gracefully with data export
  • 'r' key: Reset analytics counters
  • Mouse: Click to focus on specific detection zones
  • ESC: Emergency stop (immediate shutdown)

🚨 Emergency Vehicle Detection

  • Automatic Detection: System continuously monitors for emergency vehicles
  • Visual Alerts: Red overlay and alert messages when detected
  • Audio Notifications: Optional sound alerts (configurable)
  • Priority Routing: Automatic traffic signal preemption
  • Response Logging: All emergency events logged for analysis

📊 Web Dashboard Features

  • Real-time Monitoring: Live traffic statistics and AI decisions
  • Interactive Charts: Click and drag to explore historical data
  • System Controls: Start/stop components remotely
  • Performance Metrics: Live system health and optimization stats
  • API Access: RESTful endpoints for external integrations
    • GET /api/stats - Current system statistics
    • POST /api/update_traffic - Update traffic data
    • GET /api/3d_data - 3D visualization data

🎮 3D Visualization Controls

  • Mouse Drag: Rotate camera around intersection
  • Mouse Wheel: Zoom in/out
  • 'R' key: Reset camera to default position
  • 'P' key: Pause/resume simulation
  • 'W' key: Toggle wireframe mode
  • 'F' key: Toggle fullscreen (web versions)

🔧 Configuration & Customization

📹 Video Source Configuration

Edit config.json to customize your video sources:

{
  "video_sources": {
    "intersection_1": {
      "path": "project/videos/intersection1.mp4",
      "description": "North-West intersection camera",
      "detection_zones": [
        [[874, 1086], [1443, 1035], [793, 581], [572, 590]],
        [[1947, 920], [2129, 1007], [2778, 935], [2709, 856]]
      ]
    }
  }
}

🎯 Detection Zone Setup

Getting Zone Coordinates:

  1. Take a screenshot of your video's first frame
  2. Open in image editor (Preview, Paint, GIMP)
  3. Note pixel coordinates at zone corners
  4. Define polygons clockwise from top-left
# Example: 4-point polygon for a traffic lane
ZONE_POLYGON = np.array([
    [x1, y1],  # Top-left corner
    [x2, y2],  # Top-right corner  
    [x3, y3],  # Bottom-right corner
    [x4, y4]   # Bottom-left corner
], np.int32)

🤖 AI Model Configuration

{
  "models": {
    "traffic_ai": {
      "path": "project/models/ppo_traffic_model_v2.zip",
      "type": "PPO"
    },
    "emergency_detector": {
      "path": "project/models/yolo_emergency_detector.pt", 
      "type": "YOLOv8"
    }
  },
  "detection_settings": {
    "confidence_threshold": 0.5,
    "decision_interval_seconds": 5,
    "vehicle_classes": [2, 3, 5, 7],
    "emergency_classes": ["ambulance", "fire brigade", "police"]
  }
}

🌐 Web Interface Settings

{
  "web_interface": {
    "host": "0.0.0.0",
    "port": 5001,
    "debug": false,
    "cors_enabled": true,
    "auto_refresh_interval": 1000
  }
}

🎮 3D Visualization Options

{
  "display": {
    "window_size": {"width": 1200, "height": 800},
    "colors": {
      "emergency": [0, 0, 255],
      "normal": [0, 255, 0],
      "zone_highlight": [0, 255, 255]
    }
  }
}

📊 Performance Analytics & Metrics

🎯 Key Performance Indicators

  • Traffic Flow Optimization: Up to 35% reduction in average wait times
  • Detection Accuracy: 95%+ vehicle detection accuracy with YOLOv8
  • Emergency Response: Sub-3-second emergency vehicle detection and signal preemption
  • System Efficiency: Real-time processing of dual 1080p video streams
  • AI Decision Quality: Reinforcement learning optimization with continuous improvement

📈 Analytics Dashboard Features

  • Real-time Charts: Live traffic flow, queue lengths, and AI decisions
  • Historical Analysis: Performance trends over time with exportable data
  • Comparative Studies: Baseline vs AI-optimized performance metrics
  • Emergency Events: Detailed logging and analysis of emergency vehicle incidents
  • System Health: CPU, memory, and processing performance monitoring

🔍 Data Export & Analysis

# Generate comprehensive performance report
python project/results/plot_results.py

# Export data for external analysis
# Results saved to: project/results/
# - baseline_results.csv (traditional timing)
# - results_v3.csv (AI-optimized performance)
# - Performance comparison charts (PNG/PDF)

📊 Metrics Visualization

The system generates detailed visualizations including:

  • Traffic Flow Heatmaps: Visual representation of congestion patterns
  • AI Decision Timeline: Chronological view of traffic light decisions
  • Queue Length Trends: Real-time and historical queue analysis
  • Emergency Response Analytics: Response time distribution and patterns
  • System Performance Graphs: Processing speed and resource utilization

🛠️ Development & Extension

🔧 System Architecture for Developers

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Video Input   │───▶│  Computer Vision │───▶│   AI Decision   │
│  Multi-Camera   │    │   YOLOv8 + CV   │    │  Engine (PPO)   │
└─────────────────┘    └─────────────────┘    └─────────────────┘
         │                       │                       │
         ▼                       ▼                       ▼
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│ 3D Visualization│    │  Web Dashboard  │    │ SUMO Simulation │
│ Unity/Three.js  │    │ Flask + Socket  │    │ Traffic Control │
└─────────────────┘    └─────────────────┘    └─────────────────┘

🚀 Adding New Features

Computer Vision Extensions

# Extend processor.py for new detection types
class VisionProcessor:
    def detect_pedestrians(self, frame):
        # Add pedestrian detection
        pass
    
    def detect_cyclists(self, frame):
        # Add bicycle detection  
        pass

AI Model Development

# Train custom traffic optimization models
cd project/src/ai_core
python train_traffic_agent.py --config custom_config.json

# Train specialized emergency vehicle detector
python train_emergency_detector.py --dataset custom_emergency_data

3D Visualization Customization

// Extend 3D scenes with new elements
function addCustomBuildings(scene) {
    // Add custom 3D buildings
}

function addWeatherEffects(scene) {
    // Add rain, fog, or other weather
}

Web Dashboard Enhancement

# Add new API endpoints in api/app.py
@app.route('/api/custom_analytics')
def custom_analytics():
    return jsonify({"custom_data": get_custom_metrics()})

🧪 Testing Framework

# Run comprehensive test suite
python -m pytest tests/

# Test individual components
python test_model_loading.py      # AI model validation
python test_dashboard_connection.py  # Web interface testing

# Performance benchmarking
python benchmark_system.py --duration 300  # 5-minute benchmark

📦 Custom Model Training

Traffic Optimization Model

# Custom PPO training configuration
from stable_baselines3 import PPO
from gymnasium import make

env = make('sumo-rl-v0', net_file='custom_intersection.net.xml')
model = PPO('MlpPolicy', env, verbose=1)
model.learn(total_timesteps=100000)
model.save('custom_traffic_model')

Emergency Vehicle Detection

# Custom YOLOv8 training
from ultralytics import YOLO

model = YOLO('yolov8n.pt')
model.train(
    data='custom_emergency_dataset.yaml',
    epochs=100,
    imgsz=640,
    batch=16
)

🔌 API Integration Examples

# External system integration
import requests

# Get live traffic data
response = requests.get('http://localhost:5001/api/stats')
traffic_data = response.json()

# Send custom traffic updates
requests.post('http://localhost:5001/api/update_traffic', 
              json={'queues': [5, 3, 8, 2], 'action': 'SWITCH'})

# WebSocket real-time connection
import socketio
sio = socketio.Client()
sio.connect('http://localhost:5001')

@sio.on('traffic_update')
def on_traffic_update(data):
    print(f"Live traffic: {data}")

🤝 Contributing

We welcome contributions from developers, researchers, and traffic management professionals!

🎯 Areas for Contribution

  • AI Models: Improve traffic optimization algorithms
  • Computer Vision: Enhance detection accuracy and speed
  • 3D Visualization: Create new visualization modes and effects
  • Web Interface: Improve dashboard features and user experience
  • Documentation: Help improve guides and tutorials
  • Testing: Add test cases and performance benchmarks

🚀 Development Setup

# 1. Fork and clone the repository
git clone https://github.com/Ruchit-Gaurh/AI-Traffic-Management-System.git
cd ai-traffic-management

# 2. Create development environment
python -m venv dev-env
source dev-env/bin/activate  # Windows: dev-env\Scripts\activate

# 3. Install development dependencies
pip install -r requirements.txt
pip install pytest black flake8  # Development tools

# 4. Create feature branch
git checkout -b feature/amazing-new-feature

# 5. Make your changes and test
python -m pytest tests/
python launch.py --component test  # Verify system works

# 6. Commit and push
git add .
git commit -m "Add amazing new feature"
git push origin feature/amazing-new-feature

# 7. Create pull request on GitHub

📋 Contribution Guidelines

  • Code Style: Follow PEP 8 for Python, use black for formatting
  • Testing: Add tests for new features, ensure existing tests pass
  • Documentation: Update relevant documentation and docstrings
  • Performance: Ensure changes don't significantly impact system performance
  • Compatibility: Test with Python 3.8+ and major operating systems

🐛 Bug Reports & Feature Requests

  • Bug Reports: Use GitHub Issues with detailed reproduction steps
  • Feature Requests: Describe the use case and expected behavior
  • Performance Issues: Include system specs and performance metrics

🎓 Documentation

📚 Complete Guides

🎯 Use Cases & Applications

  • Smart City Traffic Management: Real-time optimization for urban intersections
  • Emergency Services: Priority routing for ambulances, fire trucks, police
  • Research & Education: Traffic engineering research and AI/ML education
  • Simulation & Planning: Urban planning and traffic impact analysis
  • Game Development: Realistic traffic simulation for games and VR/AR

� Liccense

This project is licensed under the MIT License - see the LICENSE file for details.

🔓 Open Source Benefits

  • Free for commercial use: Deploy in production environments
  • Modification allowed: Customize for your specific needs
  • Distribution permitted: Share and redistribute freely
  • No warranty: Use at your own risk, community support available

🙏 Acknowledgments

🏆 Core Technologies

  • SUMO: Simulation of Urban Mobility - traffic simulation platform
  • Ultralytics YOLOv8: State-of-the-art object detection
  • Stable Baselines3: Reinforcement learning algorithms
  • Three.js: 3D graphics library for web visualization
  • Flask: Web framework for dashboard and APIs

🌟 Research & Inspiration

  • OpenAI: Advancing AI research and reinforcement learning
  • Computer Vision Community: YOLO, OpenCV, and detection algorithm researchers
  • Traffic Engineering: ITS (Intelligent Transportation Systems) research community
  • Open Source Community: Contributors and maintainers of core dependencies

📞 Support & Community

🆘 Getting Help

  • 📖 Documentation: Check guides in this repository first
  • 🐛 Issues: GitHub Issues for bugs and feature requests
  • 💬 Discussions: GitHub Discussions for questions and ideas
  • 📧 Email: Create an issue for direct support needs

🌐 Community Resources

  • Example Projects: See examples/ directory for implementation samples
  • Video Tutorials: Coming soon - subscribe to releases for updates
  • Research Papers: Links to relevant academic research in docs/research.md
  • Conference Talks: Presentation materials and recordings

🚀 Professional Support

For enterprise deployments, custom development, or consulting:

  • Custom Training: AI model training for specific intersections
  • Integration Services: Connect with existing traffic management systems
  • Performance Optimization: High-throughput deployments and scaling
  • Technical Consulting: Traffic engineering and AI implementation guidance

🌟 Building Smarter Cities with AI 🌟

Real-time Traffic Optimization • Emergency Vehicle Priority • 3D Visualization • Open Source

⭐ Star this Repository🔄 Fork & Contribute📋 Report Issues💡 Request Features

Made with ❤️ by the AI Traffic Management Community