In this video tutorial, we will look at Multi-object tracking with Ultralytics YOLO.
In object tracking, we assign a unique ID to each of the detected object, and then we track that detected
object throughout the entire video frames.
We will also plot the movement of detected object across multiple video frames by drawing the trails,
which through which we can see what pattern the object is following.
The applications of object tracking are limitless, from security and surveillance to real time sports
analysis.
In this tutorial for Object tracking for object detection, I will be using Ultralytics YOLO 11 model
and for object tracking I will also use the Ultralytics package.
Ultralytics package currently supports a two object tracking algorithms, one is Boardshort and addressed
by Greg.
So currently, uh, the politics package sports two object tracking algorithm, although there are other
object tracking algorithms available as well, like Deepsort, sword and others as well.
But currently the Ultralytics package, uh, supports two object tracking algorithm, which I told you
one is called sort and other is by drag.
So we will be using, uh, Ultralytics package to integrate object tracking with our object detection,
uh, model.
So let's get started with this.
Uh, here you can see the demo is being played on your screen as well.
In this demo, you can see that, uh, for each of the detected object, we have assigned a unique ID
like one, three, nine, ten.
And you can see that we are.
So we are also, uh, checking the movement of the detected object by drawing the trace, which is telling
us what path the object is being, uh, following.
Okay.
So this is a quick demo, and let's get started with this over here.
So first of all, I will, uh, start writing down the code from scratch, and then I will show you
how you can integrate object tracking with your object detection algorithm.
So but before I go ahead, you can check this documentation by alternatives like how you can perform
multi object tracking with Ultralytics YOLO.
So we will be following this documentation uh in this complete tutorial.
So you can also check out this documentation.
And or you just need to search on Google by Multi Object Tracking with Ultralytics yolo.
And you will just find the first link.
And you can just click that link and you will be able to open this documentation.
So let's start writing down the code.
So over here you can see I have just created a project in PyCharm over here.
And you can see I have multiple video files over here.
And first of all I will just create a requirements.txt file.
He had acquired the Ultralytics badge along with Ultralytics.
Many other packages get installed as well, like OpenCV, Python, matplotlib, NumPy, Seaborn.
So we will install.
It install python r requirements dot txt.
So now I'm just installing the latest package because in my requirements.txt file I have only mentioned
the ultralytics package.
R spellings are all for the requirements.
That means.
Let's run this.
So if you have mentioned any other package in this requirements.txt file, this will also be get downloaded
with the ultralytics package as well.
So currently we only require ultralytics package along with ultralytics package like SciPy, torch,
OpenCV, Python, NumPy and many other packages get installed as well.
Like you can see matplotlib, OpenCV, Python over here.
Okay so numpy.
So you can see over here many other packages have been get installed pandas.
So let's try to create a new.py file over here.
So we'll write object.
tracking.py.
Or in the step number one we will import all the required.
Libraries.
So from Ultralytics.
In port Yolo.
We will load the YOLO in our model.
So I will write model is equal to YOLO and we want the YOLO 11 nano model.
Your 11 comes with five different models.
YOLO 11 nano is the fastest, but it is the least accurate among other YOLO 11 models.
The first one is tracking with it.
All right.
What sort?
Of test.
So if we just check out the documentation over here, uh, you will find that the default tracker is
bot sort.
Chart.
Okay, so if you don't define that tracker over here like you right.
Model or drag and don't define that tracker over here, let me just zoom in okay.
Okay.
And so if you right model or track but you don't define the tracker over here, it will use the default
tracker which is what sort.
So Ultralytics supports the following tracking algorithm about sort and by track.
Uh so you can enable uh, any of these tracking algorithms like hidden sort or by track by, by passing
the board sort dot yml file or by track dot yml file.
So uh, by passing the arrangement yml configuration, you can activate any of the tracker.
Okay.
So if you want to check uh, the informations like uh, on the alternatives tracker base, like you
can see it's post trackers, bot sort and by track.
Okay.
So let's move towards the code.
Alright results model dot.
So if I want to do object detection, I don't need to write anything.
I just need to go over here and write the source in which I need to pass the video path.
But if you want to add tracking, you just need to write track over here, and then you just need to
define the source file like your input file over here.
So I just need to go to the resources folder.
Then I just need to go to the videos folder.
And I can use video seven dot app.
If I want to show this video I can write show is equal to true.
And if you want to save the output video, uh, you can write save is equal to true.
So shortly our packages will be get installed like it's doing indexing.
So this will not take very much time.
So let's wait for this to finish and then I will just write or implement this code.
So now you can see currently we are using default tracker because I have not defined any tracker over
here okay.
So let's wait for the packages to finish.
By now you can see that finally the packages package installation is being done.
And now let's run this script over here okay, so the indexing is almost completed.
Uh, let's wait for a few seconds.
Uh, the indexing is being done, so this will not take very much time.
Okay, let's run the object.
So it takes around 5 to 6 minutes for the index package to get installed.
Because the alternative is many other packages also get installed.
So this takes some time okay.
So But first of all, it will download the YOLO 11 nano model.
It automatically downloads.
You don't need to go to the GitHub repository and specifically download the YOLO 11 nano model.
Okay?
Okay, now you can see over here.
Okay, so we are just getting issue no module named lab.
Okay.
So we can just write over here.
Uh we also need to install the lab package as well.
Um, actually it should include in their installation.
Like you don't need to install it specifically.
So now you can see I'm downloading installing the lab package over here.
and you can see your 11 nano models being downloaded.
So this.
Installation will take two seconds more as well, so let's wait for it to get finished.
Uh, we can ignore this.
Uh.
Uh, let me just check.
Hopefully it should run now.
And let's see how.
It will perform an auto update over here.
Okay, so here you can see we have the results.
Now you can see that, uh, ID is equal to one.
And here we are detecting the cars.
So okay.
but we are not able to assign the we are not able to do the reductions over here.
Okay.
So now you can see as the object comes here we are able to do the reductions.
Now you can see that each of detected object is being assigned a unique ID.
Now it looks pretty good.
Yeah.
So now you can see that each of the rejected object is being randomly assigned an ID.
Okay.
So if you use a.
Currently I am using YOLO 11 nano model.
But if you use uh, other models like YOLO 11 medium or your 11 x which have a high accuracy rate,
so you will see that detection results will further improve.
Currently I'm running it on my CPU machine.
I don't have a GPU, so therefore I'm pressing YOLO 11 nano model because it is the fastest among YOLO
11 model, but it is least accurate.
Okay, so now you have over here you can see that we are able to detection and we are able to assign
a unique ID to each of the detected object over here.
Okay.
So now you can see this is this object detection over here I am using 11 nano.
So therefore there are some false positives over here as well.
But this is what we do in object tracking.
Like you can see we are assigning a unique ID to each of the detected object.
Okay.
And to stop this I will just click the pause button and it will stop okay.
So.
For next thing what we can do is.
Now we will see how we can do tracking with by tracking okay.
So we will just define.
That is equal to model dot track.
Over here force is equal to.
Let's use some other video.
I just need to go inside the resources folder.
Then videos lets you create your eight dot board.
I want to see the output and you can see over here, uh, our output is being saved over here for the
previous, uh, run.
Okay.
So I want to save is equal to true.
And we want to use.
Byte pair dot.
YML.
And you like for the prediction in Yolo V8 model we have our different arguments.
Similarly we have arguments for the tracking as well.
Like we can set the constant ratio to 0.20.
We can set the non-expression IOU to 0.5 so you can adjust these parameters.
but we can make them too.
So let's run this now.
Okay, I made a mistake.
I just need to comment this out.
Over here.
We have the output.
Now, you can see over here, uh, we are able to detect the person over here in this video.
And you can see that, uh, with each person, we have assigned a unique ID like seven, 6321.
Like you can see, we are not able to detect these persons, but if you use, uh, other YOLO 11 models,
currently I'm using 11 nano model.
But if you use models, other YOLO 11 models which have a high accuracy rate, um, then it will be
able to detect these persons over here as well.
Okay.
And but in this tutorial we are focusing on object tracking.
Basically object tracking.
We assign a unique ID to each of the detected object.
Like you can see over here.
We have assigned a unique ID and to each detected object.
And then we can track that detected object throughout the entire video frames.
Okay.
So we are done with this.
And that was pretty fine.
Okay.
So now first of all I will just comment all this out okay.
So you can uncomment this and run this okay.
So now I will just write a Python script using OpenCV, Python and YOLO 11 to run object tracking on
video frames.
Or you can also run on the live webcam feed as well.
Okay.
So let's add a separator.
So we will write a Python script.
Using OpenCV Python and.
YOLO 11.
Run object tracking on video frames.
And.
Okay so let's get started with this as well.
So first of all will import all the required libraries.
Import cv2 from I draw Netflix and thought YOLO.
Then lower the YOLO 11 order right.
Model Yolo Yolo 11 nano dot RT.
Next we will create a video capture object like cv2 dot videocapture.
We just need to go into the resources folder.
Then we just need to go to the videos and we can use video seven.
Or we can just use video five.
Let's use some other video.
Then we will go through very famous pipeline while True red form of frame cap dot read.
So if we have able to read the video because that returns a boolean true, so it returns a that returns
a boolean true that is able to read a video.
Then.
We will run.
Event tracking on the video frames.
We will write.
Results is equal to model dot track.
We have the input frame and we will set our argument assistant is equal to true.
So this argument tells the tracker that it should expect a track from the previous frame or image in
the current frame, so persistent is equal to true.
Tells the tracker that it should expect tracks from the previous frame into the current frame.
Okay.
Well, next we will visualize the results on the frame.
We have an updated frame is equal to results 0:00.
Next we will display the annotated frame.
We can write the name of the window as YOLO 11 tracking.
So now in this window we will show short object tracking integrated with our object detection.
All right.
If it will not work.
So we will add one millisecond delay between the consecutive frames.
And if the users pass key, then the loop will break.
We can add the command break the loop.
If q key is pressed.
Simple.
And if it's unable to read the video frame, then it will break as well.
And then it will release all the frames and we will destroy all the windows as well.
So let's run this up now.
So now over here, you can see that we are able to integrate object tracking.
Like you can see that for each of the detected object, we are able to assign a unique ID like one with
this person, six with this bicycle and eight with this bicycle.
And the results look quite good.
Like you can see with traffic lights.
We are also able to assign a unique ID over here.
Okay, so the results look quite promising to me.
Okay.
Yeah, the results look quite good.
Like, we are able to assign a unique ID with each of the detected object, like 1715 Three 114.
Okay, so if I press up, that loop will break that.
So the next thing we can do is, um, now we'll see what, uh, path the object is following.
So we'll draw the trail, or we can write another Python script over here.
Object tracking trails.
Obviously, plotting tracks over time.
Or we will plot the movement of an object across multiple video frames.
First of all, I will import all the required.
Libraries.
Are full.
We will be saving the center point of each of the bounding box into our dictionary.
And then using that center point, we will be drawing lines.
Okay.
So we will be saving the center point of the bounding box in each of the frame in our dictionary.
And using that center point.
Then we will draw the lines.
So now we load the YOLO 11 model.
Then we will create the video capture check.
We just need to go inside the resources.
Then we just need to go to the videos.
And then we can use video five dot mp4.
So we will store that track history into a dictionary.
Then we will go through the video frames.
Okay.
App dot frame.
So if we are able to read the video then.
We'll run your 11.
Tracking frame.
Like model dot track.
Here we have the source.
Which is frame.
Now we need to get the bounding box coordinates and the track IDs.
Blind box.
This is equal to because this will contain the bounding box coordinates for all objects in the frame.
And then we'll loop through this so we can write.
Result zero.
New requirements.
S center coordinate.
Then we will also require the track ID.
So we'll get the track IDs for all the objects in the frame.
So similarly, in the previous.py file, we'll write the results on the frame as well.
And I annotate the frame.
So I can display the annotated frame as well.
So then we can write if cv2 dot wait.
Key one.
There is a one millisecond delay between the consecutive frames and if the user press the W key.
I missed one thing over here.
Then the loop will break.
So now I will release on the frame and destroy all the windows as well.
So let me just run this.
Now, currently you can see that we are able to implement object tracking, but we are not able to draw
the tracks.
We show the path the object is following.
So I just press the W key.
It will close.
So we need to plot the tracks.
For this I will write the code over here.
So now I will loop through the center coordinates and the track IDs.
So I will just write box format track ID and say here we have the boxes.
So I drag.
ID.
Over here we have created a default dictionary but in the variable track history.
Now it's time to use this.
And this track ID, we have the object IDs for all the objects in the frame.
I submit that each track ID will save the center point of the bounding box, and we will then connect
the center point of the bounding box for all the frames.
And then we will draw the lines which tell us the path the object is following.
So we need to obtain that points or center points for the 30 frames.
Okay.
So we write the ten track.
30.
So we will retain the center points for the 30 frames.
So if you increase this value to 60 or 90 uh the the tracking trail line will increase.
Okay.
I will just show you as well.
Now don't draw that tracking lines.
Or using Siri to dot polling lines will draw the trails or the lines.
Here we have the information or the center points.
And the thickness will be ten.
Okay, so that works pretty fine.
Let's run this up.
Now, uh, we can just use video.
Uh, okay.
So let's see how does it works.
The color of these lines will be a white because we have just set the color to white.
You can just change the color to any other color as well.
Okay, so now you can see in the white we have the trails over here.
So we chose the path the object is following.
Okay.
So now you can see over here we have the trails over here.
Uh so let me just show you with other video.
Video.
So and we can just change the color to yellow.
Let's run this now.
Okay.
So here we got the error call.
Yeah.
So how we can deal with this?
We can write.
XYZ0.
But not id.
Is not null.
Okay.
So now hopefully this will work out.
So let's see.
Now over here you can see that we are drawing the trails with a blue color over here.
Okay, okay.
Okay, so now you can see that these trails are telling us the pattern the object is following.
Okay.
So now you can see over here we are drawing the trails over here as well.
So if I just press the W key this will close.
And if I just write 90 over here let me show you what that does.
It makes.
So if I just write 90 over here, the length of these trails when further increase.
Okay.
So now you can see that like basically this is telling the path uh, the object is being following.
Now you can see that the length has how much increased okay.
So this is uh, quite makes impact.
But 30 is the ideal value.
Okay.
So now we have seen that how we can plot the movement of object, how we can integrate object tracking.
Now we will implement multi threading.
Multi threaded tracking.
So multi threaded tracking provides us the capability to run object tracking on multiple video streams
or multiple live cam webcam feeds as well.
Okay, so we can create another.py file.
For multi threaded.
Tracking provides.
The.
Capability to run check tracking on multiple video frames.
Okay so first we will import all the required libraries.
Import trading.
Import cv2 from Ultra LaTeX.
Import your.
So next we need to define model names like what different models we use need to use in the multiple
video streams and video sources.
You can also define the webcam sources as well.
We had windows in M1 nano model, and we will also use your 11 nano segmentation model as well.
So we have sources equal to just need to go inside the resources, then there will need to go to the
videos and first we'll run video seven dot mp4.
Then we have sources.
Videos video eight dot mp4 okay.
So now we will run YOLO 11 tracker in its own track for concurrent processing.
So we'll create a function.
Okay.
Run tracker that we just need to pass the model name and the file name in the input.
So we'll run YOLO 11 tracker in its.
Own thread.
For concurrent processing.
So we'll pass the model name to the.
Over here.
Then we'll write results model dot back because we need to implement bracket.
Then in the source over here we have the file name.
We need to save the output video.
Then we need in the documentation they have set stream is equal to true.
But this doesn't show the output video.
So we just need to write.
Show is equal to true.
Then we will loop through the results.
Now create and start tracker thread.
I will now create and start tracker threads using a for loop.
For now I will loop through the video files and the model names.
So the arguments will be.
Modern name and the video file.
So now we will run YOLO tracker in its own thread for concurrent processing.
I will show you in the output how this works.
Let's start this up now.
Now before it completes for example, if any video completes, it will wait for all the tracker threads
to finish.
I will clean up and close all video files.
So now it's clean up and close all windows.
So we write cv2 dot destroy all windows.
So this is how it works.
And let's run this and see if it works.
So we have 11 nano model but we don't have the segmentation model.
So it's downloading.
And let's.
So this is our first video and here is our second video.
Okay.
So you can see over here we are able to implement multithreading.
So using multithreading it provides us the capability to run object tracking on multiple video streams
like you can see over here.
On one side I am implementing object tracking with the segmentation model.
And on the other side on the left hand side I am implementing object tracking with the object detection
model.
Okay.
So now you can see that the results look pretty good.
Like you can clearly see over here.
Uh, the results look quite promising to me as well.
Okay.
So now uh, using a multithreading we are able to implement object tracking.
So that is results look quite promising.
So now we are able to implement object tracking on multiple video streams as well.
So multi threaded tracking provides us the capability to run object tracking on multiple video streams
which you can clearly see over here.
And over here you can see that we are doing concurrent processing.
We are running video seven and video eight are in parallel over here.
You can also run on live webcam feeds like if you have multiple feeds coming, you can run it on multiple
feeds as well.
And you can see this is what we call concurrent processing.
Like we are running video seven and video eight concurrently at the same time.
Okay, okay.
So this is how it works.
And using multiple threading tracking.
Uh, we have run object tracking on multiple video streams.
That's all from this tutorial.
Thank you for watching.