2.2.0 • Published 17 days ago

@getyoti/react-face-capture v2.2.0

Weekly downloads
-
License
Yoti Face Capture...
Repository
-
Last release
17 days ago

Yoti Face Capture integration

The purpose of this module is to capture a face and return the output image.

CHANGELOG

Please check the CHANGELOG to review release changes, issues, fixes, and possible breaking changes on Major releases.

Pre-requisite to adopt the dependency

Peer dependencies

The package depends on the following peer dependencies

"react": ">=16.14.0 <=18.2.0",
"react-dom": ">=16.14.0 <=18.2.0"

Browser support

BrowserVersions
and_chr123
chrome124,123,122,121
edge123,122
firefox125,124,123,122
ios_saf17.4,17.3,17.2
safari17.4,17.3,17.2,17.1

Secondly, the module needs MediaDevices.getUserMedia() to work. When this isn't supported by the browser, the error UNSUPPORTED_BROWSER will be sent.

Note: Non-Safari browsers on iOS are not supported because of an iOS limitation.

Note: Edge is only supported on desktop versions.

Finally, some devices might experience poor performance when attempting to detect a face. This is because the underlying library TensorFlow.js can still be too demanding for older less performant devices.

Note: iPhone 7 and older will take a longer time to load the face detector model than more recent devices, and in most cases, will fall back from auto to manual capture mode. If this behaviour is not desired, as an integrator you could disable the manual capture mode altogether, implementing manualCaptureFallback = false. Note, this could increase the load time for old and low-quality devices.

Note: It is possible to disable the picture-in-picture feature for Firefox in its configuration.

React Face Capture

Install dependency

npm i @getyoti/react-face-capture -S

Import js and css

import FaceCapture from "@getyoti/react-face-capture"

Copy assets

Assets are not included in the javascript bundle. To have the components to work correctly, you will need to copy library assets from ./node_modules/@getyoti/react-face-capture/assets folder into your output assets folder.

For instance, in webpack you can use the plugin copy-webpack-plugin in the following way:

new CopyPlugin({
  patterns: [
    {
      from: path.resolve(__dirname, './node_modules/@getyoti/react-face-capture/assets'),
      to: path.resolve(__dirname, './dist/assets'),
    },
  ],
});

CSS Customization

Now, it is possible to customize the style by changing the following CSS properties:

  • --fcm-primary-button-background-color changes the background color in primary buttons (Take picture, Try again, ... ).
    • --fcm-primary-button-background-color-hover changes the background color in primary buttons (Take picture, Try again, ... ) when is hovered.
  • --fcm-primary-button-text-color changes the text color in primary buttons (Take picture, Try again, ... ).
  • --fcm-secondary-button-background-color changes the background color in primary buttons (Get help).
  • --fcm-secondary-button-text-color changes the text color in secondary buttons (Get help).
    • --fcm-secondary-button-text-color-hover changes the text color in secondary buttons (Get help) when the button is hovered.
  • --fcm-font-family sets the font family.
  • --fcm-prompt-text-color changes the text color in the prompt.
  • --fcm-prompt-background-color sets the background color in the message prompt.

The CSS custom properties must be defined using the :root CSS selector. Here is an example:

:root {
  --fcm-primary-button-background-color: red;
}

Note: We do not recommend changing these css properties, all changes on the font and colors are under the integrator's responsibility and need to be tested by the integrator. In addition, we can't guarantee external customizations support on new versions of the library

Properties

