An intelligent traffic signal control system simulation that dynamically optimizes traffic light timing based on real-time vehicle detection using computer vision and adaptive algorithms.
- Real-time Vehicle Detection: YOLOv7-based vehicle detection and classification
- Adaptive Signal Timing: Dynamic traffic light control based on vehicle density
- Multi-directional Traffic Flow: Simulates 4-way intersection (Up, Down, Left, Right)
- Vehicle Type Classification: Detects and differentiates between cars, bikes, buses, trucks, and rickshaws
- Visual Simulation: Interactive Pygame-based traffic simulation with realistic graphics
- Performance Analytics: Tracks wait times, queue lengths, and signal efficiency
smart-traffic-sim/
βββ simulation.py # Main simulation engine
βββ vehicle_detection.py # YOLO-based vehicle detection module
βββ signal_time.py # Adaptive signal timing logic
βββ yolov7.cfg # YOLO model configuration
βββ coco.names # Class labels for object detection
βββ Simulation Video.mp4 # Demo video
β
βββ images/
β βββ intersection.jpg # Background intersection image
β βββ mod_int.png # Modified intersection layout
β β
β βββ signals/ # Traffic light images
β β βββ red.png
β β βββ yellow.png
β β βββ green.png
β β
β βββ up/ # Vehicles moving up
β βββ down/ # Vehicles moving down
β βββ left/ # Vehicles moving left
β βββ right/ # Vehicles moving right
β βββ bike.png
β βββ bus.png
β βββ car.png
β βββ rickshaw.png
β βββ truck.png
β
βββ output_images/ # Processed detection outputs
βββ test_images/ # Sample images for testing
- Python 3.7 or higher
- pip package manager
- Webcam (optional, for real-time detection)
pip install pygame
pip install opencv-python
pip install numpy
pip install pillowpip install torch torchvision
# OR
pip install tensorflow- Clone the repository:
git clone https://github.com/yourusername/smart-traffic-sim.git
cd smart-traffic-sim- Install dependencies:
pip install -r requirements.txt- Download YOLO weights (if not included):
# Download YOLOv7 weights
wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.ptpython simulation.pypython vehicle_detection.py --image test_images/sample.jpgpython simulation.py --spawn-rate 5 --simulation-time 300- Uses YOLOv7 model trained on COCO dataset
- Detects and classifies vehicles in each direction
- Counts vehicles at each traffic signal
- Calculates vehicle density for each lane
- Allocates green signal duration based on:
- Number of vehicles waiting
- Vehicle types (different weights for cars, trucks, buses)
- Minimum and maximum time constraints
- Pygame renders the intersection and vehicles
- Vehicles spawn at configurable rates
- Traffic lights change based on detection algorithm
- Visual feedback shows queue lengths and signal states
# Simplified signal timing logic
def calculate_signal_time(vehicle_count, vehicle_types):
base_time = 10 # seconds
# Weight different vehicle types
weights = {
'car': 1,
'bike': 0.5,
'bus': 2,
'truck': 2,
'rickshaw': 0.7
}
weighted_count = sum(weights[vtype] for vtype in vehicle_types)
signal_time = base_time + (weighted_count * 2)
# Apply constraints
return min(max(signal_time, MIN_TIME), MAX_TIME)Edit parameters in simulation.py:
# Simulation Settings
DEFAULT_SPAWN_RATE = 3 # Vehicles per direction per cycle
SIMULATION_DURATION = 600 # Seconds
SCREEN_WIDTH = 1400
SCREEN_HEIGHT = 800
# Signal Timing
MIN_GREEN_TIME = 10 # Minimum green signal (seconds)
MAX_GREEN_TIME = 60 # Maximum green signal (seconds)
YELLOW_TIME = 3
ALL_RED_TIME = 2
# Vehicle Settings
VEHICLE_SPEEDS = {
'car': 2.0,
'bike': 1.5,
'bus': 1.8,
'truck': 1.5,
'rickshaw': 1.3
}- SPACE: Pause/Resume simulation
- R: Reset simulation
- +/-: Increase/Decrease vehicle spawn rate
- S: Save screenshot
- Q/ESC: Quit simulation
The system tracks and displays:
- Average Wait Time: Mean time vehicles wait at signals
- Queue Length: Number of vehicles waiting per direction
- Throughput: Vehicles passing through per minute
- Signal Efficiency: Ratio of green time utilization
- Congestion Index: Overall traffic congestion level
| Vehicle Type | Speed | Priority Weight | Image |
|---|---|---|---|
| Bike | Fast | 0.5 | ποΈ |
| Car | Medium | 1.0 | π |
| Rickshaw | Slow | 0.7 | πΊ |
| Bus | Medium | 2.0 | π |
| Truck | Slow | 2.0 | π |
- Model: YOLOv7
- Dataset: COCO (80 classes, filtered for vehicles)
- Accuracy: ~95% for vehicle detection
- Processing: Real-time at 30 FPS
car, motorcycle, bus, truck, bicycle
- Add vehicle image to
images/{direction}/folders - Update vehicle spawn logic in
simulation.py:
VEHICLE_TYPES = ['car', 'bike', 'bus', 'truck', 'rickshaw', 'your_vehicle']Replace images/intersection.jpg with your custom intersection image.
Edit vehicle_detection.py:
CONFIDENCE_THRESHOLD = 0.5 # Detection confidence
NMS_THRESHOLD = 0.4 # Non-max suppression=== Traffic Simulation Report ===
Simulation Time: 300 seconds
Total Vehicles Processed: 487
Direction Statistics:
- North: 125 vehicles, Avg Wait: 23.4s
- South: 118 vehicles, Avg Wait: 21.7s
- East: 122 vehicles, Avg Wait: 24.1s
- West: 122 vehicles, Avg Wait: 22.8s
Signal Performance:
- Green Time Utilization: 87.3%
- Average Cycle Time: 78.5s
- Congestion Index: Medium
Issue: Pygame window not opening
# Install SDL dependencies (Linux)
sudo apt-get install python3-pygameIssue: YOLO model not loading
# Verify model weights file exists
ls -lh yolov7.ptIssue: Low FPS
- Reduce screen resolution
- Disable real-time detection
- Use lighter YOLO model (YOLOv7-tiny)
- Emergency vehicle priority system
- Pedestrian crossing integration
- Multi-intersection coordination
- Machine learning-based prediction
- Real-world camera feed integration
- Mobile app for traffic monitoring
- Historical data analysis
- Weather condition adaptation
pygame>=2.0.0
opencv-python>=4.5.0
numpy>=1.19.0
Pillow>=8.0.0
torch>=1.9.0
torchvision>=0.10.0
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see LICENSE for details.
- YOLOv7 by WongKinYiu
- Pygame community
- Traffic engineering principles from ITE (Institute of Transportation Engineers)
Your Name - @yourgithub
If you use this project in your research, please cite:
@software{smart_traffic_sim,
author = {Your Name},
title = {Smart Traffic Signal Control System},
year = {2024},
url = {https://github.com/yourusername/smart-traffic-sim}
}