12

How to Implement Eye-Enlarging and Face-Shaping Functions Using HUAWEI ML Kit's...

 3 years ago
source link: https://dzone.com/articles/how-to-implement-eye-enlarging-and-face-shaping-fu-1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Implement Eye-Enlarging and Face-Shaping Functions Using HUAWEI ML Kit's Face Detection Capability

We take a look at a quick tutorial on how to integrate a machine learning (ML) kit into a camera-based application that can detect faces and facial patterns.

Dec. 30, 20 · AI Zone · Tutorial

Join the DZone community and get the full member experience.

Join For Free

Introduction

Sometimes, we can't help taking photos to capture 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 create more impressive photos? If you have an image processing app on your phone, it can automatically detect faces in a photo and you can 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. 

14269549-26400650000004314082020101307330645188815042729261.png

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

xxxxxxxxxx
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

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

1.5 Apply for Camera Permission

xxxxxxxxxx
<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

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

2.2 Create an MLFrame Object by Using the android.graphics.Bitmap file for the Analyzer to Detect Images

xxxxxxxxxx
MLFrame   frame = MLFrame.fromBitmap(bitmap);

2.3 Call the asyncAnalyseFrame Method to Perform Face Detection

xxxxxxxxxx
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. 

xxxxxxxxxx
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

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

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

14269556-26400650000004314082020101307365606642014839320037.gif

GitHub Source Code

Learn More

For more information, please visit HUAWEI Developers.

For detailed instructions, please visit Development Guide.

You can join the HMS Core developer discussion on Reddit.

You can download the demo and sample code from GitHub.

To solve integration problems, please go to Stack Overflow.

Topics:
ai, machine learning, ml kit, facial recognition

Opinions expressed by DZone contributors are their own.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK