Today, with the rapid development of the Internet, the verification methods of various apps are becoming more and more convenient for users. From the initial password input, to the later fingerprint unlocking, it has evolved into today's face-swiping authentication. With a swipe of your face, you can unlock the device, pay online/offline, pass the access control, fast check tickets, etc. At the same time, there are also many security issues, the first is how to judge the authenticity of the user.
The face comparison and live detection capabilities of HMS Core Machine Learning Service (ML Kit) can quickly capture faces. By identifying and extracting face features in templates, users can judge whether it is a real face or a real face without the need for users to cooperate with actions. Face attack, at the same time, the template portrait and the face are compared with high precision, and the similarity value is output to determine whether the two are the same person.
Based on this, developers can quickly build face detection capabilities. For example, in financial apps, users can compare user ID photos and face detection results to determine the authenticity of user information, which can provide a fast and secure identity verification process, which is suitable for the Internet Remote account opening, face payment and other financial services. In the office app, you can take attendance by swiping your face to identify whether you are the person or not, and effectively prevent such behaviors as punching cards on your behalf.
Show results
Judging from the effect display, live detection can accurately identify fake photos on mobile phones in a few seconds.
Development steps
development preparation
- Configure relevant information in AppGallery Connect. For specific development preparations, refer to the documentation .
- Configure the Maven warehouse address of the HMS Core SDK.
Open the Android Studio project-level "build.gradle" file.
Add AppGallery Connect plugin and Maven repository.
Configure the Maven repository address of the HMS Core SDK in "allprojects" > "repositories".
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
Configure the Maven repository address of the HMS Core SDK in "buildscript" > "repositories".
buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
Add AppGallery Connect plugin configuration in "buildscript" > "dependencies".
buildscript{
dependencies {
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
}
Development of face comparison function
- Create a face alignment detector instance.
MLFaceVerificationAnalyzer analyzer = MLFaceVerificationAnalyzerFactory.getInstance().getFaceVerificationAnalyzer();
- Create an MLFrame object through android.graphics.Bitmap to set the template image. The supported image formats include: JPG, JPEG, PNG, and BMP.
// 通过bitmap创建MLFrame
MLFrame templateFrame = MLFrame.fromBitmap(bitmap);
- Set the face comparison template image. If there is no face in the template, it means that the template setting fails, and the last template set for this instance remains unchanged.
List<MLFaceTemplateResult> results = analyzer.setTemplateFace(templateFrame);
for (int i = 0; i < results.size(); i++) {
// 处理模板图片识别结果
}
- Create an MLFrame object through android.graphics.Bitmap for setting the comparison image. Supported image formats include: JPG, JPEG, PNG, BMP.
// 通过bitmap创建MLFrame
MLFrame compareFrame = MLFrame.fromBitmap(bitmap);
- Call synchronous or asynchronous methods for face comparison. The detection results mainly include comparing the detected face information in the picture, and the confidence that the detected face information and the template face are the same person. See MLFaceVerificationResult for details.
• Example code for asynchronous methods:
Task<List<MLFaceVerificationResult>> task = analyzer.asyncAnalyseFrame(compareFrame);
task.addOnSuccessListener(new OnSuccessListener<List<MLFaceVerificationResult>>() {
@Override
public void onSuccess(List<MLFaceVerificationResult> results) {
// 检测成功
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// 检测失败
}
});
• Example code for a synchronized method:
SparseArray<MLFaceVerificationResult> results = analyzer.analyseFrame(compareFrame);
for (int i = 0; i < results.size(); i++) {
// 检测结果处理
}
- When the detection is complete, stop the analyzer and release the detection resources.
if (analyzer != null) {
analyzer.stop();
}
Liveness detection function development
Default scan interface
- Create a silent liveness detection result callback to obtain the detection result.
private MLLivenessCapture.Callback callback = new MLLivenessCapture.Callback() {
@Override
public void onSuccess(MLLivenessCaptureResult result) {
//检测成功的处理逻辑,检测结果可能是活体或者非活体。
}
@Override
public void onFailure(int errorCode) {
//检测未完成,如相机异常CAMERA_ERROR,添加失败的处理逻辑。
}
};
- Create a silent liveness detection instance and start the detection.
MLLivenessCapture capture = MLLivenessCapture.getInstance();
capture.startDetect(activity, callback);
Learn more details>>
Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee
Follow us to know the latest technical information of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。