Beauty artifact-quick integration of Huawei HMS ML Kit face detection to achieve big eyes and face-lift

Preface

When encountering unforgettable and beautiful moments in life, the editor can't help but keep it by taking pictures. I believe everyone is the same as me. But most of us are not professional photographers or models. We are not satisfied with the photo because of the reasons such as the wrong light and wrong angle. What should we do? At this time, if there is a picture processing app in the mobile phone, it can automatically detect the face in the photo with a single tap, and then enlarge the eyes and thin the face, which can easily help us achieve the effect of beautification and let the beautiful self appear in the memory of the beautiful Isn’t it great in the photo? So I searched the Internet for solutions and found that Huawei HMS ML Kit provides a face detection service. After integrating this service, various Android models can easily beautify photos and restore your beauty!

Application scenario

Huawei's HMS ML Kit face detection service detects up to 855 key points on the face and returns information such as the coordinates of the face contour, eyebrows, eyes, nose, mouth, ears and other parts, as well as the deflection angle of the face. After integrating the face detection service, developers can quickly build facial beautification applications based on this information, or add some interesting elements to the face to increase the interest of the picture.

In addition to this powerful feature, the face detection service can also identify features such as whether the eyes are open, whether they wear glasses or a hat, gender, age, and whether they have a beard. After integrating this function, it can realize such as parent control. Application to prevent children’s eyes from being too close to the screen or watching the screen for too long.

In addition, the face detection service can recognize up to seven facial expressions, including smile, expressionless, anger, disgust, panic, sadness, and surprise. This function can realize interesting applications such as smile capture.
Insert picture description here

With so many capabilities above, developers can integrate as needed. In addition, the face detection service supports image and video stream detection, cross-frame tracking of faces, and simultaneous detection of multiple faces. It can be said that all the abilities that the editor can think of are available. It is really powerful! Let's follow the editor to see how to integrate HMS ML Kit's face detection capabilities to achieve thin face and big eyes.

Development combat

1. Development Preparation

For detailed preparation steps, please refer to Huawei Developer Alliance:

https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4

Here are the key development steps.

1.1 Configure Maven warehouse address in project-level gradle

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 configuration to the file header After integrating the SDK, add configuration to the file header

 apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

1.3 Configure SDK dependencies in application-level gradle

dependencies{ 
    // 引入基础SDK
    implementation 'com.huawei.hms:ml-computer-vision-face:2.0.1.300'
    // 引入人脸轮廓+关键点检测模型包
    implementation 'com.huawei.hms:ml-computer-vision-face-shape-point-model:2.0.1.300'
    // 引入表情检测模型包
    implementation 'com.huawei.hms:ml-computer-vision-face-emotion-model:2.0.1.300'
    // 引入特征检测模型包
    implementation 'com.huawei.hms:ml-computer-vision-face-feature-model:2.0.1.300'
}

}
1.4 Add the following statement to the AndroidManifest.xml file to automatically update the machine learning model

<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 Use the default parameter configuration to create a face analyzer

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

2.2 Create MLFrame object through android.graphics.Bitmap for analyzer to detect pictures

MLFrame frame = MLFrame.fromBitmap(bitmap);

2.3 Call "asyncAnalyseFrame" method for face detection

Task<List<MLFace>> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<List<MLFace>>() {
     @Override
     public void onSuccess(List<MLFace> faces) {
         // 检测成功,获取脸部关键点信息。
     }
 }).addOnFailureListener(new OnFailureListener() {
     @Override
     public void onFailure(Exception e) {
         // 检测失败。
    }
 });

2.4 Through the progress bar for different degrees of big eyes and face-lifting treatment. Call the magnifyEye method and the smallFaceMesh method to implement the big eye algorithm and the thin face algorithm respectively

private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.seekbareye: // 当大眼进度条变化时,…
            case R.id.seekbarface: // 当瘦脸进度条变化时,…
        }
    }
}

2.5 After the detection is complete, release the analyzer

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

Demo effect

The following demo shows the effect of big eyes and face-lifting, how about it, is it very convenient?
Insert picture description here

Github address

You can get more detailed source code on Github: https://github.com/HMS-Core/hms-ml-demo/tree/master/BeautyCamera

For more detailed development guidelines, please refer to the official website of Huawei Developer Alliance

https://developer.huawei.com/consumer/cn/hms/huawei-mlkit


Original link:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0203360642386400907&fid=18
Author: leave leaves

Guess you like

Origin blog.51cto.com/14772288/2541600