Skip to content

saidalbardawil/Smart-Traffic-light

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Smart Traffic Light System Using Computer Vision

A lightweight, cost-effective adaptive traffic light control system that uses computer vision and IoT technologies to optimize traffic flow in real-time.

System Architecture Platform Language License

๐Ÿš€ Project Overview

This project presents an intelligent traffic light system that leverages computer vision and cloud computing to dynamically adjust signal timings based on real-time vehicle density. Unlike traditional fixed-time systems, our solution adapts instantly to traffic conditions, reducing congestion and improving urban mobility.

Key Features

  • Real-time Traffic Monitoring: Uses OpenCV for lightweight vehicle density estimation
  • Cloud Integration: Firebase Realtime Database for seamless data synchronization
  • IoT Control: ESP32 microcontroller for adaptive traffic light management
  • Cost-Effective: Runs on low-cost hardware (Raspberry Pi + ESP32)
  • High Performance: Achieves 15-20 FPS on Raspberry Pi, 100 FPS on laptop
  • Robust Operation: Handles network failures with automatic fallback mode

๐Ÿ—ƒ๏ธ System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   IP Camera     โ”‚โ”€โ”€โ”€โ–ถโ”‚  Processing Unit โ”‚โ”€โ”€โ”€โ–ถโ”‚    Firebase     โ”‚
โ”‚  (Video Input)  โ”‚    โ”‚ (Raspberry Pi/   โ”‚    โ”‚  (Cloud DB)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚    Laptop)       โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚ - OpenCV Vision  โ”‚              โ”‚
                       โ”‚ - Density Calc   โ”‚              โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ”‚
                                                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Traffic Lights  โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  ESP32 Control   โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  Data Retrieval โ”‚
โ”‚   (LED/Real)    โ”‚    โ”‚ - Decision Logic โ”‚    โ”‚                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚ - State Machine  โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Hardware Components

Core Components

Component Model Function
Processing Unit Raspberry Pi 4B (4GB) Computer vision processing
Microcontroller ESP32 Traffic light control
Camera USB/IP Camera (720p-1080p) Video capture
LEDs 5mm RGB LEDs Traffic light simulation
Mounting Adjustable Camera Arm Stable positioning

Bill of Materials

  • Processing: Raspberry Pi 4B + SD Card + Power Supply
  • Control: ESP32 Development Board + Breadboard + Jumper Wires
  • Sensors: USB Webcam or IP Camera
  • Output: LEDs (Red, Yellow, Green) + 220ฮฉ Resistors
  • Physical: Intersection Base + Traffic Poles + Model Vehicles

Total Cost per Intersection: < $100

๐Ÿ’ป Software Stack

Computer Vision Pipeline (Python + OpenCV)

  • Image Preprocessing: Grayscale conversion, Gaussian blur
  • Segmentation: Binary thresholding, morphological operations
  • Density Estimation: ROI-based pixel counting
  • Performance: 15-20 FPS on Raspberry Pi (vs. 2 FPS for YOLO)

Cloud Integration (Firebase)

  • Real-time Database: Instant synchronization across devices
  • Scalability: Support for multiple intersections
  • Reliability: Automatic reconnection and error handling

Microcontroller Logic (ESP32 + Arduino IDE)

  • Decision Algorithm: Prioritizes lanes with highest traffic density
  • State Machine: Safe transitions (Green โ†’ Yellow โ†’ Red)
  • Failsafe Mode: Fixed-time cycle during network outages

๐Ÿšฆ How It Works

  1. Video Capture: Camera monitors intersection from overhead position
  2. Image Processing: OpenCV pipeline estimates vehicle density per lane
  3. Cloud Sync: Density values uploaded to Firebase in real-time
  4. Decision Making: ESP32 identifies busiest lane and assigns green priority
  5. Signal Control: Traffic lights adapt based on actual traffic conditions

Processing Pipeline

Raw Frame โ†’ Grayscale โ†’ Blur โ†’ Threshold โ†’ Morphology โ†’ ROI Analysis โ†’ Firebase โ†’ ESP32 โ†’ LEDs

๐Ÿ“Š Performance Results

Frame Rate Benchmarks

Hardware Without Firebase With Firebase YOLO Comparison
Laptop (i7, 16GB) ~100 FPS ~40 FPS ~5 FPS
Raspberry Pi 4B ~20 FPS ~15 FPS ~2 FPS

System Latency

  • End-to-end latency: 150-300ms (image capture โ†’ signal change)
  • Processing time: 10-30ms per frame
  • Firebase sync: 50-150ms
  • ESP32 response: <70ms

๐Ÿ”ง Installation & Setup

1. Clone Repository

git clone https://github.com/saidalbardawil/Smart-Traffic-light.git
cd Smart-Traffic-light

2. Install Python Dependencies

pip install opencv-python
pip install firebase-admin
pip install numpy
pip install cvzone

3. Firebase Configuration

  1. Create Firebase project at console.firebase.google.com
  2. Download service account key JSON
  3. Update Firebase credentials in Python script
  4. Set database rules for read/write access

