Hello everyone.
In this video tutorial we will use YOLO V8 with Deepsort object tracking to count the vehicles going
in the north, south, east and west direction.
We will count the number of cars, buses and trucks going in each of the direction.
So let's get started in front of you.
You can see the demo output video, which in front of you you can see over here.
So you can see that this is the north direction.
This is the south direction over here and this is the east and this is the west direction.
Okay.
So whenever the car or any other vehicle intersect with this line so you can see the here we see the
increment like in each direction.
Two cars have passed in past three trucks have passed in the west direction.
One truck has passed in the north direction.
We can see the one truck has also passed in the south direction.
We can also see that one truck has passed.
So let me show you the Direction Compass first and then we can go into further details.
Okay.
So this is the Direction Compass we have above is the north direction.
This is the south direction.
This is the east direction and this is the west direction.
Okay?
So just keep this direction compass in your mind and let's go back towards the output video demo.
So.
You can see that this is a north direction we have.
This is a south and this is a east and this is the west.
So whenever any bigger like truck, car or bus intersect with this line over here or over here or over
here, this green line, you can see that these green lines over here.
So when any vehicles intersect with this line, for example, if the vehicles enters going in the north
direction, for example, if the vehicle intersect with this line, this is in the north direction.
So we have a count in the north direction.
Like if the truck intersect with this line, then we can say that one truck has passed.
Okay?
Or if the car intersect with this line, this is the north direction.
So we can say that the car has passed over here.
Okay?
So in this way, we do the direction count over here.
So if you can see over here, I've just added an image over here.
So let me just make it a bit short.
Okay.
So as you can see over here.
This is a line over green line over here.
So here we can see that truck.
Okay.
So if I select this as a pick, okay.
And just select a red color from here.
Okay.
And okay, that's fine.
Let's select the red color.
Okay.
So you can see this trails line.
Okay, let me just make it a bit more brighter.
A bit.
Okay.
I think this will work.
Okay.
So you can see this trails line over here.
Okay.
You can you see this trails line.
Okay, So this is the trails line over here.
So when this trails intersect with this green line, okay, When this trail intersect with this green
line, then we can say, for example, this truck is going in the west direction.
Okay, this is a north.
So sorry.
This is going the south direction.
Okay?
This is a south and this is the east direction.
And this is the west direction.
Okay.
So as this truck is going in the south direction, so when this cart trade intersects with this green
line, then we can have an increment over here like you can see over here, like south direction, that
truck goes into the south direction and we can say, one, that truck will get increment.
Let me show you this part of the code.
For example, just let me show you this.
So currently you can see that we don't have any vehicle going in the south direction.
Okay.
Like south direction and the west direction is empty.
So we will see the west direction in the last part.
But currently focus on this only the south direction.
Okay.
Which is over here.
So let me play this video.
Okay.
So you can see just focus over here.
Okay.
So let me just play this video.
Okay.
So, okay.
Okay.
And let me just.
Okay, so when you see that when this train line, this brown line intersect with this green line,
we can say that in the south direction, one truck has passed.
Okay.
So when this green or brown line, you can see brown line intersect with this green line, then we have
an increment over here in the south direction.
One truck has passed.
Okay.
Now let's see what the west direction as well.
Okay, So just go over here.
Okay.
So now you can see that this is going the east direction.
Okay.
In the east direction.
Currently we have one truck.
Okay.
As this is an east direction.
Okay, so.
Okay.
So you can see that this when this truck has passed in this direction is direction.
So we can say that.
See that?
Currently you can see that we have one truck over here.
Okay.
So we have one truck over here.
So we let's say when this truck passes this line, you can see the truck count increases to two.
Okay.
So in this way, it works.
So like now this car is passing this line.
So now we can see that that truck car count is also increasing.
Plus, let's see the east direction.
Okay.
Let me show you the one other direction, West direction.
Okay.
So you can see that when this country we don't have any vehicle going into the west direction, basically,
this is the west direction we have.
Okay.
So when this truck, you can say that this intersect with this trace line, like this brown line intersect
with this green line truck, this brown line intersect with this green line, Then we can see the increment
over here in the west direction.
We can see that one truck has passed.
So, okay, just going a bit back.
Okay.
So you can say that when this brown line intersect with this green line, we can see in the west direction
one truck has passed.
Okay?
So in this way, all this mechanism work.
Now, you can say that this is the end result in the east direction.
Two cars, three trucks have passed.
So this is the east direction we have and in the north direction, which is over here, one truck has
passed in the south direction.
One truck also has passed, which is over here and in the west direction.
One truck has also passed, which is over here.
So let me show you again like you can please focus on this brown line over here so you can see that
when this brown line intersect with this green line, you can see that we have an increment in the truck
like one truck has passed.
So let's go towards the CoLab file, CoLab code part and further explore the thing.
And let me explain you the complete code as well.
Like how can you implement this project on your side as well?
So let's move towards the code part.
So here we have the complete code for this project.
I will be sharing this complete code with you as well.
So.
Most of all, we are use in this project.
We are using the sword object tracking GitHub repo.
Let me open this first, so just copy this link from here and just open a new window here and just and
click on enter.
So this is the GitHub repo.
Yolo v eight Deepsort Object tracking repo.
Which I am using in this project.
So this is I have created this GitHub repo, so you can just.
I will be sharing this GitHub repo link with you so you can easily access this repo as well.
Okay.
So here already the steps are provided to run the code.
So I have just followed the same steps in the Google CoLab notebook as well and I will share this notebook
file with you as well.
Okay.
So first of all, just let me change this disconnect and delete runtime because I've already run this
script previously, so let me do it from the start.
So just connecting it again over here.
So okay, so before running the script, please make sure that you have selected the runtime as hardware
accelerator as GPU.
Okay, then you just need to clone the GitHub repo in the first step.
So here I'm just cloning the GitHub repo, which you can see over here, so this might take a few seconds.
Okay.
So now the repo is cloned and you can see the GitHub repo over here by the name of V8 Deep Sword object
tracking GitHub repo.
Now we now if we see that what our present working directory is so we can just write b d this shows
us what is our present working directory.
So our present working directory is this file section.
So we need to set our present working directory or the current directory as this cloned folder.
So just copy this path over here and write CD percentage.
CD cd means current directory and just paste this path over here and just click on Enter.
So now this spawned clone folder will be set to as our current directory.
So now this clone folder is set to our current directory.
So with the folder which we have cloned is our no current directory.
In the next step we need to install all the required libraries or the dependencies.
If you skip this step, then if you run the training validation or prediction step, then you might
face that error that the following library.
For example, Hydra library, Matplotlib library, Seaborn library or any other library is not installed,
so it's necessary to install all the required libraries by installing all the dependencies.
So if I run this cell, it will install all the required libraries.
Or you can say the dependencies will get installed by if you run this cell.
Okay, so this might take few seconds to execute.
So I'm just installing all the required libraries or the dependencies required to run this project.
Okay.
So this might take few more seconds.
So let's see.
But it will be installed in few seconds.
Or like you can see markdown jinja2 library request library.
They are all getting installed.
So if you've not run this cell then you will definitely face the error.
When you run the training, validation or prediction script that the following library is not installed.
Okay.
So now as we are implementing detection in this step, like in this project, like detection and tracking
in this project, so just go to ultralytics, YOLO V8 and go to the detect folder and set this folder
as your current directory.
Okay.
So just copy path from here and set the detect folder as your current directory because we are performing
an object detection and tracking in this project.
Okay.
So in we have set the detect folder as our current directory because here we have that train predict
and validation.py files.
So now, as in this project, we are using our deepsort for object tracking.
So I will install all the deepsort files from the.
I have already placed the Deepsort files into my google drive, so I'm just downloading the deepsort
files from the Google drive into my Google CoLab notebook, so it might take few seconds to download
the Deepsort files from the Google Drive into the Google CoLab notebook.
So I have downloaded currently the zip file so you can see over here in the detect folder I have the
Deepsort PyTorch zip file over here, so I will just unzip it from here.
So what does object tracking do is object tracking.
Just assign a unique ID each of the detected object.
Just object tracking.
Just sign the unique ID to each other detected object.
Thus, we can also draw the trails as with the help of unique IDs as well.
Okay.
Like the trails which I have shown you.
Like.
Let me show you again.
These are the trails of the vehicles.
Like remove.
Just moving it to show you in a better way so you can see this brown line over here.
You can see this brown line over here.
So these are the trails.
These brown line is basically the trail.
Okay.
So I'm just.
Okay.
So basically I have run the upload unzip.
I'm just unzipping the deepsort.
So I have unzip the deepsort files over here.
You can see that.
So now if I just check it.
So we have the unzip folder of Deepsort over here which contains the files for the object tracking as
well.
Okay, now, now we have downloaded a sample video from test for testing our script printed.py file
on from Google Drive and you can see the name of our testing video is test video dot mp4, which is
this video you can see here.
This is the video which I have downloaded from the Google Drive.
Okay.
For the demo video which have shown you I have just downloaded that demo video from Google Drive.
And so basically this is the output demo video which you will get after you run the script.
So you can test this script on some other demo videos as well.
So it's all upon your choice.
But I have tested on this demo video, so I have shown the results for this demo video, so I'm just
downloading the same demo video by the name test video dot mp4 from the Google Drive and here I'm just
downloading the predict.py script updated predict.py script means basically this contains the script
which has been adjusted for this project, which is the detect direction and count vehicles in each
of the direction.
Okay.
So let me open the predict.py predict.py script and let me explain you the complete process flow in
the script as well.
Okay.
So here you can see the predict.py file which I have opened over here.
So I have already added commands before each line you can say before each line of the code so that it
will help you to better understand the code all the way.
Okay.
So let me explain the predict.py script as well.
So first of all, in the meanwhile, let me run the predict.py.
So as we run it so after we explained with the complete script, so in the meanwhile the code would
have been run.
So just running the predict.py file over here.
So it might take few seconds for it to execute.
Like you can see that the predict.py file have just executed with the predict.py file on this test video
which I have downloaded.
And here I'm using the yolo v8 x dot model v8.
Is that the just basically YOLO?
V8 has 5 or 6 different models, five different models and YOLO v8 X is the more most accurate, but
it has a less it is less fast or it is not as much fast than other YOLO V8 model like YOLO V8 and is
less accurate, but it is more fastest and YOLO text is more accurate, but it is less fast.
Or you can say it.
It takes a bit more time than other V8 models.
Okay, so here I am just importing all the required libraries, import hydra and then we have the import
torch library.
So.
As you all know, it is being built using PyTorch to perform object detection tracking using YOLO V8.
So we need to import the PyTorch module.
So to use the PyTorch library, we do import dots because it is being built using pytorch.
So to perform object detection and tracking using YOLO V8.
We need to have the PI torch module and to use PyTorch library we do import torch.
Okay.
So if you want to check the version of the PyTorch library, you can simply print torch dot dash dash
version.
So it will give you the version of the PI torch library.
Okay.
So for the ARGPARSE, basically it helps if you it is basically helps in run basically its command line
parsing library import is basically command line parsing library like you if you pass any arguments
in the command line.
Okay so here imported time if you want to calculate the so you can use this library import time, then
we have the import cv2.
So to implement object texture and then tracking, we use OpenCV Python library, which is Cv2 and two
then we have import torch dot backends dot double.
So torch dot backends basically control the behavior of various backends that PyTorch support.
Then we are importing from numpy import random.
So basically it assigns a random color to each of the detected object.
What I mean by the random color.
So let me show you if I open this.
So now you can see that the card is being assigned a pink color over here and the truck is being assigned
a brown color over here.
Okay.
So basically we get an object detection model randomly assign a color to each other.
Detected object.
Okay, so here I'm just importing from numpy, import random to randomly assign a color to each of the
detected object.
Okay.
So here I'm importing the YOLO required modules from a different file libraries or files from it.
And here I'm just importing the deepsort module.
Basically we are using deepsort to implement object tracking.
So here I'm just importing from collection library.
So DK is basically a list.
So why we can't use simply list over here because queue is preferred over list in the cases where we
need quicker append and pop operation from both ends of the container.
So why we need library over here is let me explain you.
For example, if you see this image, you can see that this object is assigned a unique ID of 59, and
this object has a unique ID of 36.
Okay.
So if the object leaves the frame.
So, for example, when this jet leaves this frame, so we need to remove this ID from our list.
Okay.
And if a new object enters the frame, for example, car enters into the frame.
So we need to add this ID, this unique ID into our list.
Okay.
So when an object leaves the frame, we need to remove the object unique ID from the list.
And when a new object enters the frame, we need to add the unique ID so when object leaves, we need
to remove that unique ID.
When object enters, we need to add that unique ID.
So to do this we use DK library.
So here you can see as DK is preferred over list in the cases where we need quicker append means if
you want to add a new ID and pop like you want to remove the ID when the object leaves the frame.
Okay.
And here we are importing the numpy library because we use numpy library to convert an array into the
list.
Okay, so here I'm just defining a random color palette so that each of the objective objects can be
assigned a random color.
So find a color palette over here.
Okay, so now here I've just created a dictionary by the name data.
So creating a dictionary by the name data is in data dictionary, which will store bottom center coordinates
of the detected objects.
So in data dictionary, we basically store the Let me show you by this.
We store the bottom center coordinates of the detected object.
So let me just remove this.
But this means this is the bottom center coordinate.
You can see that this is the bottom or this line is the bottom of the center quad.
So this is the bottom center coordinate of.
So in the data dictionary, we've stored this values.
Okay.
In each of the frame, what is this center coordinate value over here like the center coordinate value
in the bottom of the bounding box.
Like you can see that this is the bounding box.
And over here and this is the center coordinate at the bottom of the bounding box.
Okay?
So in each of the frame, we store this center, coordinate value, and using this center coordinates
value.
We draw this trails.
Okay, These trails.
Tell us what the vehicle is, what falling, what the path like.
You can see that from this trails.
I can tell this this vehicle has made a turn.
Okay.
So we will just store this bounding box bottom, coordinate like this is the bottom of the bounding
box, the box center value center coordinate value.
Okay.
So we store this center, coordinate value of at the bottom of the bounding box.
Okay.
So.
In the Dayton Visual Dictionary, we store the center coordinate value of the bonding of the D box of
each object in a center, coordinate value at each frame of each of the object we store in this data
dictionary.
Okay, so here we are, initializing the deepsort variable that will be used for that tracking purpose.
Basically we use Deepsort for that tracking.
So now here I'm just creating four dictionaries by the name object counter object counter one object,
counter two and object counter three.
So as in this project, we are counting the traffic going in each of the direction north, south, east
and west.
So first of all here, here we are creating four dictionaries by the name object, counter object,
counter one object, counter two object, counter three.
So as in this project, we are going counting the traffic going in each of the direction north, south,
east and west.
So basically we will count the number of cars, number of trucks, number of buses going in of each
of the direction.
So basically, as you can see, this output demo video, we are just calculating the number of vehicles
going in each of the direction like north, south, east and west, for example.
So I have created four dictionaries for each of the direction, like each direction I have one dictionary
for West Direction, I have one dictionary for North Direction, I have one dictionary.
And for South Direction I've also have one dictionary.
Okay.
So basically the number of trucks, the number of cars going in each of direction, for example, in
or in East Direction dictionary, I have the record number of cars, number of trucks which have gone
gone in the east direction.
So the number of cars, number of trucks which have gone in the east direction will be stored in the
East Direction Dictionary.
Like this is the object counter to represent the dictionary is object.
Counter two is a dictionary empty dictionary for the east direction.
Okay.
So now in the east direction we can see that two cars and three trucks have passed.
So object counter to dictionary will have the record.
Like in the east direction, two cars and three trucks have passed.
So in the west direction object counter three dictionary will have that record like number of trucks,
number of cars have passed in the west direction.
Okay.
So now here we are creating four lines, one line for the each direction.
So here you can see that we have four lines over here.
One, two, three, four, four green lines like this is the first green line.
This is the second green line.
This is the third green line.
This is the fourth green line.
So we are creating four lines, one line for each direction, north, south, east and one west.
When the vehicles trains intersect with this line, then there is an increment.
For example, what I'm saying is.
When this wiggle like these are trails of the vehicle.
I have explained this below.
When these trails of the vehicle intersect with this line, then we can see that increment, like the
vehicle is moving in the north direction or south direction or east or west direction.
I've already explained this thing, so here I'm just initialize the x one, y one coordinate and X2Y
coordinate for each of the line.
So let me explain this.
These workers over here, please focus at this point.
So when I go to here.
So now you can see that at that point we have 622 728 This is the x1y1 value.
And when I go to this line, I have 1533 and 859.
This is the x2 y2 values.
Okay.
So you can see that this is the x1 y1 value and this is the x2 y2 value.
Okay.
So the starting coordinate of the line and the ending coordinate of the line values over here.
So here we are initializing a Deepsort object tracker.
So now here we have the function by the name x, y x y to x y.
So in the define x, y, x, y to x y function we are the converting the bounding box output received
from YOLO V8 to a format that is compatible with deepsort.
So using this function, basically we are converting the bounding box output received by YOLO V8 to
a format that object tracker can also handle.
Okay, so using this function we convert our x and y coordinates to center coordinates which is x,
y, z along with the return the width and height of the bounding box.
Okay.
So in so basically we convert the bounding box coordinates of the YOLO V8 to a format that basically
is compatible with object tracking.
Deepsort object tracking.
So in this output we get the center coordinates of the X, y, z and width and height.
So basically, let me tell you what, at the end of this function we get.
So this function basically returns the center, coordinate like this, coordinate X and y, Z, basically,
if you.
So let's just imagine that this is the center of the boxing box bounding box.
So this function will return the center, coordinate X, y, z.
And the way I see this is.
See.
And the bit.
This is the bit.
And this is the height.
So this function returns that for each of the detected object, we get the center coordinate X, y,
Z, where this is the weight and this is the height of the bounding box.
So in return, we get these four values, okay, for each of the detected object.
So here we convert the X, y, or Z coordinates of the V8 into another format.
But this case we are looking for a but in this case we are only focusing on this x, y, two x, y.
But this function will not be used in this project currently.
Okay.
So now here we are just computing color for each of the label.
For example, we say that if the if the person is detected, then the color of the bounding box will
be this.
If the car is detected, then the color of the bounding box will be this.
So basically I'm saying that if we have detected a car like this, then this color of the bounding box
will be pinned pink.
Okay, So this color of the bounding box will be pink.
So here I've just defined the format of the pink color.
Okay.
Which bounding box color?
Okay.
So basically this is the bounding box color.
I've defined the format of this bounding box color.
Okay.
Over here.
Plus, we say that if the object if, if, if we don't have person car, motorbike and box, for example,
truck appears, for example.
Now, we have not defined the color of the bounding box of the truck.
So if the truck appeared like over here, okay, so that truck color will be assigned randomly using
this color palette which we have created above.
Okay.
So now here we are defining the draw dash border function.
So draw dash border function, create a bounding rectangle over the bounding box where put the text.
So using this draw dash border function, we create this rounded rectangle.
You can see that this rounded rectangle, this rounded rectangle can you see this?
This important part with this rounded rectangle.
So this rounded rectangle is being created.
This rounded rectangle as well is this rounded rectangle is being created using the draw dash border
function.
So when you dash box function, we are passing the cv2 dot rectangle to create a rectangle around the
detected object.
Plus we also obviously are using cv2 dot text to add labels.
So basically, uh, using this dash box function, this one function UI dash box function.
We are creating this rectangle like the square rectangle, this bounding box.
Plus we are also putting text into this.
Roundedrectangle which we have created using the draw dash border function.
So using this draw dash box function, we create this bounding box like this bounding box over here.
Plus we also put the text in this rounded rectangle which we have created using the draw dash border
function, which is above over here.
So now here we have the Intersect and CCW function.
So what this function do is basically here, for example, if we have created two lines.
So now to find whether this lines intersect or not, okay?
So to find whether these two lines intersect or not, two lines intersect or not, this line one and
this line two.
So find whether these two lines intersect or not.
We use these two function, this intersect function over here.
Okay.
So to find whether these two lines which we have intersect or not, we use this intersect function,
which is this.
Okay.
And CCW is being called in this Intersect function.
Okay.
So what we are doing over here.
We basically save.
And this trails intersect with this line.
This green line is when these trails intersect with this line.
Then we can say there is an increment that the truck has passed into the south direction.
Okay.
So basically, we need to find the intersection of these two lines, green line and the trails line
type.
So for this I am creating this Intersect function and to find where our truck is moving in which direction.
So we have created a function by the name get direction so that we can find in which direction our vehicle
is moving.
Either it is moving in the north direction, south direction or west, east or west direction.
Okay, so what are these two points?
0.1 and 0.2.
Basically 0.1 and 0.2 are the.
Let me just explain this over here.
Okay?
Just clean this up.
So basically, 0.1 and 0.2 are this center coordinates value.
This is our first value and this is a second value.
So 0.1 and 0.2 is the center coordinate of the trails value.
These like these are the trails.
So 0.1 and 0.2 are the center coordinate of the trails.
Okay.
Okay, 0.10.2 are the first and second point of the center coordinates of the trails.
Okay, Over here.
These are the trails.
This is the point one.
And this is the 0.2.
So here we are, just calculating the direction of the vehicle.
If there is going the north direction, south direction, east or west direction.
Okay, so here we have just created a draw text box function.
So in draw dash boxes function I am calling as dash box function which I've created above to draw the
bounding box around the detected object and assign unique IDs to each of the detected object.
So in this I told you that why we use object tracking is to assign unique ID to each of the detected
object, which is a function of object tracking to find a unique ID for each of the detected object.
Okay, so here we are, just assigning a unique ID to each other directory object and we are finding
the center of the boundary or bottom edge.
So this is the center of the bottom edge which we am finding like this is the center of the bottom edge
which I am finding over here using this formula.
And here we have the label, the label, we assign an ID and the object name, which you can see that
in label.
We have this ID and the object name over here.
Okay, so here we are just doing if there is an intersection calling the intersection between this,
let me just explain this.
So I'm just I'm saying over here is that you can see that if there is an intersection between this brown
line, you can see over here and this green line, let me show you.
Okay.
So you can see that when this brown line, this brown line intersect with this line, then we can see
an increment like truck goes in the south direction.
So here we are just finding the intersection, just calling the Intersect function, which we have created
above.
And when you see that we have called Cv2 dot line.
So what does it means that when there is an intersection like you can see that.
That when there is a intersection like you can set this brown line.
Intersect with this green line, the green line turns white.
Like you can see that when this brown line intersect with this green line, the green line turns white.
So here we are just doing this.
So when this brown line intersects with the green line, the green line turns white.
So I am just so this is the format of the white color, which I have written over here.
And we are just saying that if the direction of the vehicle is in south, then we will do an increment
over here.
And here we are just drawing the trails over here as well.
And here I'm just setting the counter like, where should I display the counter?
Like, let me tell you, I'm just setting this like this direction will appear here, not direction
will appear here.
So direction will appear here.
West direction will appear here.
So here I'm just setting the different elements, like where I need to put the text of the West direction
or other total count and other things as well.
And this is the class detection prediction of the V8 and just calling all this over here.
So this is all from this.
Let me show you the output.
So we have run the script over here, the predict.py file.
Okay.
So here we also have this output demo video.
So if I just download it.
Okay.
So it might be let me just download it.
Okay.
So I've just downloaded this output video so you can see that as it goes through goes in the north direction.
We have increment.
The truck has gone to the north direction and as this vehicle goes into east direction, we have an
incorrect one car has gone to the east direction and when this goes to the east direction we have increment
like car has also gone to the east direction.
We can see the increment over here.
So in this way all this process works.
I hope you have learned a lot from this video tutorial and see you all in the next video tutorial.
Till then, bye bye.
Have a very good day.