1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// This file is part of the open-source port of SeetaFace engine, which originally includes three modules:
//      SeetaFace Detection, SeetaFace Alignment, and SeetaFace Identification.
//
// This file is part of the SeetaFace Detection module, containing codes implementing the face detection method described in the following paper:
//
//      Funnel-structured cascade for multi-view face detection with alignment awareness,
//      Shuzhe Wu, Meina Kan, Zhenliang He, Shiguang Shan, Xilin Chen.
//      In Neurocomputing (under review)
//
// Copyright (C) 2016, Visual Information Processing and Learning (VIPL) group,
// Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China.
//
// As an open-source face recognition engine: you can redistribute SeetaFace source codes
// and/or modify it under the terms of the BSD 2-Clause License.
//
// You should have received a copy of the BSD 2-Clause License along with the software.
// If not, see < https://opensource.org/licenses/BSD-2-Clause>.

extern crate serde;

mod image_pyramid;

use std::mem;

pub use self::image_pyramid::{resize_image, ImageData, ImagePyramid};

#[derive(Copy, Clone, Debug, serde::Serialize)]
pub struct Rectangle {
    x: i32,
    y: i32,
    width: u32,
    height: u32,
}

impl Rectangle {
    #[inline]
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
        Rectangle {
            x,
            y,
            width,
            height,
        }
    }

    #[inline]
    pub fn x(&self) -> i32 {
        self.x
    }

    #[inline]
    pub fn set_x(&mut self, x: i32) {
        self.x = x;
    }

    #[inline]
    pub fn y(&self) -> i32 {
        self.y
    }

    #[inline]
    pub fn set_y(&mut self, y: i32) {
        self.y = y;
    }

    #[inline]
    pub fn width(&self) -> u32 {
        self.width
    }

    #[inline]
    pub fn set_width(&mut self, width: u32) {
        self.width = width;
    }

    #[inline]
    pub fn height(&self) -> u32 {
        self.height
    }

    #[inline]
    pub fn set_height(&mut self, height: u32) {
        self.height = height;
    }
}

#[derive(Clone, Debug, serde::Serialize)]
pub struct FaceInfo {
    bbox: Rectangle,
    roll: f64,
    pitch: f64,
    yaw: f64,
    score: f64,
}

impl Default for FaceInfo {
    fn default() -> Self {
        FaceInfo {
            bbox: Rectangle::new(0, 0, 0, 0),
            roll: 0.0,
            pitch: 0.0,
            yaw: 0.0,
            score: 0.0,
        }
    }
}

impl FaceInfo {
    #[inline]
    pub fn new() -> Self {
        FaceInfo::default()
    }

    #[inline]
    pub fn bbox(&self) -> &Rectangle {
        &self.bbox
    }

    #[inline]
    pub fn bbox_mut(&mut self) -> &mut Rectangle {
        &mut self.bbox
    }

    #[inline]
    pub fn set_score(&mut self, score: f64) {
        self.score = score;
    }

    #[inline]
    pub fn score(&self) -> f64 {
        self.score
    }
}

pub struct Seq<T, G>
where
    G: Fn(&T) -> T + Sized,
{
    generator: G,
    next: T,
}

impl<T, G> Seq<T, G>
where
    G: Fn(&T) -> T + Sized,
{
    pub fn new(first_element: T, generator: G) -> Self {
        Seq {
            generator,
            next: first_element,
        }
    }
}

impl<T, G> Iterator for Seq<T, G>
where
    G: Fn(&T) -> T + Sized,
{
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let next = (self.generator)(&self.next);
        let current = mem::replace(&mut self.next, next);
        Some(current)
    }
}

#[cfg(test)]
mod tests {
    use super::Seq;

    #[test]
    pub fn test_seq_take() {
        let seq = Seq::new(0, |x| x + 1);
        assert_eq!(vec![0, 1, 2, 3, 4], seq.take(5).collect::<Vec<i32>>());
    }

    #[test]
    pub fn test_seq_take_while() {
        let seq = Seq::new(0, |x| x + 1);
        assert_eq!(
            vec![0, 1, 2, 3, 4],
            seq.take_while(|x| *x < 5).collect::<Vec<i32>>()
        );
    }
}