So welcome back to Lesson 40, which looks like the last lesson here.
However, we have some lessons of looking at video, which I'll demonstrate to you after with this lesson.
But for now, this is the last I python that book in the open TV chapter in this section.
So go ahead and open this notebook.
This is we're going to do facial recognition using one of the facial recognition libraries called face
recognition.
So to do this, just install this here.
So this group pip install face recognition.
So what this library allows us to do, it allows us to compare faces and then using that comparison
scroll, we can actually use that metric now to find the face that it it's it is more similar to in
a database of faces.
So this should take maybe about about a minute to install.
So let's wait for it.
There we go.
It's complete.
So let's take a look at how we use this function, so let's stick.
That's literally initial function that we've used previously.
And let's download some images.
We're going to download an image of Trump and two different images of President Joe Biden.
So let's get those images here using the double get function.
So we now have them safe.
And let's display those images so we can take a look at the images we're looking with.
So you can see Joe Biden is here two different pictures of him where he does look somewhat similar.
And this is Donald Trump, his well, looking a little bit angry.
So let's take a look at something.
Let's import or face recognition library.
Let's load each image here.
So we use face recognition to load.
Image file has an inbuilt loading function, which probably which not probably it does load it and it's
in its own type of array here.
And what we do, it is another function in it called face recognition, dot, facial water, face and
coatings.
So we take the image that's been loaded here, put it into here, and we just index the output to get
this is to get the first image out of it.
The first, including out of it.
After multiple offices, you can use different index values to get them, but for now, where we are
using images with just one face to make this simpler.
So we have the Biden and including.
And then we call the Trump image.
Unknown image here.
So we have the unknown, including here.
So we're just going to just do that because we're going to use the unknown image to recognize it afterward.
But for now, just this is just the Trump image, and this is the sort of Trump including in the Trump
image.
And we just use the don't compare faces function in the face recognition library to compare.
Doesn't cuttings notice that it is in an array here?
The Biden encoding sorry, it's an array, and then this one isn't.
So just from the note that you this can probably take a bunch of different images here so you can get
the output, the results of that as well.
And then we index and the first result because we just want to compare to the first face of compared
to.
And we can see what it says.
It's going to compare faces and tell you if it's a match or not.
And it says the fierce match is false, which means that Biden and Trump images are this similar or
not similar.
So it doesn't it doesn't match.
So no, that's tried with two different Joe Biden images.
So let's load this and run the same functions here where we get the first face out of each one, get
the encoding and then compare them here.
And now it's as true.
So this is working as it's intended.
It just matched.
It's just as true and matched the two different images of Joe Biden, these two images.
It said it's the same person, so that's exactly what we want.
So now let's use this same function here to recognize faces and images.
So to do that, we're going to look at a sample picture here.
This is the image of Trump.
We're going to look at another sample here.
This is one of Joe Biden.
We're going to put those in cuttings here into this, this array here.
And then we're going to do similarly with the first names here.
So we store the name of Donald Trump and Joe Biden in it.
This is creating our database of names.
And then we initialize some variables here.
These are all office locations, including surface names, and process this from being true to variable
that we use inside of here.
All right.
You can see it here.
So if it's a process frame, we just do that.
This is a way we can process every other frame if we needed to in a video.
However, I just love this line of code here, just in case you wanted to see it, how it would to a
video, but it's not going to do anything here, just going to it's just going to process this year.
So what we do loaded.
So we load and undo an image.
So this unknown image is not in the database.
Remember that this is the picture of Biden.
Joe Biden, too, and we passed a free every resize it to a quarter of its size so it can improve the
facial recognition time because larger images do take longer with this algorithm.
And then we convert the image to for BGR to RGV be in that way, you can do it without using open space
functions.
This is just some indexing of the array.
And then what we do, we if this process is this frame, that's that's the one we have true.
This is allows us to skip other frames if we wanted to in the video.
But forget that for now, what we're doing here, we're processing this.
So we take this more to be frame.
We get to first locations here.
That's for the bounding box.
By the way, it has its own decode facial features detector in this and then we take that interface
out of.
That's we use face locations to extract a face from it, and we get to face and coatings here.
So now what do we do?
We get the facial, including stat, the facial recognition lighting, coatings captured and the locations
as well.
And we look through all of the faces that have been encoded from dysfunction here because remember,
there can be multiple faces.
So we know check to see if a face is a match for one of our known faces.
Remember, we created this little index here of known faces, previously unknown coatings.
So this towards the end coatings for our database.
So you can have imagine if you had like a database of 100 images, you would.
You can create this array just by looking through all of the images in the directory and appending the
results to that early here.
So no, it is hardcoded the results in this area for simplicity's sake.
And then what we do, we compared with the face, including if we just captured in this unknown image
and the name for no is unknown.
And then what do we do?
We take a look.
Do we get the first distance?
That's a way to measure the smallest, the basically the Euclidean of cosine type distance from the
two faces.
And we just use this augment function here to get the best minimum match.
That means the match that is closest to the face in our database here and then we append the names to
the end here.
So and then what we do once we have that, we can now loop through the results.
We use the first locations here to get these values top right, bottom left, use the open TV rectangle
function to draw on the face here.
Similarly, we draw a label boxed right below this rectangle so we can put the name in there and then
we use pretexts to get the name that we got from databases onto the image.
So it seems like a lot seems a little bit complicated if you're a beginner.
I can understand why this could could seem a bit overwhelming.
However, if you just take your time and go through the court line by line, it will all make sense
to you.
So let's see the results.
Well, it worked.
So you can see we are able to now recognize undoing images and apply them label to that face by this
code here.
And you can extend this for your own purposes.
If you had pictures of your employees, you can put them here and could get destroyed, including here.
And then every time there's a new face like, let's say, there's a video camera or Raspberry Pi that's
just checking feces of someone approaching a door.
You can use this to notarize shooting people to enter or subject certain people to not have access.
So that's it for this facial recognition chapter, and I hope you enjoy this.
This was a very good lesson and I've used this library.
It's not state of the art, but it's pretty good.
We will do facial recognition in more detail in the deep learning section of discourse.
So stay tuned for that.
And for now, that concludes these and see if your lessons will now move on to some open TV lessons.
Where we started working with VIDEO can take a look at these lessons here.
However, this is going to be run on my local machine just because it's easier to demonstrate these
lessons here.
So if you don't have open TV install locally, there's a video created in the beginning.
Or actually, I'll put it in this section here so you can see how to install open TV locally and run
these run these files.
These no books on your system, so stay tuned for these lessons.
Thank you.