Skip to content

Embedding

Vladimir Mandic edited this page Mar 3, 2021 · 22 revisions

Face Feature Embedding and Simmilarity Compare


To use face simmilaity compare feature, you must first enable face.embedding module
and calculate embedding vectors for both first and second image you want to compare.

For example,

const myConfig = { face: { embedding: true }};
const human = new Human(myConfig);

const firstResult = await human.detect(firstImage);
const secondResult = await human.detect(secondImage);

const firstEmbedding = firstResult.face[0].embedding;
const secondEmbedding = secondResult.face[0].embedding;

const simmilarity = human.simmilarity(firstEmbedding, secondEmbedding);

console.log(`faces are ${100 * simmilarity}% simmilar`);

If the image or video frame have multiple faces and you want to match all of them, simply loop through all results.face

for (let i = 0; i < secondResult.face.length; i++) {
  const secondEmbedding = secondResult.face[i].embedding;
  const simmilarity = human.simmilarity(firstEmbedding, secondEmbedding);
  console.log(`face ${i} is ${100 * simmilarity}% simmilar`);
}

Embedding vectors are calulated values uniquely identifying a given face and presented as array of 192 float values

They can be stored as normal arrays and reused as needed

Simmilarity function is based on Eucilidean distance between all points in vector
Eucliean distance is limited case of Minkowski distance with order of 2
Minkowski distance is a nth root of sum of nth powers of distances between each point (each value in 192-member array)

Changing order can make simmilarity matching more or less sensitive:

  const distance = ((firstEmbedding.map((val, i) => (val - secondEmbedding[i])).reduce((dist, diff) => dist + (diff ** order), 0) ** (1 / order)));

Once embedding values are calculated and stored, if you want to use stored embedding values without requiring Human library you can use above formula to calculate simmilarity on the fly.

Clone this wiki locally