A YOLOv8-based computer vision system for counting cars in traffic using deep learning.
This project implements an automated car counting system for traffic analysis using the YOLOv8 object detection model. It can process both images and videos to detect and count vehicles in real-time.
NEW: Smart Traffic Light Controller - An intelligent traffic management system that dynamically adjusts green light durations based on real-time traffic density.
- Real-time car detection and counting
- Video processing with frame-by-frame analysis
- GPU-accelerated training and inference (CUDA 13.0)
- Comprehensive visualization of results
- Jupyter notebook with complete workflow
- 4-way intersection management (North, South, East, West)
- Dynamic timing calculation based on car count
- Priority-based sequencing (most congested direction first)
- Automatic green/yellow light control
- Real-time or video-based testing
- Python 3.10+
- NVIDIA GPU with CUDA 13.0 (recommended)
- Anaconda or Miniconda
- Windows OS
# Clone the repository
git clone https://github.com/Elsaraf1/car-counting-project.git
cd car-counting-project
# Create conda environment
conda create -n car_counting python=3.10 -y
conda activate car_counting
# Install PyTorch with CUDA support
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# Install other dependencies
pip install -r requirements.txt
# Create Jupyter kernel
python -m ipykernel install --user --name=car_counting --display-name="Python (car_counting)"This project uses the Car Counting Dataset from Roboflow.
To download the dataset:
- Create a free account at Roboflow
- Get your API key from Settings β API Keys
- Use the download function in the Jupyter notebook
conda activate car_counting
jupyter lab
# Open: notebooks/car_counting_main.ipynb# Place test videos in test_videos/ folder
# Then run:
python main.pyjupyter lab
# Open: notebooks/smart_traffic_light.ipynbfrom ultralytics import YOLO
# Load pretrained YOLOv8 model
model = YOLO('yolov8n.pt')
# Train on car counting dataset
model.train(
data='path/to/data.yaml',
epochs=50,
imgsz=640,
batch=16,
device=0 # Use GPU
)# Load trained model
model = YOLO('path/to/best.pt')
# Count cars in image
results = model('test_image.jpg')
car_count = len(results[0].boxes)
print(f"Cars detected: {car_count}")- Counts cars in all 4 directions using YOLO
- Calculates timing: Green time = Cars Γ 2 seconds
- Prioritizes: Most congested direction goes first
- Executes sequence: Green β Yellow β Next direction
Green Light Time = Number of Cars Γ 2 seconds
Yellow Light Time = max(Green Time Γ 1%, 3 seconds)
Priority = Most cars β Least cars
πΈ Counting cars in all directions...
North: 15 cars
South: 5 cars
East: 25 cars (MOST)
West: 8 cars
π Calculated Timings:
East: 50s green + 3s yellow = 53s total
North: 30s green + 3s yellow = 33s total
West: 16s green + 3s yellow = 19s total
South: 10s green + 3s yellow = 13s total
π¦ Priority Order: East β North β West β South
- Architecture: YOLOv8 Nano
- Training Time: ~2-3 hours (50 epochs on GPU)
- Inference Speed: 30-60 FPS with GPU
- Expected mAP@50: ~0.85-0.90
car-counting-project/
βββ notebooks/
β βββ car_counting_main.ipynb # Car counting training & inference
β βββ smart_traffic_light.ipynb # Traffic light system (development)
βββ smart_traffic/ # Traffic light module
β βββ __init__.py
β βββ config.py # Configuration settings
β βββ car_counter.py # Car detection & counting
β βββ traffic_calculator.py # Timing calculations
β βββ light_controller.py # Main controller
βββ test_videos/ # Test videos for 4 directions
βββ data/ # Dataset (not tracked in git)
βββ models/ # Trained models (not tracked)
βββ results/ # Output results
βββ main.py # Production script for traffic light
βββ requirements.txt # Python dependencies
βββ .gitignore # Git ignore rules
βββ README.md # This file
- β 4-way intersection support
- β Dynamic timing based on traffic density
- β Priority-based sequencing
- β Configurable parameters
- β Video and camera mode support
- Emergency vehicle priority override
- Pedestrian crossing detection
- Weather-based timing adjustments
- Historical traffic pattern learning
- Web-based dashboard
- Hardware integration (Arduino, Raspberry Pi)
- Traffic flow monitoring
- Parking lot occupancy detection
- Highway congestion analysis
- Transportation research
- Intelligent intersection management
- Reducing traffic congestion
- Emergency vehicle routing
- Smart city infrastructure
- Traffic optimization research
Edit smart_traffic/config.py to customize:
TIME_PER_CAR = 2 # Seconds each car needs
YELLOW_RATIO = 0.01 # Yellow time = 1% of green
MIN_YELLOW_TIME = 3 # Minimum yellow (safety)
MAX_GREEN_TIME = 120 # Maximum green time
CONFIDENCE_THRESHOLD = 0.5 # YOLO detection confidencepython -c "import torch; print(torch.cuda.is_available())"Reduce batch size:
model.train(data='data.yaml', batch=8) # or batch=4Place videos in test_videos/ folder:
- north.mp4
- south.mp4
- east.mp4
- west.mp4
Contributions, issues, and feature requests are welcome!
This project is open source and available under the MIT License.
- Ultralytics YOLOv8 - Object detection framework
- Roboflow - Dataset hosting and management
- Computer Vision community for inspiration and support
GitHub: @Elsaraf1
Project Link: https://github.com/Elsaraf1/car-counting-project
β If you find this project helpful, please consider giving it a star!
π¦ NEW: Check out the Smart Traffic Light Controller for intelligent intersection management!