[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Introduction

Have you ever wondered whether unlocking your phone with facial recognition is really safe? What if someone masqueraded as you by using photos or videos of you, could your phone detect that it's not you in front of the camera? Well, thanks to ML Kit's liveness detection capability, it can! This feature accurately distinguishes between real faces and fake ones. Whether it’s a photo, video, or mask, liveness detection can immediately expose those fake faces!

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122226.13174130696331468471471311706143:50511012093407:2800:401D144B29FE2C73AC08D72213BBF1DA4C087BE41B685A1191E09E5D31008EA5.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122226.13174130696331468471471311706143:50511012093407:2800:401D144B29FE2C73AC08D72213BBF1DA4C087BE41B685A1191E09E5D31008EA5.jpg[/IMG]

Application Scenarios

Liveness detection is generally used to perform a face match. First, it will determine whether the person in front of the camera is a real person, instead of a person holding a photo or a mask. Then, face match will compare the current face to the one it has on record, to see if they are the same person. Liveness detection is useful in a huge range of situations. For example, it can prevent people from unlocking your phone and accessing your personal information.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122241.42545692389925244440147588505038:50511012093407:2800:0A931F4C03E4ECC62DF771370EABD22E74B2CB9B639A3CA3CCEDCB5245ED5088.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122241.42545692389925244440147588505038:50511012093407:2800:0A931F4C03E4ECC62DF771370EABD22E74B2CB9B639A3CA3CCEDCB5245ED5088.jpg[/IMG]

It can be used for real-name authentication. It determines whether the person in front of the camera is a real person, and then compares their face with the photo on the ID card, to confirm that the person handling the service is the same person on the ID card.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122254.58543168959662166981915120446549:50511012093407:2800:A6FFCD6252D11D674108C5DEEB4C63D5CB9B05C1280517FA671A1AB78F52428E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122254.58543168959662166981915120446549:50511012093407:2800:A6FFCD6252D11D674108C5DEEB4C63D5CB9B05C1280517FA671A1AB78F52428E.jpg[/IMG]

And ML Kit's liveness detection also supports silent detection, where it can determine whether the user’s face is real without them having to do anything. Pretty convenient, right? Now, I'll show you how to quickly integrate liveness detection.

Liveness 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 Configure SDK Dependencies in the App-Level build.gradle File

dependencies{
// Import the combined liveness detection package.
implementation 'com.huawei.hms:ml-computer-vision-livenessdetection:2.0.2.300'
}

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

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

1.5 Apply for Camera Permission

For detailed procedures about how to apply for camera permission, please refer to HUAWEI Developers-Assigning Permissions.

2. Code Development

2.1 Create a Liveness Detection Result Callback to Obtain the Detection Results

private MLLivenessCapture.Callback callback = new MLLivenessCapture.Callback() {
@Override
public void onSuccess(MLLivenessCaptureResult result) {
// Processing logic when the detection succeeds. The detection results indicate whether the face belongs to a real person.
}
@Override
public void onFailure(int errorCode) {
// Processing logic when the detection fails. For example, if the camera is abnormal (CAMERA_ERROR).
}
};

2.2 Create a Liveness Detection Instance and Start the Detection

MLLivenessCapture capture = MLLivenessCapture.getInstance();
capture.startDetect(activity, callback);

Demo Effect

Below, you can see how the liveness detection capability differentiates between a real face and face mask. Isn't it great?

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122341.62300100024520004989741364803243:50511012093407:2800:0D32B06F04B314994876CE22DCF4F9DC8A4ACFAC8AEF80514829E4666F93B1E0.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928122341.62300100024520004989741364803243:50511012093407:2800:0D32B06F04B314994876CE22DCF4F9DC8A4ACFAC8AEF80514829E4666F93B1E0.jpg[/IMG]

Github source code

To find out more, take a look at our official website: HUAWEI ML Kit.