Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ jobs:
- run: nix flake check
- run: nix develop --command cargo fmt -- --check
- run: nix develop --command cargo clippy
- run: nix develop --command cargo test
- run: nix build
84 changes: 84 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/oblichey-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ bincode = "1.3.3"
serde = { version = "1.0.204", features = ["derive"] }
serde_with = "3.9.0"
toml = "0.8.19"
mockall = "0.13.0"
mockall_double = "0.3.1"

[features]
rgb-webcam = []
Expand Down
93 changes: 91 additions & 2 deletions crates/oblichey-cli/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use num::{NumCast, Zero};
use num::{NumCast, Unsigned, Zero};
use std::{
fmt::Debug,
ops::{Add, Mul, Neg, Sub},
Expand Down Expand Up @@ -40,7 +40,7 @@ fn calculate_distance<T: Vec2DNumber>(min: T, max: T) -> Option<T> {
<T as NumCast>::from((max_f32 - min_f32).abs())
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Vec2D<T: Vec2DNumber> {
pub x: T,
pub y: T,
Expand Down Expand Up @@ -123,7 +123,9 @@ impl<T: Vec2DNumber> Rectangle<T> {
pub fn size(&self) -> Option<Vec2D<T>> {
Some(Vec2D::new(self.width()?, self.height()?))
}
}

impl<T: Vec2DNumber + Unsigned> Rectangle<T> {
/// Takes two `Rectangle`s and calculates the ratio between the area of their intersection and
/// the area of their union
pub fn intersection_over_union(&self, other: &Self) -> Option<f32> {
Expand Down Expand Up @@ -180,3 +182,90 @@ impl<T: Vec2DNumber> Rectangle<T> {
}
}
}

#[cfg(test)]
mod tests {
use super::{Rectangle, Vec2D};
use core::f32;

#[test]
fn calculates_rectangle_size() {
let test_cases = vec![
(Vec2D::new(0, 0), Vec2D::new(1, 1), Vec2D::new(1, 1)),
(Vec2D::new(2, 3), Vec2D::new(3, 2), Vec2D::new(1, 1)),
(Vec2D::new(-5, -4), Vec2D::new(5, 4), Vec2D::new(10, 8)),
(Vec2D::new(-4, 3), Vec2D::new(4, 3), Vec2D::new(8, 0)),
(Vec2D::new(4, -3), Vec2D::new(-4, 3), Vec2D::new(8, 6)),
];

for (min, max, expected_size) in test_cases {
let rectangle = Rectangle::new(min, max);
let size = rectangle.size().expect("Failed to calculate size");

assert_eq!(size, expected_size);
}
}

#[test]
fn calculates_intersection_over_union() {
let test_cases: Vec<(Rectangle<u32>, Rectangle<u32>, f32)> = vec![
(
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
1.0,
),
(
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(3, 1)),
Rectangle::new(Vec2D::new(1, 0), Vec2D::new(4, 1)),
0.5,
),
(
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
Rectangle::new(Vec2D::new(1, 1), Vec2D::new(2, 2)),
0.0,
),
];

for (rectangle_a, rectangle_b, expected_result) in test_cases {
let result = rectangle_a
.intersection_over_union(&rectangle_b)
.expect("Failed to calculate intersection over union");
assert!((result - expected_result).abs() <= f32::EPSILON);
}
}

#[test]
fn filters_out_colliding_rectangles() {
let test_cases: Vec<(Vec<Rectangle<u32>>, usize)> = vec![
(
vec![
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
],
1,
),
(
vec![
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
Rectangle::new(Vec2D::new(1, 1), Vec2D::new(2, 2)),
],
2,
),
(
vec![
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
Rectangle::new(Vec2D::new(0, 0), Vec2D::new(1, 1)),
Rectangle::new(Vec2D::new(2, 2), Vec2D::new(3, 3)),
Rectangle::new(Vec2D::new(2, 2), Vec2D::new(3, 3)),
],
2,
),
];

for (rectangles, expected_len) in test_cases {
let mut rectangles = rectangles.clone();
Rectangle::filter_out_colliding(&mut rectangles);
assert_eq!(rectangles.len(), expected_len);
}
}
}
3 changes: 3 additions & 0 deletions crates/oblichey-cli/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ impl Gui {
FaceForGUIAnnotationWarning::NotRecognized => {
("Not recognized".to_owned(), FACE_RECTANGLE_GREY_COLOR)
}
FaceForGUIAnnotationWarning::TooManyFaces => {
("Too many faces".to_owned(), FACE_RECTANGLE_GREY_COLOR)
}
},
FaceForGUIAnnotation::ScanningState {
scanned_sample_count,
Expand Down
4 changes: 4 additions & 0 deletions crates/oblichey-cli/src/models/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::camera::Frame;
use crate::geometry::{Rectangle, Vec2D};
use burn::tensor::backend::Backend;
use burn::tensor::{Tensor, TensorData};
#[cfg(test)]
use mockall::automock;

/// The size of the image the detector model takes as input
pub const DETECTOR_INPUT_SIZE: Vec2D<u32> = Vec2D { x: 640, y: 480 };
Expand All @@ -15,6 +17,8 @@ pub struct FaceDetector<B: Backend> {
model: Model<B>,
}

#[cfg_attr(test, automock)]
#[cfg_attr(test, allow(unused))]
impl<B: Backend> FaceDetector<B> {
pub fn new(device: &B::Device) -> Self {
Self {
Expand Down
4 changes: 4 additions & 0 deletions crates/oblichey-cli/src/models/recognizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{
processors::face::{FaceEmbedding, FaceEmbeddingData, FaceRecognitionData},
};
use burn::tensor::{backend::Backend, Tensor, TensorData};
#[cfg(test)]
use mockall::automock;

/// The size of the image the recognizer model takes as input
pub const RECOGNIZER_INPUT_SIZE: Vec2D<u32> = Vec2D { x: 128, y: 128 };
Expand All @@ -15,6 +17,8 @@ pub struct FaceRecognizer<B: Backend> {
model: Model<B>,
}

#[cfg_attr(test, automock)]
#[cfg_attr(test, allow(unused))]
impl<B: Backend> FaceRecognizer<B> {
pub fn new(device: &B::Device) -> Self {
Self {
Expand Down
Loading