Face stylization guide for Android

The MediaPipe Face Stylizer task lets you apply face stylizations to faces in an image. You can use this task to create virtual avatars in various styles.

The code sample described in these instructions is available on GitHub. For more information about the capabilities, models, and configuration options of this task, see the Overview.

Code example

The MediaPipe Tasks example code is a basic implementation of a Face Stylizer app for Android. The example applies face stylization to images provided to the app.

You can use the app as a starting point for your own Android app, or refer to it when modifying an existing app. The Face Stylizer example code is hosted on GitHub.

Download the code

The following instructions show you how to create a local copy of the example code using the git command line tool.

To download the example code:

  1. Clone the git repository using the following command:
    git clone https://github.com/googlesamples/mediapipe
    
  2. Optionally, configure your git instance to use sparse checkout, so you have only the files for the Face Stylizer example app:
    cd mediapipe
    git sparse-checkout init --cone
    git sparse-checkout set examples/face_stylization/android
    

After creating a local version of the example code, you can import the project into Android Studio and run the app. For instructions, see the Setup Guide for Android.

Key components

The following files contain the crucial code for this face stylization example application:

Setup

This section describes key steps for setting up your development environment and code projects specifically to use Face Stylizer. For general information on setting up your development environment for using MediaPipe tasks, including platform version requirements, see the Setup guide for Android.

Dependencies

The Face Stylizer task uses the com.google.mediapipe:tasks-vision library. Add this dependency to the build.gradle file of your Android app:

dependencies {
    implementation 'com.google.mediapipe:tasks-vision:latest.release'
}

Model

The MediaPipe Face Stylizer task requires a trained model bundle that is compatible with this task. For more information on available trained models for Face Stylizer, see the task overview Models section.

Select and download the model, and store it within your project directory:

<dev-project-root>/src/main/assets

Specify the path of the model within the ModelAssetPath parameter.

val modelName = "https://storage.googleapis.com/mediapipe-models/face_stylizer/blaze_face_stylizer/float32/latest/face_stylizer_color_sketch.task"
baseOptionsBuilder.setModelAssetPath(modelName)

Create the task

The MediaPipe Face Stylizer task uses the createFromOptions() function to set up the task. The createFromOptions() function accepts values for the configuration options. For more information on configuration options, see Configuration options.

val baseOptionsBuilder = BaseOptions.builder().setModelAssetPath(modelName)
val baseOptions = baseOptionBuilder.build()

val optionsBuilder =
    FaceStylizer.FaceStylizerOptions.builder()
        .setBaseOptions(baseOptionsBuilder.build())

val options = optionsBuilder.build()

FaceStylizer =
    FaceStylizer.createFromOptions(context, options)

Configuration options

This task has the following configuration options for Android apps:

Option Name Description Value Range Default Value
errorListener Sets an optional error listener. N/A Not set

Prepare data

Face Stylizer works with still images. The task handles the data input preprocessing, including resizing, rotation and value normalization. The following code demonstrates how to hand off data for processing.

import com.google.mediapipe.framework.image.BitmapImageBuilder
import com.google.mediapipe.framework.image.MPImage

// Convert the input Bitmap object to an MPImage object to run inference
val mpImage = BitmapImageBuilder(image).build()

Run the task

Use the FaceStylizer.stylize() method on the input image to run the stylizer:

val result = FaceStylizer.stylize(mpImage)

Handle and display results

The Face Stylizer returns a FaceStylizerResult object, which contains a MPImage object with a stylization of the most prominent face within the input image.

The following shows an example of the output data from this task:

The output above was created by applying the Color sketch model to the following input image: