Using ML Kit in Android — Smart Features like Face Detection and OCR
Introduction
In today’s mobile-first world, users expect apps to be smarter and more intuitive than ever before. Whether it’s scanning documents, unlocking phones with a smile, or reading QR codes, intelligent features powered by machine learning are now essential parts of the Android experience.
But building these features from scratch with traditional machine learning pipelines can be time-consuming, require significant domain knowledge, and add complexity to your app.
That’s where ML Kit comes in.
ML Kit, provided by Google, offers on-device and cloud-based machine learning APIs that are easy to use and highly optimized for mobile. From text recognition to face detection, barcode scanning, and language identification, ML Kit makes it incredibly easy to add powerful ML features to your Android apps without deep ML expertise.
In this article, you’ll learn how to integrate ML Kit into your Android project and build features like face detection and OCR (optical character recognition) — step by step.
What Is ML Kit?
ML Kit is a mobile SDK developed by Google that brings Google’s machine learning expertise to Android and iOS apps in a powerful and easy-to-use package. It supports:
- On-device APIs (offline, fast, privacy-friendly)
- Cloud-based APIs (higher accuracy, more power)
Some of the key capabilities include:
- Text Recognition (OCR)
- Face Detection
- Barcode Scanning
- Image Labeling
- Pose Detection
- Language Identification
- Smart Reply
- Translation
Why Use ML Kit?
✅ Easy to integrate: No complex ML model training needed
✅ Runs on-device: Low latency, works offline, better privacy
✅ High accuracy: Built on Google’s ML infrastructure
✅ Cross-platform: Available for Android and iOS
✅ Cost-efficient: Most features are free with generous usage limits
Setting Up ML Kit in Your Android Project
As of 2025, the recommended way to add dependencies is via the libs.versions.toml file (Version Catalogs):
Step 1: Enable version catalogs (if not already enabled)
In your settings.gradle.kts:
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
enableFeaturePreview("VERSION_CATALOGS")
Step 2: Add dependencies to your libs.versions.toml
[versions]
mlkit = "17.0.0"
[libraries]
mlkit-face-detection = { group = "com.google.mlkit", name = "face-detection", version.ref = "mlkit" }
mlkit-text-recognition = { group = "com.google.mlkit", name = "text-recognition", version.ref = "mlkit" }
Step 3: Use the library in your build.gradle.kts
dependencies {
implementation(libs.mlkit.face.detection)
implementation(libs.mlkit.text.recognition)
}
Implementing Face Detection (On-Device)
Face detection helps identify faces in images or camera previews. This is great for security features, effects, or even fun filters.
1. Create a FaceDetector instance:
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)
2. Convert your input (Bitmap or MediaImage) to InputImage:
val image = InputImage.fromBitmap(bitmap, rotationDegree)
3. Process the image:
detector.process(image)
.addOnSuccessListener { faces ->
for (face in faces) {
val bounds = face.boundingBox
val rotY = face.headEulerAngleY
val smileProb = face.smilingProbability
}
}
.addOnFailureListener {
// Handle errors
}
Implementing OCR (Text Recognition)
Text recognition is incredibly useful for reading documents, IDs, license plates, receipts, etc.
1. Get an instance of the TextRecognizer:
val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
2. Create an InputImage :
val image = InputImage.fromBitmap(bitmap, rotationDegree)
3. Process the image:
recognizer.process(image)
.addOnSuccessListener { visionText ->
for (block in visionText.textBlocks) {
val text = block.text
val boundingBox = block.boundingBox
}
}
.addOnFailureListener {
// Handle error
}
Tips for Better Accuracy and Performance
- Use on-device models for faster results and offline support
- Use good lighting and high-resolution images for better accuracy
- Rotate images correctly (use EXIF or camera orientation)
- Reuse detector instances instead of recreating them each time
Real-World Use Cases
- Finance apps: Scan and extract data from bills or invoices
- E-commerce: Scan barcodes and product labels
- Security apps: Face recognition and presence detection
- Productivity tools: Scan and digitize handwritten notes
Conclusion
Google’s ML Kit is an incredibly powerful and easy-to-use toolkit that can turn your Android app into a smart, user-friendly product with just a few lines of code. With on-device support, robust APIs, and real-time processing, it empowers developers to implement features like face detection and text recognition — without needing to train or manage ML models themselves.
In the next article, we’ll go deeper into Barcode Scanning and Language ID, two more powerful features that can elevate your app’s intelligence.