Detect, Track, and Count Objects Using YOLO V8 and Deep SORT
Hello everyone, Anurag welcomes you all.
In this article, I will explain how to detect, track, and count objects using YOLO V8 and the Deep SORT algorithm.
All the necessary code is available in on my other upcoming blogs which you can clone to follow along. The Deep SORT code is stored in a specific folder, and the Jupyter Notebook used in this tutorial is also included.
Setting Up the Environment
Before diving into object detection and tracking, ensure that your environment is correctly set up. Here are the required versions:
Python Version: 3.9.10
Ultralytics Version: 8.0.106
Install it using: pip install ultralytics mentioned below.
pip install ultralytics
This package is necessary for YOLO V8.
Torch Version: 2.0.1 (compiled with CUDA 11.7)
GPU Used: RTX 3090
Ultralytics Version:
For those using a CPU, install a compatible Torch version. If using a GPU, check your CUDA version and install the appropriate PyTorch version accordingly.
Running YOLO V8
Now, let’s begin the object detection process. First, navigate to the folder containing all the necessary files and import the required modules.
The Jupyter Notebook provided in the repository will be used.
The primary goal is to use YOLO V8 to detect objects and extract:
Bounding box coordinates
Confidence scores
Detected classes
These variables are crucial for tracking.
In this tutorial, Anurag will focus on detecting people using YOLO V8. The COCO dataset pre-trained model contains a person class, which will be used here.
Here’s how the model is applied:
The output image with detections will be saved in the runs/detect/predict folder.
Applying the Deep SORT Algorithm
After detection, the next step is to track objects using the Deep SORT algorithm.
First, import all necessary files from the Deep SORT folder.
Load the pre-trained Deep SORT weight file, which is provided in the repository.
Set the Max Age parameter to 70.
This determines how long an object remains tracked if it disappears from consecutive frames.
Processing a Video for Detection and Tracking
I will now demonstrate how to track and count objects in a video dataset.
Load the video dataset and retrieve its frame width, height, and FPS.
Save the processed output after detection, tracking, and counting.
Use unique track IDs to count distinct objects.
Each frame is processed as follows:
YOLO V8 detects objects.
Bounding box data is passed to the tracking algorithm.
Deep SORT assigns unique tracking IDs to each detected object.
The algorithm keeps track of objects across multiple frames.
Tracking IDs ensure each detected person is counted only once.
Displaying the Results
Each tracked object is assigned a unique ID and a bounding box.
The total number of people in the frame is counted and displayed.
The final output dataset contains all detected objects with their assigned tracking IDs.
Example Output:
Total Person Count: 4
Person 1 → ID: 4
Person 2 → ID: 3
Person 3 → ID: 2
Conclusion
In this article, Anurag has demonstrated how to use YOLO V8 and Deep SORT to:
Detect objects in an image or video dataset
Track objects across multiple frames
Count distinct objects using unique tracking IDs
This technique is useful for applications like crowd analysis, traffic monitoring, and security surveillance.
If any new AI company needs marketing services, contact Global Reputation Guru. Anurag and the team provide marketing solutions worldwide to help AI businesses grow.