Search code examples
face-detectionhuawei-mobile-servicescamera-viewhuawei-ml-kit

How to integrate Huawei HMS Face Liveness Detection with CameraView?


First, I tried Huawei Face Liveness Detection. With the sample code, it works.

Next, I tried CameraView. Also, by just following the sample code, I am able to perform frame processing, achieving face detection and face recognition.

<com.otaliastudios.cameraview.CameraView
        app:cameraFacing="front"
        android:id="@+id/cameraView"
        app:cameraEngine="camera2"
        app:cameraPreview="glSurface"
        android:keepScreenOn="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:cameraAudio="off"
        app:cameraExperimental="true">
</com.otaliastudios.cameraview.CameraView>

Question: How to integrate Huawei Face Liveness Detection into CameraView?

Provided the Face Liveness Detection code below, I tried changing the view container (mPreviewContainer as below), but it just throws error and app exits.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liveness_custom_detection);
        mPreviewContainer = findViewById(R.id.surface_layout); //<------ Change this to CameraView
        mlLivenessDetectView = new MLLivenessDetectView.Builder()
                .setContext(this)
                .setFaceFrameRect(new Rect(0, 0, 0, 200))
                .setDetectCallback(new OnMLLivenessDetectCallback() {
                ...

    }

I am curious how to integrate the Huawei Face Liveness Detection into CameraView (or even normal Camera2 or CameraX)? Can the HMS take the input frames from CameraView, instead of opening another camera?

P.S.: The first error appeared (out of the lengthy message):

I/BufferQueue: [unnamed-11129-0](this:0x70859fb800,id:0,api:0,p:-1,c:-1) BufferQueue core=(11129:com.example.cv1)
E/AndroidRuntime: FATAL EXCEPTION: CameraViewEngine
    Process: com.example.cv1, PID: 11129
    com.otaliastudios.cameraview.CameraException
        at com.otaliastudios.cameraview.engine.Camera2Engine$2.onDisconnected(Camera2Engine.java:435)
        at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:252)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:226)
        at android.os.HandlerThread.run(HandlerThread.java:65)
E/CameraEngine: EXCEPTION: Handler thread is gone. Replacing.
E/CameraEngine: EXCEPTION: Scheduling on the crash handler...

Solution

  • Update:

    Please kindly refer to ML Kit Face Verification. It recognizes and extracts key features of the face in the template, compares the features with those of the face in the input image, and then determines whether the two faces belong to the same person based on their similarity.


    To achieve liveness detection and face detection/face recognition, there are two services we need: liveness detection service and face detection (Actually face comparison service, which will be supported in 2021.) Currently, HMS Liveness Detection does not support the method of CameraView (by taking the input frames) to achieve face recognition. You may try this two services: Facial recognition (LocalAuthentication Engine) or Facial comparison (HiAI Engine).


    Q: Can the HMS take the input frames from CameraView, instead of opening another camera?

    No, it cannot take input frames from CameraView. Because the liveness detection is a multi-frame detection solution. Currently, the logic of frame sending is encapsulated. You app only needs to apply for the camera permission and use the camera on the device for identification or detection.