In this lesson we will start our Python project launch pi charm and open the main dot P. wide file which
we created earlier in the file.
We set some codes to test pi term.
You can clear all the test codes.
We will be using the main dot P Why file for operating the drone.
So I want to create a different folder for the sample codes that we make in this lesson.
Right click on P Y Tello and choose new.
Then choose directory we'll set the directory name to tools and hit.
OK you can see that the tools directory has been created under P Y Tello.
Now I want you to grab that to XM Mal files and the image data which we saved onto our desktop drag
the three files into the tools directory and save next right click on tools and choose new then click
on python file.
We'll name this file image based detect
I'll expand the window so you can see better.
For this lesson we will be using the new file.
Choose the file go to the go tab and click the black triangle next to the word go juice choose edit
configurations in our previous lesson we chose main up y as our path.
This time we need to choose the X AML file we imported click on the folder icon choose tools and go
to the image face detect dot P Y that we just created press open and confirm.
Okay let's print test to see if we set the path correctly as we run this code.
You can see that the test has been successfully printed I usually have my results on the right side
of the screen click on the settings icon on the right center Jews move to and select right you'll see
that the layout changed.
If you prefer to have it on the bottom you don't need to make this change.
Now we are prepared to write some codes.
Start by importing c v two as C V.
So we can import the open C V.
Next we'll get the example files we downloaded create a variable face cascade and set c v choose the
cascade classifier method this will be followed by the and mail phone name go to the heart cascade frontal
face X and mail file and right click choose refactor and rename.
Here we will see the full file name.
So copy it.
I'll close this window and paste the name between the quotation mark.
We will do the same with the HA cascade.
I exemplified create another variable and paste the excel file name change the variable name to I cascade.
Now we've completed setting the two XM l file to be used in the script.
Next we need to prepare the image data create a variable I AMG and set CB dot I m r e a d method.
This method will enable us to read the image.
This will be followed by the image file name which is image dot JP G.
Here we've set all the files we need.
First we will need to create the image into a grayscale.
Create a variable Gray and set C V dot CBT color.
Set the image as a first argument then set CV dot color B G are two gray.
Next we want to detect the face of this gray image.
Create a variable faces and set the face cascade detect multi scale method followed by the variable
Gray as the first argument.
The second argument will be the scale factor as in the sample code.
We will set the scale to one point three by changing this number to a smaller number.
You will be able to detect the image in smaller pieces but keep in mind that the more smaller the scale
becomes the more time it takes to process.
So I will suggest that one point three will be a good number just start off with for the next argument
will be the men neighbors which we will set to 5.
This is clarifying how the system would detect faces that are overlapping.
Don't think too much and try to understand what all the argument stands for and simply use the values
in the sample code.
Now we've set the code to detect the face I'll set to print out the number of faces detected by printing
length of the faces.
Next we set a for loop to get the x y coordinate and also the width and height point of the faces based
on the data we want to set a square on the face.
We can use the c v dot rectangle method and set the image as the first argument the next argument will
be all the points of the face which is x y and x plus width and Y plus height the next argument will
be the color of the rectangle.
Here we'll use to fifty five point zero point zero which will be the color blue last.
We set the thickness of the lines here let's set it to two.
Now let's talk about what the x and y points mean.
Here is the sample image the left top corner will be the beginning of the coordinate going down the
image the Y point goal is higher going right the X point goes higher.
In this case the first point to be detected will be the left top corner the value for x and y will be
set as the second argument to determine how large the rectangle will be.
We need to add the width of the face to X and the height of the face to Y.
Here we have created a rectangle that frames the face.
Next we set the color of the rectangle to be blue.
Lastly we set the thickness of the line to be 2 which is the thickness you will see in this image.
Next we will go on and change the color inside this rectangle to gray so we can detect where the eyes
are.
Let's create a variable ie Gray and set the variable Gray with the size to y as Y plus H an X as x plus
W.
This means we are cutting out the face image and setting it into variable ie.
Great.
We also want to cut out the color of the eyes so we set a variable eye color and cut out the image by
setting y as Y plus H and x as x plus W to detect where the eyes are.
We will set the variable eyes and set the eye cascade detect multi scale method.
The argument will be set to ie Gray which we just create and now we can set the rest of the arguments
but I'll skip that for now.
Again we will be setting a for loop to get the four points.
Let's set the points t e x E Y E W and H.
This should loop as many times the process detects size we then create a rectangle by using the c v
a rectangle method onto the eye color image.
Followed by the four points as argument.
E x e y e x plus E W E Y plus E H.
This time we will set the color to zero to fifty five point zero to make a green rectangle.
Lastly the line thickness will be the same two it'll be easier to understand how the process ran through
by looking at the final image.
So we set C V I am show method to show the new image we created as I am G.
Then we set C the weight t to 0.
The process will go into a loop to show the image and wait for the next action to occur on the keyboard.
Once an action occurs we want to finish showing the image.
So we set c v destroy all windows.
Now it's the moment to see how the process goes.
Once we run this code you will see the number one as the result.
I have to drive the image in since it popped out of the screen the number one is showing the number
of faces detected.
Since we set to print out the length of the faces as you see we've successfully detected the face in
the eyes.
A blue rectangle is placed on my face and two green rectangles on my eyes.
With these simple codes you manage your program of face recognition system to delete the image hit any
key on your keyboard.
Once you hit a key on the keyboard the image will disappear and the process has been finished.
As we set the weight key method here the system went into a loop showing you the image.
I'm going to run this code again to properly finish showing this image.
You need to be selecting the image before you hit the keyboard.
If the image isn't selected nothing will happen.
Remember to select the image and hit a key.
Then the process will go onto the destroy all windows and finish the process.
I want you to try downloading some images and setting it into this script to see if your face recognition
system works some other images in our next lesson.
We will be detecting faces in videos.