[Huawei Developers] John Wo:
We all like to take selfies from time-to-time, but because most of us are not professional photographers, our photographs can fall short of our expectations. Wouldn't it be great if there was an app that beautified users' photos, and even better, gave the option to add various stickers?

Or, wouldn't it be convenient if there was a parental control function in your app, which prevented children from getting too close to their phone, or staring at the screen for too long? Well, with ML Kit's face detection capability, you can do all of this!

ML Kit's face detection capability detects up to 855 facial keypoints and returns the coordinates for the eyebrows, eyes, nose, mouth, and ears, as well as the contour and angle of the face. Once you've integrated this capability, you can quickly create beauty apps which enable users to add fun facial effects and stickers to their images. Face detection also detects whether the subject's eyes are open, whether they are wearing glasses or a hat, whether they have a beard, and even their gender and age. In addition, it can detect up to seven facial expressions: smiling, neutral, angry, disgusted, frightened, sad, and surprised. This is great if you want to create apps such as smile-cameras.

Eye-Enlarging and Face-Shaping Functions-Development Practice

1. Preparations

To find detailed information about the preparations you need to make, please refer to Development Process. Here, we'll just look at the most important procedures.

1.1 Configure the Maven repository address in the project-level build.gradle file.

buildscript {
repositories {
...
maven {url 'https://developer.huawei.com/repo/'}
}
}
dependencies {
...
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
allprojects {
repositories {
...
maven {url 'https://developer.huawei.com/repo/'}
}
}

1.2 Add configurations to the file header.

After integrating the SDK, add the following configuration to the file header:

apply plugin: 'com.android.application'

apply plugin: 'com.huawei.agconnect'

1.3 Configure SDK dependencies in the app-level build.gradle file.

dependencies{
// Import the base SDK.
implementation 'com.huawei.hms:ml-computer-vision-face:2.0.1.300'
// Import the contour and keypoint detection model package.
implementation 'com.huawei.hms:ml-computer-vision-face-shape-point-model:2.0.1.300'
// Import the facial expression detection model package.
implementation 'com.huawei.hms:ml-computer-vision-face-emotion-model:2.0.1.300'
// Import the facial feature detection model package.
implementation 'com.huawei.hms:ml-computer-vision-face-feature-model:2.0.1.300'
}

1.4 Add these statements to the AndroidManifest.xml file so the machine learning model can update automatically.

<manifest
...
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value= "face"/>
...
</manifest>

1.5 Obtain camera permissions

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

2. Code Development

2.1 Create a face analyzer by using the default parameter configurations.

analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer();

2.2 Create an MLFrame object by using the android.graphics.Bitmap so the analyzer can detect images.

MLFrame frame = MLFrame.fromBitmap(bitmap);

2.3 Call the asyncAnalyseFrame method to perform face detection.

Task<List<MLFace>> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<List<MLFace>>() {
@Override
public void onSuccess(List<MLFace> faces) {
// Detection success. Obtain the face keypoints.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Detection failure.
}
});

2.4 Use progress bar to process the face in the image. Call the magnifyEye and smallFaceMesh methods to implement the eye-enlarging and face-shaping algorithms.

private SeekBar.OnSeekBarChangeListener new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.seekbareye: // When the progress bar of the eye enlarging changes, ...
case R.id.seekbarface: // When the progress bar of the face shaping changes, ...
}
}
}

2.5 Release the analyzer when the detection ends.

try {
if (analyzer != null) {
analyzer.stop();
}
} catch (IOException e) {
Log.e(TAG, "e=" + e.getMessage());
}
Demo Effects

Face Stickers-Development Practice

1. Preparations

1.1 Add the Maven repository to the project-level build.gradle file.

Open the build.gradle file in the root directory of your Android Studio project.

Add the following Maven repository address:

buildscript {
{
maven {url 'http://developer.huawei.com/repo/'}
}
}
allprojects {
repositories {
maven { url 'http://developer.huawei.com/repo/'}
}
}

1.2 Add SDK dependencies to the app-level build.gradle file.

// Face detection SDK.
implementation 'com.huawei.hms:ml-computer-vision-face:2.0.1.300'
// Face detection model.
implementation 'com.huawei.hms:ml-computer-vision-face-shape-point-model:2.0.1.300'

1.3 Apply for the camera, network access, and storage permissions in the AndroidManifest.xml file.

<!-- Camera permission -->
< uses-feature android:name="android.hardware.camera" />
< uses-permission android:name="android.permission.CAMERA" />
< !-- Write permission -->
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
< !-- Read permission -->
< uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2. Code Development

2.1 Set the face analyzer.

MLFaceAnalyzerSetting detectorOptions;
detectorOptions = new MLFaceAnalyzerSetting.Factory()
.setFeatureType(MLFaceAnalyzerSetting.TYPE_UNSUPPORT_FEATURES)
.setShapeType(MLFaceAnalyzerSetting.TYPE_SHAPES)
.allowTracing(MLFaceAnalyzerSetting.MODE_TRACING_FAST)
.create();
detector = MLAnalyzerFactory.getInstance().getFaceAnalyzer(detectorOptions);

More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0203423486077750006