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.
embedding = model(face_image) distance = compare(embedding1, embedding2)
embedding1 = model(face_image1) embedding2 = model(face_image2) distance = np.linalg.norm(embedding1 - embedding2)
distance = np.linalg.norm(embedding1 - embedding2)
threshold = 0.6
is_same = distance < thresholdimport 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}")
emb1 and emb2 stored as lists?import numpy as np emb1 = np.array([0.1, 0.2, 0.3]) emb2 = np.array([0.1, 0.25, 0.35]) distance = np.linalg.norm(emb1 - emb2) print(round(distance, 3))
emb1 = [0.1, 0.2, 0.3] emb2 = [0.1, 0.25, 0.35] distance = (emb1 - emb2).norm() print(distance)
embA = [0.3, 0.4, 0.5] and embB = [0.31, 0.39, 0.52]. You want to decide if they belong to the same person using a threshold of 0.05 on Euclidean distance. Which code correctly implements this decision?Pass the test and this pattern moves unknown