Skip to content

Liveness

Liveness is a UI module that displays camera preview and analyzes video streams from a user's device to guide them through a process of detection.

Display Liveness

Liveness comes with the configuration allowing you to change the behavior and appearance of some UI elements. For more UI customization, check out UI Customization.

Info

At the moment, the configuration is only available for the iOS and Android platforms.

Start the Liveness process with configuration:

let configuration = LivenessConfiguration {
    $0.cameraPosition = .front
    $0.cameraSwitchEnabled = true
}

FaceSDK.service.startLiveness(
    from: viewController,
    animated: true, 
    configuration: configuration,
    onLiveness: { response in
        // ... check response.liveness for detection result.
    },
    completion: nil
)
RFSLivenessConfiguration *configuration = [RFSLivenessConfiguration configurationWithBuilder:^(RFSLivenessConfigurationBuilder  * _Nonnull builder) {
    builder.cameraPosition = RFSCameraPositionFront;
    builder.cameraSwitchEnabled = YES;
}];

[RFSFaceSDK.service startLivenessFrom:viewController
                             animated:YES
                        configuration:configuration
                           onLiveness:^(RFSLivenessResponse * _Nonnull response) {
    // ... check response.liveness for detection result.
} completion:nil];
val configuration = LivenessConfiguration.Builder()
    .setCameraId(0)
    .setCameraSwitchEnabled(true)
    .build()

FaceSDK.Instance().startLiveness(this@MainActivity, configuration) { response ->
    // ... check response.getLiveness(); for detection result.
}
LivenessConfiguration configuration = new LivenessConfiguration.Builder()
    .setCameraId(0)
    .setCameraSwitchEnabled(true)
    .build();

FaceSDK.Instance().startLiveness(MainActivity.this, configuration, livenessResponse -> {
    // ... check livenessResponse.liveness for detection result.
});
FaceSDK.startLiveness(livenessResponse => {
    const response = LivenessResponse.fromJson(JSON.parse(livenessResponse));
    // ... check response.liveness for detection result.
}, e => { });
FaceSDK.startLiveness().then((livenessResponse) {
    var response = LivenessResponse.fromJson(jsonDecode(livenessResponse));
    // ... check response.liveness for detection result.
});
FaceSDK.startLiveness(livenessResponse => {
    const response = FaceSDK.LivenessResponse.fromJson(JSON.parse(livenessResponse));
    // ... check response.liveness for detection result.
}, e => { });
FaceSDK.startLiveness().then(livenessResponse => {
    const response = LivenessResponse.fromJson(JSON.parse(livenessResponse));
    // ... check response.liveness for detection result.
});

To keep the default configuration, you can omit the configuration parameter.

To stop the Liveness processing programmatically, there is a call:

FaceSDK.service.stopLivenessProcessing()
[RFSFaceSDK.service stopLivenessProcessing];
FaceSDK.Instance().stopLivenessProcessing(this@MainActivity)
FaceSDK.Instance().stopLivenessProcessing(MainActivity.this);
FaceSDK.stopLivenessProcessing(() => {}, () => {});
FaceSDK.stopLivenessProcessing();
FaceSDK.stopLivenessProcessing();
FaceSDK.stopLivenessProcessing();

Response

Status

The LivenessResponse contains the result of processing in the form of the liveness status and the error properties.

To determine the result of the detection, check for the liveness property:

if case response.liveness = .passed {
    // continue with successful path.
}
if (response.liveness == RFSLivenessStatusPassed) {
    // continue with successful path.
}
if (response.liveness == LivenessStatus.PASSED) {
    // continue with successful path.
}
if (response.getLiveness() == LivenessStatus.PASSED) {
    // continue with successful path.            
}
if (response.liveness == LivenessStatus.PASSED) {
    // continue with successful path.
}
if (response?.liveness == LivenessStatus.PASSED) {
    // continue with successful path.
}
if (response.liveness == FaceSDK.Enum.LivenessStatus.PASSED) {
    // continue with successful path.
}
if (response.liveness == LivenessStatus.PASSED) {
    // continue with successful path.
}

Operation GUID

The Liveness response also contains the guid property – the id of the processing from the Liveness web service. The property contains a formatted UUID string such as E4C72506-70D9-4A04-9EA0-90513213821C. This guid allows for matching the requests from the client and the processing logs on the web server.

Suppose you want to find a video recording of the liveness process for a specific request. To achieve that, you just need to find the log file that contains this guid property in the filename.

Example:
The liveness operation has been made from the mobile client. The response's guid value contains E4C72506-70D9-4A04-9EA0-90513213821C. With that information you can proceed to the logs folder and find the following filename:

/logs/liveness/video/liveness_default/2022/2/17/5/2022_02_17_05_56_57_E4C72506-70D9-4A04-9EA0-90513213821C_1_in

