Well here is that train.py script.
That train.py script contains the convolutional neural network model.
I trained the convolution neural model on the face images of men and women.
So let me show you the dataset images as well.
So in front of you you can see the men and women folder.
So both these folders contain this folder, contain the men images, and in the woman images I have
the women images face images of women and in the men folder I have the face images of men.
So I train the convolution neural network model on the face images of men and women.
So this is the architecture of the convolution neural network model, which you can see over here.
The the convolution neural network consists of multiple layers of convolution 2D layers.
You can see over here we have the batch normalization, we have the max pooling 2D layers, we have
the flatten layer, we have the dense layer as well.
And I trained this convolution neural network model on 100 epochs, which you can see over here.
I will not go into the training as again, but after training the convolution neural network model,
I saved the model into my text by the name gender detection dot model.
So I will use this train gender detection model of in when after doing the face detection, I will use
this gender detection model to classify whether it is a man or woman.
So for face detection, I will use YOLO V8 and to classify whether it is a man and woman, I will use
convolution this gender detection model which is trained on the images of men and women.
Or okay, so here is the gender detection model.
After saving and here I have the training loss, validation, loss, training, accuracy and validation
accuracy.
So you can see that the results are fine and we are getting a very fine results as well.
And it's ready to do the gender classification as well.
Okay.
So you can do the training as well if you want, but I'm not doing the training again because it will
the video will become very lengthy.
So let's move towards the CoLab script in which we will first see how we can train the YOLO V8 model
for the face detection.
Then we will use this gender detection model after doing the face detection to classify whether it is
a man or woman.
And then we will further write the script for the crowd counting and see how we can generate an alert
system when when there is an overcrowd like more than five people.
But you can set it at like more more than 100 people appear in a frame.
Then we can generate an alert that is overcrowded.
Okay, so let's move towards the YOLO script and see how we can train the YOLO model on the face for
the face detection.
Well, not.
We will see how we can fine tune your V8 model for face detection.
So for face detection I will be using this face images dataset which is available publicly on roboflow.
So here we have around 1369 images of faces and we have only one class over here which is of face.
So we will use using this dataset to detect the faces in the video or in an image.
So you can see that the dataset consists of only one class, which is face, and we have around 1369
images in our dataset.
But after doing the augmentation, the dataset images in the training set increased to 2900.
And in the validation set we have the 267 images and in the train testing set we have around 145 images.
So first of all, we will be using this dataset to detect the faces.
So I will fine tune the YOLO V8 model using this dataset.
And so after fine tuning the YOLO V8 model on this dataset, my model will be able to detect the faces.
So let's first overview this complete process in the Google CoLab notebook.
So here we have the Google CoLab notebook.
So first of all, please make sure that you select the runtime as GPU.
I've already run this script, so first of all, I will leave, disconnect and delete the runtime so
all my previous files are lost now.
So I will now start from the start and explain you the all the process one by one.
And thus first I will select the runtime as GPU.
Okay.
Then i will import that from ipython dot display import image.
Basically I'm using this library to import any or to import the image library.
I'm basically use the image library when I want to display any input or output image into our Google
CoLab notebook.
So to display input or output image into the Google CoLab notebook, we use the image library.
Okay, so in the next step I will download some required files of YOLO V8 from the Google Drive.
So I have placed the files in the form of zip format into my Google Drive, so I'm just downloading
the files from my drive.
So these are the files necessary to run the YOLO script.
So here we have the unzipped folder of all these files based detection, YOLO V8.
So here I've just downloaded the required files from my Google Drive into the Google CoLab notebook.
Then I want to set this downloaded folder as my current directory.
So I'm just setting this folder into my as my current directory.
Then we need to install all the required libraries necessary to run this project.
So in this step I'll install all the dependencies.
So it will install all the required libraries which you can see over here like matplotlib numpy torch
or seaborne hydra.
So it is necessary to run this cell because if you don't run this cell or skip this step, then you
might face this error.
Then the following libraries is not installed or some libraries not installed.
So it's always necessary to run this cell so that all the required packages gets installed.
Okay, so here we have the Jinja2 library and all the libraries, so it's necessary to install all the
required libraries first.
So as here we are just doing the detection, face detection.
So we will go to ultralytics, YOLO V8 and go to the detect folder and set this folder as our current
directory because here we have the prediction training validation.py file.
So just go to here, click on copy path and set this folder as our current directory.
Okay.
So in the next step, what we need to do is we need to export this data set from Roboflow into our Google
CoLab notebook.
So just click on download.
So here, just go to select the YOLO V5, PyTorch or YOLO V8 format.
Basically Yolov5, PyTorch and YOLO V8 both were released from Roboflow so you can select any of the
format.
Then just click here and click on copy and just paste this over here.
So in this way you can download the dataset from Roboflow into your Google CoLab notebook, but it's
necessity that you make an account on Roboflow as well.
Like you can see here, I have created an account on Roboflow by the name my name, so you can go to
sign up and create your account on Roboflow as well.
Then you can easily access this dataset and download this dataset from Roboflow into your Google CoLab
notebook as well.
So the dataset is being downloaded over here.
So it might take a few seconds.
Okay.
So you can see that it's currently in the zip format.
So after this, it will automatically unzip and we can see these files as well.
Okay.
So, okay, so it's 75%.
It's 77%.
It's 81.
86%.
There's 95% and okay, so it's 100% as well.
So the data set is completely downloaded.
And now here we can see the face detection dataset.
Here we have the train test validation files, script file, validation folder, train test and validation
folders, which contains the images and labels folder as well.
Okay, so here we have the data dot HTML file which contains the number of classes.
We have only one class, which is space, and here we have the path for the train test and validation
folders.
Okay, so now in the next step we need to train the custom model.
I have already done the training so I will not train the model again.
So you can see that I have trained the model for eight epochs, so I will not train the model again.
So here we have the training results.
So you can see that, uh, that we got the mean average precision with IOU 50 as 0.904 and the mean
average precision with 5295 is 0.585.
So we are getting fine results like the mean average precision with 50 is 90.4% and mean average precision
with IOU, 50 to 95 is 58.5%.
Okay.
So after training, I have saved the weights of the best weights of my model on the Google Drive.
So I will just download those weights from the Google Drive into the Google CoLab notebook.
So you it is always better to save the weights because you can require those weights after this as well.
So I always save the weights into my Google Drive so I can just directly download those weights from
the Google drive into my Google CoLab notebook whenever it is required.
So just now you can see the weights file over here as well.
So I'll just refresh it.
And here you can see the best the weight file as well.
So now you can validate this custom model as well, or you can validate that train model as well.
Like you can see here, I've passed the path of the best dot PDF file, so I'm just validating my model
on the test images.
I have not used the test data, set images for that training, so it's always better to have a look
and see how what, how our model is performing on the test images.
So these are the test images on which I'm talking about.
So I'm just testing my model on these test images.
Okay.
So let's see.
What results do we get over here?
Okay, so what?
See what mean average precision we get.
So the mean average precision with 50 we get is 91.2% and mean average precision with 52.
95 is 58.9%.
Okay.
So here we are also performing the object tracking using Deepsort.
Like for each of the detected object, we will assign a unique ID to each of the detected object.
Like Let me show you what I am talking about so you can have a more better understanding for it as well.
So let me just share my screen and explain this.
What basically I'm talking.
So now you can see over here like you can see that.
Let me just stop.
So for each of the detected object, like you can see the man face, we have a unique ID 192 over here.
Okay.
Like for now, for here, man as well.
We have a unique ID 193 over here.
Now for here.
WOMAN We have a unique ID of 94 over here.
Now for here.
MAN We have a unique ID of 195 as well.
So you can see here we have a unique ID of 208.
So using object tracking, we assign a unique ID to each of the detected object like or each of the
detected face over here.
So whether it is a man face or a woman face, we assign a unique ID like 205, 154 over here, 121 over
here.
So using object tracking, we assign a basically unique ID to each of the detected object so we can
have save the record for that as well.
Okay.
So to do a object tracking here, we will be using deepsort for the object tracking.
So first of all, we will download a deepsort file from the Google Drive and unzip those files.
Okay, so I've downloaded the files or deepsort files from the Google Drive and unzip those files.
So here we have the Deepsort PyTorch folder.
So I will use deepsort for the object tracking.
Okay, so now I'm in the next step.
I showed you that I have saved the gender detection model, which I showed you over here.
So let me just show it here.
So I have saved this gender detection model on on basically, I have trained a convolution neural network
model on the image dataset of men and woman faces.
So after this, I saved this gender detection model.
So I will just use this gender detection model, which you can see here.
Okay?
So I just save this as gender detection model over here.
Like you can see gender detection model.
The gender detection model is trained on the images of men and woman.
So gender detection model is basically a convolution neural network model, which is trained on the
images of men and women.
Okay.
So.
I'm just downloading have the gender detection model on the drive.
So I'm just downloading this model from the drive into the Google CoLab notebook over here.
Okay, so this might take two seconds.
Okay, so the model is downloaded.
It's around one and a four MB.
So let me now explain you the prediction dot pi script as well.
So I'm just opening the prediction dot pi script over here and let me explain you the whole process
flow.
Okay.
So basically let me download the updated prediction dot pi script so that, so here I will download
some demo videos to test my fine tune V8 model and I'm just download the updated prediction dot pi script
and I will explain you the complete script file as well.
Okay.
So this might take a few seconds.
So let's wait.
And as it gets downloaded, I will explain you the complete process.
So here we have the updated prediction dot pie script.
I have updated this prediction dot pie script as per this project.
So here you can see the prediction dot pie script.
So let me explain you this complete code here as well.
Well, here we have the code.
I will only explain the code that is not a part of the official repository and that which I have added
in the script.
So if we go beyond down over here.
We can see that I have created a dictionary by the name founded dash classes to store the information
about the detected items.
So to store the information about the detected items.
I have created a dictionary by the name founded dash classes.
Then we have a variable and over here the variable n basically contain the total count of data for the
detections per class.
Like here we have only one class which is face like.
I'm only detecting the face.
So in a frame, for example, I have detected five faces.
Okay, so the variable n will contain the total count for the face detected.
So if in a frame five faces are detected, the variable n value will be five.
And here we have the now we have the class index variable, which is equal to an integer C.
So the class basically this C represents the class ID, okay?
In this C variable, we have the class ID, So in this case we have only one class as we are detecting
only face.
So face is the only class and its class ID will be zero.
Okay.
So the class index contain the class ID of the detected object.
Or you can say the the class ID of the detected object.
So here we have only one class which is face.
So the class ID will be zero and the class ID is saved in the variable class index.
Then we have the variable name count of object object, count of object in is equal to integer.
And so the total count is saved as an integer called count of object.
So the variable n contain the total count of the detected object.
So here we have only one object object which we are detecting, which is face.
So the if we have detected five faces in a frame, then the count of object will be five.
If we have detected ten faces in a frame, then the count of object will be is equal to five.
Okay, So we have already explained this to you over here.
And so we have the total count is saved in an integer called count of object.
So in this variable count of object, we are saving the total count of the detected object in a frame.
Okay.
So then we have the founded class.
We are just calling the founded class dictionary over here.
So in the founded class dictionary I am just saving in the so dictionary contains a key variable and
a value.
Okay.
So in the key variable we have the object name detected object name.
So here we are only detecting the face.
So the key variable will contain the face and in the integer n is basically a total count.
So in the value.
So as the dictionary contains two values, one is the key and other is the value variable.
So in dictionary we have two variables or you can say two things like one is the key and other is the
value.
So key is basically the object name and the value is the total count of the object.
So in the key variable we have the face and in a frame is the face is detected five times, so the value
variable will be five.
Okay.
So then we are using a calling a function by the name count.
So I have created this function over here.
So in the count function I am just setting the like where should be the total count should appear.
Let me show you.
So if we go to this video over here so you can see that here we can see face is equal to 26.
So in in this basically in this count function we are setting the that.
In the top right corner.
And in the top right corner, we will have this a rounded rectangle like this.
And the first item in the rounded rectangle is place then is equal to the number of detected faces in
the frame.
Okay, so we are just setting the in the count variable like where where the total count of faces should
appear in the output video.
Okay.
So in the count function we are just setting the UI.
So in this count function we are just setting the UI like the where in the output video, the total
count will appear.
Okay.
So.
So I'm just using the count function to set that like in the.
Output video where showed the total count of like face and this total count will appear.
So let me go back to the script as well.
So here you can see the count function.
And in this count function, we are just.
Setting the UI like the total count of the detected object.
Where should it appear on the output video?
Okay, so here I've just created a class by the name gender classifier.
Okay, so here I'm just pre-processing the image because we are reading the video frame by frame.
So here I'm just processing the first frame and then I'm just loading the weights of the model, like
gender classification dot model.
So I'm just loading this model with gender detection dot model over here.
And then I'm doing the prediction over here where there is a man or woman face.
Okay.
The other thing which have done over here is if the count of the object like if the like here we have
the total count of the object in each of the frames.
So if in a frame more than five phases are detected, then the text will appear as overcrowded.
And here I'm just setting the UI like where the overcrowded will appear.
Let me show you.
So if I go to the video again so you can see here.
So here I'm just setting the, like, overcrowded, appearing over here.
And if you can see that currently we have 26 spaces.
So it's, uh, so here it is appearing that it's overcrowded, so.
Okay, so you can set it as per your requirement.
Like if they are more than 100 faces, then you can set that it is overcrowded.
Okay.
But I'm saying that if they are more than five detected faces in a class in a frame, sorry if more
than five faces are detected in a single frame, then the text like overcrowded should appear and it
should appear in the center like I'm showing over here.
Okay.
So you can set it like 111 50 faces appear in a frame in a single frame.
Then you can set the text as overcrowded.
And here I'm just setting the UI like where should the text will be appear in the output video.
Okay, so here we have the output video.
So basically, let me run this script now.
Okay.
So if you can run this script on any demo video as well.
So let me run this script on some demo video.
So let me run the script on this sample one demo video and show you the results.
What results do we get?
Okay, so.
This might take some time for the script to execute over here.
So as it starts, I will just pause the video and when this processing completes, I will then just
be back.
Okay, so now you can see that we are detecting three, two, three, two face.
So it might take some time.
So let me just run this script and complete the processing of this on this video and just get back when
this is done.
Okay?
The processing on the video is done.
And here is our output demo video.
Let me download this video and see what results do we get.
So I'm just downloading this video and then will like me get my screen to watch this video and show
you what results we are getting.
Okay.
So just to navigate, let me navigate my screen towards this video which I have opened over here.
Just give me a minute.
Just a minute, please.
Okay.
So just navigating my screen to watch this video at.
So here is the output video.
You can see you on your screen.
Let me play it so you can see that the face count is less around 5 to 3.
So as it goes to five, we will see that our coded.
So you can see over here.
So let me show you as well.
Again, you can see that as it reaches five, we have this.
Okay.
So you can see that as the face count increases five.
So we have the signal or the arm as overcrowded.
Okay.
So you can see the signal as the face count increases.
Why?
We have the signal as the overcrowded over here, you can see.
Okay.
So.
This is all.
So let's test on some other demo video as well.
Let me just go back and okay, so I'm done doing processing on other the demo video as well.
Demo Video three So as it completed, I will share my screen and see what results do we get.
Okay, so it's currently 198 out of 355 so it might take a few seconds more.
So let me get back as it is complete.
Bioprocessing on the demo video three is also done.
Let me just download this video and show you what results do we get over here.
So here we have the output video.
Let me just download it and show you what results we are actually getting.
So the video is downloaded.
Let me just navigate my screen towards this output video and show you so you can see that this is our
output video.
You can see that as the count increases, the quantity increases to five from five.
So we can see the signal as overcrowded.
But if they are less than five, so we don't have the alert of overcrowded.
But as the phase count increases from five, we have the signal as overcrowded, which you can see over
here.
So this is all from this video tutorial.
I hope you have learned a lot from this video tutorial.
See you all in the next video tutorial.
Till then, bye bye.