4. ESP32 Setup

  1. Install Arduino IDE and ESP32 board support
  2. Install required libraries:
    • WiFi
    • FirebaseESP32
  3. Update WiFi credentials and Firebase config
  4. Upload code to ESP32

5. Hardware Connections

ESP32 GPIO Connections:
Lane 0: Green(15), Yellow(2), Red(4)
Lane 1: Green(12), Yellow(14), Red(27)  
Lane 2: Green(25), Yellow(32), Red(33)
Lane 3: Green(5), Yellow(18), Red(19)

๐ŸŽฏ Usage

Running the System

  1. Start Processing Unit:

    python traffic_vision.py
  2. Power ESP32: Upload firmware and connect to power

  3. Position Camera: Mount overhead for clear intersection view

  4. Monitor Operation: Check Firebase console for real-time data

Testing with 3D Model

For development/testing without real traffic:

  1. Use provided prototype models for simulation
  2. Generate traffic simulation video
  3. Feed video to processing pipeline

๐Ÿ“ˆ Evaluation Results

Traffic Scenarios Tested

  • โœ… Heavy Traffic: System correctly prioritizes congested lanes
  • โœ… Balanced Traffic: Fair random selection when densities are equal
  • โœ… Pedestrian Interference: Successfully ignores non-vehicle objects
  • โœ… Network Failure: Automatic fallback to fixed-time operation
  • โœ… Dynamic Changes: Real-time adaptation to changing conditions

Performance Advantages

  • 10x faster than YOLO on Raspberry Pi
  • Real-time responsiveness on low-cost hardware
  • No GPU required unlike deep learning approaches
  • Scalable architecture for city-wide deployment

๐ŸŒŸ Key Innovations

  1. Lightweight Computer Vision: Pixel-density method instead of complex object detection
  2. Implicit Vehicle Weighting: Larger vehicles (buses/trucks) naturally get higher priority
  3. Cloud-IoT Integration: Real-time synchronization without expensive infrastructure
  4. Fail-Safe Operation: Automatic fallback ensures continuous operation
  5. Cost-Effective Deployment: Sub-$100 per intersection vs. $1000s for traditional systems

๐Ÿ”ฎ Future Enhancements

  • Multi-Intersection Coordination: Network-wide traffic optimization
  • Advanced Camera Features: Night vision, weather resistance
  • V2I Communication: Integration with connected vehicles
  • Predictive Analytics: ML-based traffic pattern analysis
  • Emergency Vehicle Priority: Automatic preemption for ambulances/fire trucks

๐Ÿ“š Technical Documentation

Project Structure

Smart-Traffic-light/
โ”œโ”€โ”€ Computer_vision_code/    # OpenCV traffic density analysis
โ”œโ”€โ”€ ESP32_controller_code/   # Microcontroller firmware
โ”œโ”€โ”€ docs/                   # Documentation and specifications
โ”œโ”€โ”€ Prototype/              # 3D models and simulation files
โ”œโ”€โ”€ .gitattributes         # Git configuration
โ”œโ”€โ”€ .gitignore            # Git ignore rules
โ”œโ”€โ”€ LICENSE               # License file
โ””โ”€โ”€ README.md            # Project documentation

Dependencies

  • Python: OpenCV, NumPy, Firebase Admin SDK, cvzone
  • Arduino: WiFi, FirebaseESP32, GPIO libraries
  • Hardware: Raspberry Pi OS, Arduino IDE

๐Ÿ† Project Information

Smart Traffic Light System - Mechatronics Engineering Project

Date: June 2025

๐Ÿ“„ License

๐Ÿ”น License

This project is shared for viewing and academic reference only. It is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0). You may read and cite this work, but you may not modify or use it commercially.

License: CC BY-NC-ND 4.0

What this means:

  • โœ… Attribution: You must give appropriate credit and cite this work
  • โœ… Academic Use: Free to reference in research and educational contexts
  • โŒ No Commercial Use: Cannot be used for commercial purposes
  • โŒ No Derivatives: Cannot modify, transform, or build upon this work

For commercial licensing or permission to modify, please contact the authors.

๐Ÿค Contributing

Note: This project is licensed under CC BY-NC-ND 4.0, which means modifications and derivatives are not permitted. However, you can:

  • โญ Star the repository to show support
  • ๐Ÿ“– Use it as a reference for your own implementations
  • ๐Ÿ“š Cite it in academic work and research
  • ๐Ÿ’ฌ Discuss ideas and improvements in Issues (for academic discussion)
  • ๐Ÿ“ง Contact authors for collaboration opportunities

Academic Citation

If you use this work in your research, please cite:

Smart Traffic Light System Using Computer Vision. (2025). 
Mechatronics Engineering Project.

๐Ÿ“ž Contact

For questions, suggestions, or collaboration opportunities:

  • Project Repository: GitHub
  • Documentation: See /docs folder for detailed technical specifications

โญ Star this repository if you found it helpful!

๐Ÿ”ฅ This project demonstrates that intelligent traffic management can be achieved with low-cost hardware and lightweight algorithms, making it ideal for developing urban environments with budget constraints.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published