DEV Community

Cover image for Face Matching in 5 Lines of Python: Detect, Encode, Compare
AI Engine
AI Engine

Posted on • Originally published at ai-engine.net

Face Matching in 5 Lines of Python: Detect, Encode, Compare

Your fintech app needs KYC. A user uploads a selfie and a photo of their ID. Your backend needs to answer one question: is this the same person?

Between the shutter click and the green checkmark, three things happen. This article breaks down those three steps, then shows how to implement the whole pipeline in 5 lines of Python.

The 3 Steps of Face Matching

Every face matching system follows the same pipeline:

Step 1: Detect

Find the face in the image. The detector outputs a bounding box around each face.

Detector Accuracy (WIDER FACE) Speed Best for
RetinaFace 96.9% ~3.8s/face (CPU) Max accuracy, batch
MTCNN 84.8% ~0.4s/face Good balance
MediaPipe Lower ~0.04s/face Real-time, mobile
YuNet Good ~0.03s/face Fastest on CPU

Step 2: Encode

Convert the face into a numerical vector (embedding). Two photos of the same person produce similar vectors.

Photo of Alice → [0.23, -0.45, 0.78, 0.12, ...]  (128 numbers)
Photo of Alice → [0.21, -0.47, 0.76, 0.14, ...]  (similar!)
Photo of Bob   → [-0.67, 0.89, -0.12, 0.55, ...]  (different)
Enter fullscreen mode Exit fullscreen mode
Model LFW Accuracy Embedding Size
FaceNet (Google, 2015) 99.63% 128
ArcFace (2018) 99.83% 512
SFace (2021) 99.60% 128

Step 3: Compare

Measure the distance between two embeddings.

Metric How it works Match threshold
Cosine similarity Angle between vectors. 1.0 = identical > 0.6
Euclidean distance Straight-line distance. Lower = closer < 1.1

The Code (5 Lines)

import requests

response = requests.post(
    "https://faceanalyzer-ai.p.rapidapi.com/compare-faces",
    headers={"x-rapidapi-key": "YOUR_API_KEY", "x-rapidapi-host": "faceanalyzer-ai.p.rapidapi.com"},
    files={"source_image": open("selfie.jpg", "rb"), "target_image": open("id_photo.jpg", "rb")},
)
print(response.json()["body"])
Enter fullscreen mode Exit fullscreen mode

matchedFaces is not empty = same person. unmatchedFaces lists faces that did not match.

Real Tests

Same person, different photos: Matched: 1, Unmatched: 0. Correct.

Two different people: Matched: 0, Unmatched: 1. Correct.

Where Developers Use This

  • KYC onboarding: selfie vs ID document photo
  • Access control: employee vs face database
  • Duplicate detection: block multiple accounts with same face
  • Photo apps: group photos by person

Sources

👉 Read the full article with benchmark tables, architecture diagrams, and API response examples

Top comments (0)