Hello everyone.
In this video tutorial we will see how we can implement vehicle counting using YOLO V8 and Deepsort
object tracking.
So this is the output we get when we run YOLO.
When we integrate object tracking using Deepsort with YOLO V8, which we have done in the previous lectures.
So let me show you the output.
So this is the output we get when we integrate YOLO object tracking with YOLO V8.
So you can see that we have truck over here, we have cars over here.
Okay, so now we need to count the vehicles.
Currently you can see that we are getting a unique ID with each of the object and the trails as well.
Okay.
But in this lecture, our goal is to count each of the vehicle.
For example, one, two, three, four, five, six, seven, eight.
So I need to count the number of vehicles passing this line and the number of vehicles going over here
as well.
So I need to count the total number of vehicles which passed this line in the video.
Okay.
So to count the number of vehicles in a video or if you want to count any object in a video, so how
you can do this.
So first of all, let me save a photo from this video.
Then I will try to explain you the concept that how we will do this.
So it will save a photo from the video.
So, okay, click on Save a Photo.
And then it will just in test three, dash one.
So now we'll go to the pictures folder.
Okay.
So.
Let's go to the pictures folder and it's about test three moment.
And let me open paint or open with.
And so let us open with paint.
Okay.
So basically.
And if we want to count the number of vehicles in this scenario.
So first I will what I will do first of all, I will just create a line over here.
You can see that I will just create a line over here and I will say that whenever the vehicle crosses
this line, so whenever any vehicle crosses this line from here.
And from here as well on both of the sides, when the vehicle crosses this line, like vehicles coming
from here and cross this line and the vehicle coming from here, cross this line, I will increment
the count like car number of cars.
For example, Three cars have passed.
Four have passed have passed this line or and when the truck passes this line, similarly, I will increment
the count like that.
Like that truck has passed.
Five trucks have passed.
Six trucks have passed.
What we will do in this case is like you can see that this is the center of the bottom, but basically
center coordinate of the bottom of the bounding box.
Can you see this over here?
So when this center coordinate like this one.
See the center coordinate of the bottom of the bounding box.
Okay.
So when this center point crosses this line, like this black line is crossed by this center coordinate
of the bottom of the bounding box.
Then we will increment the counter like three cars have passed.
Four cars has passed like.
And the truck, three trucks has passed.
Five trucks have passed.
So when this center coordinate of the bottom of the bounding box crosses this line, then we will increment
the counter like this.
Coordinate crosses this line.
Okay.
So.
For this.
We need to find the intersection between this line and the sorted center coordinate.
So we need to find the intersection between this line, this black line and the center coordinate.
Okay.
So first of all, what we will do is, first of all, I will go to this repo.
Yolo, Deepsort object tracking repo.
And here I have created a folder for this project by the name recounting your tutorial.
So I will open PyCharm.
You can choose any other ID as per your choice.
So I will open PyCharm.
Suck my dick.
Two seconds.
Okay.
So it will it is preparing the working space workspace.
So it is about to ready.
Okay.
So as it gets ready, then we will start.
Okay.
So I will go to file.
I will click one file and create a new project.
A new project and then I will go to here and select the folder first.
So we add course vehicle contain, and here we have the folder which we have created.
Name of the folder is vehicle coding tutorial.
Okay, just refresh it.
Okay.
This is the.
Folder which we have created for this project.
Select this.
And you can see our vehicle continue the tutorial.
We will continue the tutorial.
Then we'll create a new project, select this window, and now the new project is created.
First of all, I will open the terminal.
So just open the terminal over here.
Pockets, creating a virtual environment.
So it might take some few seconds more.
Okay, so just open the terminal over here.
And just go to this repo.
And just follow the steps run code.
So just copy this step and just paste this over here and click on Enter.
So in order to clone the repo over here.
It might take two seconds to for the completed report to be blown over here.
So we will wait for a few seconds until it gets completely blown.
It's the 1% in law.
What it will execute much faster shortly, like you can say 67% now.
And now it will go to 85 and then onward 75.
We'll now eat this stool and then it will go to 99 and 100.
For canceling that episode.
Now we need to set the current directory as this current repo.
I have already explained these things in the previous.
Okay.
I think I've doubled this, but I've written this.
Multiple times.
So now I am installing all the dependencies, which are the requirements as well.
So just copy and press enter.
So now all the dependencies will be installed.
So it means dependencies are the required libraries required to run the code.
So this might take few seconds.
So.
As we are going to perform the detection over here.
So I will go to ultralytics, YOLO V8 and select the grand directory as my detection folder.
So just have just copy the command from here.
And now my current directory is set with the detection folder.
Okay.
So now we are downloading that.
We will download the deepsort files from the Google Drive.
You can just copy this link and just download the files from here.
So I have already downloaded this file, so I will just go over here.
Just copy this zip file and just come here.
This is my folder.
Unzip it.
Now.
Okay, so just unzip it now and after unzipping go to here, copy this folder and just go to Radix YOLO
v eight detect and just paste this folder over here.
Okay, so now basically we are implementing object tracking using Deepsort.
So we need to have the Deepsort code integrated with the YOLO V8.
Okay.
Now we will download a demo video from Google Drive.
So basically in which we will test our model.
So, okay.
I think there were mistakenly just copy this.
Angus incident Press Enter now.
No demo video will be downloaded from the Google Drive on which we will test our model.
Next is to basically run the python predict.py model.
So if you run this script it will basically perform the object tracking.
Okay.
But I need to explain the concept of vehicle counting.
So let's me just open the predict.py script and add the vehicle counting code and then we will run this.
Scripts so that we can in the output, we can not only see the object tracking, but also we can see
that a vehicle counting as well.
So the first thing you need to do is basically we need to just go over here basically.
First thing we need to create a line.
So we need to find the coordinates on which we need to create the line.
So.
You can see over here this when just pointer please see this point where my current mouse is.
Please focus on this point.
Okay.
So currently the value at this point is 249 438.
Okay.
Average values to 14 to 54, 41 to 5441.
So now I will just create a line over here.
I will write line is equal to.
Okay.
So the point was, I'm just again forgetting it's 250 to 250 and 441.
Okay.
So it's two 5441 and the next end point is where the line end is.
So the value at this point is 1452.
Okay.
1452.
Just write it here.
1000.
452 over here.
Okay, So this is the line which we need to create when the basically when the.
So this is the line we need to create.
Basically, when this center coordinate intersect this this line.
So we can say that the car has passed and we can we can increment the counter.
Okay.
So first then we need to create a dictionary by the name of counter.
We will store the count.
So object counter is equal to basically empty dictionary currently.
So in this basic in object counter, we will store the total count of each vehicle.
Okay, guys.
So next we will go ahead.
And now what we need to do is basically we need to basically write a function for the intersection.
Okay?
So basically now we need to write the function for the intersection, like when these this center coordinate
intersect with this line.
We need to increment the counter of the vehicle.
So.
You need to write the intersection function over here.
So basically, let me show you one thing.
Just go to here.
I have already worked on it.
So here, just open this with paint and so.
Guys when these two lines intersect.
So to represent in the python code form, we usually have this.
Rewrite this function.
Basically, we are calling CCW inside this Intersect function.
So we require this function.
Okay, so.
And again, I'm telling you basically in Python to write the intersection, to find the intersection
between two points, two lines in Python, to find the intersection between two lines, see line one
and this is line two.
We usually write this python code.
It will returns true if the line segment of line line, segment, AB and CD intersect.
For example, this is like a line segment.
AB and C this is a line segment.
CD So this function returns true if the line segment AB and CD intersect.
Two So I will write this function.
Over here.
Just now write down this mansion and just over here.
So first we will write, define, Intersect.
Eight comma B, comma C, comma d, and just enter the column here and then will return a CCW.
Basically, we will create a function after this.
And here will basically I'm passing all the four points one three by three four.
Okay, So just write a single command over here and then.
Yes.
Right.
This.
C equals C comma D over here and then write a CCW.
Then I will write a comma, B, comma, C over here, and then I will write a CCW.
Then I think about CCW and a comma B, comma.
D over here.
Okay, so we can see I have just spelled A and over here, so it's now fine.
We have a comma, C, comma, D, B, comma, C, comma d, and then we have a, b, c, a b, d.
Okay, that's fine.
So now we need to create this CCW function.
So to create this CCW function, just write to the CCW, a comma, B, comma, C, and just I've just
mistakenly.
And then press enter and return.
Basically, I'm just writing down this function, which I have shown you over here.
I'm just writing down these two functions over here.
Okay, so return C one.
Minus.
Well, I have written this function.
So here we have the Intersect and here is the CCW function.
So now we'll go down and over here.
So, okay, so just now what we need to do is we need to write if then that.
These.
Great.
The minute I will explain to you what I am doing currently over here.
So if you see this image.
So you can see that here.
This is a center coordinate.
Basically, we if you can see this center coordinate length.
So this trail length is Max.
We we have set that that maximum length of trails should be 64.
Okay.
So that these trails, maximum length is 64.
So how these trails are calculated, basically, we append the center, coordinate values into the data
dictionary, like the center, coordinate values of each of the frame of each of the frame are appended
to the data dictionary.
So when these center cabinet lengths increases, this trails length increases.
Okay, so now we are saying that we should at least have two values of the center coordinate in that
dictionary.
Okay.
That is the dictionary.
And it should contain at least two values of the center coordinate.
So when there are at least two values of the center coordinate, then we can then the object can intersect
with the line with this line.
And with this line and then we can have an increment in the counter.
Okay, so just write if intersect.
Now I'm just calling this function over here, and this is a data first value just passing.
And now I'm passing the second value of data over here.
Okay, then I'm passing the.
Then I am over here.
I will post at this point of the line and next I will pass this point of the line.
So just passing the.
X1Y1 point of the line.
Okay.
And now I will pass the other point of the line over here.
Okay, that's good.
So.
What I want to do is so when this when the center coordinates, I want when the center coordinates intersect
with this line, the color of this black line should change to white.
Okay, So for this I will write cv2 dot line.
Image.
And then line zero.
Walden line one just passing these venues and what color we want is.
While sewing the white color is provided as two over five, two over five.
You can check this as well and the line thickness should be three.
Okay.
The next we will do is.
So now we want to we will append each of the detected object into a dictionary.
So when the.
So let me show you again.
So.
So each of the detected object will be appended into this when the.
Let me explain a better way.
So when this object crosses intersect with this line, okay, so the object will be saved in this dictionary,
which is name is object counter.
So when the object of the same name appears again, it will automatically increment like two cars have
passed.
Three cars have passed.
Okay, so just doing this now.
And I am doing this over here.
Okay, So if you check.
Is more in.
Object counter.
Okay, So basically, I'm trying.
Object.
Boundary which we have created here.
Okay, so then we have object name.
Just close this and just click over here.
Then we can say object counter is equal to one.
Then ends.
Object.
Under.
Mass is equal to one.
Okay, So this is fine.
Now we will say that object counter is if the object name is there, not in object counter, we can
say that object counter is equal to one else.
Object counter plus is equal to one.
Now I want to display this count in like, I want to display the count of each of the vicar over here.
Okay, So for this, let's write down the script.
So I will write.
Oh.
ADX.
Let me write down.
So just.
Okay, so I'm here acting for x, comma, comma value.
And a.
Like now we write object counter.
Darn.
I don't.
Okay.
So then I will just check all the all here then.
Kone's frame is equal to.
SGI.
And I'm read this and then I like.
Just to add the underscore doctor as a right here this process.
Then I would write cv2 dot line.
So, Mr. Game, just creating a line right here.
It could be at or a thick line.
We're just setting the coordinates.
Hold the line where the total width -150 distinguished in the top right corner.
What, five?
Idexx.
40.
Then I will ride their spine completely.
Then what I will do is.
Okay.
That's okay.
Now we have defined the width as well.
And in the next step I will define.
Where this line will end.
The end point of this line is point 25.
Last.
It.
X was 40.
And then I have now defined the endpoint of this line.
Okay.
So now the color of line we need to define over here, which is 85.
45.
You can choose any color of the line.
I'm just randomly assigning this color and the line thickness should be 30, should be a very big line.
Then I will write cv2 dot, put text over here and then I will write image count string and then I will
write this.
Now I'm just adjusting my text into on this line where.
-150.
Rate -150.
My 35.
35 miles.
80 Cross 40.
And let's finish this over here.
Then I will write zero comma one.
And then I will write the color of the text and it should be like white color.
And the thickness of the text should be two.
And for the line type, I'm just adjusting line type is equal to.
Cv2 dot zero.
Notice here we don't have the option line.
Okay, so I have done this from my side completely.
Let us run this script and see if there are any errors.
Then we can correct it shortly.
Okay.
Just giving a look towards the test once.
So now we will just run this script.
So let's go over here.
This goes in this.
And just copy this and just place it and press enter.
So we want to show the output that the runtime.
So let's see what it goes.
So it might take some time to execute.
If there is no error in case of error, it will generate an error.
So then we need to fix this error by.
I am hopeful that it will work.
But let's see.
What does it cost?
So it can be downloading the V8 model.
For the size of the model is 83.7.
So quite large size.
So it will take some time as well.
So plus I'm running on or on CPU, so this will usually take more time than normal because on Google
CoLab we have the free GPU available, but so the processing becomes very fast.
But in that case don't have the GPU available so the things might get slow.
But as it has started for the first epoch, it's fine.
Okay.
So I think there is an error.
Let's see what the error is.
Then we can fix it.
Toward the end was a spacing issue over here.
I just corrected it and just tested this script.
It works fine.
Let me show you the output as well.
So I have just run the script.
Python predict.py model v8.pt and it will take some time to execute.
Okay.
So now it has started.
So I have set show is equal to true.
So we have the output in the runtime so we can see the line currently.
Okay.
So here is the line which we have drawn.
Okay.
So.
This is the object which is going to pass over here.
Let's see if it does count or not.
Okay.
So that was important that there should be the count because we need our model to.
Found as when like the when this so you can now please see this carefully when this line line this point
this straight intersect with this line then we'll see the count.
Okay, Let's see if there is a count or not.
There is an error and then we will fix this.
So when does testing on CPU?
So you can imagine that the processing will be very slow.
If you have a GPU, then this is great.
You can test your model on the GPU.
Well, okay.
So whether this will intersect this point or not, let's.
Okay.
I think it might miss because it's going to dock.
Okay, let's see.
No, it will intersect shortly.
Okay.
I'm going to see an increment.
Okay.
So you can see that as this point intersect this line, we see a count.
So when this car crosses this line, we will see the count as well.
Okay.
Let me show you other detecting.
Not correctly the truck, but if you try with the YOLO V8 model, it will definitely detect it correctly.
So let us see when the car crosses, what does it show us?
Let's wait when the car crosses this line.
So it might take a few seconds more.
So let's wait for the guard to cross.
So.
And just waiting for the car to cross.
And then we will move towards the next video tutorial in which we will be adding the speed estimation.
Into it.
So just waiting for the car.
To cross this line.
So.
Uh, we can better see that the is coming from here.
Are also incrementing the count over here.
So it might take a few more seconds, so.
And because I'm running a CPO, so the processing will definitely be slowed.
In the next tutorial, I will add the speed estimation code in this script.
So and then we will continue.
From here, so they will not be much changing.
So I will just add the speed estimation code in this script and no other thing will be changed.
Okay.
So.
And it's just going to happen now so we can see over here.
Like you can see now.
Now this trail intersect this.
Okay, So the color should change.
Okay.
So you can see the car counter here as well.
So in this way, when this car pass, the car count will increase to two.
When this car pass, the car count will increase to two.
So in this way, all this process is happening.
So I hope you have understood the main concept over here.
See you all in the next video tutorial in the speed estimation.
And then.
Bye bye.