Skip to content

Comments

Face scan#5

Open
Akyarhsu wants to merge 1 commit intoateymoori:masterfrom
Akyarhsu:patch-1
Open

Face scan#5
Akyarhsu wants to merge 1 commit intoateymoori:masterfrom
Akyarhsu:patch-1

Conversation

@Akyarhsu
Copy link

private fun takePhotoAndDetectFace() {
previewView.bitmap?.let { bitmap ->
val image = InputImage.fromBitmap(bitmap, 0)
val detector = FaceDetection.getClient(FaceDetectorOptions.Builder()
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)
.build())

    detector.process(image)
        .addOnSuccessListener { faces ->
            if(faces.isNotEmpty()) {
                val score = calculateScore(faces[0])
                showResult(score)
            } else {
                Toast.makeText(this, "No face detected!", Toast.LENGTH_SHORT).show()
            }
        }
        .addOnFailureListener { e ->
            e.printStackTrace()
        }
}

}

private fun calculateScore(face: Face): Int {
val smileProb = face.smilingProbability ?: 0.5f
return (smileProb * 100).toInt() // approximation
}

private fun showResult(score: Int) {
val message = when(score) {
in 80..100 -> "ရုပ်ချော! 🌟"
in 50..79 -> "ရုပ်ရည်အသင့် 🙂"
else -> "ရုပ်ဆိုး 😅"
}
AlertDialog.Builder(this)
.setTitle("Your Score: $score")
.setMessage(message)
.setPositiveButton("OK", null)
.show()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant