Sign in
Log inSign up
Facial Recognition using Python library

Facial Recognition using Python library

Hemanth Raju's photo
Hemanth Raju
·Jul 10, 2021·

3 min read

Implementing Face Recognition For Real World Applications

Hi Everyone! How are you all, I hope all are doing well.

In this article, I will write about one of the most popular Python libraries called “face_recognition”.

Introduction :

  • Face-Recognition library built using the “dlib” library, which is built using C++ Language.

  • This library helps to recognize the face of a person with 99.38% Accuracy.

  • Face-Recognition library is easy to import to your project.

  • It is having some amazing methods, we will discuss some of those functionalities later in this article.

Let's go and start with the Installation part :simple_smile:

Installation :

  • Face-Recognition library can be easily installed using pip.

  • Make sure you install the dlib library before installing face_recognition.

pip3 install face_recognition

If you are facing any issue with the installation part I recommend you to go through this link:https://github.com/ageitgey/face_recognition/issues/175#issue-257710508

Hurry, we have installed a face-recognition library into our system, now we are good to import to our projects.

Facial-RecognitionFacial-Recognition

Features :

  1. Find Faces in Picture.

  2. To find faces in a given picture, we can use the face_locations(image file) function from the Face_recognition library.

face_recognition.face_locations()face_recognition.face_locations()

import face_recognition
image = face_recognition.load_image_file("your_file.jpg")
face_locations = face_recognition.face_locations(image)

The above code can be used if you want to detect faces in a file called “your_file.jpg”.

  • Instead, you can also use this method in a live video stream to detect faces with the help of OpenCV.

2. Comparing Faces :

  • For comparing faces, we have a method called compare_faces() in the facial recognition library.

  • compare_faces() returns a list of True/False values indicating which known_face_encodings match the face encoding to check

Compare FacesCompare Faces

import face_recognition
known_image = face_recognition.load_image_file("hemanth.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")

hemanth_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([hemanth_encoding], unknown_encoding)
  • We can see from the above code that we have one known image called “hemanth.jpg” and one unknown image called “unknown.jpg”. We are trying to compare these two faces.

  • And also we can see the face_encodings() method, which returns the 128-dimension face encoding for each face in the image.

  • This method will help when you are doing projects on facial recognition storing the faces in a database and then comparing the current face with each of the faces in the database.

Real-World Applications:

  1. In ATM Centers, we can use facial recognition instead of debit or credit card. So that we can avoid fraud. It increases security.

  2. In Shopping Malls, we can use our face instead of using cash, as it protects us from physical contact from the shopkeeper (Particularly nowadays due to the pandemic(Corona-Virus)).

And in many more centers, we can use Facial Recognition for security purposes.

Have a try with this amazing python library and comment below about how you like this python library.

Thanks for reading, Have a Great Day!

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer