Logo

Implementing Facial Recognition with Python and OpenCV

Default Alt Text

Table of Contents

What is Facial Recognition

Facial recognition, as the name suggests, is a biometric solution designed to identify or verify a person by comparing and analyzing patterns based on the person’s facial contours. Originating in the 1960s, it was initially a manual and quite painstaking process of identifying facial features from photographs and footage. However, thanks to advancements in computing power and image recognition technology, it became possible in the late ’90s to automate this process with a level of accuracy that made it practical for widespread use. Today, facial recognition is pervasive, helping in a myriad of applications from unlocking our smartphones to identifying individuals of interest in large crowds for law enforcement. Utilizing technology like Python and OpenCV allows us to implement facial recognition systems in a more accessible and cost-effective manner, making it an essential tool in our increasingly digital world.

Python and OpenCV – Why they are chosen for facial recognition

Python, a dynamic and versatile programming language favored by developers worldwide, is often the go-to choice for developing facial recognition systems due to its simplicity and the wealth of libraries it offers. One such powerful library is OpenCV (Open Source Computer Vision Library), widely used for real-time image processing. Specifically for facial recognition, OpenCV offers pre-trained machine learning models which ease the process of developing a robust, efficient facial recognition system. Furthermore, it supports a wide range of algorithms allowing developers to choose the one most suited to their specific requirements. Combined with Python’s easy-to-understand syntax, OpenCV enables rapid development and prototyping of facial recognition systems, making them a popular choice for this task.

Setting up the Development Environment

Installation process – Necessary Python packages for facial recognition

Before we begin implementing facial recognition, it’s crucial to set up our development environment. The first step is to install necessary Python packages that offer the functionalities required for facial recognition. Some of these include OpenCV, NumPy, and dlib. OpenCV, which stands for Open Source Computer Vision Library, is fundamental for most computer vision projects, offering thousands of algorithms to recognize patterns in images and videos. NumPy works in tandem with OpenCV, providing robust data structures to manage complex datasets. Dlib, although not directly used in our example, is another highly useful library for real-world machine learning and can be employed while enhancing your recognition features. Installing these libraries might require some prerequisites, which can vary based on your operating system. It is essential to follow the specific installation guidelines to prevent compatibility issues. Once your environment is ready, we can move on to the coding part.

Python and OpenCV introduction

Python is one of the most popular programming languages in today’s tech industry. Its simplicity and versatility make it an excellent choice for a wide range of applications, including machine learning and computer vision tasks. OpenCV (Open Source Computer Vision Library), on the other hand, is a highly optimized library with over 2500 algorithms for machine and computer vision. It offers a robust interface to facilitate efficient image and video processing. The marriage of Python and OpenCV provides developers with a powerful toolset for performing complex computer vision tasks, such as facial recognition. Their combined abilities to efficiently process data and ease of usage make Python and OpenCV the ideal choice for implementing facial recognition systems.

Basics of Image Processing with OpenCV

Reading, Displaying and Saving Images

In this Python code snippet, we’ll use the OpenCV library to read an image from a file, display the image in a window, and then save the image to a new file. Here’s how it can be done:

import cv2


image = cv2.imread('input.jpg')


cv2.imshow('Image Window', image)


cv2.waitKey(0)


cv2.imwrite('output.jpg', image)


cv2.destroyAllWindows()

In the above code, `cv2.imread()` function is used to read the image from the file. It takes the image file path as an argument and returns the image in an array format. We then use `cv2.imshow()` to display the read image, and `cv2.waitKey(0)` to keep the image window open until any key is pressed. Finally, `cv2.imwrite()` is used to write the image data back to a new file, and `cv2.destroyAllWindows()` is used to close all the windows opened during this process.

Color spaces in Images

The following Python code uses OpenCV to handle color space conversion. We will input an image file and convert it from RGB (Red, Green, Blue) color space to the HSV (Hue, Saturation, Value) color space.

import cv2


image = cv2.imread('/path/to/your/image.jpg')


hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)


cv2.imwrite('/path/to/save/hsv_image.jpg', hsv_image)

In this code, the ‘cv2.imread’ function is used to load an image file. The ‘cv2.cvtColor’ function then converts this image from the BGR color space (which is how OpenCV reads images by default) to the HSV color space. Finally, the ‘cv2.imwrite’ function writes the converted image back to a file. While the RGB images are more intuitive as they correspond to the primary colors, the HSV color space can be more useful in many applications because it separates image intensity (i.e., luminance) from color information.

Image Thresholding

In the next code snippet, we’re going to focus on image thresholding techniques. The input of this code will be a grayscale image and it will output the binary image.

import cv2
import numpy as np


img = cv2.imread('image.png',0)


ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)


cv2.imshow('Binary Image',thresh1)


cv2.imwrite('binary_image.png', thresh1)


cv2.destroyAllWindows()

The code above first loads a grayscale image using the `cv2.imread()` function. Then, we use the `cv2.threshold()` function to apply a binary threshold on the image. Any pixel intensity value that is less than the threshold value (in this case, 127) turns into 0 (black), and any value that is greater than 127 turns into 255 (white), creating a binary image. This function returns two outputs – `ret` which is the threshold value that was used and `thresh1` which is the thresholded image. We then display this image using `cv2.imshow()`. Finally, we save the binary image using `cv2.imwrite()` and destroy all created windows for displaying images with `cv2.destroyAllWindows()`. This technique provides a simple segmentation of the image and can be used as a first step in systems for object detection, character recognition etc.

Implementing Facial Recognition with OpenCV

Face Detection

In this section, we’ll be implementing the Haar feature-based cascade classifiers to detect faces within an image. Cascade classifiers are a type of machine learning-based method where a cascade function is trained from a lot of positive and negative images. OpenCV provides us with pre-trained cascades that have been trained on thousands of faces and non-faces and they are saved as XML files which we can directly use in our program.

import cv2


face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')


img = cv2.imread('test.jpg')


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


faces = face_cascade.detectMultiScale(gray, 1.1, 4)


for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)


cv2.imshow('img', img)
cv2.waitKey()

This Python code utilizes OpenCV to first load the trained Haar cascade classifier, then uses it to detect faces within an image file. The identified faces are outlined with a rectangle. Using the imshow and waitKey functions, the output image with marked faces is then displayed.

Remember to replace `’test.jpg’` with the actual path of your image. The function `detectMultiScale` does the job of detecting the faces. It has two parameters — the scale factor and minimum neighbors. The scale factor compensatively reduces the size of the image at each scale, and minimum neighbors dictates how many neighbors each candidate rectangle should have to retain it. You may have to tweak these parameters according to your image for best results.

Face Recognition

This application applies the Local Binary Patterns Histograms (LBPH) method for face recognition using the OpenCV library in Python. The LBPH algorithm creates a histogram of local binary patterns from an image and looks for recognisable patterns between images for facial recognition. In the below example, we will learn the faces with the function “face_recognizer.train” and then make predictions with the function “face_recognizer.predict”.

import cv2
import numpy as np


font = cv2.FONT_HERSHEY_SIMPLEX 


face_recognizer = cv2.face.LBPHFaceRecognizer_create()


images = [cv2.imread("path_to_image_1"), cv2.imread("path_to_image_2")]
tags = [0, 1] 
face_recognizer.train(images, np.array(tags))


test_image = cv2.imread("path_to_test_image")
gray = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)

faces_rects = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);

for (x,y,w,h) in faces_rects:
     roi_gray = gray[y:y+h, x:x+w]
     label, confidence = face_recognizer.predict(roi_gray)
     cv2.putText(test_image, str(label)+"-"+str(confidence), (x, y-5), font, 0.5, (255,255,255), 2)

