DEV Community

vmodal_ai
vmodal_ai

Posted on

Build Face Recognition in Android with MediaPipe and Kotlin

Face recognition has become a key feature in modern Android applications, powering everything from attendance systems and smart authentication to augmented reality (AR) filters and photo organization. Google's MediaPipe makes it easy to build real-time, on-device computer vision applications with high performance and low latency.

In this tutorial, you'll learn how to integrate MediaPipe into an Android app using Kotlin to detect faces in real time and prepare the foundation for a face recognition system.

Note: MediaPipe provides robust face detection and face landmark tracking. A complete face recognition system (identifying specific individuals) typically requires an additional face embedding model and a matching algorithm. This tutorial focuses on MediaPipe's face detection and landmarks, which are the first step toward recognition.


Why MediaPipe?

MediaPipe is an open-source framework developed by Google for building real-time machine learning pipelines.

Key Benefits

  • 🚀 Real-time face detection
  • 📱 Runs completely on-device
  • 🔒 Better privacy without cloud processing
  • ⚡ Low latency
  • 🤖 Cross-platform support
  • 🎯 High accuracy for facial landmarks

Popular use cases include:

  • Face authentication
  • Attendance systems
  • AR face filters
  • Emotion analysis
  • Head pose estimation
  • Video conferencing
  • Virtual makeup
  • Smart cameras

Prerequisites

Before getting started, make sure you have:

  • Android Studio
  • Kotlin
  • Android SDK 24+
  • A physical Android device
  • Basic Android development knowledge

Step 1: Create a New Android Project

Open Android Studio and create a new Empty Activity project.

Choose:

  • Language: Kotlin
  • Minimum SDK: API 24 or later

Step 2: Add MediaPipe Dependency

Add the MediaPipe dependency to your build.gradle.

dependencies {
    implementation("com.google.mediapipe:tasks-vision:latest.version")
}
Enter fullscreen mode Exit fullscreen mode

Replace latest.version with the latest stable release available when you build your project.

Sync Gradle after adding the dependency.


Step 3: Add Camera Permission

Update your AndroidManifest.xml.

<uses-permission android:name="android.permission.CAMERA"/>
Enter fullscreen mode Exit fullscreen mode

Request camera permission at runtime for Android 6.0 and above.


Step 4: Add the Face Detection Model

Download the MediaPipe face detection model and place it inside the assets folder.

app/
 └── src/
      └── main/
           └── assets/
                └── face_detector.task

Enter fullscreen mode Exit fullscreen mode

The model enables on-device face detection without requiring an internet connection.


Step 5: Initialize the Face Detector

Create a detector instance using MediaPipe Tasks.

val options = FaceDetector.FaceDetectorOptions.builder()
    .setRunningMode(RunningMode.LIVE_STREAM)
    .build()

val detector = FaceDetector.createFromOptions(
    context,
    options
)
Enter fullscreen mode Exit fullscreen mode

This configures MediaPipe for real-time camera processing.


Step 6: Process Camera Frames

Capture camera frames using CameraX and send each frame to the detector.

detector.detectAsync(
    image,
    System.currentTimeMillis()
)
Enter fullscreen mode Exit fullscreen mode

MediaPipe processes each frame and returns face detection results asynchronously.


Step 7: Read Detection Results

Handle the detection callback.

override fun onResults(result: FaceDetectorResult) {

    result.detections().forEach {

        println(it.boundingBox())
    }

}

Enter fullscreen mode Exit fullscreen mode

Each detection contains useful information such as:

  • Face bounding box
  • Detection confidence
  • Face location

Step 8: Draw Face Bounding Boxes

Overlay a rectangle around each detected face.

Typical information displayed includes:

  • Face position
  • Detection confidence
  • Face count

This visual feedback is useful for camera preview applications.


Step 9: Build Toward Face Recognition

MediaPipe detects faces but does not identify individuals by itself.

To build a complete recognition system:

  1. Detect the face with MediaPipe.
  2. Crop the detected face.
  3. Generate a face embedding using a recognition model such as FaceNet, MobileFaceNet, or ArcFace.
  4. Compare the embedding with stored embeddings using cosine similarity or Euclidean distance.
  5. Match the closest identity if it exceeds a confidence threshold.

This two-stage pipeline is commonly used in attendance, access control, and identity verification systems.


Performance Tips

To improve real-time performance:

  • Use CameraX for camera management.
  • Resize frames before inference if supported by your model.
  • Process frames on a background thread.
  • Reuse detector instances.
  • Limit unnecessary UI updates.
  • Release camera resources when the app is paused.

Common Challenges

Camera Permission Denied

Request camera permission before starting the detector.

Low Detection Accuracy

Ensure the environment has sufficient lighting and the face is clearly visible.

Slow Performance

Reduce the input image resolution or skip frames if real-time performance is affected.

Multiple Faces

MediaPipe supports detecting multiple faces in a single frame, allowing applications such as classroom attendance or group photo analysis.


Real-World Applications

MediaPipe powers many intelligent Android applications, including:

  • 👤 Employee attendance systems
  • 🔓 Face-based authentication
  • 📷 Smart camera apps
  • 🎭 AR masks and filters
  • 🎥 Video conferencing enhancements
  • 🏥 Patient identification workflows
  • 🎮 Interactive games
  • 📚 Classroom monitoring

Best Practices

  • Always request user consent before processing facial data.
  • Store biometric information securely using encryption.
  • Perform recognition on-device whenever possible to improve privacy.
  • Inform users about how facial data is used.
  • Test your application across different lighting conditions and devices.

MediaPipe vs ML Kit

Feature MediaPipe ML Kit
Face Detection
Face Landmarks ✅ Advanced Limited
Real-Time Performance Excellent Excellent
Offline Support
Cross-Platform Limited
Face Recognition Requires additional model Requires additional model

MediaPipe is often preferred for applications requiring detailed facial landmarks and advanced computer vision capabilities.


Conclusion

MediaPipe makes it simple to build high-performance face detection features for Android applications. By combining MediaPipe with CameraX and Kotlin, you can create responsive, privacy-friendly experiences that run entirely on the user's device.

If your goal is full face recognition, MediaPipe serves as an excellent first stage. Pair it with a face embedding model such as FaceNet or ArcFace to recognize individuals while maintaining a secure and efficient architecture. This combination is suitable for attendance systems, smart authentication, AR experiences, and many other AI-powered Android applications.


Frequently Asked Questions

Does MediaPipe support face recognition?

MediaPipe focuses on face detection and landmark tracking. To recognize individuals, you need an additional face embedding model and a matching algorithm.

Can MediaPipe run offline?

Yes. MediaPipe performs inference locally on the device without requiring an internet connection.

Is MediaPipe free?

Yes. MediaPipe is an open-source framework developed by Google.

Can I use CameraX with MediaPipe?

Absolutely. CameraX is the recommended camera library for integrating MediaPipe into modern Android applications.



SDK Flutter: https://github.com/v-modal/vmodal_sdk_flutter
SDK Android: https://github.com/v-modal/vmodal_sdk_android
Discord: https://discord.gg/K72z28KUx

Tags

android kotlin mediapipe ai computervision machinelearning camerax

Top comments (0)