In this lecture, you will learn how to detect human faces.
Specifically, we will write a script which will draw a rectangle to each of these faces in this picture.
So the output will be an image of this image, but with those rectangles overlaid on top to do this,
you need two things.
One is an image file, which has some humans inside with faces which the scripts could detect.
And also, you need this higher cascade file, which is an example format, so you can download these
two files from the luxury sources.
If you don't have them, a whole Cascades file is basically a set of instructions of how faces human
faces to look like.
So Python will use this blueprint of this phase blueprint, and it will use it to search for faces in
an image.
So let's write this script.
We need open service, so see two.
So the first thing we might want to do is loads an image with CV to dots.
I am read humans that JPEG, and so let's load it in color.
Then next, what we want to load is this XML file.
So to do that, you could say something like face cascades.
So it's called the Cascades hard cascade that is equal to see two dots cascades.
Classifier, which gets as input the path to the XML file, so we have an image, we have a face cascades,
what can we do with these two is detect faces.
Using face cascades that detect multi skill, which means that you want to detect faces in different
scales so you could have a small face.
Here you have a big face here.
You know, depending on the depth of the photo and you want to detect everything from that image object,
which we have in here, so image image?
No, it's good to have two other arguments here.
The first one after the image is the scale factor, which usually it's good to have something like one
zero five or one, one of the next factor is the minimum neighbors.
So what is the skill factor and what is minimum neighbors?
Well, what happens when Python detects faces is that its image has pixels.
So the first pixel, the second, the third and so on.
And basically, the program will go through each pixel.
But some phases could be bigger, as I said, and some phases could be smaller.
And so the detection depends on the scale of the image.
So if the algorithm is looking at certain scales at certain zoom, so to see and it has in mind how
a face looks like, if that's Zoom, it could not find one because maybe the face in that level of zooming
is bigger or smaller.
Therefore, the algorithm has to check and check for image multiple times of different scales.
So with this number here, basically you are seeing how much to increase the scale between different
skill checks or the zoom.
Let's say you want to start with a zoom of X, and the algorithm will check for faces there, and then
it will zoom in with a factor of 1.1, so x times 1.1 and so one.
So basically, we have a pyramid of the same images, but in different scales, and then we have four.
Now four is a minimum neighbors, which means there are interpolation between pixels.
So if Python is checking this pixel, for example, a two also consider the neighboring pixels around
it.
So with four, we are saying we're telling Python to consider four pixels around and to note, for example,
more pixels, which should be like here and maybe here and so on relative to that pixel.
So that's more or less that these are the popular numbers to use that have been tested to work well.
But you can try with different numbers and see the results.
Now what is phases?
Let's bring it out and see what we have this far.
So that is the face is variable, it's a list of.
Lists, so one list here, another year.
And so one.
So basically a Nampai array, a 2D Ray.
This contains the coordinates over rectangles, which has faces inside.
So basically, we have one two three four two faces in total, which makes sense because there's a lot
of people in this photo.
So twenty two faces.
And for example, these are the coordinates the pixel coordinates in the image of the rectangle to be
thrown around a face around the first phase and then another face and so on.
What we could do now is to iterate through that number early using a table.
So x y width and height.
What are these in phases, right?
So the typical here.
This table will get each of these lists.
So basically, X will be replaced with nine o one in the first iteration, Y will be replaced two one
eight five W with 60 and H with 60 and so on.
So if we had an image like this and then we would have nine oh, one one eight five, that'd mean something
like that nine hundred to one column, which would be somewhere, he writes.
And the 185 rule, which is somewhere here.
So this one here, this points here would be the start of the rectangle and then we would go 60 pixels
to the right and 60 down.
So we would have something like a square right, 60 here, 60 there.
That would be a rectangle.
And inside here it's supposed to be a face.
So that's the idea we are capturing those numbers from each of these lists, and for each number, we
will draw a rectangle using the Siri 2.0 rectangle methods, which gets the image object as first inputs.
So image this one in here.
We will draw those rectangles in that image and then a second argument.
It will give the coordinates of the start of the rectangle, which is X or Y.
As I explained to the corner, then you have X plus w and Y plus height.
So H.
So what are these?
Well, that is the coordinates of the first corner of the rectangle.
That one.
And then we say X Plus, which.
Which takes us somewhere here.
And then why plus age, so it takes us at this point here.
So basically, we are writing, we are drawing a rectangle using these two points as points to define
that rectangle.
That's what this rectangle methods expects.
The two corners, the coordinates of those two corners.
Right.
It also gets two more arguments.
One is the color if you want to have a rectangle.
So why do you want to give 255 red and to 55 green at 255 blue and then the width, perhaps for some
of the width of the rectangle sides?
Then once you write all the rectangles, so all these rectangles in the image, then you want to say
to the spine or sides and the right that image actually.
First, you need to specify the name of the file of the new image.
So let's say human faces, that's g jpeg and image or image file object, which now has the rectangles
in.
So it's one single image and the run.
And here is the generated image, let's open it.
And this is the ultimate.
So as far as I can see, it has done quite a good job.
Even though this face here is blurred, the script still detected.
It's untrue and drew a rectangle around it.
This one, too, even though it's also sort of a profile, it's not front face.
This one was successful also, even though the person has glasses, and it can also detect people with
hats.
And sometimes the script also thinks that a fist is a face, as you see in here.
So, of course, it's not 100 percent accurate, but it's still doing a great job.
I think this face here is also a half face.
So the script couldn't detect it as a face.
I mean, that's could be good, but it could also be a bad thing for certain scenarios where you don't
want to detect health faces.
So that's the reality.
I hope you enjoy this, and I'll talk to you later.