Image Credits: wallpapercave.com, edited by PySeek.
Introduction
Humans can easily recognize each other by looking at their faces. But imagine what it would be like if a machine could recognize a person by looking at their face.
Nowadays machines can do that too. There are so many examples in the real-life where this technology is using vastly. If I take an example, you can easily find may IT companies are using it to keep the record of their employees or secure their premises. You can read more about the applications of facial recognition technology from here.
In this tutorial, we will develop a Face Recognition Project in Python. It will recognize the Money Heist series characters by their faces and show their name on the screen.
No, they are not coming here to perform this task. I just take them as an example to show the output of our project. To know how, you need to read this article completely.
Money Heist is one of the most popular TV series on Netflix. You don't need to watch this web series to make this project. You can also identify other faces through it(Please read the Project Details).
☛Visit Also: Six Degrees of Kevin Bacon in Python: Artificial Intelligence Project
Table of Content
1. The Project Details
The main project file contains one python program file, and one folder, named 'Images'. This folder contains images of different characters of the Money Heist series. One image belongs to only one person.
The program will try to detect the faces through the Webcam of Your system. If any face detects then, it will try to recognize the face with those images present in the 'Images' folder. If the Webcam finds any known face, then it will print the name of that person otherwise, it will print 'Unknown'.
You can identify other faces using this project; you just need to put those images in that 'Images' folder.
2. You must Install these libraries
You have to install cmake and dlib before installing face_recognition.
Let's perform the installation process one after one as same as I shown here. Use pip3 instead of pip for Linux.Install cmake
🔹Command - pip install cmake
Install dlib
Dlib is a cross-platform software library; written in C++. You've to install cmake before installing dlib. It may take around 15-20 minutes to install. So, don't be surprised if it takes extra time.
🔹Command - pip install dlib
Install face recognition
Now, install the face_recognition library. You can read the documentation from here.
🔹Command - pip install face_recognition
Install OpenCV
OpenCV-Python is a python library that is designed to solve computer vision problems. It's a python wrapper for the original OpenCV, implemented using C++.
Python is slower as compared to C/C++. But it can be extended with C++ using Python wrappers, which will be used as Python modules. It will give us two advantages. First, the code will be as fast as the C++ code, and second, it will be easy to code in Python than C++.
🔹Command - pip install opencv-python
Install NumPy
NumPy is a Python library that provides a powerful N-dimensional array object, several mathematical functions, and much more. It's used widely in machine learning, deep learning, image processing, etc. for working with multi-dimensional arrays. I've used it in this project for performing a specific task.
🔹Command - pip install numpy
3. Import the Libraries
Now start writing the code by importing these libraries.
# Import face recognition
import face_recognition as fr
import numpy as np
import os
import cv2
4. Encode the images stored in the Images folder
def encode_faces():
encoded_data = {}
for dirpath, dnames, fnames in os.walk("./Images"):
for f in fnames:
if f.endswith(".jpg") or f.endswith(".png"):
face = fr.load_image_file("Images/" + f)
encoding = fr.face_encodings(face)[0]
encoded_data[f.split(".")[0]] = encoding
# return encoded data of images
return encoded_data
I've downloaded several images of Money Heist characters(Example, Professor, Helsinki, Berlin, Alicia, Tokyo, and more other characters) from Google and stored those in the 'Images' folder.
The tasks of the above function are like these.
1. Finding image files in the 'Images' folder.
2. Selecting files that ended with .jpg or .png extensions.
3. Then, the task is to store those selected files into a variable and encode them one by one.
4. The Encoded data will store in a dictionary variable, named 'encoded_data'.
5. In the end, the function returns that 'encoded_data' variable.
5. Detect and Recognize the faces through the Webcam
def detect_faces():
faces = encode_faces()
encoded_faces = list(faces.values())
faces_name = list(faces.keys())
video_frame = True
video = cv2.VideoCapture(0)
while True:
ret, frame = video.read()
if video_frame:
face_locations = fr.face_locations(frame)
unknown_face_encodings = fr.face_encodings(frame, face_locations)
face_names = []
for face_encoding in unknown_face_encodings:
matches = fr.compare_faces(encoded_faces, face_encoding)
name = "Unknown"
face_distances = fr.face_distance(encoded_faces, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = faces_name[best_match_index]
face_names.append(name)
video_frame = not video_frame
detect_faces() function identifies faces through the WebCam. At first, it'll try to locate a face in the video frame through this line of code: fr.face_locations(frame). If the code found a face, it'll encode it and compares with the previously encoded faces found in the 'Images' folder.
If the face matches then, it'll store the name of the recognized person in a list variable named 'face_names'.
5.1. Draw a Rectangle around the face and show the name of the Person
video_frame = not video_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
cv2.rectangle(frame, (left-20, top-20), (right+20, bottom+20), (0, 255, 0), 2)
cv2.rectangle(frame, (left-20, bottom -15), (right+20, bottom+20),
(0, 255, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left -20, bottom + 15), font, 0.85, (255, 255, 255), 2)
This segment of the code will draw a rectangle around the face and show the name of the person next to the rectangle.
Let's understand the parameters I passed into this function: cv2.rectangle().
frame: The video or image frame where the object or the face is presented.
(left-20, top-20): The (top, left) location of the rectangle.
(right+20, bottom+20): The (bottom, right) location of the location.
(0, 255, 0): Color of the rectangle. It's in RGB format. In this case, the color is Green.
5. 2 = Thickness of the rectangle.
The next lines are the same too.
5.2. Exit window
cv2.imshow('Video', frame)
code = cv2.waitKey(1)
if code == ord('q'):
break
cv2.destroyAllWindows()
cv2.imshow('Video', frame): It'll show the video frame until you pressed the 'q' button. Once You press the 'q' button the iteration will take a break from the while loop and close the window.
6. The Main Function
if __name__ == "__main__":
detect_faces()
7. Compact Code
For your convenience, here is the full code at once.
# Import face recognition
import face_recognition as fr
import numpy as np
import os
import cv2
def encode_faces():
encoded_data = {}
for dirpath, dnames, fnames in os.walk("./Images"):
for f in fnames:
if f.endswith(".jpg") or f.endswith(".png"):
face = fr.load_image_file("Images/" + f)
encoding = fr.face_encodings(face)[0]
encoded_data[f.split(".")[0]] = encoding
# return encoded data of images
return encoded_data
# Function for face detection & recognition
def detect_faces():
faces = encode_faces()
encoded_faces = list(faces.values())
faces_name = list(faces.keys())
video_frame = True
# Capturing video through the WebCam
# Real Time Video Streams
video = cv2.VideoCapture(0)
while True:
ret, frame = video.read()
if video_frame:
face_locations = fr.face_locations(frame)
unknown_face_encodings = fr.face_encodings(frame, face_locations)
face_names = []
for face_encoding in unknown_face_encodings:
# Comapring faces
matches = fr.compare_faces(encoded_faces, face_encoding)
name = "Unknown"
face_distances = fr.face_distance(encoded_faces, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = faces_name[best_match_index]
face_names.append(name)
video_frame = not video_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Draw a rectangular box around the face
cv2.rectangle(frame, (left-20, top-20), (right+20, bottom+20),
(0, 255, 0), 2)
# Draw a Label for showing the name of the person
cv2.rectangle(frame, (left-20, bottom -15), (right+20, bottom+20),
(0, 255, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
# Showing the name of the detected person through the WebCam
cv2.putText(frame, name, (left -20, bottom + 15), font, 0.85, (255, 255, 255), 2)
cv2.imshow('Video', frame)
code = cv2.waitKey(1)
# Press 'q' for close the video frame
if code == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == "__main__":
detect_faces()
8. Output
9. Detect and Recognize a face in an Image
So far we have learned to find and recognize a face from video streams captured through the WebCam. Now we will do the same task but from an image. The code for this task is almost the same as the previous, just a little difference, marked in the yellow line.
# Detect and Recognize a face in the image
import face_recognition as fr
import numpy as np
import os
import cv2
def encode_faces():
encoded_data = {}
for dirpath, dnames, fnames in os.walk("./Images"):
for f in fnames:
if f.endswith(".jpg") or f.endswith(".png"):
face = fr.load_image_file("Images/" + f)
encoding = fr.face_encodings(face)[0]
encoded_data[f.split(".")[0]] = encoding
# return encoded data of images
return encoded_data
# Function for face detection & recognition
def detect_faces(image):
faces = encode_faces()
encoded_faces = list(faces.values())
faces_name = list(faces.keys())
# Reading the image data
frame = cv2.imread(image, 1)
# detect faces in the image
face_locations = fr.face_locations(frame)
unknown_face_encodings = fr.face_encodings(frame, face_locations)
face_names = []
for face_encoding in unknown_face_encodings:
# Comapring faces
matches = fr.compare_faces(encoded_faces, face_encoding)
name = "Unknown"
face_distances = fr.face_distance(encoded_faces, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = faces_name[best_match_index]
face_names.append(name)
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Draw a rectangular box around the face
# cv2.rectangle image
cv2.rectangle(frame, (left-20, top-20), (right+20, bottom+20), (0, 255, 0), 2)
# Draw a Label for showing the name of the person
cv2.rectangle(frame, (left-20, bottom -15), (right+20, bottom+20),
(0, 255, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
# Showing the name of the detected person through the WebCam
cv2.putText(frame, name, (left -20, bottom + 15), font, 0.85, (255, 255, 255), 2)
while True:
cv2.imshow('Video', frame)
code = cv2.waitKey(1)
# Press 'q' for close the video frame
if code == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == "__main__":
detect_faces("sample.jpg")
In this case, I passed a sample image file("sample.jpg") to the "detect_faces()" function for recognizing any faces in that. The image file should be present in the main directory where the python program file is in.
9.1. Output
10. Download the Code
Download the Code from my GitHub page(https://github.com/subhankar-rakshit) through the download button.
☛Visit Also: Tic Tac Toe Game in Python: with Artificial Intelligence
11. Summary
In this tutorial, we build a face recognition project in Python. It'll recognize the faces of the Money Heist Characters through the WebCam. You can also use it to identify someone else's face. Just put the picture of that person in the 'Images' folder beforehand.
Try to recognize your face using this project and let me know if it could do that. I hope you'll enjoy this. For any query related to this topic, leave your comment below. You'll get a reply soon.
Thanks for reading!💙
PySeek