Skip to content

Commit e7295f8

Browse files
committed
Added example for serialization to save data (addresses #20)
1 parent d61d632 commit e7295f8

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

examples/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1515
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1616
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
1717
QuartzImageIO = "dca85d43-d64c-5e67-8c65-017450d5d020"
18+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1819
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"

examples/read.jl

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
#=
3+
exec julia --project="$(realpath $(dirname $0))/" "${BASH_SOURCE[0]}" "$@" -e "include(popfirst!(ARGS))" \
4+
"${BASH_SOURCE[0]}" "$@"
5+
=#
6+
7+
8+
#=
9+
Adapted from https://github.com/Simon-Hohberg/Viola-Jones/
10+
=#
11+
12+
13+
println("\033[1;34m===>\033[0;38m\033[1;38m\tLoading required libraries (it will take a moment to precompile if it is your first time doing this)...\033[0;38m")
14+
15+
include(joinpath(dirname(dirname(@__FILE__)), "src", "FaceDetection.jl"))
16+
17+
using .FaceDetection
18+
const FD = FaceDetection
19+
using Printf: @printf
20+
using Images: imresize
21+
using Serialization: deserialize
22+
23+
println("...done")
24+
25+
function main(;
26+
smart_choose_feats::Bool=false, alt::Bool=false
27+
)
28+
29+
# we assume that `smart_choose_feats = true`
30+
main_path = dirname(dirname(@__FILE__))
31+
data_path = joinpath(main_path, "data")
32+
main_image_path = joinpath(main_path, "data", "main")
33+
alt_image_path = joinpath(main_path, "data", "alt")
34+
35+
if alt
36+
# pos_testing_path = joinpath(alt_image_path, "testing", "pos")
37+
# neg_testing_path = joinpath(homedir(), "Desktop", "Assorted Personal Documents", "Wallpapers copy")
38+
pos_testing_path = joinpath(main_image_path, "testset", "faces")#joinpath(homedir(), "Desktop", "faces")#"$main_image_path/testset/faces/"
39+
neg_testing_path = joinpath(main_image_path, "testset", "non-faces")
40+
else
41+
pos_testing_path = joinpath(main_image_path, "testset", "faces")#joinpath(homedir(), "Desktop", "faces")#"$main_image_path/testset/faces/"
42+
neg_testing_path = joinpath(main_image_path, "testset", "non-faces")
43+
end
44+
45+
# pos_testing_path = joinpath(data_path, "lizzie-testset", "faces")
46+
# neg_testing_path = joinpath(data_path, "lizzie-testset", "nonfaces")
47+
48+
if ! isfile(joinpath(dirname(@__FILE__), "data", "haar-like_features"))
49+
error(throw("You do not have a data file. Ensure you run \"write.jl\" to obtain your Haar-like features before running this script/"))
50+
end
51+
52+
# read classifiers from file
53+
classifiers = deserialize(joinpath(dirname(@__FILE__), "data", "haar-like_features"))
54+
55+
FD.notify_user("Loading test faces...")
56+
57+
faces_testing = FD.load_images(pos_testing_path)[1]
58+
# faces_ii_testing = map(FD.to_integral_image, faces_testing)
59+
faces_ii_testing = map(FD.to_integral_image, faces_testing)
60+
println("...done. ", length(faces_testing), " faces loaded.")
61+
62+
FD.notify_user("Loading test non-faces..")
63+
64+
non_faces_testing = FD.load_images(neg_testing_path)[1]
65+
non_faces_ii_testing = map(FD.to_integral_image, non_faces_testing)
66+
println("...done. ", length(non_faces_testing), " non-faces loaded.\n")
67+
68+
FD.notify_user("Testing selected classifiers...")
69+
correct_faces = 0
70+
correct_non_faces = 0
71+
72+
# correct_faces = sum([FD._get_feature_vote(face, classifiers) for face in faces_ii_testing])
73+
# correct_non_faces = length(non_faces_testing) - sum([FD._get_feature_vote(nonFace, classifiers) for nonFace in non_faces_ii_testing])
74+
correct_faces = sum(FD.ensemble_vote_all(faces_ii_testing, classifiers))
75+
correct_non_faces = length(non_faces_testing) - sum(FD.ensemble_vote_all(non_faces_ii_testing, classifiers))
76+
correct_faces_percent = (float(correct_faces) / length(faces_testing)) * 100
77+
correct_non_faces_percent = (float(correct_non_faces) / length(non_faces_testing)) * 100
78+
79+
faces_frac = string(correct_faces, "/", length(faces_testing))
80+
faces_percent = string("(", correct_faces_percent, "% of faces were recognised as faces)")
81+
non_faces_frac = string(correct_non_faces, "/", length(non_faces_testing))
82+
non_faces_percent = string("(", correct_non_faces_percent, "% of non-faces were identified as non-faces)")
83+
84+
println("...done.\n")
85+
FD.notify_user("Result:\n")
86+
87+
@printf("%10.9s %10.15s %15s\n", "Faces:", faces_frac, faces_percent)
88+
@printf("%10.9s %10.15s %15s\n\n", "Non-faces:", non_faces_frac, non_faces_percent)
89+
end
90+
91+
@time main(smart_choose_feats=true, alt=false)

examples/write.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env bash
2+
#=
3+
exec julia --project="$(realpath $(dirname $0))/" "${BASH_SOURCE[0]}" "$@" -e "include(popfirst!(ARGS))" \
4+
"${BASH_SOURCE[0]}" "$@"
5+
=#
6+
7+
8+
#=
9+
Adapted from https://github.com/Simon-Hohberg/Viola-Jones/
10+
=#
11+
12+
13+
println("\033[1;34m===>\033[0;38m\033[1;38m\tLoading required libraries (it will take a moment to precompile if it is your first time doing this)...\033[0;38m")
14+
15+
include(joinpath(dirname(dirname(@__FILE__)), "src", "FaceDetection.jl"))
16+
17+
using .FaceDetection
18+
const FD = FaceDetection
19+
using Printf: @printf
20+
using Images: imresize
21+
using Serialization: serialize
22+
23+
println("...done")
24+
25+
function main(;
26+
smart_choose_feats::Bool=false, alt::Bool=false
27+
)
28+
# we assume that `smart_choose_feats = true`
29+
main_path = dirname(dirname(@__FILE__))
30+
data_path = joinpath(main_path, "data")
31+
main_image_path = joinpath(main_path, "data", "main")
32+
alt_image_path = joinpath(main_path, "data", "alt")
33+
34+
if alt
35+
pos_training_path = joinpath(alt_image_path, "pos")
36+
neg_training_path = joinpath(alt_image_path, "neg")
37+
else
38+
pos_training_path = joinpath(main_image_path, "trainset", "faces")
39+
neg_training_path = joinpath(main_image_path, "trainset", "non-faces")
40+
end
41+
42+
# pos_training_path = joinpath(data_path, "lfw-all")
43+
# neg_training_path = joinpath(data_path, "all-non-faces")
44+
45+
num_classifiers = 10
46+
47+
min_size_img = (19, 19) # default for our test dataset
48+
if smart_choose_feats
49+
# For performance reasons restricting feature size
50+
notify_user("Selecting best feature width and height...")
51+
52+
max_feature_width, max_feature_height, min_feature_height, min_feature_width, min_size_img = determine_feature_size(pos_training_path, neg_training_path)
53+
54+
println("...done. Maximum feature width selected is $max_feature_width pixels; minimum feature width is $min_feature_width; maximum feature height is $max_feature_height pixels; minimum feature height is $min_feature_height.\n")
55+
else
56+
min_feature_height = 8
57+
max_feature_height = 10
58+
min_feature_width = 8
59+
max_feature_width = 10
60+
end
61+
62+
63+
FD.notify_user("Loading faces...")
64+
65+
faces_training = FD.load_images(pos_training_path)[1]
66+
faces_ii_training = map(FD.to_integral_image, faces_training) # list(map(...))
67+
println("...done. ", length(faces_training), " faces loaded.")
68+
69+
FD.notify_user("Loading non-faces...")
70+
71+
non_faces_training = FD.load_images(neg_training_path)[1]
72+
non_faces_ii_training = map(FD.to_integral_image, non_faces_training) # list(map(...))
73+
println("...done. ", length(non_faces_training), " non-faces loaded.\n")
74+
75+
# classifiers are haar like features
76+
classifiers = FD.learn(faces_ii_training, non_faces_ii_training, num_classifiers, min_feature_height, max_feature_height, min_feature_width, max_feature_width)
77+
78+
# write classifiers to file
79+
serialize(joinpath(dirname(@__FILE__), "data", "haar-like_features"), classifiers)
80+
end
81+
82+
@time main(smart_choose_feats=true, alt=false)

0 commit comments

Comments
 (0)