Vue
Follow these steps to get started with Vue.
-
Set up the workspace by running the following command:
npm create vue@latest livenesstestA folder/directory named
livenesstestshould appear and the folder/ directory structure should look like this:livenesstest/ | +-- node_modules/ | +-- public/ | | | \-- vite.svg | +-- src/ | | | +-- assets\ | | | +-- components\ | | | +-- App.vue | | | +-- main.js | | | \-- style.css | +-- .gitignore | +-- index.html | +-- vite.config.js | +-- package-lock.json | +-- package.json | \-- README.md -
Put the
livenessfolder/directory that contains the liveness engine and builders likebuilder.js, into thepublicfolder/directory. -
Add the liveness package to
package.jsonby running this command in thelivenesstestVue project.npm i file:public/livenessThis will add
@verihubs/livenessdependency inpackage.json. -
Add an import to
main.jsfile insidesrcfolder.import Builder from '@verihubs/liveness'; -
Add the following code to the
main.jsfile insidesrcfolder.window.LivenessSDK = new Builder() .setCredential({ clientId: 'YOUR_CLIENT_ID', secret: 'YOUR_SECRET', }) .setInstruction(['look_left', 'look_right'], { commands: ['open_mouth'], seedLimit: 1, }) .setProxyMiddleware({ PassiveLiveness: { url: 'http://localhost:8888/liveness/face', headers: { 'App-ID': '<replace-me-with-app-id>', 'API-Key': '<replace-me-with-api-key>', }, }, License: { url: 'http://localhost:8888/license/{license_id}/check', headers: { 'App-ID': '<replace-me-with-app-id>', 'API-Key': '<replace-me-with-api-key>', }, }, }) .setTimeout(60000) .setURL('./liveness') .setVirtualCameraLabel(['OBS', 'Virtual']) .build();Replace proxy middleware URLs with a URL that points to the correct endoints, and replace
<replace-me-with-app-id>and<replace-me-with-api-key>with the appropriate APP-ID and APIKey for the passive liveness (remove the headers if the api doesn't need credentials or the url is pointing towards an intermediate endpoint).Look Out!To not accidentally leak of
AppIDandAPIKeyfor passive liveness, always pass them to an intermediate endpoint (like a proxy middleware) and append theAppIDandAPIKeyheader before reaching Verihubs' passive liveness service in production. Additional data can also be passed to this intermediate endpoint for end user identification.For more information about creating a Proxy Middleware, refer to Proxy Middleware.
For API documentation of the Builder, refer to
Builder. -
Populate the component logic in
<script setup>by the code below:<script setup> import { ref, onMounted, onBeforeUnmount } from 'vue'; const image = ref(''); const data = ref(''); const livenessMessageListener = ({ data: { data: msgData, subject } }) => { switch (subject) { case 'Verification.Verbose': console.log('[Verbose]', msgData); break; case 'Camera.NotAllowed': case 'Camera.NotFound': case 'Camera.PermissionDenied': case 'ScreenOrientation.NotAllowed': case 'Verification.Disrupted': case 'Verification.Timeout': console.log({ data: msgData, subject }); alert(subject); window.LivenessSDK.onDestroy(); break; case 'Verification.Success': setTimeout(() => { image.value = `data:image/png;base64,${msgData.image.url}`; data.value = JSON.stringify(msgData, null, 2); window.LivenessSDK.onDestroy(); }, 1500); break; default: console.log({ data: msgData, subject }); alert(subject); break; } }; const doLivenessVerification = () => { window.LivenessSDK.onStart(); }; onMounted(() => { window.addEventListener('message', livenessMessageListener); }); onBeforeUnmount(() => { window.removeEventListener('message', livenessMessageListener); }); </script>
InfoCheck System Messages for full system message references.
- Replace the template component inside
App.vuewith the code below:
<template>
<div>
<img v-if="image" :src="image" alt="Liveness Verification Result" />
<pre>{{ data }}</pre>
<button @click="doLivenessVerification">Start Liveness Verification</button>
</div>
</template>-
Add the dependency to
optimizeDeps.includeandbuild.commonjsOptions.includein Vite.config.js with code below:export default defineConfig({ plugins: [vue()], optimizeDeps: { include: ["@verihubs/liveness"], }, build: { commonjsOptions: { include: [/liveness/, /node_modules/], }, }, });
To try the above implementation you can run the following command from the liveness project:
npm run devUpdated about 2 months ago
