feat: Added facenet

This commit is contained in:
uttarayan21
2025-08-08 15:01:25 +05:30
parent a3ea01b7b6
commit d52b69911f
9 changed files with 208 additions and 94 deletions

View File

@@ -4,11 +4,11 @@ pub use color::Rgba8;
use ndarray::{Array1, Array3, ArrayViewMut3};
pub trait Draw<T> {
fn draw(&mut self, item: T, color: color::Rgba8, thickness: usize);
fn draw(&mut self, item: &T, color: color::Rgba8, thickness: usize);
}
impl Draw<Aabb2<usize>> for Array3<u8> {
fn draw(&mut self, item: Aabb2<usize>, color: color::Rgba8, thickness: usize) {
fn draw(&mut self, item: &Aabb2<usize>, color: color::Rgba8, thickness: usize) {
item.draw(self, color, thickness)
}
}

View File

@@ -5,10 +5,17 @@ pub trait Roi<'a, Output> {
type Error;
fn roi(&'a self, aabb: Aabb2<usize>) -> Result<Output, Self::Error>;
}
pub trait RoiMut<'a, Output> {
type Error;
fn roi_mut(&'a mut self, aabb: Aabb2<usize>) -> Result<Output, Self::Error>;
}
pub trait MultiRoi<'a, Output> {
type Error;
fn multi_roi(&'a self, aabbs: &[Aabb2<usize>]) -> Result<Output, Self::Error>;
}
#[derive(thiserror::Error, Debug, Copy, Clone)]
pub enum RoiError {
#[error("Region of intereset is out of bounds")]
@@ -36,7 +43,7 @@ impl<'a, T: Num> RoiMut<'a, ArrayViewMut3<'a, T>> for Array3<T> {
let x2 = aabb.x2();
let y1 = aabb.y1();
let y2 = aabb.y2();
if x1 >= x2 || y1 >= y2 || x2 > self.shape()[1] || y2 > self.shape()[0] {
if x1 > x2 || y1 > y2 || x2 > self.shape()[1] || y2 > self.shape()[0] {
return Err(RoiError::RoiOutOfBounds);
}
Ok(self.slice_mut(ndarray::s![y1..y2, x1..x2, ..]))
@@ -95,3 +102,47 @@ pub fn reborrow_test() {
};
dbg!(y);
}
impl<'a> MultiRoi<'a, Vec<ArrayView3<'a, u8>>> for Array3<u8> {
type Error = RoiError;
fn multi_roi(&'a self, aabbs: &[Aabb2<usize>]) -> Result<Vec<ArrayView3<'a, u8>>, Self::Error> {
let (height, width, _channels) = self.dim();
let outer_aabb = Aabb2::from_x1y1x2y2(0, 0, width, height);
aabbs
.iter()
.map(|aabb| {
let slice_arg =
bbox_to_slice_arg(aabb.clamp(&outer_aabb).ok_or(RoiError::RoiOutOfBounds)?);
Ok(self.slice(slice_arg))
})
.collect::<Result<Vec<_>, RoiError>>()
}
}
impl<'a, 'b> MultiRoi<'a, Vec<ArrayView3<'b, u8>>> for ArrayView3<'b, u8> {
type Error = RoiError;
fn multi_roi(&'a self, aabbs: &[Aabb2<usize>]) -> Result<Vec<ArrayView3<'b, u8>>, Self::Error> {
let (height, width, _channels) = self.dim();
let outer_aabb = Aabb2::from_x1y1x2y2(0, 0, width, height);
aabbs
.iter()
.map(|aabb| {
let slice_arg =
bbox_to_slice_arg(aabb.clamp(&outer_aabb).ok_or(RoiError::RoiOutOfBounds)?);
Ok(self.slice_move(slice_arg))
})
.collect::<Result<Vec<_>, RoiError>>()
}
}
fn bbox_to_slice_arg(
aabb: Aabb2<usize>,
) -> ndarray::SliceInfo<[ndarray::SliceInfoElem; 3], ndarray::Ix3, ndarray::Ix3> {
// This function should convert the bounding box to a slice argument
// For now, we will return a dummy value
let x1 = aabb.x1();
let x2 = aabb.x2();
let y1 = aabb.y1();
let y2 = aabb.y2();
ndarray::s![y1..y2, x1..x2, ..]
}