Face recognition has been widely used in mobile phone unlocking, face payment, gate authentication and other life scenarios. However, although face recognition ability brings great convenience, it cannot identify whether the face is real, such as using high simulation Pictures, precision plaster or 3D modeling masks can easily break the face recognition algorithm. Using this ability alone has great security risks.

HUAWEI's machine learning service 's motion liveness detection capability uses command-and-action coordination for liveness detection. Three of the six motions of blinking, opening mouth, shaking left, shaking right, staring, and nodding are randomly selected, allowing users to complete the action according to the command. , using face key points and face tracking technology, through continuous pictures, calculate the ratio of the changing distance to the constant distance, and compare the previous frame image with the next frame image, so as to verify whether the user is a real living body. Attacks on photos, videos and masks have a good defensive effect, which is a prerequisite for effective application of face recognition.

In addition, in the process of using the motion detection capability , it supports guided detection for detection scenarios such as occlusion and poor lighting, such as timely display of "dark light prompts", "blurred portraits", "sunglasses and masks occlusion", "people Face is too close, too far” and other prompts to achieve a more friendly interactive experience and create a safe and reliable payment-level liveness detection capability.

Compared with silent liveness detection that does not require the user to make coordinated actions, the interactive liveness detection capability is more suitable for scenarios such as banking, finance, and medical care that require human-computer interaction. For example, using this technology in the financial field allows users to open financial accounts, insurance and wealth management remotely without having to go to the bank in person; in offline supermarkets and other self-service payment scenarios, users need to complete the payment through motion detection to ensure the safety of personal funds. In social security, medical insurance, personal tax and other operating scenarios, it is also necessary to accurately verify whether the operator is a living body through motion live detection, so as to improve the safety of operation.

So how to integrate motion detection capabilities? Proceed as follows.

1 Development steps

Before developing, you need to complete the necessary development preparations, and make sure that the Maven warehouse address of the HMS Core SDK has been configured in your project, and the SDK integration of this service has been completed.

Method 1: fullSDK integration

 dependencies{
    // 引入动作活体检测集合包。
    implementation 'com.huawei.hms:ml-computer-vision-interactive-livenessdetection
: 3.2.0.122'
}

Method 2: Basic SDK method integration

 dependencies{
    // 引入活体检测plugin包。
    implementation 'com.huawei.hms:ml-computer-vision-interactive-livenessdetection-plugin:3.2.0.122'
}

Action liveness detection provides two invocation methods, and you can choose the corresponding invocation method to build the liveness detection service according to your needs.

 1.1    默认扫描界面
1.创建活体检测结果回调,用于获取检测结果。
private MLInteractiveLivenessCapture.Callback callback = new MLInteractiveLivenessCapture.Callback() {
    @Override
    public void onSuccess(MLInteractiveLivenessCaptureResult result) {
        // 检测成功的处理逻辑,检测结果可能是活体或者非活体。
        swich(result.getStateCode()) {
            case InteractiveLivenessStateCode.ALL_ACTION_CORRECT:
            //验证通过后对应具体操作

            case InteractiveLivenessStateCode.IN_PROGRESS:
            //正在检测时对应具体操作
            …
    }

    @Override
    public void onFailure(int errorCode) {
        // 检测未完成,如相机异常CAMERA_ERROR,添加失败的处理逻辑。
    }
};
2.创建活体检测实例,启动检测。
MLInteractiveLivenessConfig interactiveLivenessConfig = new MLInteractiveLivenessConfig.Builder().build();

        MLInteractiveLivenessCaptureConfig captureConfig = new MLInteractiveLivenessCaptureConfig.Builder()
                .setOptions(MLInteractiveLivenessCaptureConfig.DETECT_MASK)
                .setActionConfig(interactiveLivenessConfig)
                .setDetectionTimeOut(TIME_OUT_THRESHOLD)
                .build();
MLInteractiveLivenessCapture capture = MLInteractiveLivenessCapture.getInstance();
capture.startDetect(activity, callback);
1.2    自定义扫描界面
1.创建MLInteractiveLivenessDetectView,并加载到Activity布局。
/**
* I.绑定相机预览界面,设置活体识别区域。
*在相机预览流中,活体检测会对人脸在不在预览视频流的人脸框中进行判断,为了提高活*体的通过率,建议人脸框放在屏幕中间,且活体识别区域比绘制的人脸框范围略大。
* II.设置是否检测口罩。
* III.设置结果回调。
* IV.将MLInteractiveLivenessDetectView加载到Activity。
*/
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liveness_custom_detection);
        mPreviewContainer = findViewById(R.id.surface_layout);
        MLInteractiveLivenessConfig interactiveLivenessConfig = new MLInteractiveLivenessConfig.Builder().build();
mlInteractiveLivenessDetectView = new MLInteractiveLivenessDetectView.Builder()
                .setContext(this)
                //设置是否检测口罩
                .setOptions(MLInteractiveLivenessCaptureConfig.DETECT_MASK)
                //设置检测动作,静默为0,动作为1。
                .setType(1)
                //设置相机视频流预览位置(左上右下像素值基于预览view)
                   .setFrameRect(new Rect(0, 0, 1080, 1440))
                //设置动作活体调用
                .setActionConfig(interactiveLivenessConfig)
                //设置人脸框相对于预览view的位置(左上右下基于640*480图像坐标,建议宽高比符合实际人脸比例),人脸框作用为检测人脸远近和是否偏移
                .setFaceRect(new Rect(84, 122, 396, 518))
                //设置检测超时时间,建议10000毫秒左右。
                .setDetectionTimeOut(10000)
                //设置结果回调
                .setDetectCallback(new OnMLInteractiveLivenessDetectCallback() {
                    @Override
                    public void onCompleted(MLInteractiveLivenessCaptureResult result) {
                    // 活体检测完成时的结果回调
                    swich(result.getStateCode()) {
                        case InteractiveLivenessStateCode.ALL_ACTION_CORRECT:
                        //验证通过后对应具体操作

                        case InteractiveLivenessStateCode.IN_PROGRESS:
                        //正在检测时对应具体操作
                        …
                        }
                    }

                    @Override
                    public void onError(int error) {
                    // 活体检测发生错误时的错误码回调
                    }
                }).build();

        mPreviewContainer.addView(mlInteractiveLivenessDetectView);
        mlInteractiveLivenessDetectView.onCreate(savedInstanceState);
}

2.对MLInteractiveLivenessDetectView设置生命流程监听。
@Override
protected void onDestroy() {
    super.onDestroy();
    MLInteractiveLivenessDetectView.onDestroy();
}

@Override
protected void onPause() {
    super.onPause();
    MLInteractiveLivenessDetectView.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    MLInteractiveLivenessDetectView.onResume();
}

@Override
protected void onStart() {
    super.onStart();
    MLInteractiveLivenessDetectView.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    MLInteractiveLivenessDetectView.onStop();
}

Learn more details>>

Visit the Machine Learning Service official website

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~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙应用,和开发者共同成长。