feat(compare): add face comparison functionality with cosine similarity
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user