0
0
Computer Visionml~5 mins

Face embedding and comparison in Computer Vision

Choose your learning style9 modes available
Introduction
Face embedding turns a face image into a list of numbers that captures its unique features. Comparing these lists helps us check if two faces are the same person.
Unlocking your phone with face recognition.
Finding friends in photos automatically.
Checking if a person is allowed to enter a building.
Organizing photos by who is in them.
Detecting if a face in a video matches a known person.
Syntax
Computer Vision
embedding = model(face_image)
distance = compare(embedding1, embedding2)
The model converts a face image into a vector called an embedding.
Comparing embeddings usually means calculating the distance between them.
Examples
Calculate Euclidean distance between two face embeddings to see how similar they are.
Computer Vision
embedding1 = model(face_image1)
embedding2 = model(face_image2)
distance = np.linalg.norm(embedding1 - embedding2)
Decide if two faces match by checking if their distance is below a set threshold.
Computer Vision
distance = np.linalg.norm(embedding1 - embedding2)
threshold = 0.6
is_same = distance < threshold
Sample Model
This code simulates face embeddings by flattening and normalizing small arrays as fake face images. It then compares distances between embeddings to show which faces are more similar.
Computer Vision
import numpy as np
from sklearn.preprocessing import normalize

def dummy_face_embedding(face_image):
    # Simulate embedding by flattening and normalizing image
    flat = face_image.flatten()
    norm = np.linalg.norm(flat)
    return flat / norm if norm > 0 else flat

def compare_embeddings(emb1, emb2):
    return np.linalg.norm(emb1 - emb2)

# Simulated face images as 3x3 grayscale arrays
face1 = np.array([[0, 1, 2], [1, 2, 3], [2, 3, 4]], dtype=float)
face2 = np.array([[0, 1, 2], [1, 2, 3], [2, 3, 5]], dtype=float)
face3 = np.array([[10, 10, 10], [10, 10, 10], [10, 10, 10]], dtype=float)

emb1 = dummy_face_embedding(face1)
emb2 = dummy_face_embedding(face2)
emb3 = dummy_face_embedding(face3)

dist12 = compare_embeddings(emb1, emb2)
dist13 = compare_embeddings(emb1, emb3)

print(f"Distance between face1 and face2: {dist12:.4f}")
print(f"Distance between face1 and face3: {dist13:.4f}")
OutputSuccess
Important Notes
Real face embeddings come from deep learning models trained on many faces.
Lower distance means faces are more alike; higher means more different.
Choosing a good threshold depends on your application and data.
Summary
Face embedding converts a face image into a unique number list.
Comparing embeddings helps decide if two faces are the same.
Distance between embeddings measures similarity.