This project is a Traffic Light Management System that uses a YOLO-based object detection model and OpenCV to monitor traffic density and dynamically control traffic lights. The system analyzes real-time camera input to detect vehicles and adjust the traffic signals accordingly, improving traffic efficiency.
- Uses YOLO (You Only Look Once) for real-time vehicle detection.
- Implements OpenCV for video processing and overlaying traffic light visuals.
- Supports multi-threading for smooth processing.
- Detects traffic congestion levels and changes traffic lights dynamically.
- Displays real-time vehicle count and traffic status messages.
- Logs system events for debugging and monitoring.
- Python
- OpenCV (for image processing and video capture)
- YOLO (Ultralytics) (for object detection)
- Threading & ThreadPoolExecutor (for parallel processing)
- Logging & JSON Configuration
- Python 3.7+
- OpenCV (
opencv-python
) - Ultralytics YOLO (
ultralytics
) - NumPy
- A webcam (or a pre-recorded video feed for testing)
-
Clone the repository
git clone https://github.com/Burhanali2211/Traffic-Light-Management-System.git cd traffic-light-system
-
Install dependencies
pip install -r requirements.txt
-
Download YOLO model Download a YOLO model (e.g.,
yolov8n.pt
) from Ultralytics YOLO and place it in the project directory. -
Modify
config.json
Configure settings like camera index, model path, and traffic thresholds.
{
"model_path": "yolov8n.pt",
"camera_index": 0,
"vehicle_classes": [2, 3, 5, 7],
"high_traffic_threshold": 10,
"medium_traffic_threshold": 5,
"min_state_duration": 5
}
- vehicle_classes: Object classes representing vehicles (YOLO classes: car=2, motorbike=3, bus=5, truck=7)
- high_traffic_threshold: Number of vehicles required to trigger red light
- medium_traffic_threshold: Number of vehicles required to trigger yellow light
- min_state_duration: Minimum duration before switching traffic lights
Run the script using:
python app.py
- Press
q
to exit the program.
- Captures video frames from the camera.
- Processes frames using YOLO to detect vehicles.
- Counts detected vehicles and determines traffic congestion level.
- Changes the traffic light state based on congestion.
- Overlays traffic light visuals and vehicle count on the video feed.
- Displays the live feed with updated traffic conditions.
Ensure the camera is connected and configured properly in config.json
. When running, the system will:
- Show the live feed with a traffic light overlay.
- Update traffic lights dynamically based on vehicle count.
- Log status messages for debugging.
Logs are saved in traffic_system.log
for tracking events and debugging issues.
- Integrate with Raspberry Pi for real-world deployment.
- Use multiple cameras for large-scale traffic management.
- Enhance YOLO model training with a custom dataset for better accuracy.
- Implement automated reports based on traffic patterns.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is open-source and available under the MIT License.