Skip to content

Commit

Permalink
Merge pull request #21 from darwinfrancis/dev
Browse files Browse the repository at this point in the history
fixed issue #20
  • Loading branch information
darwinfrancis committed Aug 26, 2022
2 parents f0db05f + 243d02a commit 0c64905
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 41 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

version=1.1.2
version=1.1.4
98 changes: 58 additions & 40 deletions still/src/main/java/com/darwin/viola/still/FaceAnalyser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,42 @@ internal class FaceAnalyser {
Util.printLog("Processing face with {CropAlgorithm.$cropAlgorithm} algorithm.")
var faceSize = getFaceSizePercentage(face, bitmap, cropAlgorithm)
if (faceSize >= minFaceSize) {

val portraitData = cropFace(face, bitmap, cropAlgorithm)
val croppedBitmap = portraitData.first
val smileProbability = face.smilingProbability!!.roundFloat()
val leftEyeOpenProbability = face.leftEyeOpenProbability!!.roundFloat()
val rightEyeOpenProbability = face.rightEyeOpenProbability!!.roundFloat()
val pixelBetweenEyes = portraitData.second.roundDouble()
faceSize = getAreaOfFaceRelativeToBitmap(
croppedBitmap.width.toFloat(),
croppedBitmap.height.toFloat(),
bitmap
)
portraitData?.let {
val croppedBitmap = portraitData.first
val smileProbability = face.smilingProbability!!.roundFloat()
val leftEyeOpenProbability = face.leftEyeOpenProbability!!.roundFloat()
val rightEyeOpenProbability = face.rightEyeOpenProbability!!.roundFloat()
val pixelBetweenEyes = portraitData.second.roundDouble()
faceSize = getAreaOfFaceRelativeToBitmap(
croppedBitmap.width.toFloat(),
croppedBitmap.height.toFloat(),
bitmap
)

val facePose = FacePose(
eulerValueToAngle(face.headEulerAngleX),
eulerValueToAngle(face.headEulerAngleY),
eulerValueToAngle(face.headEulerAngleZ)
)
val facePose = FacePose(
eulerValueToAngle(face.headEulerAngleX),
eulerValueToAngle(face.headEulerAngleY),
eulerValueToAngle(face.headEulerAngleZ)
)

val ageRange = if (ageClassification) getAgeRange(croppedBitmap) else null

return FacePortrait(
croppedBitmap,
smileProbability,
leftEyeOpenProbability,
rightEyeOpenProbability,
pixelBetweenEyes,
faceSize,
facePose,
ageRange
)
val ageRange = if (ageClassification) getAgeRange(croppedBitmap) else null

return FacePortrait(
croppedBitmap,
smileProbability,
leftEyeOpenProbability,
rightEyeOpenProbability,
pixelBetweenEyes,
faceSize,
facePose,
ageRange
)
} ?: run {
Util.printLog("Unable to crop the face, please check exception for details. skipping face.")
return null
}
} else {
Util.printLog("The face size{$faceSize} is below minimum threshold value{${minFaceSize}}, skipping face.")
return null
Expand All @@ -133,7 +139,7 @@ internal class FaceAnalyser {
face: Face,
bitmap: Bitmap,
cropAlgorithm: CropAlgorithm
): Pair<Bitmap, Double> {
): Pair<Bitmap, Double>? {
Util.printLog("Face crop with {CropAlgorithm.$cropAlgorithm} algorithm is started.")
if (cropAlgorithm == CropAlgorithm.LEAST) {
return cropFaceByLeastAlgorithm(face, bitmap)
Expand Down Expand Up @@ -216,17 +222,22 @@ internal class FaceAnalyser {
finalHeight -= heightRemainder
}

val croppedBitmap =
Bitmap.createBitmap(bitmap, finalStartX, finalStartY, finalWidth, finalHeight)
return Pair(croppedBitmap, eyeDistance)
try {
val croppedBitmap =
Bitmap.createBitmap(bitmap, finalStartX, finalStartY, finalWidth, finalHeight)
return Pair(croppedBitmap, eyeDistance)
} catch (e: IllegalArgumentException) {
Util.printLog("Exception occurred when cropping image with {CropAlgorithm.$cropAlgorithm}, Exception: ${e.message}")
}
return null
}
}


private fun cropFaceByLeastAlgorithm(
face: Face,
bitmap: Bitmap
): Pair<Bitmap, Double> {
): Pair<Bitmap, Double>? {
val eyeDistance = getPixelBetweenEyes(face)

val heightTopOffset = (eyeDistance / 2).toInt()
Expand Down Expand Up @@ -260,14 +271,21 @@ internal class FaceAnalyser {
height -= heightRemainder
}

val croppedBitmap = Bitmap.createBitmap(
bitmap,
startX,
startY,
width,
height
)
return Pair(croppedBitmap, eyeDistance)


try {
val croppedBitmap = Bitmap.createBitmap(
bitmap,
startX,
startY,
width,
height
)
return Pair(croppedBitmap, eyeDistance)
} catch (e: IllegalArgumentException) {
Util.printLog("Exception occurred when cropping image with {CropAlgorithm.LEAST}, Exception: ${e.message}")
}
return null
}

private fun getFaceSizePercentage(
Expand Down

0 comments on commit 0c64905

Please sign in to comment.