PIXELBANKv8.2.1
Menu

Cosine Similarity Face Matching

Implement face verification using cosine similarity between face embeddings. The goal is to determine if two faces match by comparing their embedding vectors.

Face recognition networks output embedding vectors, which are dense representations of facial features. The similarity between two faces can be measured using the cosine similarity formula, which calculates the dot product of two vectors divided by the product of their magnitudes.

Here are the steps to calculate cosine similarity:

  1. Obtain face embedding vectors from a face recognition network.
  2. Calculate the dot product of the two embedding vectors.
  3. Calculate the magnitudes of the two embedding vectors.
  4. Compute the cosine similarity using the formula.
similarity=e1e2e1e2=cos(θ)\text{similarity} = \frac{\mathbf{e}_1 \cdot \mathbf{e}_2}{\|\mathbf{e}_1\| \|\mathbf{e}_2\|} = \cos(\theta)

This technique is widely used in security and surveillance systems.

Example:

Input:
embeddings = [[1,0,0], [0,1,0], [0.9,0.1,0]]
query = [1, 0, 0]
threshold = 0.8
Output:
[(0, 1.0), (2, 0.99)]
Reasoning:

Similarities:

  • emb[0] vs query: cos([1,0,0], [1,0,0]) = 1.0 ✓
  • emb[1] vs query: cos([0,1,0], [1,0,0]) = 0.0 ✗
  • emb[2] vs query: cos([0.9,0.1,0], [1,0,0]) = 0.99 ✓

Matches above 0.8 threshold: indices 0 and 2

Constraints:

  • embeddings: List of face embedding vectors (N, D)
  • query: Query face embedding (D,)
  • threshold: Similarity threshold for matching
  • Return: List of (index, similarity) for matches above threshold
Editor

Test Results

0/0
Run code to see test results.