Files
zeekayandHanzo Dev 9679655fb0 dub: phase 3 — S3FD face detector + crop/paste
vision_models/s3fd/{mod,model,nms}: faithful port of face_alignment's SFD
(the detector MuseTalk crops from). VGG16-SSD backbone, dilated fc6/fc7,
conv6/conv7 extra layers, 6 anchor scales [4,8,16,32,64,128], max-out
background on conv3_3, L2Norm on conv3_3/4_3/5_3. detect() does BGR
mean-sub preprocess -> forward -> per-scale softmax + anchor decode
(variances 0.1/0.2) -> greedy IoU NMS, with max_side downscale + box
rescale. hanzo-vision gains crop + paste_resized (the inverse pair the
animator uses to round-trip a face patch through MuseTalk).

Tests: nms iou/decode/suppression (pure), detect_graph_runs_random_weights
(full forward+decode+nms wiring), detect_known_bbox (IoU>=0.7, weights-gated),
geometry crop/paste roundtrip + clamp.

Co-authored-by: Hanzo Dev <[email protected]>
2026-06-28 07:59:28 -07:00

87 lines
2.8 KiB
Rust

//! This crate provides vision utilities for hanzo inspired by torchvision.
//! In particular, it represents transformations on some `Self` type which are applied
//! sequentially.
//!
//! ## Example
//! ```rust
//! use hanzo_ml::Device;
//! use image::{ColorType, DynamicImage};
//! use hanzo_vision::{ApplyTransforms, Normalize, ToTensor, Transforms};
//!
//! let image = DynamicImage::new(3, 4, ColorType::Rgb8);
//! let transforms = Transforms {
//! input: &ToTensor,
//! inner_transforms: &[&Normalize {
//! mean: vec![0.5, 0.5, 0.5],
//! std: vec![0.5, 0.5, 0.5],
//! }],
//! };
//! let transformed = image.apply(transforms, &Device::Cpu).unwrap();
//! assert_eq!(transformed.dims(), &[3, 4, 3]);
//! ```
use hanzo_ml::{Device, Result, Tensor};
use image::DynamicImage;
mod geometry;
mod ops;
mod pad;
mod transforms;
pub(crate) mod utils;
pub use geometry::{crop, paste_resized};
pub use ops::{get_resize_image_size, make_pixel_mask, pad};
pub use pad::{pad_to_max_edge, pad_to_max_image_size};
pub use transforms::{InterpolateResize, Normalize, Rescale, ToTensor, ToTensorNoNorm};
/// A transform over an image. The input may vary but the output is always a Tensor.
pub trait ImageTransform {
type Input;
type Output;
fn map(&self, x: &Self::Input, device: &Device) -> Result<Self::Output>;
}
/// Transforms to apply, starting with the `input` and then with each transform in
/// `inner_transforms` applied sequentially
#[derive(Clone, Copy)]
pub struct Transforms<'a> {
pub input: &'a dyn ImageTransform<Input = DynamicImage, Output = Tensor>,
pub inner_transforms: &'a [&'a dyn ImageTransform<Input = Tensor, Output = Tensor>],
}
/// Transforms, with each of `inner_transforms` applied sequentially
#[derive(Clone, Copy)]
pub struct TensorTransforms<'a> {
pub inner_transforms: &'a [&'a dyn ImageTransform<Input = Tensor, Output = Tensor>],
}
/// Application of transforms to the Self type.
pub trait ApplyTransforms<'a> {
fn apply(&self, transforms: Transforms<'a>, device: &Device) -> Result<Tensor>;
}
impl<'a> ApplyTransforms<'a> for DynamicImage {
fn apply(&self, transforms: Transforms<'a>, device: &Device) -> Result<Tensor> {
let mut res = transforms.input.map(self, device)?;
for transform in transforms.inner_transforms {
res = transform.map(&res, device)?;
}
Ok(res)
}
}
/// Application of transforms to the Self type.
pub trait ApplyTensorTransforms<'a> {
fn apply(&self, transforms: TensorTransforms<'a>, device: &Device) -> Result<Tensor>;
}
impl<'a> ApplyTensorTransforms<'a> for Tensor {
fn apply(&self, transforms: TensorTransforms<'a>, device: &Device) -> Result<Tensor> {
let mut res = self.clone();
for transform in transforms.inner_transforms {
res = transform.map(&res, device)?;
}
Ok(res)
}
}