Property nameTypeDefaultMandatoryDescription
captureMethodString manual/autoauto-Capture method to take the photo: by clicking a button or auto-capture.
manualCaptureFallbackBooleantrue-In auto capture method, allow the use of manual capture mode as fallback. This would be triggered only when low performance conditions are detected, usually on very old devices. NOTE: If this option is turned off, some users might not be able to successfully submit an image.
secureBooleantrue-Use the secure mode
clientSdkIdString-Yes*Identifies your Yoti Hub application. This value can be found in the Hub, within your application section, in the keys tab.
onSuccessFunction(payload, base64PreviewImage)-YesCallback called once the result (payload) is complete. The content of capture will vary when using secure or non-secure mode (See secure mode section). The second argument base64PreviewImage will only be provided if returnPreviewImage is set to true.
onErrorFunction--Callback called when there is an error. See Appendix for the list of error codes we currently support.
onReadyForCaptureFunction--Callback called when the Face Capture is ready to take images (camera feed and face scan are ready).
showOverlayBooleantrue-Optional use of the face overlay.
resolutionTypeString hd/full_hdhd-Image resolution constraints passed to getUserMedia.
formatString jpeg/pngjpeg-Image format type.
imageTypeString original/croppedoriginal-Selects if the image will be the original or it will be cropped in order to improve the timing response when processing the image in the API call.
qualityTypeString high/medium/lowhigh-Sets the image quality of jpeg format images only. High (1) - Medium (0.96) - Low (0.90).
languageLanguage code **en-The language code to set the Face Capture language.
a11yLiveRegionModeString assertive/politepolite-Determines the politeness setting of the live region used to read out prompts for screen reader users
showInitialGuidanceBooleantrue-Show the initial help guidance.
faceCaptureAssetsRootUrlStringassets/face-capture/-Root url where the face detection assets are located. See copy assets section for assets configuration.
loadTimeoutNumber15000 milliseconds-Sets the time (ms) the Face Capture will use to notify through the onError callback about downloading taking too long. The Face Capture will keep downloading after the notification.
showGetHelpButtonBooleantrue-Show the Get Help button.
autoSessionReloadBooleantrue-Automatically renews sessions after they expire.
userRetryErrorBooleantrue-Gives the possibility to users to retry several times when an error occurs.
returnPreviewImageBooleanfalse-Provide the image captured (base64PreviewImage) in the onSuccess callback.
encryptImageBooleantrue-Encrypt the image inside the payload argument on the onSuccess callback when it runs the secure mode.
allowBackgroundFacesBooleanfalse-Allows faces to be present in the background when the image is being captured. The face capture will return a cropped image with the main face when set to true. Note: This feature is designed for retail terminals and unsuitable for online user's face capture.
faceSelectionMethodString center/areacenter-Defines where the main face can be placed, in the center or anywhere in the image. Note: This feature is designed for retail terminals and unsuitable for online user's face capture. It is recommend to enable allowBackgroundFaces when faceSelectionMethod is set to area.
faceStabilityThresholdNumber0.8-Configures how strict the stability check will be. The minimum value is 0.4 and the maximum value is 0.9.
numStableFramesNumber4-Determines how many frames are used for the stability check. The minimum value is 2 and the maximum is 5.

(*) This field is only mandatory when using the secure mode. (secure = true)

(**) Current languages supported:

  • en-GB: English
  • ar-XN: Arabic
  • bg-BG: Bulgarian
  • cs-CZ: Czech
  • da-DK: Danish
  • de-DE: German
  • el-GR: Greek
  • es: Spanish (Spain) ***
  • es-ES: Spanish (Spain)
  • es-419: Spanish (Latin America)
  • et-EE: Estonian
  • fa-IR: Persian
  • fi-FI: Finnish
  • fr-FR: French
  • he-IL: Hebrew
  • hi-IN: Hindi
  • hu-HU: Hungarian
  • hy-AM: Armenian
  • id-ID: Bahasa
  • it-IT: Italian
  • ja-JP: Japanese
  • ko-KR: Korean
  • lt-LT: Lithuanian
  • lv-LV: Latvian
  • ms-MY: Malay
  • nb-NO: Norwegian
  • nl-NL: Dutch
  • pl-PL: Polish
  • pt: Portuguese (Portugal) ***
  • pt-BR: Portuguese (Brazil)
  • pt-PT: Portuguese (Portugal)
  • ro-RO: Romanian
  • ru-RU: Russian
  • sk-SK: Slovak
  • sv-SE: Swedish
  • th-TH: Thai
  • tl-PH: Tagalog
  • tr-TR: Turkish
  • uk-UA: Ukrainian
  • ur-PK: Urdu
  • vi-VN: Vietnamese
  • zh-CN: Chinese (Simplified)

(***) Language codes marked with *** are listed for disambiguation purposes on the fallback. They are exactly the same as their respective regional variants.

Language fallback

A mechanism used for the property language to avoid issues when the value passed is wrong. Example: first try exact match (es-es or es-ES, ignore case):

  • If there is no match, try the mainstream (es), by removing the region part (-es).
  • If there is no mainstream, try any other available es-region sibling (example: es-419).
  • If there is no regional sibling, use the default language: en.

Error codes

Error codeDescription
NO_CAMERANo camera detected on the user’s device.
GENERIC_CAMERA_ERRORUndefined camera error. The reasons can be various and inconsistent across browsers.
UNSUPPORTED_BROWSERThe user’s browser is not supported. This could be because the API needed for the camera interaction is missing. Note: Older Non-Safari browsers on iOS fall into this category.
NO_CAMERA_PERMISSIONThe user rejected the camera permission.
OVERCONSTRAINEDThe camera constraints are not compatible with available camera devices. Note: One recovery option could be to use hd resolution.
FACE_DETECTION_INIT_ERRORThe face detection has failed to initialise. This usually means the required model assets are missing from the host application.
INTERNAL_ERRORThis can be due to a programming error, or the user using an old unsupported browser.
CAPTURE_LOAD_ERRORThe secure module could not be loaded. This usually means your token is not updated, or there is an error on the module provider.
VIDEO_STREAM_INTERRUPTEDThe camera stream has stopped unexpectedly.
SECURE_SESSION_EXPIREDThe secure session has expired (the token expire field is older than the current time).
EXCEEDED_TIME_TO_LOADThe module is taking more than the time set in the FaceCapture component to load. The property to change that value is loadTimeout which by default is 15 seconds.
VERSION_NO_LONGER_AVAILABLEThe requested Face Capture secure module is no longer available. Note that the FCM will log a warning in the console if the module was deprecated.
INVALID_SECURE_CLIENT_SDK_IDThe client SDK ID has a wrong format, it isn't included on the internal product mapping or there is a mismatch between the SDK from the JWT and the one related to the internal product contained in the BE request.

