Face Anti-Spoofing for Facial Recognition In Python

In today’s world, facial recognition technology is widely being used for security purposes, but it comes with some vulnerabilities. Imagine having a facial recognition-based entrance system, where someone could gain access to your building using a printed photo of an authorized person. This is where face anti-spoofing or liveness detection comes into play. In this blog post, we’ll explore how to implement these security measures using the DeepFace library in Python. We’ll cover real-time analysis, face detection, face verification, face recognition, and even analyzing facial attributes like age, gender, emotion, and race. With just a few lines of code, you can enhance the security of your facial recognition systems and protect against spoof attacks.

Face Anti-Spoofing Test

Vlog

You can either continue to read this tutorial or watch the following video tutorial. They both cover hands-on the face anti-spoofing for facial recognition pipelines with DeepFace in python.


🙋‍♂️ You may consider to enroll my top-rated machine learning course on Udemy

Decision Trees for Machine Learning

Pre-trained Model

DeepFace utilizes MiniVision’s Silent Face Anti-Spoofing models in the background, which are licensed under the Apache License 2.0. This means the models are free to use for both private and commercial purposes. Built on the PyTorch framework, the model functions as a classification system, expecting 80×80 RGB images as input. The implementation involves feeding these inputs into the MiniFASNetV1 and MiniFASNetV2 models and summing the probabilities in the classification layer. Based on the dominant probability, the system determines whether the input image is real or spoofed.

MiniFASNet Backbone

Of course, you don’t need to understand the details of the backbone model. You can easily use it through DeepFace with just a single line of code!

Real Time Spoofing Test

You can perform spoofing tests using DeepFace’s real-time analysis module. This module supports both facial recognition and facial attribute analysis, including age, gender, emotion, and race prediction. In this experiment, I will disable facial attribute analysis and just perform facial recognition. By default, it can detect and analyze faces in real-time through the webcam, distinguishing between real and spoofed ones.

The Stream function simply requires the database path, which is the exact location of the folder containing your facial images. For this experiment, I put the following images of myself and Zuckerberg in the target database folder.

Facial Database
from deepface import DeepFace
DeepFace.stream(
   db_path = "/Users/sefik/Desktop/db",
   enable_face_analysis=False
)
Default Behavior’s of Real Time Analysis with DeepFace

As you can see from the image above, the Stream function can detect faces from the webcam and identify them from our database. However, even a printed image of Mark’s face can still be identified. Imagine implementing this technology at the entrance of Meta—someone could use a picture of Mark to gain access! This poses a significant security risk.

To address this security issue, you simply need to set the anti-spoofing argument to True in the Stream function.

from deepface import DeepFace
DeepFace.stream(
   db_path = '/Users/sefik/Desktop/db',
   enable_face_analysis=False,
   anti_spoofing = True,
)
Anti-Spoofing Enabled Real Time Analysis

Once anti-spoofing is enabled, the system can still detect faces belonging to both myself and Mark. However, it distinguishes between them by highlighting my face in green and Mark’s face in red. This indicates that a spoof attack has been detected in Mark’s face. With this added layer of security, individuals attempting to gain access using a printed image of Mark’s face will no longer be able to enter Meta’s building.





It’s important to note that the Stream function of DeepFace is designed solely for demonstration purposes and does not store analysis results anywhere. However, you can still utilize the anti-spoofing module within facial recognition pipelines using DeepFace, enhancing security in real-world applications.

Real Time Spoofig In The Browser

Meanwhile, you can run face verification tasks directly captured image from your webcam in real time in your browser with its custom ui built with ReactJS.

Face Detection

Similar to the stream function, you have the option to set the anti_spoofing argument to True in the extract faces function. This will result in an additional key being returned in the response, containing is_real and antispoofing_score keys. By checking the is_real key, you can determine the authenticity of the extracted faces.

face_objs = DeepFace.extract_faces(img_path=img_path, anti_spoofing = True)
assert face_objs[0].get("is_real") is True, "spoof attacked detected"

Face Verification

To enable liveness testing in face verification, you can set the optional anti_spoofing argument to True in the verify function. Ensure that the exact image paths of the image pairs are provided as inputs to the function for accurate verification results.

DeepFace.verify(
   img1_path="dataset/img1.jpg",
   img2_path="dataset/img2.jpg",
   anti_spoofing = True
)

In the event that a spoof is detected in one of the image pairs, the function will raise a ValueError with the message “Spoof detected in given image.” This exception serves as an alert for potential security breaches during face verification.

Face Recognition

While searching for an identity in a database, the find function of DeepFace fulfills this task. By setting the anti_spoofing argument to True in the find function, it will verify whether the given image path is real or fake. It’s important to note that this process does not conduct spoofing tests on the items within your database; rather, it specifically evaluates the authenticity of the provided image path for the searched identity.

DeepFace.find(
   img_path="dataset/img1.jpg",
   db_path="/Users/sefik/Desktop/db",
   anti_spoofing=True
)

Similar to the verify function, if the given image is not real, the find function will raise a ValueError with the message “Spoof detected in the given image.” This exception serves as an indication of potential spoofing attempts during the identity search process.

Facial Attribute Analysis

Finally, spoof attacks can also be tested in age, gender, emotion, and race & ethnicity prediction tasks, similar to other functionalities within DeepFace. This comprehensive approach ensures that the system remains robust and reliable across various facial recognition tasks, safeguarding against potential security breaches posed by spoofing attempts.

DeepFace.analyze(
   img_path="dataset/img1.jpg",
   anti_spoofing = True
)

In the event that the given image is fake, the system will raise a ValueError with the message “Spoof detected in the given image.” This serves as a crucial alert, helping to prevent potential security breaches caused by spoofing attempts.





API

DeepFace comes with a restful web service for verification, represent and analysis tasks. You can set optional anti_spoofing boolean argument to true if you want perform anti spoofing analysis in your API calls.

Also, you can use containerized deepface service as well! But pytorch is an optional dependency and you have to customize dockerfile and install this package if you want to use that feature.

React JS Based UI

DeepFace also has an UI built with React JS for real time analysis purposes.

Anti-spoofing feature is available in the facial recognition ui built with React JS and DeepFace

Liveness detection is also available in the facial attribute analysis ui with React JS and DeepFace

Conclusion

In conclusion, implementing face anti-spoofing techniques is crucial for enhancing the security of facial recognition systems. With the DeepFace library in Python, you can easily perform real-time analysis to detect and prevent spoof attacks, ensuring that only authorized individuals gain access. By incorporating features such as face detection, verification, recognition, and attribute analysis, you can build a robust and reliable security system. Whether you’re using facial recognition for building access, personal devices, or other applications, these measures will help protect against unauthorized entry and maintain the integrity of your security infrastructure. Start enhancing your facial recognition systems today with DeepFace and take a significant step towards a more secure future.






Support this blog if you do like!

Buy me a coffee      Buy me a coffee