Face Verification with Photo ID using DeepFace Framework

Madhura Jayade
5 min readNov 20, 2023

--

Face Recognition:

Face recognition is a bio-metric technology that involves identifying or verifying individuals based on their facial features. It is a form of computer vision and image processing that has applications in various fields including security, surveillance, authentication, and human-computer interaction. The process typically involves several steps, including face detection, feature extraction, and matching.

Types of Face Recognition:

  1. Verification (1:1): This type of face recognition is used to confirm whether a person is who they claim to be.
  2. Identification (1:N): In this case, the system compares the detected face to a database of faces to find a match without requiring the user to claim their identity.

Deep Learning for Face Recognition:

Now-a-days CNN models are used for extracting facial features. These CNN models have been trained on large datasets of Face images, viz., LFW Dataset, CASIA WebFace Dataset, VGGFace2 Dataset etc. Consequently, we can use the these trained models directly for face recognition.

Here, we are going to compare and analyse different CNN models for Face verification using DeepFace Framework.

DeepFace:

DeepFace is a lightweight face recognition and facial attribute analysis (age, gender, emotion and race) framework for python. It is a hybrid face recognition framework wrapping state-of-the-art models: VGG-Face, Google FaceNet, OpenFace, Facebook DeepFace, DeepID, ArcFace, Dlib and SFace.

from deepface import DeepFace

result = DeepFace.verify(img1_path, img2_path, model_name="VGG-Face", detector_backend="opencv", distance_metric="cosine", enforce_detection=True, align=True, normalization="base")
Print(result)

In the above code, img1_path, img2_path can be in the form of exact image path as string or numpy array (BGR) of image or based64 encoded images. The model name also can be changed to the preferred one from the list of state of the art models.

Storing Photo IDs in MongoDB:

For any authentication purpose, the photo IDs are stored digitally in cloud database. Here we are storing it in MongoDB in the base64 encoded format.

import pymongo
import os
import cv2
import numpy
import base64

# Step 1: Connect to MongoDB
client = pymongo.MongoClient("mongodb+srv://your_mongodb_url") # Change the URL to match your MongoDB instance
db = client["test"] # Change the database name as needed
collection = db["face_images"] # Change the collection name as needed

#Store BASE64 format image:
def store_base64_image(image_path):
with open(image_path, "rb") as image_file:
image_data = image_file.read()
base64_image = base64.b64encode(image_data).decode("utf-8")
filename = os.path.basename(image_path)
image_document = {
"name": filename,
"data": base64_image
}
collection.insert_one(image_document)
print(f"Image '{filename}' stored in MongoDB.")

The URL for MongoDB can be get after the MongoDB account creation. MongoDB Compass can be installed in the system and can be connected to the URL. Database and xyz can be created using the MongoDB compass.

Creating API for Face verification:

There are many Python API frameworks like Django REST, Flask RESTful, FastAPI etc. Here we are using Flask to create API for face verification. The input of the API are real time captured photo of a person (in the base64 format) and photo ID name of that person. Search function is written to find image the given person in database. The input and output are supposed to be in the json format.

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/face_verification', methods=['POST'])
def post_example():
input_data = request.get_json()
param1 = input_data['webcam_img']
param2 = input_data['name_in_ID']

# Search for an image by its name in mongoDB
def search_image_by_name(param2):
query = {"name": str(param2)}
result = collection.find_one(query)
return result["data"]

# Add "data:image/" before the base64 encoded image data
image2_data = search_image_by_name(param2)
data_2 = f"data:image/{param2.split('.')[-1]};base64,{image2_data}"
data_1 = param1
result = DeepFace.verify(data_1, data_2, model_name="VGG-Face", detector_backend="opencv", distance_metric="cosine", enforce_detection=True, align=True, normalization="base")

# Return the result as part of the JSON response
response_data = {
"verification_distance": result['distance'],
"verification_threshold": result['threshold'],
"verification_result": bool(result['verified'])
}
return jsonify(response_data)

if __name__ == '__main__':
app.run(debug=True)

There are 3 important metrics in the output. The distance metric is used to yield dissimilarity score between the two images. In the DeepFace framework, there are 2 types of distance metric viz., Cosine distance and Euclidean distance. The threshold can be adjusted according to the testing results such that it can give correct verification result as TRUE or FALSE.

Postman for API testing:

Results of Testing:

5 person’s real time images are captured and they are compared with their stored photo IDs. Also we can see that 5 people’s data is cross compared with each other so that accurate value of threshold can be decided.

There are some constraints to get accurate matching score viz., the resolution of photo ID to be stored is presumed to be more (> 672*672) and the photo on ID card is not apparently to be more than 6 years old. Over and above that the brightness while capturing photo, presence spectacles in one photo and absence in the other one can affect the dissimilarity score and hence the accuracy of face verification.

Conclusion:

The accuracy of the model can be decided by the clear differentiating range of True positives and False positives. Based on these ranges threshold can be adjusted.

From the above results, we can state that VGG-Face gives best performance whereas performances of Facenet-512 and Arc-Face are acceptable.

References:

I hope this content will help you to find best Face recognition model for your problem.

Thank you!!!

--

--

Madhura Jayade

A Machine Learning Engineer | Fascinated by Artificial Intelligence, Computer Vision and data science | Work to help computers see