The error codes can be imported as follow:

import { ERROR_CODE } from '@getyoti/react-face-capture';

Secure mode

The Secure mode allows Yoti back-end service to verify that the image captured on the client-side browser with the FCM hasn't been modified in any form. Note, the secure mode of Yoti FCM makes a request to Yoti back-end to download the encrypted capture package that takes the photo and create the secure result request. The secure result information will be returned in onSuccess callback (payload argument). It is the information that the Yoti back-end service needs to run a prediction. The service will return an error if the payload is modified:

{
  "img": "<base64_img>",
  "secure": {
    "version": "<fcm_version>",
    "token": "<session_token>",
    "signature": "<result_signature>",
    "verification": "<verification_data>"
  }
}

<fcm_version> is the current version of FCM (embedded in the module) and <session_token> is the session token generated from the request.

Possible issues

The secure mode requests Yoti services to download the encrypted module on demand, therefore the client must be able to handle those requests. Keep in mind if your client uses any mechanism to prevent data injection attacks or cross-site scripting like CSP you will need to allow the FCM requests.

Virtual cameras

The secure mode detects modifications on the camera stream source and fraudulent camera hardware. In these scenarios, it will return an UNTRUSTED_SECURE_SESSION error code when calling the Yoti BE services.

Example

import FaceCapture from '@getyoti/react-face-capture';

export function App() {
  const onSuccess = (payload, base64PreviewImage) => console.log('Length = ', payload.img.length);
  const onError = (error) => console.log('Error =', error);

  return <FaceCapture returnPreviewImage onSuccess={onSuccess} onError={onError} />;
}

Face Capture vanilla

For projects without a React tech stack, you can use the Face Capture vanilla version. When you install the library on your machine, It's located inside the folder vanilla.

Add the package to your codebase and serve the static assets:

1. Serve package static assets

/@getyoti/react-face-capture/vanilla/assets/face-capture/
/@getyoti/react-face-capture/vanilla/index.js

2. Add Face Capture script

Inside the page you want to have Face Capture, add Face Capture scripts and styles like in the example below. faceCaptureAssetsRootUrl is a property you can use to instruct the library where neural net files are served otherwise we look for the files relative to the page location assets/face-capture/.

3. Render Face Capture

Our scripts will give you access to Yoti.FaceCaptureModule.render where you can set 2 parameters. The first parameter is the properties, the same ones used in the integration with react. The second parameter is a reference to HTML DOM, so we know where we can render the UI.

In order to unmount the component, it is highly recommended to call the Yoti.FaceCaptureModule.unmount function instead of destroying the HTML element, which could lead to some issues or misbehaviour.

index.html

<script src="/@getyoti/react-face-capture/vanilla/index.js"></script>
<script>
  const props = {
    faceCaptureAssetsRootUrl: '/@getyoti/react-face-capture/vanilla/assets/face-capture/',
    onSuccess: (data) => {
      console.log('Length = ', data.img.length);
    },
    onError: (error) => {
      console.log('Error =', error);
    },
  };

  const fcm = Yoti.FaceCaptureModule.render(props, document.getElementById('face-capture-module-root'));

  function reload() {
    fcm.reload();
  }

  function unmount() {
    fcm.unmount();
  }
</script>

<div id="face-capture-module-root"></div>

Debugging Yoti Face Capture

Yoti Face Capture is protected with code obfuscation techniques for security reasons.

This protection is present in both secure and non-secure modes and it makes it almost impossible to debug the module by using development tools on any browser, as the application will crash or keep looping right after the tool menu is opened.

Troubleshooting

NextJS

The Face Capture does not support server side rendering. It needs to be loaded dynamically:

import dynamic from 'next/dynamic';
const FaceCapture = dynamic(() => import('@getyoti/react-face-capture'), {
  ssr: false,
});

export default function CustomFCM(props) {
  return <FaceCapture {...props} />;
}

Webpack

The secure mode loads secure javascript code from server side on demand. The Face Capture uses the comment /* webpackIgnore: true */ order to tell webpack that it does not need to resolve the dependency locally so this comment cannot be removed before webpack builds the final javascript files for the web application.

2.2.1-dev.1

17 days ago

2.2.0

29 days ago

2.1.0

3 months ago

2.0.0

5 months ago

1.4.0

1 year ago

2.0.0-beta.1

6 months ago

1.3.1-test.1

7 months ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

2 years ago

1.0.0

2 years ago

1.0.0-alpha.4

2 years ago

1.0.0-beta.3

2 years ago

1.0.0-beta.2

2 years ago

0.5.0

2 years ago

0.6.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.1.0-beta.1

3 years ago