Overview

The face detection service provided by HUAWEI ML Kit detects a user's facial features, such as the face contour, positions of the eyes, ears, nose, and mouth, and roll angles of a face. You can utilize the returned detection results to develop your apps.

What You Will Create

In this codelab, you will create a face detection app.

What You Will Learn

Use the HMS Core ML SDK.

Hardware Requirements

Software Requirements

Click the following link to download the demo project of this codelab.

Demo Project Package

Decompress the downloaded package to a local directory, for example, D:\mlkit-demo.

Configuring the Project and Device

  1. Select Open from the File drop-down list box and import the demo project from the directory where the decompressed demo project is stored, for example, D:\mlkit-demo\FaceDemo.
  2. If a dialog box similar to the following is displayed, click OK.
  3. Synchronize the project with the .gradle file.
  4. If the following information is displayed, the project is successfully synchronized.

  5. Ensure that the phone is correctly connected to the computer.
  6. If Unknown Device or No device is displayed, run thadb kill-serverer anadb start-server**er commands in the CLI window to restart the ADB service.

    (Note 1: The default installation path of the ADB is C:\Users*USER_NAME*\AppData\Local\Android\Sdk\platform-tools\adb.

    Note 2: If the demo still fails to be started, run the killPORT_NUMBER command to stop the process that occupies port 5037.

    Note 3: You can run the Netstat –ano | findstr 5037 command to query the process that occupies port 5037.)

    If the mobile phone icon is displayed on the toolbar, the configuration takes effect.

Adding the Camera Permission

Add the following content to the AndroidManifest.xml file.

<!--todo step 1: add authorization of camera --> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA"/>

Creating a Face Analyzer Based on the Device-Side Algorithm

Add the following content to the createFaceAnalyzer method in the LiveImageDetectionActivity class.

// todo step 2: add on-device face analyzer MLFaceAnalyzerSetting setting = new MLFaceAnalyzerSetting.Factory() .setFeatureType(MLFaceAnalyzerSetting.TYPE_FEATURES) .setPerformanceType(MLFaceAnalyzerSetting.TYPE_SPEED) .allowTracing() .create(); analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer(setting);

Creating a LensEngine Object

Add the following content to the createLensEngine method in the LiveImageDetectionActivity class.

// todo step 3: add on-device lens engine mLensEngine = new LensEngine.Creator(context, analyzer) .setLensType(lensType) .applyDisplayDimension(1600, 1024) .applyFps(25.0f) .enableAutomaticFocus(true) .create();

Displaying Face Detection Results

Add the following content to the transactResult method in the FaceAnalyzerTransactor class.

// todo step 4: add on-device face graphic MLFaceGraphic graphic = new MLFaceGraphic(mGraphicOverlay, faceSparseArray.valueAt(i)); mGraphicOverlay.add(graphic);

Running the App

Click on the toolbar of Android Studio.

Testing Your App

Focus the camera on the face. The facial contour and landmark points are correctly displayed.

Well done. You have successfully completed this codelab and learned how to:

To learn more about HUAWEI ML Kit, visit the HUAWEI Developers website:

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

You can click the button below to download the source code.

Download

This project is only for demonstration, and the actual development process should strictly follow the Development Guide.

Code copied