Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

refactor(faces): model crop and plane shift inputs

Dylan M. Taylor694b237f0a47
diff --git a/crates/engine/src/faces_worker.rs b/crates/engine/src/faces_worker.rs
index 61dbc2544..c9512f6cb 100644
--- a/crates/engine/src/faces_worker.rs
+++ b/crates/engine/src/faces_worker.rs
@@ -13,7 +13,7 @@ use std::sync::{Arc, Condvar, Mutex, PoisonError};
use std::time::Duration;
use lightcraft_catalog::PhotoId;
-use lightcraft_faces::align::{Rgb, align_to_template, crop_box};
+use lightcraft_faces::align::{FaceCrop, Rgb, align_to_template, crop_box};
use lightcraft_faces::runtime::Embedder;
use lightcraft_faces::yunet::{Detector, Face, Options};
use lightcraft_geom::Rect;
@@ -221,13 +221,11 @@ pub(crate) fn process(p: Prepared, embedder: &Embedder) -> Done {
Some((_, f)) => align_to_template(&img, &scaled(f), iw, ih),
None => crop_box(
&img,
- rect.x0 as f32 * w as f32,
- rect.y0 as f32 * h as f32,
- rect.x1 as f32 * w as f32,
- rect.y1 as f32 * h as f32,
- BOX_PAD,
- iw,
- ih,
+ FaceCrop {
+ bounds: (rect.x0 as f32 * w as f32, rect.y0 as f32 * h as f32, rect.x1 as f32 * w as f32, rect.y1 as f32 * h as f32),
+ padding: BOX_PAD,
+ output_size: (iw, ih),
+ },
),
};
// shown by the detector's box where it found this face: every face then has the same tightness
diff --git a/crates/faces/src/align.rs b/crates/faces/src/align.rs
index 4982325ae..387d028a6 100644
--- a/crates/faces/src/align.rs
+++ b/crates/faces/src/align.rs
@@ -19,6 +19,14 @@ pub struct Rgb<'a> {
pub height: usize,
}
+/// Face bounds, padding factor, and output dimensions for box-based alignment.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct FaceCrop {
+ pub bounds: (f32, f32, f32, f32),
+ pub padding: f32,
+ pub output_size: (usize, usize),
+}
+
impl Rgb<'_> {
fn valid(&self) -> bool {
self.width > 0 && self.height > 0 && self.width.checked_mul(self.height).and_then(|n| n.checked_mul(3)) == Some(self.data.len())
@@ -145,7 +153,10 @@ pub fn align_to_template(img: &Rgb, landmarks: &[(f32, f32); 5], out_w: usize, o
}
/// A square around a face box (pixels of `img`), `pad` times its larger side, as `out_w` × `out_h`.
-pub fn crop_box(img: &Rgb, x0: f32, y0: f32, x1: f32, y1: f32, pad: f32, out_w: usize, out_h: usize) -> Option<Vec<u8>> {
+pub fn crop_box(img: &Rgb, crop: FaceCrop) -> Option<Vec<u8>> {
+ let (x0, y0, x1, y1) = crop.bounds;
+ let pad = crop.padding;
+ let (out_w, out_h) = crop.output_size;
if ![x0, y0, x1, y1, pad].iter().all(|v| v.is_finite()) {
return None;
}
@@ -247,16 +258,16 @@ mod tests {
}
}
let img = Rgb { data: &data, width: w, height: h };
- let out = crop_box(&img, 90.0, 40.0, 110.0, 60.0, 2.0, 64, 64).unwrap();
+ let out = crop_box(&img, FaceCrop { bounds: (90.0, 40.0, 110.0, 60.0), padding: 2.0, output_size: (64, 64) }).unwrap();
// the 20 px block fills the middle half of the 64 px output
let at = |x: usize, y: usize| out[(y * 64 + x) * 3 + 1];
assert!(at(32, 32) > 250 && at(20, 20) > 250 && at(44, 44) > 250 && at(5, 5) < 5 && at(60, 32) < 5);
- assert!(crop_box(&img, 5.0, 5.0, 5.0, 5.0, 2.0, 64, 64).is_none(), "an empty box");
- assert!(crop_box(&img, f32::NAN, 0.0, 10.0, 10.0, 2.0, 64, 64).is_none());
- assert!(crop_box(&img, 0.0, 0.0, 10.0, 10.0, 2.0, 0, 64).is_none());
- assert!(crop_box(&img, 0.0, 0.0, 10.0, 10.0, 2.0, 100_000, 64).is_none());
+ assert!(crop_box(&img, FaceCrop { bounds: (5.0, 5.0, 5.0, 5.0), padding: 2.0, output_size: (64, 64) }).is_none(), "an empty box");
+ assert!(crop_box(&img, FaceCrop { bounds: (f32::NAN, 0.0, 10.0, 10.0), padding: 2.0, output_size: (64, 64) }).is_none());
+ assert!(crop_box(&img, FaceCrop { bounds: (0.0, 0.0, 10.0, 10.0), padding: 2.0, output_size: (0, 64) }).is_none());
+ assert!(crop_box(&img, FaceCrop { bounds: (0.0, 0.0, 10.0, 10.0), padding: 2.0, output_size: (100_000, 64) }).is_none());
let bad = Rgb { data: &data[..10], width: w, height: h };
- assert!(crop_box(&bad, 0.0, 0.0, 10.0, 10.0, 2.0, 64, 64).is_none(), "wrong data length");
+ assert!(crop_box(&bad, FaceCrop { bounds: (0.0, 0.0, 10.0, 10.0), padding: 2.0, output_size: (64, 64) }).is_none(), "wrong data length");
}
#[test]
@@ -264,7 +275,7 @@ mod tests {
let data = vec![80u8; 50 * 50 * 3];
let img = Rgb { data: &data, width: 50, height: 50 };
// a box mostly outside the picture is padded by clamping, not an error
- assert!(crop_box(&img, -30.0, -30.0, 20.0, 20.0, 1.5, 32, 32).is_some());
+ assert!(crop_box(&img, FaceCrop { bounds: (-30.0, -30.0, 20.0, 20.0), padding: 1.5, output_size: (32, 32) }).is_some());
let far: [(f32, f32); 5] = [(1e6, 1e6), (1e6 + 5.0, 1e6), (1e6 + 2.0, 1e6 + 3.0), (1e6, 1e6 + 6.0), (1e6 + 5.0, 1e6 + 6.0)];
let _ = align_to_template(&img, &far, 112, 112);
let tiny: [(f32, f32); 5] = [(1.0, 1.0), (1.01, 1.0), (1.0, 1.01), (1.01, 1.01), (1.005, 1.005)];
diff --git a/crates/faces/src/net/kernels.rs b/crates/faces/src/net/kernels.rs
index 71ae95874..6ad7e83dd 100644
--- a/crates/faces/src/net/kernels.rs
+++ b/crates/faces/src/net/kernels.rs
@@ -1,5 +1,5 @@
//! Safe CPU kernels shared by detection and recognition. No inner thread pool.
-use super::*;
+use super::{ConvJob, Graph, MAX_TENSOR, NetError, Node, Result, Tensor, chw, conv_direct, pads, pair, program, shape, shape_err};
use ndarray::{ArrayView2, ArrayViewMut2, linalg::general_mat_mul};
const BLOCK: usize = 256;
#[derive(Default)]
@@ -30,7 +30,16 @@ fn multiply(w: &[f32], x: &[f32], y: &mut [f32], m: usize, k: usize, n: usize) -
}
/// For equal-width, unit-stride maps a shifted plane is one contiguous copy. Only horizontal
/// border stripes need masking; clipping the flat source range also removes vertical padding.
-fn shifted_plane(plane: &[f32], width: usize, ky: usize, kx: usize, pt: usize, pl: usize, start: usize, row: &mut [f32]) -> Result<()> {
+#[derive(Clone, Copy)]
+struct PlaneShift {
+ width: usize,
+ kernel: (usize, usize),
+ padding: (usize, usize),
+ start: usize,
+}
+
+fn shifted_plane(plane: &[f32], row: &mut [f32], shift: PlaneShift) -> Result<()> {
+ let PlaneShift { width, kernel: (ky, kx), padding: (pt, pl), start } = shift;
let end = start + row.len();
let delta = (ky as i128 - pt as i128) * width as i128 + kx as i128 - pl as i128;
let lo = (-delta).clamp(start as i128, end as i128);
@@ -145,13 +154,8 @@ pub(super) fn conv(n: &Node, x: &Tensor, g: &Graph, scratch: &mut Scratch) -> Re
if sh == 1 && sw == 1 && ow == w {
shifted_plane(
plane,
- w,
- ky,
- kx,
- pt,
- pl,
- start,
row.get_mut(..len).ok_or_else(|| NetError::Shape("short lower panel".into()))?,
+ PlaneShift { width: w, kernel: (ky, kx), padding: (pt, pl), start },
)?;
continue;
}
diff --git a/crates/faces/src/net/program.rs b/crates/faces/src/net/program.rs
index e00e51da1..db7dcba5d 100644
--- a/crates/faces/src/net/program.rs
+++ b/crates/faces/src/net/program.rs
@@ -9,7 +9,7 @@
use std::borrow::Cow;
-use super::*;
+use super::{ConvJob, Data, HashMap, Net, NetError, Result, kernels, pads, pair, shape, shape_err, unsupported};
/// Value 0 is the network's input.
pub const INPUT: usize = 0;
diff --git a/crates/faces/src/net/shape.rs b/crates/faces/src/net/shape.rs
index a1594ca6b..39e585c61 100644
--- a/crates/faces/src/net/shape.rs
+++ b/crates/faces/src/net/shape.rs
@@ -1,5 +1,5 @@
//! Bounded, allocation-free (apart from small shape lists) graph checking.
-use super::*;
+use super::{Data, Graph, HashMap, MAX_TENSOR, NetError, Node, Result, pads, pair, shape_err, unsupported};
use crate::graph::{Attr, Weight};
pub(super) fn elements(s: &[usize]) -> Result<usize> {