Vehicle Counting and Speed Estimation Using YOLO and SORT

Oct 23, 2023 | Data Science

In this guide, we’ll walk through the process of setting up a fascinating project that utilizes YOLOv3 (You Only Look Once) for vehicle detection and the SORT (Simple Online and Realtime Tracker) for tracking those vehicles in a video feed. This project includes several features such as vehicle counting, lane segmentation, lane change detection, and speed estimation, all documented in a CSV file.

Project Overview

This project is versatile and can be adapted for various applications in traffic analysis. The main tasks it implements include:

  • Vehicle Counting
  • Lane Segmentation
  • Lane Change Detection
  • Speed Estimation
  • Exporting Details to a CSV File

Getting Started

Let’s set up everything you need to get this project running smoothly:

1. Clone the Repository

First, you will need to download the code. You can do this using the following command:

git clone https://github.com/bamwanicar/counting-and-speed-estimation-yolo-sort-python

2. Install Required Dependencies

Navigate into your project directory and install the necessary dependencies:

cd car-counting-and-speed-estimation-yolo-sort-python
pip3 install -r requirements.txt

3. Download YOLO Weights

The next step is to download the YOLO weights file using:

bash download_weights

4. Configure Detection Parameters

Make sure to adjust the detection line and lane segmentation parameters based on your specific video requirements. Additionally, you can fine-tune the threshold and confidence levels for the YOLO model.

5. Run the Main Script

You can run the main script using the following command, substituting the paths for your input and output video files:

python3 main.py -input path_to_video_file.avi -output path_for_output_video_file.avi -yolo path_to_YOLO_directory

Speed Detection Method

This project tackles a common challenge in speed detection caused by camera shaking, which can result in unreliable pixel distance measurements. Instead of using traditional methods, we utilized a unique approach called Speed Between Two Lines (SBTL).

Understanding Speed Between Two Lines (SBTL)

Imagine you’re timing how fast a car travels between two markers on the road. You observe the vehicle as it crosses these lines, measuring the time taken. With the knowledge of the speed limit and assuming at least one vehicle maintains this limit, we can effectively calculate the speed of other vehicles on screen:

  • Two lines are drawn on the frame, perpendicular to the vehicle’s direction of movement.
  • The minimum time it takes for any vehicle to cross these two lines is recorded.
  • You use the simple formula: speed = distance / time to calculate the speed for all cars.

This method, although not foolproof, significantly enhances the accuracy of speed detection compared to traditional pixel mapping methods.

Troubleshooting

If you run into issues while executing this project, here are some troubleshooting ideas:

  • Ensure all dependencies are correctly installed. If you encounter errors, double-check the requirements.txt file.
  • Verify that the YOLO weights have been successfully downloaded; any issues with this step can lead to detection failures.
  • If the detection parameters need adjustments, revisit your detection and lane segmentation settings for accuracy.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With the right setup and understanding of the project, you can harness the power of YOLO and SORT for effective vehicle detection and speed analysis. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox