Face Recognition using Python | Face_Recognition Library Explain

Face Recognition is the most exciting field of the computer Science and Engineering.Facial Recognition is the technology which uses science and mathematics to recognize the faces.This technique is capable not only identify the faces but also verify a person from the image or from any video.

There are so many method to recognize the faces but the simplest way is to compare the images and try to find out the similarities aong them.

face recognition


Facial recognition was never that much easy until open source community not contributed in this field. Because of open source community efforts we are now feel very easy to recognize the faces using some dedicated libraries. In this article we are going to understand to use worlds easiest library to recognize the faces.

In this blog i am going to explain how to recognize the face using python face_recognition library which is capable to recognize the face from the python programming. It has built in state of the art face recognition capability and most exciting thing of this library is, its accuracy...This library having the accuracy of 99.38% as per the documentation.

Lets take a quick glance what this library can do :

  • It can find faces in the picture.
  • It can find and manipulate the facial features in the image.
  • You can even do some stuff like make up to the image.
  • Real time face recognition using face_recognition libraries.

Requirement of the libraries :

  • we required Python 3.3 or latest version.
  • Can work on platform like Linux and Mac (Not supported on Windows you need to install it via third party method)

Code to recognize the face :

import face_recognition
from PIL import Image, ImageDraw #This pillow libary is required for drawing rectangle around the face

pics = fr.load_image_file('path of unknown image') #Load the Image
face_loc = fr.face_locations(pics) #To find out the number of faces and its count.

print(f'There are {len(face_loc)} people in this image')

salmankhan = fr.load_image_file('path of known image') 
salmankhan_pic  =  fr.face_encodings(salmankhan)[0]#for encoding the image

known_face_encoding = [salmankhan_pic] #Assign the list to the encoder variable
known_face_name = ["salman khan"]

test_image = fr.load_image_file('path of unknown image')
face_location = fr.face_locations(test_image)
face_encoding = fr.face_encodings(test_image,face_location)
pil_image = Image.fromarray(test_image)
Draw = ImageDraw.Draw(pil_image)

for(top,right,bottom,left),face_encoding in zip (face_location,face_encoding): #Repeat the same upto all the faces get recognize
    matches  = fr.compare_faces(known_face_encoding,face_encoding)
    name = "Unknow Person"
     if True in matches: #If matches found and got True draw the rectangle and labelled it.
      first_match_index = matches.index(True)
      name = known_face_name[first_match_index]
      Draw.rectangle(((left, top), (right, bottom)), outline=(255,0,0))
      text_width , text_height = Draw.textsize(known_face_name)
      Draw.rectangle(((left,bottom - text_height - 10),(right,bottom)),outline = (97, 149, 232))
      Draw.text((left+6,bottom-text_height-5),name , fill=(5, 247, 247))

del Draw
pil_image.show() #Show the final image.