Google ML Kit provides on-device face detection with landmark and expression analysis. Learn detection, feature extraction, and real-time processing.
Add ML Kit Dependency
implementation("com.google.mlkit:face-detection:16.1.5")
Basic Face Detection
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetectorOptions
val options = FaceDetectorOptions.Builder()
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)
.setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL)
.setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_ALL)
.build()
val detector = FaceDetection.getClient(options)
val image = InputImage.fromBitmap(bitmap)
detector.process(image)
.addOnSuccessListener { faces ->
for (face in faces) {
val bounds = face.boundingBox
val rotY = face.headEulerAngleY
val rotZ = face.headEulerAngleZ
}
}
Extract Face Landmarks
for (face in faces) {
val landmarks = face.landmarks
for (landmark in landmarks) {
val landmarkType = landmark.landmarkType
val position = landmark.position // PointF(x, y)
when (landmarkType) {
FaceLandmark.MOUTH_BOTTOM -> { /* chin */ }
FaceLandmark.LEFT_EYE -> { /* eye position */ }
FaceLandmark.NOSE_BASE -> { /* nose */ }
}
}
}
Analyze Expressions
for (face in faces) {
val smileProbability = face.smilingProbability ?: 0f
val leftEyeOpenProbability = face.leftEyeOpenProbability ?: 0f
val rightEyeOpenProbability = face.rightEyeOpenProbability ?: 0f
when {
smileProbability > 0.7 -> showMessage("Smiling")
leftEyeOpenProbability < 0.3 && rightEyeOpenProbability < 0.3 ->
showMessage("Eyes closed")
}
}
Real-Time CameraX Integration
class FaceAnalyzer : ImageAnalysis.Analyzer {
override fun analyze(imageProxy: ImageProxy) {
val image = InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees)
detector.process(image)
.addOnSuccessListener { faces ->
updateUI(faces)
imageProxy.close()
}
}
}
ML Kit face detection runs on-device for privacy. Suitable for face unlock, mood detection, and interactive gaming filters.
8 Android app templates (Habit Tracker, Budget Manager, Task Manager, and more) available on Gumroad
Top comments (0)