Recognizing Faces By Capturing them Using Python

Introduction

 
The process of recognizing a person's face in front of the camera by capturing their image and processing it is one of the concepts widely used for automation. It can be done in a very simple way. Here, I am using Python programming for recognizing faces. For using the system for this concept, you need to prepare your system by installing the required attributes for the python. I am using Python 3.7.3 for this process. In this article, we will discuss recognizing the face of a person in front of the camera by capturing their image.
 

Preparing the System

 
Before the process, you need to prepare your system by installing some additional attributes to the Python for this purpose. You can run the code given below in the command prompt one by one for installing the packages.
  1. pip install cmake  
  2. pip install dlib  
  3. pip install face_recognition  
  4. pip install numpy  
  5. pip install opencv-python   
If you found any error in the installation of the dlib attribute, you can download the dlib file and install it by executing the codes given below in the command prompt.
  1. cd C:\Users\Dhanush\Downloads\  
  2. pip install dlib
You need to keep the cascade files in the same directory where you are storing the Python program.
 

Training the System to Recognize Faces

 
To have the system recognize your face, you need to train the system to recognize their images. For that create a folder named faces in the same directory where you are saving the python program and rename the image in the name of that person. The name given for the image is shown on the screen if the person is recognized. Make sure that the images contain the face of a single person.
 
Recognizing The Faces By Capturing The Faces Using Python
 

Recognizing THE FACE

 
To recognize the face of a person, you use the Python code given below for that process. By modifying that code, it will detect the faces from the images. For detecting the faces, you need to store the image in the same directory in the name of the test and you need to make the changes on the code based on the extension of your image.
 
Recognizing The Faces By Capturing The Faces Using Python
  1. import face_recognition as fr  
  2. import os  
  3. import cv2  
  4. import face_recognition  
  5. import numpy as np  
  6. from time import sleep  
  7.   
  8. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')  
  9. cap=cv2.VideoCapture(0)  
  10. _, img = cap.read()  
  11. def get_encoded_faces():  
  12.     """ 
  13.     looks through the faces folder and encodes all 
  14.     the faces 
  15.  
  16.     :return: dict of (name, image encoded) 
  17.     """  
  18.     encoded = {}  
  19.   
  20.     for dirpath, dnames, fnames in os.walk("./faces"):  
  21.         for f in fnames:  
  22.             if f.endswith(".jpg"or f.endswith(".png"):  
  23.                 face = fr.load_image_file("faces/" + f)  
  24.                 encoding = fr.face_encodings(face)[0]  
  25.                 encoded[f.split(".")[0]] = encoding  
  26.   
  27.     return encoded  
  28.   
  29. def unknown_image_encoded(img):  
  30.     """ 
  31.     encode a face given the file name 
  32.     """  
  33.     face = fr.load_image_file("faces/" + img)  
  34.     encoding = fr.face_encodings(face)[0]  
  35.   
  36.     return encoding  
  37.   
  38.   
  39. def classify_face(im):  
  40.     """ 
  41.     will find all of the faces in a given image and label 
  42.     them if it knows what they are 
  43.  
  44.     :param im: str of file path 
  45.     :return: list of face names 
  46.     """  
  47.     faces = get_encoded_faces()  
  48.     faces_encoded = list(faces.values())  
  49.     known_face_names = list(faces.keys())  
  50.   
  51.     #img = cv2.imread(im, 1)  
  52.     #img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)  
  53.     #img = img[:,:,::-1]  
  54.    
  55.     face_locations = face_recognition.face_locations(img)  
  56.     unknown_face_encodings = face_recognition.face_encodings(img, face_locations)  
  57.   
  58.     face_names = []  
  59.     for face_encoding in unknown_face_encodings:  
  60.         # See if the face is a match for the known face(s)  
  61.         matches = face_recognition.compare_faces(faces_encoded, face_encoding)  
  62.         name = "Unknown"  
  63.   
  64.         # use the known face with the smallest distance to the new face  
  65.         face_distances = face_recognition.face_distance(faces_encoded, face_encoding)  
  66.         best_match_index = np.argmin(face_distances)  
  67.         if matches[best_match_index]:  
  68.             name = known_face_names[best_match_index]  
  69.   
  70.         face_names.append(name)  
  71.   
  72.         for (top, right, bottom, left), name in zip(face_locations, face_names):  
  73.             # Draw a box around the face  
  74.             cv2.rectangle(img, (left-20, top-20), (right+20, bottom+20), (25500), 2)  
  75.   
  76.             # Draw a label with a name below the face  
  77.             cv2.rectangle(img, (left-20, bottom -15), (right+20, bottom+20), (25500), cv2.FILLED)  
  78.             font = cv2.FONT_HERSHEY_DUPLEX  
  79.             cv2.putText(img, name, (left -20, bottom + 15), font, 1.0, (255255255), 2)  
  80.   
  81.   
  82.     # Display the resulting image  
  83.     while True:  
  84.   
  85.         cv2.imshow('IMAGE', img)  
  86.         return face_names   
  87.   
  88.   
  89. print(classify_face("test"))  

Output Verification

 
While you are running the program, your camera will automatically turn on and capture the face. The system will recognize the face that is captured. If it is an unknown face, it shows up as unknown.
 
Recognizing The Faces By Capturing The Faces Using Python
 

Conclusion

 
This is an article for capturing and recognizing faces. In the future, I will upload the article for recognizing the faces from live video from the camera. I hope this article will help you to learn about face recognition. You can download the files for this project from my Github page by clicking here or from the download section.