feat(compare): add face comparison functionality with cosine similarity
Some checks failed
build / checks-matrix (push) Successful in 19m23s
build / codecov (push) Failing after 19m18s
docs / docs (push) Failing after 28m50s
build / checks-build (push) Has been cancelled

This commit is contained in:
uttarayan21
2025-08-21 17:34:07 +05:30
parent f8122892e0
commit bfa389b497
15 changed files with 1188 additions and 107 deletions

View File

@@ -4,6 +4,7 @@ pub mod ort;
use crate::errors::*;
use error_stack::ResultExt;
use ndarray::{Array1, Array2, ArrayView3, ArrayView4};
use ndarray_math::{CosineSimilarity, EuclideanDistance};
/// Configuration for face embedding processing
#[derive(Debug, Clone, PartialEq)]
@@ -32,9 +33,9 @@ impl FaceEmbeddingConfig {
impl Default for FaceEmbeddingConfig {
fn default() -> Self {
Self {
input_width: 160,
input_height: 160,
normalize: true,
input_width: 320,
input_height: 320,
normalize: false,
}
}
}
@@ -63,15 +64,14 @@ impl FaceEmbedding {
/// Calculate cosine similarity with another embedding
pub fn cosine_similarity(&self, other: &FaceEmbedding) -> f32 {
let dot_product = self.vector.dot(&other.vector);
let norm_self = self.vector.mapv(|x| x * x).sum().sqrt();
let norm_other = other.vector.mapv(|x| x * x).sum().sqrt();
dot_product / (norm_self * norm_other)
self.vector.cosine_similarity(&other.vector).unwrap_or(0.0)
}
/// Calculate Euclidean distance with another embedding
pub fn euclidean_distance(&self, other: &FaceEmbedding) -> f32 {
(&self.vector - &other.vector).mapv(|x| x * x).sum().sqrt()
self.vector
.euclidean_distance(other.vector.view())
.unwrap_or(f32::INFINITY)
}
/// Normalize the embedding vector to unit length