In this lecture, you will learn how to find images that contain human faces.
So I have this ripple here.
The code is from the previous lecture, so this code detects and throws a rectangle around faces in
one single image.
What we will do now is we will write a script which will go through this input folder, which contains
four images.
This one which don't have faces.
This one also this image, which contains a face.
And this one also which contains two faces.
So two of them contains faces, and our scripts will check this folder.
And then if it finds images with faces in them, it will draw a rectangle in those images around the
face, and it will move those images to the output folder, which is empty.
For now, let's do this since our script is getting bigger now.
I would like to do some quick changes, such as defining cost and values in our script, which is,
for example, the Cascades file path.
And that is faces that example, right?
That is the hard category that we use, and it's located here in my folder now.
The path sort of the script will use that as instructions, as a blueprint to find faces and then what
constants we're going to have else inputs folder, perhaps.
Eagles to input.
So this folder and also what puts.
Folder equals soup output, why I'm writing this in capital letters, you ask.
Well, that's a good practice.
That's a convention.
You don't have to use capital letters, but when you write variables that are constants which are going
to be used in the scripts.
So we don't plan to change these views.
For example, input folder may be used by several instances in our scripts here.
So that's convention the naming.
So in face cascades, now we can just see Cascade and let's see what we can change from this code.
This is finding faces in one single image.
So we want to find faces, so we want to check if a phase is in an image.
Therefore, I think we could start with a function such as has phase right, has phase could be a function
which would receive one single image path, image, file name and return the same image with rectangles
thrown around the faces in that image.
So this function will return of the ends and image rights, and it will the return of the image if the
image has faces inside.
Otherwise, it will return none.
So then down here, then ignore these for now.
Down here, we would do something like images, eagles to always dots list their right.
So 11 imports imports OS and the folder is Inputs folder.
So we create a list of images and then we iterate over those image file names, such as for image in
images.
And then we create a face image object, which is equal to has face to face and image as the file name
correct.
So has face is called in here.
And as I told you, as face, we will write some codes here, of course.
But basically this will return the image object.
And so we grab that image objects in here.
But sometimes that image object will be just a value, right?
So if there's no faces, we will return none.
Therefore, we can check something like if face image is not none.
The non special object, then we write that image objects into a file.
Inside output folder.
Slash.
Image.
So images, the file name that therefore we're going to see, right, that image in output slash the
image name, such as, for example, air air not be written there because it doesn't have faces, but
garden the jet pack.
That's the structure more or less.
Now what's left to do is just implement this function algorithm.
So now we could just take this code here that we had from the previous video and just paste it inside
the function.
Be careful with an indentation from there to there.
You want to press tab to indent everything, right?
So this is what we do.
Has face expects an image file name.
So let's say image path.
So air, the jetpack is going to go here.
Also, galaxy in that jetpack is going to go here.
So we get that Galaxy J pack and we provide that to, I'm reads.
So image path, right?
A color image, correct?
Face cascades is that.
That's correct.
Face is the take to the face is from image from that image object.
So don't confuse image with this image path.
That's just the string, which represents the file name.
This image here is the actual key to open.
See the object right.
We get the faces to the mattresses.
We can print them out just to see them on the terminal.
And then we iterate in each of those many faces, one face or multiple faces or nothing in that image.
And so see two rectangle draw a rectangle.
That's OK.
You could also get this to just copy it from there and create a variable out of that, such as color.
So that's a color, right?
Equal to a vegetable and then in here you see color.
Four is the width.
So with equals two for the full replace that with width, color width.
We could also do the same with that's one point one suit.
Scale Eagles to 1.1 scale here and four wars minimum neighbors.
Just neighbors is enough.
That was for all right now.
We need to add more codes here to change some details.
But I do want to just keep adding code.
I want to run the script now so that you see what is going on and we take things step by step.
We fix the errors if there are any and so on.
So I just ran the script.
Right.
That's the first error image, empty in function, I am right in line 23, line 23 is this one in here?
So I'm right, this has no place here because we are writing that in this function.
Therefore, we want to just delete that and actually, we want to return the image here, right?
So image?
This function returns.
The image object of this gets that image object that is variable, and we check it if it's not none
and writes it.
So let's run again.
OK, we have four empty tables, so the faces here are completely empty.
Now this is not an error.
It's just some output and it's sort of an empty output that makes it harder to troubleshoot the issue.
And therefore you we want to use your logic now.
There isn't a problem with open civvy.
That's when you read an image which doesn't exist.
The item reads Function will just give you an empty array so you can see you can print out image here
and to run and you're going to see that you get none.
So it's not a match, but it's not an error, either.
So that means we're not reading any images here because you see that face image.
His face is just getting an image file name and this image file name is just.
That's galaxy the jet pack, but Galaxy the jet pack is inside this folder, so we can fix that by providing
the full path through an f string.
So like, that's.
And another place holder here, a slash between inputs folder slash image.
That should give us some different altitude now.
Yeah, yeah, so we got the images printed out as you see the mattresses.
And then we got stuck somewhere else.
So I'm right.
Missing record argument.
I am sure it's missing the image.
I'm right here.
Yeah.
So we only provided that path.
Therefore, let's verify the image, which is not image, but the face image.
So this image here could be a bit more clear.
We could say image path four image path images.
So it's explicitly declare that this is an image we know now that this is an image path, not an image
object image path there and image path here as well.
So famous image is the object.
Image path is just a stream.
Let's run again.
OK, so we're getting something printed out.
You see, no God and the JPEG is printed in here and then faces of God and the JPEG are printed afterwards.
Then if we look up working, the pack also has these faces and also air the jet pack so outs, remove
this print image here and just print the face is to see faces are printed out.
So that we don't get all these mattresses.
So I just run again, and, you know, now it's more compact of the output.
You see, Galaxy J Pack has these faces, so an empty table working, the jetpack has two faces.
You remember this two faces.
And if you see here?
Yup.
The face here is detected a swirl of false positive swirl here and here as well.
But we cannot be hundred percent accurate.
Galaxy has no detected faces, air neither, so we got this rectangle drone around faces inside these
two images.
But our script somehow moved all the files in to output.
We want only the files with faces to be moved there.
Why did that happen?
Well, that's happened because this is what we're doing, right?
We iterate and we write a rectangle, but then we return the image for everything.
So we need to do something here to suggest a conditional to distinguish between different phases.
So an empty table here, an empty table here.
And this is a list.
What we could do is we could say something like if Len of faces is different from zero, then we draw
faces.
So I indented this so that this happens.
We draw the rectangles.
And we intend to return image if that's the case.
Otherwise, you could see something else return none, but that is not necessarily because by default,
Python will return none if this is not executed.
So this block here is only executed when this is evaluated to true, which means when this is different
from zero different from zero east in the case of this and that.
So let's give it another try now I'm going to delete.
These images.
From output.
And run.
All right.
My terminal is not displaying very well the ultimate yet, no, it's it's here.
And both my outward folder.
Has now of these two images.
So we filter out only the files which have faces in them.
That's our final code.
Thanks for following and go to the next video.
And we do some more coding.