Attention

The logging of processing results should be enabled on the Web Service. Please take a look at the settings documentation.

Error Handling

The errors and exceptions give you an insight into what happened to the Liveness processing from a user canceling the operation to a missing License on a web service.

Here is an example of how you can handle a failed LivenessResponse:

if let error = response.error {
    // There is an error. Lets see what type is it.
    switch error {
        case LivenessError.cancelled:
            print("User cancelled the processing.")
        case LivenessError.noLicense:
            print("Web Service is missing a valid License.")
        case LivenessError.processingAttemptsEnded:
            print("Reached the number of possible attempts. See `RFSLivenessConfiguration.attemptsCount` for more information.")
        case LivenessError.processingFailed:
            print("Bad input data.")
        case LivenessError.processingTimeout:
            print("Processing finished by timeout.")
        case LivenessError.apiCallFailed:
            print("Web Service API call failed due to networking error or backend internal error.")
        default:
            break
    }
}
if (response.error) {
    // There is an error. Lets see what type is it.
    switch ((RFSLivenessError)response.error.code) {
        case RFSLivenessErrorCancelled:
            NSLog(@"User cancelled the processing.");
            break;
        case RFSLivenessErrorNoLicense:
            NSLog(@"Web Service is missing a valid License.");
            break;
        case RFSLivenessErrorProcessingAttemptsEnded:
            NSLog(@"Reached the number of possible attempts. See `RFSLivenessConfiguration.attemptsCount` for more information.");
            break;
        case RFSLivenessErrorProcessingFailed:
            NSLog(@"Bad input data.");
            break;
        case RFSLivenessErrorProcessingTimeout:
            NSLog(@"Processing finished by timeout.");
            break;
        case RFSLivenessErrorAPICallFailed:
            NSLog(@"Web Service API call failed due to networking error or backend internal error.");
            break;
    }
}
val exception = response.exception
if (exception != null) {
    when (exception.errorCode) {
        LivenessErrorCode.CANCELLED ->
            print("User cancelled the processing.")
        LivenessErrorCode.NO_LICENSE ->
            print("Web Service is missing a valid License.")
        LivenessErrorCode.PROCESSING_ATTEMPTS_ENDED ->
            print("Reached the number of possible attempts. See `LivenessConfiguration.attemptsCount` for more information.")
        LivenessErrorCode.PROCESSING_FAILED ->
            print("Bad input data.")
        LivenessErrorCode.PROCESSING_TIMEOUT ->
            print("Processing finished by timeout.")
        LivenessErrorCode.API_CALL_FAILED ->
            print("Web Service API call failed due to networking error or backend internal error.")
        LivenessErrorCode.CONTEXT_IS_NULL ->
            print("Provided context is null.")
        LivenessErrorCode.IN_PROGRESS_ALREADY ->
            print("Liveness has already started.")
        LivenessErrorCode.ZOOM_NOT_SUPPORTED ->
            print("Camera zoom support is required.")
    }
}
LivenessErrorException exception = response.getException();
if (exception != null) {
    switch (exeption.getErrorCode()) {
        case LivenessErrorCode.CANCELLED:
            Log.d("liveness", "User cancelled the processing.");
            break;
        case LivenessErrorCode.NO_LICENSE:
            Log.d("liveness", "Web Service is missing a valid License.");
            break;
        case LivenessErrorCode.PROCESSING_ATTEMPTS_ENDED:
            Log.d("liveness", "Reached the number of possible attempts. See `LivenessConfiguration.attemptsCount` for more information.");
            break;
        case LivenessErrorCode.PROCESSING_FAILED:
            Log.d("liveness", "Bad input data.");
            break;
        case LivenessErrorCode.PROCESSING_TIMEOUT:
            Log.d("liveness", "Processing finished by timeout.");
            break;
        case LivenessErrorCode.API_CALL_FAILED:
            Log.d("liveness", "Web Service API call failed due to networking error or backend internal error.");
            break;
        case LivenessErrorCode.CONTEXT_IS_NULL:
            Log.d("liveness", "Provided context is null.");
            break;
        case LivenessErrorCode.IN_PROGRESS_ALREADY:
            Log.d("liveness", "Liveness has already started.");
            break;
        case LivenessErrorCode.ZOOM_NOT_SUPPORTED:
            Log.d("liveness", "Camera zoom support is required.");
            break;
    }
}
const exception = response.exception;
if (exception) {
    switch (exception.errorCode) {
        case LivenessErrorCode.CANCELLED:
            console.log('liveness: User cancelled the processing.');
            break;
        case LivenessErrorCode.NO_LICENSE:
            console.log('liveness: Web Service is missing a valid License.');
            break;
        case LivenessErrorCode.PROCESSING_ATTEMPTS_ENDED:
            console.log('liveness: Reached the number of possible attempts. See `LivenessConfiguration.attemptsCount` for more information.');
            break;
        case LivenessErrorCode.PROCESSING_FAILED:
            console.log('liveness: Bad input data.');
            break;
        case LivenessErrorCode.PROCESSING_TIMEOUT:
            console.log('liveness: Processing finished by timeout.');
            break;
        case LivenessErrorCode.API_CALL_FAILED:
            console.log('liveness: Web Service API call failed due to networking error or backend internal error.');
            break;
        case LivenessErrorCode.CONTEXT_IS_NULL:
            console.log('liveness: Provided context is null.');
            break;
        case LivenessErrorCode.IN_PROGRESS_ALREADY:
            console.log('liveness: Liveness has already started.');
            break;
        case LivenessErrorCode.ZOOM_NOT_SUPPORTED:
            console.log('liveness: Camera zoom support is required.');
            break;
    }
}
var exception = response?.exception;
if (exception != null) {
  switch (exception.errorCode) {
    case LivenessErrorCode.CANCELLED:
      print('liveness: User cancelled the processing.');
      break;
    case LivenessErrorCode.NO_LICENSE:
      print('liveness: Web Service is missing a valid License.');
      break;
    case LivenessErrorCode.PROCESSING_ATTEMPTS_ENDED:
      print('liveness: Reached the number of possible attempts. See `LivenessConfiguration.attemptsCount` for more information.');
      break;
    case LivenessErrorCode.PROCESSING_FAILED:
      print('liveness: Bad input data.');
      break;
    case LivenessErrorCode.PROCESSING_TIMEOUT:
      print('liveness: Processing finished by timeout.');
      break;
    case LivenessErrorCode.API_CALL_FAILED:
      print(
          'liveness: Web Service API call failed due to networking error or backend internal error.');
      break;
    case LivenessErrorCode.CONTEXT_IS_NULL:
      print('liveness: Provided context is null.');
      break;
    case LivenessErrorCode.IN_PROGRESS_ALREADY:
      print('liveness: Liveness has already started.');
      break;
    case LivenessErrorCode.ZOOM_NOT_SUPPORTED:
      print('liveness: Camera zoom support is required.');
      break;
  }
}
const exception = response.exception;
if (exception) {
    switch (exception.errorCode) {
        case FaceSDK.Enum.LivenessErrorCode.CANCELLED:
            console.log('liveness: User cancelled the processing.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.NO_LICENSE:
            console.log('liveness: Web Service is missing a valid License.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.PROCESSING_ATTEMPTS_ENDED:
            console.log('liveness: Reached the number of possible attempts. See `LivenessConfiguration.attemptsCount` for more information.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.PROCESSING_FAILED:
            console.log('liveness: Bad input data.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.PROCESSING_TIMEOUT:
            console.log('liveness: Processing finished by timeout.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.API_CALL_FAILED:
            console.log('liveness: Web Service API call failed due to networking error or backend internal error.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.CONTEXT_IS_NULL:
            console.log('liveness: Provided context is null.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.IN_PROGRESS_ALREADY:
            console.log('liveness: Liveness has already started.');
            break;
        case FaceSDK.Enum.LivenessErrorCode.ZOOM_NOT_SUPPORTED:
            console.log('liveness: Camera zoom support is required.');
            break;
    }
}
const exception = response.exception;
if (exception) {
    switch (exception.errorCode) {
        case LivenessErrorCode.CANCELLED:
            console.log('liveness: User cancelled the processing.');
            break;
        case LivenessErrorCode.NO_LICENSE:
            console.log('liveness: Web Service is missing a valid License.');
            break;
        case LivenessErrorCode.PROCESSING_ATTEMPTS_ENDED:
            console.log('liveness: Reached the number of possible attempts. See `LivenessConfiguration.attemptsCount` for more information.');
            break;
        case LivenessErrorCode.PROCESSING_FAILED:
            console.log('liveness: Bad input data.');
            break;
        case LivenessErrorCode.PROCESSING_TIMEOUT:
            console.log('liveness: Processing finished by timeout.');
            break;
        case LivenessErrorCode.API_CALL_FAILED:
            console.log('liveness: Web Service API call failed due to networking error or backend internal error.');
            break;
        case LivenessErrorCode.CONTEXT_IS_NULL:
            console.log('liveness: Provided context is null.');
            break;
        case LivenessErrorCode.IN_PROGRESS_ALREADY:
            console.log('liveness: Liveness has already started.');
            break;
        case LivenessErrorCode.ZOOM_NOT_SUPPORTED:
            console.log('liveness: Camera zoom support is required.');
            break;
    }
}

Info

For more information on LivenessConfiguration and LivenessResponse, see the SDK Reference page.