Face Capture - Encode - Recognition
In the grand tapestry of human perception, recognizing a familiar face is as natural as distinguishing an orange from an apple. Our brains effortlessly bridge the gap between the visual input and the 'aha' moment of recognition. When we see a friend like Jim, our mind lights up with an immediate 'There's Jim'. This intuitive process is facial recognition at its finest.
However, the world of computers and machines operates on a different wavelength. They lack the human-like spontaneity. Instead, their journey through facial recognition is a structured one:
The system encounters a face.
It meticulously collects and deciphers data.
This data is archived and paired with a name.
When the system encounters the same face again, it retraces its steps, gathering and analyzing the data.
The critical step: a systematic comparison between the new data and the stored records.
Only upon a positive match does it declare 'Eureka!' and reveal the identity.
In this blog, we will venture into the realm of face recognition, where computer algorithms replace our intuitive cognition, uncovering faces, connecting names, and unlocking the potential of this transformative technology.
In this project, we'll dissect the process into its constituent parts, breaking down the complexities of face detection, encoding, and recognition. Each step brings us closer to a comprehensive understanding of how machines are capable of matching faces to identities. We'll introduce and explore each code file, shedding light on its unique role in achieving the end goal of recognizing faces with remarkable accuracy.
Fig 1: Basic Flowchart of the project structure, specifying and explaining the usage of each file
Capture.py
This Python file is based on capturing images of the user and saving them in a folder for future comparison and recognition.
- Prerequisites
Before you start, make sure you have the necessary libraries installed. You can install them using pip.
OpenCV (cv2): This library is used for image processing.
face_recognition: A popular Python library that simplifies face recognition tasks.
pickle: A module for serializing and deserializing Python objects.
import cv2
import os
import pickle
import face_recogntion
from os.path import join
(Pip is a package manager for Python that allows you to install additional libraries and packages that are not part of the standard Python library, for example in node.js we have npm)
pip install pip install opencv-python face_recognition numpy cvzone
- Setting Up Environment
Now, in our Python file, we import the packages we installed. Then we proceed to open our webcam.
import cv2
import face_recognition
import cvzone
cam = cv2.VideoCapture(0)
data = cv2.CascadeClassifier("harrfile.xml")
The harrfile.xml you see in the code you provided refers to a trained Haar Cascade Classifier XML file.
- Capturing Faces
The code shows a loop that continuously captures frames from the webcam. If its not able to then it prints a message at the terminal. Else converts the frame captured into greyscale.
Then it uses the detectMultiScale method which is used to detect faces by sliding a detection window across the image and applying the learned patterns to identify areas that resemble faces. The coordinates of the detected faces are then returned (x, y, w, h) and by using cvzone.cornerRect rectangles are drawn around these regions to visually highlight the detected faces.
white True:
check, frame = cam.read()
if not check:
print("Failed to capture frame from the webcam")
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
coordinates = data.detectMultieScale(gray)
for x,y,w,h in coordinates:
cvzone.cornerReact(frame, [x,y,w,h], rt=0)
cv2.imshow("capture", frame)
- Saving the captured Image
Next, we proceed to capture the image and save it in our images folder. Using the cv2.waitKey function, If the 'S' key, prompts you to enter the name of the person. Next, we save the current frame (which contains the detected face) using the cv2.imwrite function. We save it as a JPG image file in a specified folder. It constructs the filename using the provided name (entered by the user) and saves it in the specified folder. Make sure to replace "Path_To_your_images_folder/" with the actual path to the folder where you want to save the images. To release the camera, we press ‘B’. This causes the webcam to close (otherwise released).
key = cv2.waitKey(1)
if key == ord("s") or key == ord("S")
name = input("Enter the name of the person = ")
cv2.imwrite(filename = "Path_to_Your_Images_folder/" + name + ".jpg", img = frame)
break
elif key == 66 or key == 98:
break
cam.release()
Encoding.py
This file is to encode the face captured, that is all the images in the folder. These are then sent to the main file for comparison.
- Prerequisites
Perquisites for this file are the same as capture.py
- Listing Paths
In this code snippet, pathlist stores a list of filenames in a specified directory. A loop processes each filename: it reads the corresponding image, appends it to imglist, and extracts the image's identifier (name without the file extension), adding it to the ids list. Essentially, this code populates two lists: imglist with image data and ids with image identifiers. These lists are crucial for further processing and analysis of the images in the specified directory.
pathlist = os.listdir("Path_to_your_images_folder")
imglist = []
ids = []
for path in pathlist:
imglist.append(cv2.imread(join("path_to_your_images_folder", path)))
s = (os.path.splitext(path)[0])
ids.append(s)
- Encoding Images
Now we proceed to the encoding part. Firstly, we end up converting the images to the RGB color format, and then extract facial encodings using the face_recognition library. These encodings are numerical representations of the facial features. The function collects these encodings into a list called encodelist and returns it. This list is essential for comparing and recognizing faces based on their unique features. It enables tasks like identifying individuals or verifying identity through face recognition. The function is a crucial step in preparing images for subsequent face recognition operations because at the end we use the pickle. dump package to dump the encoded files in our main.py file.
def encoding(imglist):
ecodelist = []
for img in imglist:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
encode = face_recognitino.face_encodings(img)[0]
encodelist.append(encode)
return encodelist
Capture.py
The code snippet in this file works by continuously capturing video frames from a webcam, making it suitable for various applications. Here's a detailed breakdown of its core components:
Frame Capture: The code utilizes the webcam
to capture video frames.
Face Detection: For the initial step, the code employs a Haar Cascade Classifier, an established machine-learning model for object detection. In this case, it's used to locate faces within the video frames. When a face is detected, its coordinates (x, y, width, and height) are determined.
Face Encoding: The next crucial step involves encoding the detected faces using the face_recognition
library. This process translates the facial features and characteristics into numerical representations, making it easier to compare and recognize faces.
Face Comparison: The code then compares the encoded faces with a pre-existing set of known face encodings. The code calculates the similarity between the detected face and the known faces, resulting in a list of Boolean values (`matches`) indicating whether a match was found.
Highlighting Detected Faces: As a visual aid, the code draws rectangles around the detected faces in the video frames. This serves both for visual confirmation of the detection and as a reference for the user or system administrator.
Displaying Recognized Names: When a match is found (i.e., a detected face matches one of the known faces), the code retrieves the name or identifier associated with that known face. It then superimposes this name onto the video frame near the detected face, providing real-time identification of individuals.
Webcam Release: After all of this we proceed to close the webcam after pressing the key specified.
Congratulations! You're now equipped to capture and recognize faces. With these tools in your arsenal, you can capture and recognize the faces of anyone you choose.
As you continue your journey in this exciting realm, remember that innovation and exploration are your greatest allies. Feel free to adapt, experiment, and push the boundaries of what's possible with these scripts.
From enhancing security to enabling creative applications, the potential is boundless. We're excited to see what you'll create and the impact you'll make. So, go forth, capture the world's faces, and let your imagination soar. Happy coding!
This post was written by Agrima Jain