rustface/common/
mod.rs

1// This file is part of the open-source port of SeetaFace engine, which originally includes three modules:
2//      SeetaFace Detection, SeetaFace Alignment, and SeetaFace Identification.
3//
4// This file is part of the SeetaFace Detection module, containing codes implementing the face detection method described in the following paper:
5//
6//      Funnel-structured cascade for multi-view face detection with alignment awareness,
7//      Shuzhe Wu, Meina Kan, Zhenliang He, Shiguang Shan, Xilin Chen.
8//      In Neurocomputing (under review)
9//
10// Copyright (C) 2016, Visual Information Processing and Learning (VIPL) group,
11// Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China.
12//
13// As an open-source face recognition engine: you can redistribute SeetaFace source codes
14// and/or modify it under the terms of the BSD 2-Clause License.
15//
16// You should have received a copy of the BSD 2-Clause License along with the software.
17// If not, see < https://opensource.org/licenses/BSD-2-Clause>.
18
19mod image_pyramid;
20
21use std::mem;
22
23pub use self::image_pyramid::{resize_image, ImageData, ImagePyramid};
24
25#[derive(Copy, Clone, Debug)]
26pub struct Rectangle {
27    x: i32,
28    y: i32,
29    width: u32,
30    height: u32,
31}
32
33impl Rectangle {
34    #[inline]
35    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
36        Rectangle {
37            x,
38            y,
39            width,
40            height,
41        }
42    }
43
44    #[inline]
45    pub fn x(&self) -> i32 {
46        self.x
47    }
48
49    #[inline]
50    pub fn set_x(&mut self, x: i32) {
51        self.x = x;
52    }
53
54    #[inline]
55    pub fn y(&self) -> i32 {
56        self.y
57    }
58
59    #[inline]
60    pub fn set_y(&mut self, y: i32) {
61        self.y = y;
62    }
63
64    #[inline]
65    pub fn width(&self) -> u32 {
66        self.width
67    }
68
69    #[inline]
70    pub fn set_width(&mut self, width: u32) {
71        self.width = width;
72    }
73
74    #[inline]
75    pub fn height(&self) -> u32 {
76        self.height
77    }
78
79    #[inline]
80    pub fn set_height(&mut self, height: u32) {
81        self.height = height;
82    }
83}
84
85#[derive(Clone, Debug)]
86pub struct FaceInfo {
87    bbox: Rectangle,
88    roll: f64,
89    pitch: f64,
90    yaw: f64,
91    score: f64,
92}
93
94impl Default for FaceInfo {
95    fn default() -> Self {
96        FaceInfo {
97            bbox: Rectangle::new(0, 0, 0, 0),
98            roll: 0.0,
99            pitch: 0.0,
100            yaw: 0.0,
101            score: 0.0,
102        }
103    }
104}
105
106impl FaceInfo {
107    #[inline]
108    pub fn new() -> Self {
109        FaceInfo::default()
110    }
111
112    #[inline]
113    pub fn bbox(&self) -> &Rectangle {
114        &self.bbox
115    }
116
117    #[inline]
118    pub fn bbox_mut(&mut self) -> &mut Rectangle {
119        &mut self.bbox
120    }
121
122    #[inline]
123    pub fn set_score(&mut self, score: f64) {
124        self.score = score;
125    }
126
127    #[inline]
128    pub fn score(&self) -> f64 {
129        self.score
130    }
131}
132
133pub struct Seq<T, G>
134where
135    G: Fn(&T) -> T + Sized,
136{
137    generator: G,
138    next: T,
139}
140
141impl<T, G> Seq<T, G>
142where
143    G: Fn(&T) -> T + Sized,
144{
145    pub fn new(first_element: T, generator: G) -> Self {
146        Seq {
147            generator,
148            next: first_element,
149        }
150    }
151}
152
153impl<T, G> Iterator for Seq<T, G>
154where
155    G: Fn(&T) -> T + Sized,
156{
157    type Item = T;
158
159    #[inline]
160    fn next(&mut self) -> Option<Self::Item> {
161        let next = (self.generator)(&self.next);
162        let current = mem::replace(&mut self.next, next);
163        Some(current)
164    }
165}
166
167#[cfg(test)]
168mod tests {
169    use super::Seq;
170
171    #[test]
172    pub fn test_seq_take() {
173        let seq = Seq::new(0, |x| x + 1);
174        assert_eq!(vec![0, 1, 2, 3, 4], seq.take(5).collect::<Vec<i32>>());
175    }
176
177    #[test]
178    pub fn test_seq_take_while() {
179        let seq = Seq::new(0, |x| x + 1);
180        assert_eq!(
181            vec![0, 1, 2, 3, 4],
182            seq.take_while(|x| *x < 5).collect::<Vec<i32>>()
183        );
184    }
185}