This project demonstrates vehicle counting and speed estimation using YOLO (You Only Look Once) for object detection and SORT (Simple Online and Realtime Tracking) for tracking the detected vehicles in a video stream. The aim is to automate the process of monitoring traffic flow, which is essential for effective traffic management and planning.
Traffic congestion is a common issue in urban areas, leading to increased travel time, fuel consumption, and pollution. Manual traffic monitoring is labor-intensive and prone to errors. An automated system that can accurately count vehicles and estimate their speeds in real-time can significantly enhance traffic management and reduce congestion.
This project leverages state-of-the-art computer vision techniques to detect and track vehicles in real-time. The combination of YOLO for object detection and SORT for object tracking provides a robust solution for vehicle counting and speed estimation.
YOLO is an object detection algorithm known for its speed and accuracy. It divides the input image into a grid and predicts bounding boxes and class probabilities for each grid cell. YOLO is capable of detecting multiple objects in a single frame, making it suitable for real-time applications.
SORT is an efficient tracking algorithm that associates detections from YOLO across frames to maintain object identities. It uses a combination of the Kalman filter for predicting object positions and the Hungarian algorithm for data association. SORT is known for its simplicity and high performance in real-time tracking scenarios.
In addition to counting vehicles, the system estimates their speeds by analyzing the displacement of the bounding boxes between frames. This is crucial for traffic management, as it helps in understanding traffic flow dynamics and identifying potential speeding violations.
- Traffic Monitoring: Automated counting and speed estimation of vehicles can help traffic authorities monitor congestion in real-time and take necessary actions.
- Urban Planning: Data collected from this system can be used for urban planning, such as optimizing traffic light timings, planning new roads, and managing traffic during events.
- Law Enforcement: Speed estimation can assist in identifying and penalizing speeding vehicles, improving road safety.
Here is the link for breakdown and casestudy for this project Vehicle Counting Blog
- Installation
- Usage
- References
Before you begin, ensure you have met the following requirements:
- Python 3.7+
- pip package manager
- Clone the repository:
git clone https://github.com/shahrul-amin/vehicle-counting.git cd vehicle-counting
- Create and activate a virtual environment.
- Install the required packages.
- Download the YOLOv3 weights and configuration files by running:
bash download_weights
- Place the input video file in the
input
directory. Ensure the file is namedinput_video.mp4
or update the script with the correct file name. - Run the vehicle counting script using
python vehicle_counting.py
. - The output video with the counted vehicles and estimated speeds will be saved in the
output
directory asoutput_video.avi
.
The main script performs the following steps:
- Imports necessary packages.
- Initializes the SORT tracker and necessary variables.
- Loads the YOLO model and initializes the video stream.
- Processes each frame: reads frames, performs object detection using YOLO, tracks objects using SORT, counts vehicles crossing a predefined line, and estimates their speeds.
- Saves the processed frames to an output video file.