-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
74 lines (64 loc) · 2.26 KB
/
main.rs
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
use image::{DynamicImage, Rgb};
use imageproc::drawing::draw_hollow_rect_mut;
use imageproc::rect::Rect;
use serde_json::json;
use facegrep::{create_detector, detect_faces};
fn main() {
// Parse args
let args: Vec<String> = std::env::args().into_iter().collect();
let cmd_name: &str = args[0].as_ref();
if args.len() < 2 || args[1] == "-h" {
println!("Usage: {} <image-path> [--json]", cmd_name);
println!("Example: {} ~/Downloads/my-image.jpg", cmd_name);
std::process::exit(0);
}
let image_path = args[1].clone();
let model_path: Option<String> = match std::env::var("RUSTFACE_MODEL") {
Ok(v) => Some(v),
Err(_e) => None,
};
if model_path.is_none() {
println!("[ERROR] Missing environment variable: RUSTFACE_MODEL");
std::process::exit(1);
}
let json_output = args.len() == 3 && args[2] == "--json";
// Init detector with model
let mut detector = create_detector(model_path.as_deref().unwrap());
// Load image
let image: DynamicImage = match image::open(&image_path) {
Ok(image) => image,
Err(message) => {
println!("Failed to read image: {}", message);
std::process::exit(1)
}
};
// Detect the faces
let mut rgb = image.to_rgb8();
let (faces, detector_msg) = detect_faces(&mut *detector, &image.to_luma8());
let mut faces_json: Vec<String> = Vec::new();
// Draw bounding boxes
for face in faces {
let bbox = face.bbox();
let rect = Rect::at(bbox.x(), bbox.y()).of_size(bbox.width(), bbox.height());
draw_hollow_rect_mut(&mut rgb, rect, Rgb([255, 0, 0]));
if json_output {
let face_json = json!({
"score": face.score(),
"bbox": json!({"x": bbox.x(), "y": bbox.y() })
});
faces_json.push(face_json.to_string());
}
}
// Save output file
let output_file = format!("{}-facegrep.png", &image_path);
let result = match rgb.save(output_file.clone()) {
Ok(_) => output_file,
Err(message) => message.to_string(),
};
if json_output {
println!("[{}]", faces_json.join(","));
} else {
println!("{}", detector_msg);
println!("{}", result)
}
}