This project simulates a 4-way traffic intersection powered by real-time vehicle detection using a pre-trained YOLO model.
Instead of using fixed signal timers, the system dynamically adjusts green and red light durations based on vehicle counts detected from real traffic video feeds.
The system integrates:
- Computer Vision (YOLO + OpenCV)
- Simulation Engine (Pygame)
- GUI-based File Selection (Tkinter)
- Multi-threaded Processing
- Custom Traffic Time Allocation Algorithm
Traditional traffic systems:
- Use fixed signal timings
- Cause unfair red-light wait times
- Waste green time when traffic is low
This project aims to:
- Detect vehicles from 4 real traffic videos
- Count vehicles per lane
- Simulate a 4-sided intersection
- Dynamically calculate signal timing
- Maintain fairness in red-light distribution
- Prevent unnecessary signal time waste
Libraries Used:
cv2(OpenCV)- YOLO (Pre-trained object detection model)
numpy
Each of the four traffic videos is processed using YOLO to:
- Detect vehicles (cars, buses, trucks, bikes)
- Draw bounding boxes
- Count vehicles per direction
The detected vehicles are not randomly generated. They are derived from actual detection results.
Libraries Used:
pygamerandomtime
A visual 4-way traffic junction is simulated using Pygame.
Features:
- Four traffic sides
- Virtual vehicles spawned based on detection count
- Real-time signal switching
- Dynamic timer display
The simulation visually represents traffic conditions detected from the videos.
Libraries Used:
tkinterfiledialog
Tkinter is used to:
- Select four traffic video files
- Allow flexible input instead of hardcoded paths
This makes the system interactive and user-driven.
Libraries Used:
threading
Threading ensures:
- Video processing runs in parallel
- Simulation remains responsive
- Detection and rendering occur simultaneously
This improves system efficiency and realism.
This is the core innovation of the project.
Unlike traditional traffic lights:
- Signals are not fixed duration
- Green time is not random
- Red time is not unfairly long
- Higher vehicle count → Longer green duration
- Lower vehicle count → Shorter green duration
- Red time balanced across all sides
- Continuous recalculation based on updated counts
- No direction is starved
This ensures:
- Fair signal distribution
- Efficient vehicle movement
- Reduced idle green time
- User selects 4 traffic videos via Tkinter
- YOLO detects and counts vehicles using OpenCV
- Vehicle counts are fed into simulation
- Time allocation algorithm calculates signal duration
- Pygame updates traffic lights dynamically
- Process repeats continuously
- Python
- OpenCV (
cv2) - YOLO (Pre-trained object detection model)
- Pygame
- Tkinter
- NumPy
- Threading
- Real-time object detection
- Vehicle counting using bounding boxes
- Multi-threaded system design
- Simulation modeling
- Fairness-aware algorithm design
- Dynamic resource allocation
- Integration of multiple libraries into one system
This project combines:
- Computer Vision
- Algorithm Design
- Simulation Systems
- Real-Time Processing
It demonstrates the ability to:
- Integrate ML models into working systems
- Translate detection output into intelligent decision logic
- Design fairness-based traffic optimization
- Build simulation-based prototypes for smart city systems
This is a simulation-based smart traffic system.
Although real traffic videos are used for vehicle detection, the intersection itself is simulated using Pygame.
The goal was to build a realistic prototype of a dynamic traffic control system.
- Live CCTV feed integration
- Emergency vehicle prioritization
- Reinforcement learning-based optimization
- Add metrics (average wait time, throughput efficiency)
- Deploy as a real-time dashboard
SK Salman
Machine Learning & Computer Vision Enthusiast