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
extern crate base64;
extern crate image;
#[cfg(feature = "mesalock_sgx")]
extern crate rustface;
use std::convert::TryFrom;
use teaclave_types::{FunctionArguments, FunctionRuntime};
#[derive(Default)]
pub struct FaceDetection;
#[derive(serde::Deserialize)]
struct FaceDetectionArguments {
image: Vec<u8>,
window_size: Option<u32>,
slide_window_step_x: Option<u32>,
slide_window_step_y: Option<u32>,
min_face_size: Option<u32>,
max_face_size: Option<u32>,
pyramid_scale_factor: Option<f32>,
score_thresh: Option<f64>,
}
impl TryFrom<FunctionArguments> for FaceDetectionArguments {
type Error = anyhow::Error;
fn try_from(arguments: FunctionArguments) -> Result<Self, Self::Error> {
use anyhow::Context;
serde_json::from_str(&arguments.into_string()).context("Cannot deserialize arguments")
}
}
impl FaceDetection {
pub const NAME: &'static str = "builtin-face-detection";
pub fn new() -> Self {
Default::default()
}
pub fn run(
&self,
arguments: FunctionArguments,
_runtime: FunctionRuntime,
) -> anyhow::Result<String> {
let arguments = FaceDetectionArguments::try_from(arguments)?;
let image = arguments.image;
let img = image::load_from_memory(&image)?;
let mut detector = rustface::create_default_detector()?;
if let Some(window_size) = arguments.window_size {
detector.set_window_size(window_size);
}
if let (Some(step_x), Some(step_y)) =
(arguments.slide_window_step_x, arguments.slide_window_step_y)
{
detector.set_slide_window_step(step_x, step_y);
}
if let Some(min_face_size) = arguments.min_face_size {
detector.set_min_face_size(min_face_size);
}
if let Some(max_face_size) = arguments.max_face_size {
detector.set_max_face_size(max_face_size);
}
if let Some(pyramid_scale_factor) = arguments.pyramid_scale_factor {
detector.set_pyramid_scale_factor(pyramid_scale_factor);
}
if let Some(score_thresh) = arguments.score_thresh {
detector.set_score_thresh(score_thresh);
}
let faces = rustface::detect_faces(&mut *detector, img);
let result = serde_json::to_string(&faces)?;
Ok(result)
}
}
#[cfg(feature = "enclave_unit_test")]
pub mod tests {
use super::*;
use serde_json::json;
use std::untrusted::fs;
use teaclave_runtime::*;
use teaclave_test_utils::*;
use teaclave_types::*;
pub fn run_tests() -> bool {
run_tests!(test_face_detection)
}
fn test_face_detection() {
let input = "fixtures/functions/face_detection/input.jpg";
let image = fs::read(input).unwrap();
let arguments = FunctionArguments::from_json(json!({
"image": &image,
"min_face_size": 20,
"score_thresh": 2.0,
"pyramid_scale_factor": 0.8,
"slide_window_step_x": 4,
"slide_window_step_y": 4
}))
.unwrap();
let input_files = StagedFiles::new(hashmap!());
let output_files = StagedFiles::new(hashmap!());
let runtime = Box::new(RawIoRuntime::new(input_files, output_files));
let result = FaceDetection::new().run(arguments, runtime).unwrap();
let json_result: serde_json::Value = serde_json::from_str(&result).unwrap();
assert_eq!(json_result.as_array().unwrap().len(), 29);
}
}