What Is Face Recognition with Python?
Face recognition is the process of detecting or confirming the identity of individuals based on facial images. Various companies and applications often use a facial recognition system to verify the identity of people in videos, photos, or the real world.
Python is a high-level, object-oriented programming language. It has dynamic semantics and built-in data structures, making it useful for developing applications quickly. Its dynamic binding and typing mean that Python serves as a glue or scripting language, helping integrate components within an application or between applications.
There are several use cases for face recognition. The following discusses Python-based methods for implementing face recognition, focusing on two approaches. First, using the face_recognition library built into Python, and second, using the OpenCV computer vision library.
In This Article
Face Recognition with the Python face_recognition Library
This tutorial will use the Python library called face_recognition, which has methods and algorithms developed using deep learning techniques.
Setting Up Libraries
To set up the libraries required for face recognition through Python:
1. Go to the terminal and download the dlib library:
pip install dlib
The dlib library is a C++ toolkit that features machine learning tools and algorithms. It is important to install it to use the face_recognition library.
2. Use the following command to install the face_recognition library:
pip install face recognition
3. Use the following command to download the OpenCV library:
pip install opencv
The OpenCV library will come in handy for pre-processing steps.
4. Use the following commands to install other relevant libraries:
import numpy as np
import face_recognition as faceRegLib
Loading Training Images
To load the sample image to the face_recognition library:
Run the following code in the Python shell:
demo_img_bgr = faceRegLib.load_image_file('demo_tutorial_images/obama.jpg')>/p>
emo_img_rgb = cv2.cvtColor(demo_img_bgr,cv2.COLOR_BGR2RGB)
These commands convert a training image from the BGR format to RGB using OpenCV since the
face_recognition library only accepts RGB images. The images before and after conversion will get displayed.
Learning To Detect And Locate Faces
To use the face_recognition library for face detection and location:
1. Run the following code to prepare an image as input for the face_recognition library:
obama_img_rgb = cv2.cvtColor(obama_img,cv2.COLOR_BGR2RGB)
2. Use the following commands to locate the face in the image and create its copy:
face = face_recognition.face_locations(obama_img_rgb)
copy = obama_img_rgb.copy()
3. Use the following commands to draw rectangles around the faces:
cv2.rectangle(copy, (face, face),(face, face), (255,0,255), 2)
The last commands show both versions of the image side-by-side, one with the face located and the original one.
Face Recognition With Face Encodings Example
To use face encodings while doing face recognition with Python:
Use the following code to perform face recognition in Python through face encodings:
img_obama = faceRegLib.load_image_file('demo_training_images/obama.jpg')
img_obama = cv2.cvtColor(img_obama,cv2.COLOR_BGR2RGB)
obama_face = faceRegLib.face_locations(img_obama) demo_train_encode = faceRegLib.face_encodings(img_obama) demo = faceRegLib.load_image_file('demo_training_images/obama.jpg')
demo = cv2.cvtColor(demo, cv2.COLOR_BGR2RGB)
demo_encode = faceRegLib.face_encodings(demo) print(faceRegLib.compare_faces([demo_train_encode],demo_encode))
cv2.rectangle(img_obama, (face, face),(face, face), (255,0,255), 1)
This code took two images of Barack Obama and used the compare_faces method to return True since both images had the same face.
The model converts every image it gets into a numerical encoding. First, the face_encodings method returns an encoding of the input image. Then, the compare_faces method compares the encodings through a distance parameter to see if there is a match. Then, the encoding with the least distance gets selected since it’s the closest match. After getting the match, the image’s title is retrieved using the image’s index in the list.
Face Recognition in Python with OpenCV
OpenCV is a popular computer vision library available in Python. It was originally written in C/C++ and now provides Python bindings. It has machine learning-backed methods for advanced face detection. The algorithms detect faces in an image by breaking it down into thousands of patterns and features that it matches. The tasks to match such features are called classifiers.
This tutorial shows an example of using OpenCV to detect faces in images. It requires that OpenCV is pre-installed on the machine.
Install and Import OpenCV
To import the relevant libraries:
Use the following command to import openCV:
Reading Image and Detecting Faces
To read images and detect faces in them using OpenCV:
1. Open a terminal and use the following command to take the path to the image and cascade names:
demoImagePath = sys.argv
democascPath = sys.argv
The democascPath is the path to the cascade, the XML file with data to help detect faces.
2. Create a default cascade and initialize it with the face cascade for detecting faces:
demoFaceCascade = cv2.CascadeClassifier(democascPath)
The face cascade gets loaded into the memory with this command.
3. Use the following code to read the images present on the demoImagePath:
demoImage = cv2.imread(demoImagePath)
demoImageInGray = cv2.cvtColor(demoImage, cv2.COLOR_BGR2GRAY)
The last command changes the image into grayscale since OpenCV operations happen on grayscale images.
4. Use the following code to detect faces in the image:
facesInDemoImage = demoFaceCascade.detectMultiScale(
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
The detectMultiScale method detects objects in an image. Since here it is getting called on a face cascade, it will detect faces. The scaleFactor compensates for any faces that may be bigger due to being closer to the camera. minSize specifies the size of the moving window the algorithm uses to detect objects.
The command returns a list of rectangles possibly containing faces.
5. Use the following command to draw the rectangles on the image:
for (a, b, c, d) in facesInDemoImage:
cv2.rectangle(image, (a, b), (a+c, b+d), (0, 255, 0), 2)
rectangle()function draws the rectangles on the image.