How to Quickly Recognize Fake Faces
Have you ever wondered whether unlocking your phone with facial recognition is really safe?
Join the DZone community and get the full member experience.
Join For FreeIntroduction
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!
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.
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.
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
xxxxxxxxxx
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
xxxxxxxxxx
dependencies{
// Import the combined liveness detection package.
implementation 'com.huawei.hms:ml-computer-vision-livenessdetection:2.0.2.300'
}
1.3 Add Configurations to the File Header
xxxxxxxxxx
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
1.4 Add these Statements to the AndroidManifest.xml File so the Machine Learning Model can Update Automatically
xxxxxxxxxx
<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
xxxxxxxxxx
private MLLivenessCapture.Callback callback = new MLLivenessCapture.Callback() {
public void onSuccess(MLLivenessCaptureResult result) {
// Processing logic when the detection succeeds. The detection results indicate whether the face belongs to a real person.
}
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
xxxxxxxxxx
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?
To find out more, take a look at our official website: HUAWEI ML Kit.
Opinions expressed by DZone contributors are their own.
Comments