diff --git a/example.jl b/example.jl index be3bfb725..4eae2df50 100755 --- a/example.jl +++ b/example.jl @@ -16,7 +16,7 @@ include(joinpath("src", "FaceDetection.jl")) using .FaceDetection using Printf: @printf -using Images: Gray, clamp01nan, save, imresize +using Images: Gray, clamp01nan, save, imresize, load function main(smartChooseFeats::Bool=false, alt::Bool=false, imageReconstruction::Bool=false, featValidaton::Bool=true) @@ -93,6 +93,9 @@ function main(smartChooseFeats::Bool=false, alt::Bool=false, imageReconstruction @printf("%10.9s %10.9s %15s\n", "Faces:", facesFrac, facesPercent) @printf("%10.9s %10.9s %15s\n\n", "Non-faces:", nonFacesFrac, nonFacesPercent) + + randImg = FaceDetection.getRandomImage(posTestingPath, negTestingPath, true) + println("For face (or non-face) ", randImg, " we have a face-likeness of ", sum([c.weight * FaceDetection.getVote(c, FaceDetection.toIntegralImage(getImageMatrix(randImg))) for c in classifiers])) if imageReconstruction # Just for fun: putting all Haar-like features over each other generates a face-like image @@ -115,4 +118,4 @@ end -@time main(false, false, true, true) +@time main(false, false, false, false) diff --git a/src/AdaBoost.jl b/src/AdaBoost.jl index f0ef262b1..3fd496c9f 100755 --- a/src/AdaBoost.jl +++ b/src/AdaBoost.jl @@ -18,7 +18,7 @@ using ProgressMeter: @showprogress using .HaarLikeFeature: FeatureTypes, HaarLikeObject, getVote using .Utils: notifyUser -export learn #, _get_feature_vote, _create_features +export learn, _get_feature_vote, _create_features function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassifiers::Int64=-1, minFeatureWidth::Int64=1, maxFeatureWidth::Int64=-1, minFeatureHeight::Int64=1, maxFeatureHeight::Int64=-1)#::Array{HaarLikeObject,1} @@ -170,7 +170,7 @@ function _create_features(imgHeight::Int64, imgWidth::Int64, minFeatureWidth::In """) end - for feature in HaarLikeFeature.FeatureTypes # from HaarLikeObject.jl (FeatureTypes are just tuples) + for feature in HaarLikeFeature.FeatureTypes # (FeatureTypes are just tuples) featureStartWidth = max(minFeatureWidth, feature[1]) for featureWidth in range(featureStartWidth, stop=maxFeatureWidth, step=feature[1]) featureStartHeight = max(minFeatureHeight, feature[2]) diff --git a/src/FaceDetection.jl b/src/FaceDetection.jl index 862745772..a1f238486 100755 --- a/src/FaceDetection.jl +++ b/src/FaceDetection.jl @@ -14,7 +14,7 @@ include("AdaBoost.jl") include("Utils.jl") using .IntegralImage: toIntegralImage, sumRegion -using .HaarLikeFeature: FeatureTypes, HaarLikeObject, getScore, getVote +using .HaarLikeFeature: FeatureTypes, HaarLikeObject, getScore, getVote, getFacelikeness using .AdaBoost: learn, _get_feature_vote, _create_features using .Utils: displaymatrix, notifyUser, loadImages, getImageMatrix, ensembleVote, ensembleVoteAll, reconstruct, getRandomImage, generateValidationImage diff --git a/src/HaarLikeFeature.jl b/src/HaarLikeFeature.jl index 51f9a237c..2ba8172d3 100755 --- a/src/HaarLikeFeature.jl +++ b/src/HaarLikeFeature.jl @@ -11,7 +11,7 @@ include("IntegralImage.jl") using .IntegralImage: sumRegion -export FeatureTypes, HaarLikeObject, getScore, getVote +export FeatureTypes, HaarLikeObject, getScore, getVote, getFacelikeness FeatureTypes = [(1, 2), (2, 1), (3, 1), (1, 3), (2, 2)] @@ -114,4 +114,32 @@ function getVote(feature, intImg::AbstractArray)#function getVote(feature::HaarL end +function getFacelikeness(feature, intImg::AbstractArray) + #= + Get facelikeness for a given feature. + + parameter `feature`: given Haar-like feature (parameterised replacement of Python's `self`) [type: HaarLikeObject] + parameter `intImg`: Integral image array [type: Abstract Array] + + return `score`: Score for given feature [type: Float] + =# + + score = 0 + + if feature.featureType == FeatureTypes[1] # two vertical + score = 1 + elseif feature.featureType == FeatureTypes[2] # two horizontal + score = 2 + elseif feature.featureType == FeatureTypes[3] # three horizontal + score = 3 + elseif feature.featureType == FeatureTypes[4] # three vertical + score = 4 + elseif feature.featureType == FeatureTypes[5] # four + score = 5 + end + + return score +end + + end # end module diff --git a/src/Utils.jl b/src/Utils.jl index f8a1da6c7..ff0aab2ee 100755 --- a/src/Utils.jl +++ b/src/Utils.jl @@ -199,7 +199,7 @@ function getRandomImage(facePath::AbstractString, nonFacePath::AbstractString="" face = rand(Bool) fileName = rand(filter!(f -> ! occursin(r".*\.DS_Store", f), readdir(face ? facePath : nonFacePath, join=true))) return fileName#, face - elseif ! nonFaces + else fileName = rand(filter!(f -> ! occursin(r".*\.DS_Store", f), readdir(facePath, join=true))) return fileName#, face end