Track and Count Objects Using YOLOv8
Counting moving objects is one of the most popular use cases in computer vision (CV). It is used, among other things, in traffic analysis and as part of the automation of manufacturing processes. That is why understanding how to do it well is crucial for any CV engineer.
With this tutorial, you will be able to build a reusable script that you can successfully apply to your project. We will also test it on two seemingly different examples to see how it would perform in the wild.
Follow along using the open-source notebook (opens in a new tab) we have prepared, where you will find a working example.
Blueprint
Before we start, let’s create the blueprint for our application. We have a few key steps to make - detection tracking, counting, and annotation. For each of those steps, we’ll use state-of-the-art tools - RF-DETR (opens in a new tab), ByteTrack (opens in a new tab), and Supervision (opens in a new tab). RF-DETR is released under Apache 2.0, so it's free for commercial use.
vehicle detection, tracking, and counting with RF-DETR (opens in a new tab), ByteTrack, and Supervision
Object Detection with RF-DETR
As mentioned, our work starts with detection. There are dozens of libraries for object detection or image segmentation; in principle, we could use any of them. However, for this project, we will use RF-DETR (opens in a new tab), Roboflow's real-time transformer-based detector. Its native pip package support will make our lives much easier.
pip install rfdetrInstalling RF-DETR
RF-DETR provides an SDK that allows training or prediction in just a few lines of Python code. This post will only focus on a selected part we need for this project.
from rfdetr import RFDETRBase
# load model
model = RFDETRBase()
# predict
detections = model.predict(frame)loading the RF-DETR model and inferring over a single frame
We will use two basic features — model loading and inference on a single image. In the example above, RFDETRBase loads the pre-trained base variant of the model; you can swap in RFDETRLarge if you need higher accuracy at the cost of speed. A frame is a numpy array representing a loaded photo or frame from a video.
vehicle detection with RF-DETR
Object Tracking with ByteTrack
In order to count how many individual objects have crossed a line, we need a tracker. As with detectors, we have many options available — SORT (opens in a new tab), DeepSort (opens in a new tab), FairMOT (opens in a new tab), etc. Those who remember our Football Players Tracking project will know that ByteTrack is a favorite, and it’s the one we will use this time as well.
This time, however, we will not use the official repository. Instead, we will use an unofficial package (opens in a new tab) developed by the community. This change will allow us to shorten the installation process to just one line.
pip install bytetrackerInstalling ByteTrack
The process of integrating the detector with the tracker is quite convoluted. Refer to the notebook (opens in a new tab), where we have explained all the steps in detail.
tracking with ByteTrack
Counting Objects with Supervision
Just ask yourself how many times you’ve scrolled Twitter or LinkedIn and seen a demo showing how to count cars moving down the road. However, despite this, there are no reusable open-source tools you can use to streamline that process.
At Roboflow, we want to make it easier to build applications using computer vision, and sharing open-source tools is an important part of that mission. This is one of the reasons that motivated us to create Supervision —a Python library to solve common problems you encounter in any computer vision project.
To get started, install the package using pip. Supervision is still in beta, so to avoid problems, we recommend freezing the version.
pip install supervision==0.1.0installing Supervision
Supervision is more than just counting detections that have crossed a line. If you want to learn more about the Supervision API take a look at Github (opens in a new tab) or the documentation (opens in a new tab).
Now, let’s quickly go through the elements we have simplified with our new library. First things first — reading frames from the source video and writing the processed ones to the output file.
from supervision.video.dataclasses import VideoInfo
from supervision.video.sink import VideoSink
from supervision.video.source import get_video_frames_generator
video_info = VideoInfo.from_video_path(SOURCE_VIDEO_PATH)
generator = get_video_frames_generator(SOURCE_VIDEO_PATH)
with VideoSink(TARGET_VIDEO_PATH, video_info) as sink:
for frame in tqdm(generator, total=video_info.total_frames):
frame = ...
sink.write_frame(frame)reading frames from the source video and writing the processed frames to the output video
We can’t forget our main key component— LineCounter, which counts how many individual detections have crossed a virtual line.
from supervision.tools.line_counter import LineCounter
from supervision.geometry.dataclasses import Point
LINE_START = Point(50, 1500)
LINE_END = Point(3790, 1500)
line_counter = LineCounter(start=LINE_START, end=LINE_END)
for frame in frames:
detections = ...
line_counter.update(detections=detections)counting detections that crossed the line
Example: Counting Candy
As promised, we tested our application on a completely different use case: counting candy moving along a conveyor belt. Below you can see the results with almost no changes to the code. The only thing adjusted was the path to the model and the location of the lines.
candy detection, tracking, and counting with RF-DETR, ByteTrack, and Supervision
Conclusion
You now know how to use Supervision (opens in a new tab) to bootstrap your project and unleash your creativity by tracking (opens in a new tab) and counting objects of interest. As a next step, deploy your trained model locally or on-device using the open source Roboflow Inference Server (opens in a new tab).
Stay up to date with the projects we are working on at Roboflow (opens in a new tab) and on my GitHub (opens in a new tab)! Most importantly, let us know what you’ve been able to build.

