rust_faces/
detection.rs

1use crate::Rect;
2use ndarray::ArrayViewD;
3use thiserror::Error;
4
5/// Error type for RustFaces.
6#[derive(Error, Debug)]
7pub enum RustFacesError {
8    /// IO errors.
9    #[error("IO error: {0}")]
10    IoError(std::io::Error),
11    /// Errors related to image processing
12    #[error("Image error: {0}")]
13    ImageError(String),
14    /// Errors related to inference engine (e.g. ONNX runtime)
15    #[error("Inference error: {0}")]
16    InferenceError(String),
17    /// Other errors.
18    #[error("Other error: {0}")]
19    Other(String),
20}
21
22impl From<std::io::Error> for RustFacesError {
23    fn from(err: std::io::Error) -> Self {
24        RustFacesError::IoError(err)
25    }
26}
27
28pub type RustFacesResult<R> = Result<R, RustFacesError>;
29
30/// Face detection result.
31#[derive(Debug, Clone)]
32pub struct Face {
33    /// Face's bounding rectangle.
34    pub rect: Rect,
35    /// Confidence of the detection.
36    pub confidence: f32,
37    /// Landmarks of the face.
38    pub landmarks: Option<Vec<(f32, f32)>>,
39}
40
41/// Face detector trait.
42pub trait FaceDetector: Sync + Send {
43    /// Detects faces in the given image.
44    ///
45    /// # Arguments
46    ///
47    /// * `image` - Image to detect faces in. Should be in RGB format.
48    fn detect(&self, image: ArrayViewD<u8>) -> RustFacesResult<Vec<Face>>;
49}