Face Recognition System in Python is a biometric technology capable of recognizing and verifying an image or video of an individual.
Facial recognition allows one to identify the system administrator.
Today, this biometric system plays a very important role in the protection of smartphones and consumers.
Let’s Code face Recognition System in Python!
Installing Packages for Face Recognition in Python
Install Face Recognition Package from Python:-
pip install face-recognition
If you get CMake and dlib error, also install CMake and dlib to solve the problem.
pip install cmake
pip install dlib
Also Install Pillow, for image manipulation, drawing shapes on images and text on images for writing names of the person.
pip install Pillow
Face Recognition Python Algorithm
Steps to follow to make a Facial Recognition System. You can also make this system for Hardware like Raspberry pi / Arduino projects.
1.Import the needed packages.
import face_recognition
from PIL import Image, ImageDraw, ImageFont
2. Load image of the person
Load the image of the person on which you want to try facial recognition.
image_of_person = face_recognition.load_image_file(‘./img/face_models/person.jpg’)
3. Store Face Encoding
This Process stores the facial features of the person.
person_face_encoding = face_recognition.face_encodings(image_of_person)[0]
4. Create List of Encodings and Names
Create a list of face encodings with the values of face encodings of every selected face model. Also, create a list of face names that will map the names of the matched persons.
Note:- The face encodings list and the face names list must be in the correct order. Otherwise, the names will be displayed in the wrong order and thus the Facial recognition system will be a failure.
known_face_encodings = [ person_face_encoding ]
known_face_names = [ “Name of the Person” ]
5. Load the Test Image
Load the image in which you want to recognize your face.
test_image = face_recognition.load_image_file(‘./img/test_image.jpg’)
6. Find Face locations & Encodings in test_image
This process stores the face location of every face in the test image and then stores the face encoding of every face using its face location, stored in the previous step.
face_locations = face_recognition.face_locations(test_image)
face_encodings = face_recognition.face_encodings(test_image, face_locations)
7. Create test_image to PIL format
pil_image = Image.fromarray(test_image)
Create an Instance of ImageDraw, because we have to draw a rectangle on the matched faces and display the text of the matched names of the person, using ImageDraw instance.
draw = ImageDraw.Draw(pil_image)
8. Recognize Faces
Loop through every face in the test image and compare the face encodings with each face.
If any of the faces in test_image matches the face encoding of the person, then a rectangle is drawn on the face and the person’s name is labeled on it.
If the person’s face doesn’t match with any of the faces in the test image then the default value of the name i.e “Unknown Person” is labeled on the face.
# Loop through faces in test image
for (t, r, b, l), face_encoding in zip(face_locations, face_encodings):
matches = face_recognition.compare_faces(
known_face_encodings, face_encoding)
name = "Unknown Person"
# IF MATCH
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
# Draw Box
draw.rectangle(((l, t), (r, b)), outline=(0, 255, 0))
# Draw label
text_width, text_height = draw.textsize(name)
draw.rectangle(((l, b - text_height - 90), (r, b)),
fill=(0, 0, 0), outline=(0, 255, 0))
draw.text((l+6, b-text_height-75), name, fill=(0, 255, 0), font=font)
del draw
# Display image
pil_image.show()
Face Recognition Python GitHub
For the Source Code of the facial recognition system, visit the GitHub Page.
Source Code Facial Recognition
Lanes Detection
Self-Driving Cars are the future, and they detect lanes to drive on the road so that the car doesn’t go outside the road. Learn How lanes detection is implemented in self-driving cars.
Motion Detection
Motion Detection is used in many places like CCTV cameras to detect any kind of motion. Learn how to make Motion Detection using OpenCV.