broken: Remove the FaceDetectionConfig

This commit is contained in:
uttarayan21
2025-08-05 19:17:31 +05:30
parent 043a845fc1
commit f7aae32caf

View File

@@ -6,50 +6,13 @@ use nalgebra::{Point2, Vector2};
use ndarray_resize::NdFir; use ndarray_resize::NdFir;
use std::path::Path; use std::path::Path;
pub struct FaceDetectionConfig { pub struct FaceDetectionConfig {}
anchor_sizes: Vec<Vector2<usize>>,
steps: Vec<usize>,
variance: Vec<f32>,
threshold: f32,
nms_threshold: f32,
}
impl FaceDetectionConfig { impl FaceDetectionConfig {}
pub fn with_min_sizes(mut self, min_sizes: Vec<Vector2<usize>>) -> Self {
self.anchor_sizes = min_sizes;
self
}
pub fn with_steps(mut self, steps: Vec<usize>) -> Self {
self.steps = steps;
self
}
pub fn with_variance(mut self, variance: Vec<f32>) -> Self {
self.variance = variance;
self
}
pub fn with_threshold(mut self, threshold: f32) -> Self {
self.threshold = threshold;
self
}
pub fn with_nms_threshold(mut self, nms_threshold: f32) -> Self {
self.nms_threshold = nms_threshold;
self
}
}
impl Default for FaceDetectionConfig { impl Default for FaceDetectionConfig {
fn default() -> Self { fn default() -> Self {
FaceDetectionConfig { FaceDetectionConfig {}
anchor_sizes: vec![
Vector2::new(16, 32),
Vector2::new(64, 128),
Vector2::new(256, 512),
],
steps: vec![8, 16, 32],
variance: vec![0.1, 0.2],
threshold: 0.8,
nms_threshold: 0.4,
}
} }
} }
pub struct FaceDetection { pub struct FaceDetection {
@@ -89,79 +52,6 @@ pub struct FaceDetectionOutput {
impl FaceDetectionModelOutput { impl FaceDetectionModelOutput {
pub fn postprocess(self, config: &FaceDetectionConfig) -> Result<FaceDetectionProcessedOutput> { pub fn postprocess(self, config: &FaceDetectionConfig) -> Result<FaceDetectionProcessedOutput> {
let mut anchors = Vec::new();
for (k, &step) in config.steps.iter().enumerate() {
let feature_size = 1024 / step;
let min_sizes = config.anchor_sizes[k];
let sizes = [min_sizes.x, min_sizes.y];
for i in 0..feature_size {
for j in 0..feature_size {
for &size in &sizes {
let cx = (j as f32 + 0.5) * step as f32 / 1024.0;
let cy = (i as f32 + 0.5) * step as f32 / 1024.0;
let s_k = size as f32 / 1024.0;
anchors.push((cx, cy, s_k, s_k));
}
}
}
}
let mut boxes = Vec::new();
let mut scores = Vec::new();
let mut landmarks = Vec::new();
let var0 = config.variance[0];
let var1 = config.variance[1];
let bbox_data = self.bbox;
let conf_data = self.confidence;
let landmark_data = self.landmark;
let num_priors = bbox_data.shape()[1];
for idx in 0..num_priors {
let dx = bbox_data[[0, idx, 0]];
let dy = bbox_data[[0, idx, 1]];
let dw = bbox_data[[0, idx, 2]];
let dh = bbox_data[[0, idx, 3]];
let (anchor_cx, anchor_cy, anchor_w, anchor_h) = anchors[idx];
let pred_cx = anchor_cx + dx * var0 * anchor_w;
let pred_cy = anchor_cy + dy * var0 * anchor_h;
let pred_w = anchor_w * (dw * var1).exp();
let pred_h = anchor_h * (dh * var1).exp();
let x_min = pred_cx - pred_w / 2.0;
let y_min = pred_cy - pred_h / 2.0;
let x_max = pred_cx + pred_w / 2.0;
let y_max = pred_cy + pred_h / 2.0;
let score = conf_data[[0, idx, 1]];
if score > config.threshold {
boxes.push(Aabb2::from_x1y1x2y2(x_min, y_min, x_max, y_max));
scores.push(score);
let left_eye_x = landmark_data[[0, idx, 0]] * anchor_w * var0 + anchor_cx;
let left_eye_y = landmark_data[[0, idx, 1]] * anchor_h * var0 + anchor_cy;
let right_eye_x = landmark_data[[0, idx, 2]] * anchor_w * var0 + anchor_cx;
let right_eye_y = landmark_data[[0, idx, 3]] * anchor_h * var0 + anchor_cy;
let nose_x = landmark_data[[0, idx, 4]] * anchor_w * var0 + anchor_cx;
let nose_y = landmark_data[[0, idx, 5]] * anchor_h * var0 + anchor_cy;
let left_mouth_x = landmark_data[[0, idx, 6]] * anchor_w * var0 + anchor_cx;
let left_mouth_y = landmark_data[[0, idx, 7]] * anchor_h * var0 + anchor_cy;
let right_mouth_x = landmark_data[[0, idx, 8]] * anchor_w * var0 + anchor_cx;
let right_mouth_y = landmark_data[[0, idx, 9]] * anchor_h * var0 + anchor_cy;
landmarks.push(FaceLandmarks {
left_eye: Point2::new(left_eye_x, left_eye_y),
right_eye: Point2::new(right_eye_x, right_eye_y),
nose: Point2::new(nose_x, nose_y),
left_mouth: Point2::new(left_mouth_x, left_mouth_y),
right_mouth: Point2::new(right_mouth_x, right_mouth_y),
});
}
}
Ok(FaceDetectionProcessedOutput {
bbox: boxes,
confidence: scores,
landmarks,
})
} }
} }