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. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  8. cap=cv2.VideoCapture(0)
  9. _, img = cap.read()
  10. def get_encoded_faces():
  11. """
  12. looks through the faces folder and encodes all
  13. the faces
  14. :return: dict of (name, image encoded)
  15. """
  16. encoded = {}
  17. for dirpath, dnames, fnames in os.walk("./faces"):
  18. for f in fnames:
  19. if f.endswith(".jpg") or f.endswith(".png"):
  20. face = fr.load_image_file("faces/" + f)
  21. encoding = fr.face_encodings(face)[0]
  22. encoded[f.split(".")[0]] = encoding
  23. return encoded
  24. def unknown_image_encoded(img):
  25. """
  26. encode a face given the file name
  27. """
  28. face = fr.load_image_file("faces/" + img)
  29. encoding = fr.face_encodings(face)[0]
  30. return encoding
  31. def classify_face(im):
  32. """
  33. will find all of the faces in a given image and label
  34. them if it knows what they are
  35. :param im: str of file path
  36. :return: list of face names
  37. """
  38. faces = get_encoded_faces()
  39. faces_encoded = list(faces.values())
  40. known_face_names = list(faces.keys())
  41. #img = cv2.imread(im, 1)
  42. #img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
  43. #img = img[:,:,::-1]
  44. face_locations = face_recognition.face_locations(img)
  45. unknown_face_encodings = face_recognition.face_encodings(img, face_locations)
  46. face_names = []
  47. for face_encoding in unknown_face_encodings:
  48. # See if the face is a match for the known face(s)
  49. matches = face_recognition.compare_faces(faces_encoded, face_encoding)
  50. name = "Unknown"
  51. # use the known face with the smallest distance to the new face
  52. face_distances = face_recognition.face_distance(faces_encoded, face_encoding)
  53. best_match_index = np.argmin(face_distances)
  54. if matches[best_match_index]:
  55. name = known_face_names[best_match_index]
  56. face_names.append(name)
  57. for (top, right, bottom, left), name in zip(face_locations, face_names):
  58. # Draw a box around the face
  59. cv2.rectangle(img, (left-20, top-20), (right+20, bottom+20), (255, 0, 0), 2)
  60. # Draw a label with a name below the face
  61. cv2.rectangle(img, (left-20, bottom -15), (right+20, bottom+20), (255, 0, 0), cv2.FILLED)
  62. font = cv2.FONT_HERSHEY_DUPLEX
  63. cv2.putText(img, name, (left -20, bottom + 15), font, 1.0, (255, 255, 255), 2)
  64. # Display the resulting image
  65. while True:
  66. cv2.imshow('IMAGE', img)
  67. return face_names
  68. 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.