Secured Identity Verification with Azure Face API
Azure Face API, part of Azure's Cognitive Services suite, is a powerful cloud-based facial recognition and analysis service that AI developers and tech leads can easily integrate into their applications via a REST API or SDK. It provides advanced capabilities such as face detection, identification (matching faces to known identities), verification (confirming if two faces belong to the same person), and even emotion recognition from facial expressions.
These AI-driven features power high-value business use cases – enabling secure, touchless access control, delivering highly personalized user experiences, and streamlining identity verification in frictionless digital onboarding. The technology's real-world impact ranges from enhanced security to improved customer engagement.
To illustrate these capabilities, the accompanying blog post presents a simple Python notebook example that uses Azure Face API to identify and verify faces by matching incoming images against a repository of known faces, providing a hands-on guide to implementing these capabilities in practice.
Step 1: Import all necessary libraries & create Face client
# Import Azure AI Vision library
pip install --upgrade azure-ai-vision-face
# Import necessary libraries and create the client
from dotenv import load_dotenv
import os
load_dotenv()
from azure.core.credentials import AzureKeyCredential
from azure.ai.vision.face import FaceClient
from azure.ai.vision.face.models import (
FaceDetectionModel,
FaceRecognitionModel
)
import os
# 2. Your endpoint and key
ENDPOINT = os.getenv("url")
KEY = os.getenv("key")
# 3. Authenticate
credential = AzureKeyCredential(KEY)
face_client = FaceClient(endpoint=ENDPOINT, credential=credential)
Step 2: Create a function to detect & generate Face ID from an image
def detect_face_id(image_path: str) -> str:
"""
Detects the first face in the image using the latest models
and returns its transient faceId.
"""
with open(image_path, "rb") as f:
img = f.read()
faces = face_client.detect(
img,
detection_model=FaceDetectionModel.DETECTION03,
recognition_model=FaceRecognitionModel.RECOGNITION04,
return_face_id=True
)
if not faces:
raise RuntimeError(f"No face detected in {image_path}")
return faces[0].face_id
Recommended by LinkedIn
Step 3: Create a function to verify the input image against a list of reference images
def verify_against_repository(input_image: str, reference_images: list[str]):
"""
Verifies the input image against each reference image in the repo,
printing whether they match and the confidence score.
"""
input_id = detect_face_id(input_image)
for ref in reference_images:
ref_id = detect_face_id(ref)
result = face_client.verify_face_to_face(
face_id1=input_id,
face_id2=ref_id
)
print(f"Comparing to {os.path.basename(ref)}:")
print(f" Is identical? {result['isIdentical']}")
print(f" Confidence: {result['confidence']:.2f}\n")
Step 4: Prepare a list of reference images with which input image would be compared against
from pathlib import Path
cwd = Path(os.getcwd())
pattern = "refimage*"
matched = list(cwd.glob(pattern))
print(matched)
Step 5: Test the face verification for both matching images and also image of a completely different individual
inp_image_path='inpimage2.jpg'
# display the input image
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open(inp_image_path)
plt.imshow(img)
plt.axis('off')
plt.show()
verify_against_repository(inp_image_path,matched)
For matching images, the result is displayed as below:
Comparing to refimage1.jpg:
Is identical? True
Confidence: 0.93
Comparing to refimage2.jpg:
Is identical? True
Confidence: 0.94
Comparing to refimage3.jpg:
Is identical? True
Confidence: 0.90
Comparing to refimage4.jpg:
Is identical? True
Confidence: 0.86
Happy learning!
Assistant Accountant at Shell
3dAn amazing interface to add to my PC and phone
Assistant Accountant at Shell
3dAn amazing interface to add to my phone/PC.
Technical Specialist, Architect - Data & AI at Microsoft
1wVery well explained and good snip of code, Sankha !!