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.

Face recognition tensorflow

tl;dr

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[0][1]))
print("Is image 1 and 3 similar? ", bool(comparisons[0][2]))

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.

Face Recognition

So, how Face Recognition works?

Face Recognition
Courtesy of OpenFace

As you can see from the picture above, the steps are like this:

  1. Get an input image which should contain face (s)
  2. You need to find, where exactly the face is, and put a bounding box around the face
  3. 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.
  4. Then crop, obviously
  5. Input the cropped picture into the Facenet algorithm. Which is a Deep Neural Network.
  6. It will output a vector representation of that face. It was 128 dimensional vector back then, it is 512 dimensional right now.
  7. 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,

  1. Alignment, input an image, and output the aligned cropped face
  2. Embedding, input the face, and output the representation
  3. 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.

me
A picture of me (image1.jpg)

And this.

me
Another picture of me (image2.jpg)

And also this.

lil bro
My lil’ bro (image3.jpg)

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.

aligned
Image 1 to 3 from left to right

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,

embeddings
I don’t recommend you to look at the numbers

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[0][1]))

Is image 1 similar to image 3?

print("Is image 1 and 3 similar? ", bool(comparisons[0][2]))

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.

Final Thoughts

If you want to know about the paper behind this amazing technology, you can look here as well as here.

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


Leave a Reply

Your email address will not be published. Required fields are marked *