Maybe you have seen it before. Maybe some of you use a face unlock feature that some phones have. This technology called Face Recognition is simply amazing. But, do you think it is hard to make an application based on that technology? It is actually not that hard. You can even make it with less than 10 lines of codes! Seriously. Here is my Face Recognition Tensorflow tutorial, special for you.
Here is the code, in case you don’t want to read the article. LOL.
from easyfacenet.simple import facenet images = ['images/image1.jpg', 'images/image2.jpg', 'images/image3.jpg'] aligned = facenet.align_face(images) comparisons = facenet.compare(aligned) print("Is image 1 and 2 similar? ", bool(comparisons)) print("Is image 1 and 3 similar? ", bool(comparisons))
And it will output these.
Is image 1 and 2 similar? True Is image 1 and 3 similar? False
Wait, wait. No tensorflow? There is tensorflow of course. For this tutorial I use an algorithm called Facenet that was developed with tensorflow. And while it is easy enough for me to use Facenet out of the box while coding tensorflow syntax, I don’t think most of you comfortable with that.
So, I decide to create another interface on top of Facenet, which I called Easy Facenet. To install the library, you can just type
pip install easyfacenet
And you are good to go!
The article should not end like this, should it? Of course not.
Let me explain to you, line by line the tutorial. At the same time, how Face Recognition works in the first place.
So, how Face Recognition works?
As you can see from the picture above, the steps are like this:
- Get an input image which should contain face (s)
- You need to find, where exactly the face is, and put a bounding box around the face
- For consistency of the algorithm, you need to transform the picture, so that the position of mouth, nose, and eyes, are consistent for different pictures.
- Then crop, obviously
- Input the cropped picture into the Facenet algorithm. Which is a Deep Neural Network.
- It will output a vector representation of that face. It was 128 dimensional vector back then, it is 512 dimensional right now.
- Then you can do what you want with that representation. You can do classification, clustering, or just use a similarity computation between pictures.
Wow, that was hell lot of stuffs. Why is it so difficult? Well, basically you can group those 7 steps into 3 steps, which are,
- Alignment, input an image, and output the aligned cropped face
- Embedding, input the face, and output the representation
- Comparison, compare those representation, are those similar or not?
Because only 3 simple steps, the code should be as simple as that, shouldn’t it?
Yes, you can, using easyfacenet.
The simplest Face Recognition Tensorflow library available
Let’s break down the code one by one.
from easyfacenet.simple import facenet
Import the facenet file from simple module. There are three methods you can use inside the file. They are, align_face, embedding, and compare.
Easily, you can tell that each of this methods is representing each step for Face Recognition.
images = ['images/image1.jpg', 'images/image2.jpg', 'images/image3.jpg']
Now we can define the images. What are those images? Well, this.
And also this.
We got the images. Now the real deal.
Step 1. Alignment
aligned = facenet.align_face(images)
The library will try to find the face inside the image and crop the face as well as prewhiten the face. Prewhitening will make the training easier when training time, so in inference time, you will also need to prewhiten the image.
The prewhiten aligned face will look like this.
Now, you are ready for the next step. Take a breather and chew slowly!
Step 2. Embeddings
Wait, wait. I don’t see embeddings in the example above? Well, that’s because the compare method already called embedding inside. If you want to use embedding somehow, use this.
embeddings = facenet.embedding(aligned)
The embeddings will look like this,
Step 3. Comparison
comparisons = facenet.compare(aligned)
If you have 3 images, the comparisons variable will have 3 x 3 values. Which are permutations of each images compared to each other. In example, if you want to get “is image 1 is similar to image 2?“, then
print("Is image 1 and 2 similar? ", bool(comparisons))
Is image 1 similar to image 3?
print("Is image 1 and 3 similar? ", bool(comparisons))
You didn’t forget already that array is zero indexed, did you? Haha
And, that’s it. You can get your comparison result just like that. The comparison technique I used is cosine similarity. You can use any other similarity method you want. You can definitely use another method like clustering or classification. Something like Siamese Network is the thing you need to look for.
What can you do next?
As, I have said, this is the simplest Face Recognition Tensorflow library available, therefore you can start do your thing the fastest possible.
If you are the hacky one, you can explore the library and create the real Face Recognition Tensorflow code. Take a look at the code here because that is the cornerstone of the library. Furthermore, extend the code or you can create your own functionality.
In conclusion, utilizing easy facenet can help you tremendously in creating your face recognition project. Moreover, this Face Recognition Tensorflow library is maintained solely by me, so it is easy for you if you want to ask for some kind of functionality.
As for the actual implementation for the other similarity method, I will bring you there in the next tutorial and due to that reason, I will add exclusively the method inside the library.
Finally, if you want to explore another piece of amazing Machine Learning technology, you can read them here.