cv2.imshow("LBPH Face Recognition", test_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this code, we first initialize an LBPH Face Recognizer with “cv2.face.LBPHFaceRecognizer_create()”. We then train this recognizer on our training images using “face_recognizer.train()”.When an unrecognised face is encountered, the algorithm will mark the detected faces in the test image with the associated tags and confidence, based on the histogram comparison to the training dataset. Remember, the face detection and recognition processes are highly reliant on the quality and resolution of the images provided for training.

Understanding and Implementing Eigenfaces, Fisherfaces, and LBPH

Exploring multiple face recognition algorithms and evaluating their performance is essential in order to select the most suitable for your specific application. Python and OpenCV provide easy-to-use interfaces for several state-of-the-art face recognition algorithms. In the sample code below, we will use Eigenfaces, Fisherfaces and Local Binary Patterns Histogram (LBPH) methods for face recognition. We use FaceRecognizer class in OpenCV and use different functions to implement each algorithm. Lastly, we compute and compare the accuracy of all three methods to determine the most effective strategy.

import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score


images = np.load('sample_images.npy')
labels = np.load('labels.npy')


train_images, test_images, train_labels, test_labels = train_test_split(images, labels, test_size=0.2)


recognizer_eigen = cv2.face.createEigenFaceRecognizer()
recognizer_fisher = cv2.face.createFisherFaceRecognizer()
recognizer_lbph = cv2.face.createLBPHFaceRecognizer()


recognizer_eigen.train(train_images, train_labels)
recognizer_fisher.train(train_images, train_labels)
recognizer_lbph.train(train_images, train_labels)


predicted_label_eigen = recognizer_eigen.predict(test_images)
predicted_label_fisher = recognizer_fisher.predict(test_images)
predicted_label_lbph = recognizer_lbph.predict(test_images)


accuracy_eigen = accuracy_score(test_labels, predicted_label_eigen)
accuracy_fisher = accuracy_score(test_labels, predicted_label_fisher)
accuracy_lbph = accuracy_score(test_labels, predicted_label_lbph)

print('Accuracy with Eigenfaces: ',accuracy_eigen)
print('Accuracy with Fisherfaces: ',accuracy_fisher)
print('Accuracy with LBPH: ',accuracy_lbph)

With the code, we are training the three algorithms with the same training data and testing their performance on the same unseen testing data. Performance is evaluated in terms of accuracy, which shows how accurately the algorithms can predict the identity of the faces in the test data. Remember, higher accuracy is better, but it’s also significant to consider your specific application requirements and limitations when choosing a face recognition algorithm.

Conclusion

Facilitating our exploration into Python and OpenCV’s applications in facial recognition has illuminated the powerful capabilities these tools bring to image processing and machine learning. We’ve walked through setting up a foundational development environment, explored various image processing techniques, and finally applied facial recognition using OpenCV. We brought faces to light through detection and pushed further into the realm of recognition. While this post provided a startup guide, remember that the field of facial recognition is vast, and technologies are evolving rapidly. Therefore, I encourage you to continue building on this foundational knowledge by exploring more sophisticated algorithms or delving into deep learning techniques. After all, mastering these realms opens up a plethora of possibilities – from inclusive technologies that facilitate secure authentication, to innovations in entertainment, healthcare, and much more. Undeniably, the facial recognition journey with Python and OpenCV is quite exciting, and we’ve only just scratched the surface!

Share This Post

More To Explore

Default Alt Text
AWS

Integrating Python with AWS DynamoDB for NoSQL Database Solutions

This blog provides a comprehensive guide on leveraging Python for interaction with AWS DynamoDB to manage NoSQL databases. It offers a step-by-step approach to installation, configuration, database operation such as data insertion, retrieval, update, and deletion using Python’s SDK Boto3.

Default Alt Text
Computer Vision

Automated Image Enhancement with Python: Libraries and Techniques

Explore the power of Python’s key libraries like Pillow, OpenCV, and SciKit Image for automated image enhancement. Dive into vital techniques such as histogram equalization, image segmentation, and noise reduction, all demonstrated through detailed case studies.

Do You Want To Boost Your Business?

drop us a line and keep in touch