[Huawei Developers] Sherlock F:
Introduction

Sometimes, we can't help taking photos to keep our unforgettable moments in life. But most of us are not professional photographers or models, so our photographs can end up falling short of our expectations. So, how can we produce more impressive snaps? If you have an image processing app on your phone, it can automatically detect faces in a photo, and you can then adjust the image until you're happy with it. So, after looking around online, I found HUAWEI ML Kit's face detection capability. By integrating this capability, you can add beautification functions to your apps. Have a try!

Application Scenarios

ML Kit's face detection capability detects up to 855 facial keypoints and returns the coordinates for the face's contour, eyebrows, eyes, nose, mouth, and ears, as well as the angle of the face. Once you've integrated this capability, you can quickly create beauty apps and enable users to add fun facial effects and features to their images.

Face detection also detects whether the subject's eyes are open, whether they're wearing glasses or a hat, whether they have a beard, and even their gender and age. This is useful if you want to add a parental control function to your app which prevents children from getting too close to their phone, or staring at the screen for too long.

In addition, face detection can detect up to seven facial expressions, including smiling, neutral, angry, disgusted, frightened, sad, and surprised faces. This is great if you want to create apps such as smile-cameras.

You can integrate any of these capabilities as needed. At the same time, face detection supports image and video stream detection, cross-frame face tracking, and multi-face detection. It really is powerful! Now, let's see how to integrate this capability.

Face Detection Development

1. Preparations

You can find detailed information about the preparations you need to make on the HUAWEI Developers-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 Apply for Camera Permission

<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 for the Analyzer to 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 the Progress Bar to Process the Face in the Image

Call the magnifyEye and smallFaceMesh methods to implement the eye-enlarging algorithm and face-shaping algorithm.

private SeekBar.OnSeekBarChangeListener 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 After the Detection is Complete

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

Demo

Now, let's see what it can do. Pretty cool, right?

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201013073656.06642014839320037207981829205995:50511020032949:2800:C372BAD8E1EB98D3A3365E3C979ECF7525F9EFBEE3924F3A11D14944CD5E67DA.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201013073656.06642014839320037207981829205995:50511020032949:2800:C372BAD8E1EB98D3A3365E3C979ECF7525F9EFBEE3924F3A11D14944CD5E67DA.gif[/IMG]