Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions app/javascript/packages/document-capture/context/acuant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ interface AcuantContextProviderProps {
* disabled.
*/
passiveLivenessSrc: string | undefined;
/**
* Loaded by AcuantPassiveLivness directly. We load it for caching purposes to speed up the AcuantSelfieCamera startup
*/
faceLandmarkWeightsSrc: string | undefined;
/**
* Loaded by AcuantPassiveLivness directly. We load it for caching purposes to speed up the AcuantSelfieCamera startup
*/
tinyFaceLandmarkWeightsSrc: string | undefined;
/**
* Loaded by AcuantPassiveLivness directly. We load it for caching purposes to speed up the AcuantSelfieCamera startup
*/
faceLandmarkModelSrc: string | undefined;
/**
* Loaded by AcuantPassiveLivness directly. We load it for caching purposes to speed up the AcuantSelfieCamera startup
*/
faceLandmarkShardSrc: string | undefined;
/**
* SDK credentials.
*/
Expand Down Expand Up @@ -213,6 +229,10 @@ function AcuantContextProvider({
cameraSrc,
passiveLivenessOpenCVSrc,
passiveLivenessSrc,
faceLandmarkWeightsSrc,
tinyFaceLandmarkWeightsSrc,
faceLandmarkModelSrc,
faceLandmarkShardSrc,
credentials = null,
endpoint = null,
glareThreshold,
Expand Down Expand Up @@ -319,16 +339,47 @@ function AcuantContextProvider({
const passiveLivenessScript = document.createElement('script');
// Open CV script load. Open CV is required only for passive liveness
const passiveLivenessOpenCVScript = document.createElement('script');
// The following four scripts are used interally by the AcuantPassiveLiveness script
// Load them here so they are cached, which greatly reduces the apparent
// startup time for the AcuantSelfieCamera on slow networks.
const tinyFaceLandmarkWeights = document.createElement('script');
const faceLandmarkWeights = document.createElement('script');
const faceLandmarkModel = document.createElement('script');
const faceLandmarkShard = document.createElement('script');
if (passiveLivenessSrc) {
passiveLivenessScript.async = true;
passiveLivenessScript.src = passiveLivenessSrc;
passiveLivenessScript.onerror = () => setIsError(true);
passiveLivenessOpenCVScript.async = true;
passiveLivenessOpenCVScript.src = passiveLivenessOpenCVSrc;
passiveLivenessOpenCVScript.onerror = () => setIsError(true);
if (tinyFaceLandmarkWeightsSrc) {
tinyFaceLandmarkWeights.async = true;
tinyFaceLandmarkWeights.src = tinyFaceLandmarkWeightsSrc;
tinyFaceLandmarkWeights.onerror = () => setIsError(true);
}
if (faceLandmarkWeightsSrc) {
faceLandmarkWeights.async = true;
faceLandmarkWeights.src = faceLandmarkWeightsSrc;
faceLandmarkWeights.onerror = () => setIsError(true);
}
if (faceLandmarkModelSrc) {
faceLandmarkModel.async = true;
faceLandmarkModel.src = faceLandmarkModelSrc;
faceLandmarkModel.onerror = () => setIsError(true);
}
if (faceLandmarkShardSrc) {
faceLandmarkShard.async = true;
faceLandmarkShard.src = faceLandmarkShardSrc;
faceLandmarkShard.onerror = () => setIsError(true);
}
}
document.body.appendChild(passiveLivenessScript);
document.body.appendChild(passiveLivenessOpenCVScript);
document.body.appendChild(faceLandmarkWeights);
document.body.appendChild(tinyFaceLandmarkWeights);
document.body.appendChild(faceLandmarkModel);
document.body.appendChild(faceLandmarkShard);

return () => {
window.acuantConfig = originalAcuantConfig;
Expand All @@ -337,6 +388,10 @@ function AcuantContextProvider({
document.body.removeChild(cameraScript);
document.body.removeChild(passiveLivenessScript);
document.body.removeChild(passiveLivenessOpenCVScript);
document.body.removeChild(faceLandmarkWeights);
document.body.removeChild(tinyFaceLandmarkWeights);
document.body.removeChild(faceLandmarkModel);
document.body.removeChild(faceLandmarkShard);
};
}, []);

Expand Down
13 changes: 13 additions & 0 deletions app/javascript/packs/document-capture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ const App = composeComponents(
passiveLivenessSrc: getSelfieCaptureEnabled()
? acuantVersion && `/acuant/${acuantVersion}/AcuantPassiveLiveness.min.js`
: undefined,
faceLandmarkWeightsSrc: getSelfieCaptureEnabled()
Copy link
Copy Markdown
Contributor Author

@charleyf charleyf Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduth This is where the condition for the script loading is. Two points:

  • These scripts are loaded only when the selfie is required.
  • These scripts aren't actually -used- anywhere in our code. The point here is to cache them when we load the other Acuant scripts so when a user opens the selfie capture, there's a much smaller delay.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha 👍 The main thing for me was only doing the background load when selfie was enabled, which looks like this code is handing. Not sure why I wasn't seeing this code when I searched "weights" earlier! Probably user error 😄

? acuantVersion && `/acuant/${acuantVersion}/tiny_face_detector_model-weights_manifest.json`
: undefined,
tinyFaceLandmarkWeightsSrc: getSelfieCaptureEnabled()
? acuantVersion &&
`/acuant/${acuantVersion}/face_landmark_68_tiny_model-weights_manifest.json`
: undefined,
faceLandmarkModelSrc: getSelfieCaptureEnabled()
? acuantVersion && `/acuant/${acuantVersion}/face_landmark_68_tiny_model.bin`
: undefined,
faceLandmarkShardSrc: getSelfieCaptureEnabled()
? acuantVersion && `/acuant/${acuantVersion}/tiny_face_detector_model-shard1`
: undefined,
credentials: getMetaContent('acuant-sdk-initialization-creds'),
endpoint: getMetaContent('acuant-sdk-initialization-endpoint'),
glareThreshold,
Expand Down