veryfi-lens-wasm 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
package/veryfi-lens.js CHANGED
@@ -2,11 +2,6 @@ import DeviceUUID from "./wasm/device-uuid.js";
2
2
  import { WasmWrapper } from "./wasm/wasm.js";
3
3
  import gatherBrowserData from "./wasm/device-data.js";
4
4
  import UAParser from "./wasm/ua-parser.cjs";
5
- import cv from "./wasm/opencv.cjs";
6
- // if (typeof document !== 'undefined') {
7
- // const script = document.createElement("script");
8
- // script.src = "./wasm/opencv.js";
9
- // }
10
5
 
11
6
  const VeryfiLens = (function () {
12
7
  const DEFAULT_BOX_COlOR = "rgba(84, 192, 139, 0.6)";
@@ -76,6 +71,8 @@ const VeryfiLens = (function () {
76
71
  let CCPhase = "ScanningFront";
77
72
  let timeStamp = 0;
78
73
  let ShouldProcessCC = true;
74
+ // let cv = null;
75
+ let cvReady = false;
79
76
 
80
77
  let cardData = {
81
78
  status: "",
@@ -84,7 +81,6 @@ const VeryfiLens = (function () {
84
81
  date: "",
85
82
  cvv: "",
86
83
  };
87
-
88
84
  const releaseCanvas = (canvas) => {
89
85
  if (canvas) {
90
86
  canvas.width = 1;
@@ -452,32 +448,6 @@ const VeryfiLens = (function () {
452
448
  }
453
449
  };
454
450
 
455
- // const testCCVideo = async () => {
456
- // const video = videoRef;
457
- // video.src = './cardTest.mp4';
458
- // wasmWrapper.setCardDetectorCallback(logCard);
459
- // };
460
-
461
- const testCCVideo = async () => {
462
- console.log('TEST VIDEO')
463
- // Create a video element
464
- const video = document.createElement('video');
465
- video.src = './cardTest.mp4';
466
- video.autoplay = true;
467
- video.muted = true; // Mute the video to avoid audio playback
468
- video.loop = true; // Loop the video
469
-
470
- // Attach the video element to the document body
471
- document.body.appendChild(video);
472
-
473
- // Set the video element as the source object of the videoRef
474
- const videoRef = document.getElementById('veryfi-video-ref'); // Assuming you have a video element with this ID
475
- videoRef.srcObject = video.captureStream();
476
-
477
- // Mock the behavior of wasmWrapper.setCardDetectorCallback
478
- wasmWrapper.setCardDetectorCallback(logCard);
479
- };
480
-
481
451
  const loadImage = (src) => {
482
452
  return new Promise((resolve, reject) => {
483
453
  const img = new Image();
@@ -714,13 +684,20 @@ const VeryfiLens = (function () {
714
684
  CCPhase = "FlipCard";
715
685
  case "ScanningBack":
716
686
  console.log("ITS CASE 2");
717
- wasmWrapper.resetAutoCapture(false, false, false, true);
687
+ const scanForNumber = !cardData.number;
688
+ const scanForName = !cardData.name;
689
+ const scanForDate = !cardData.date;
690
+ const scanForCvv = !cardData.cvv;
691
+ wasmWrapper.resetAutoCapture(
692
+ scanForNumber,
693
+ scanForName,
694
+ scanForDate,
695
+ scanForCvv
696
+ );
718
697
  if (cardData.status == "AutoCaptureResultDone") {
719
698
  ShouldProcessCC = false;
720
699
  wasmWrapper.resetAutoCapture(true, true, true, true);
721
700
  wasmWrapper.releaseCallback();
722
- // wasmWrapper.release();
723
- // CCPhase = "ScanningFront";
724
701
  }
725
702
  }
726
703
  }
@@ -997,7 +974,7 @@ const VeryfiLens = (function () {
997
974
  };
998
975
 
999
976
  const startWasm = async (client_id) => {
1000
- if(!wasmWrapper.loaded){
977
+ if (!wasmWrapper.loaded) {
1001
978
  await wasmWrapper.initialize(client_id);
1002
979
  }
1003
980
  if (wasmWrapper) {
@@ -1013,8 +990,8 @@ const VeryfiLens = (function () {
1013
990
  };
1014
991
 
1015
992
  const startWasmLong = async (client_id) => {
1016
- if(!wasmWrapper.loaded){
1017
- await wasmWrapper.initialize(client_id);
993
+ if (!wasmWrapper.loaded) {
994
+ await wasmWrapper.initialize(client_id);
1018
995
  }
1019
996
  if (wasmWrapper) {
1020
997
  getVideoWasmLong();
@@ -1045,11 +1022,12 @@ const VeryfiLens = (function () {
1045
1022
  const startUploadWasm = async (client_id) => {
1046
1023
  await wasmWrapper.initialize(client_id);
1047
1024
  if (wasmWrapper) {
1048
- wasmWrapper.setDocumentCallback(logDocument);
1025
+ await wasmWrapper.setDocumentCallback(logDocument);
1049
1026
  }
1050
1027
  };
1028
+
1051
1029
  const startWasmCC = async (client_id) => {
1052
- if (!wasmWrapper.loaded){
1030
+ if (!wasmWrapper.loaded) {
1053
1031
  await wasmWrapper.initialize(client_id);
1054
1032
  }
1055
1033
  if (wasmWrapper.loaded) {
@@ -1067,24 +1045,6 @@ const VeryfiLens = (function () {
1067
1045
  }, INTERVAL);
1068
1046
  }
1069
1047
  };
1070
- const testWasmCC = async (client_id) => {
1071
- await wasmWrapper.initialize(client_id);
1072
- console.log('first')
1073
- if (wasmWrapper) {
1074
- testCCVideo();
1075
- requestAnimationFrame(displayVideo);
1076
- intervalRef = setInterval(() => {
1077
- if (
1078
- cardData.status === "AutoCaptureResultDone" &&
1079
- CCPhase === "ScanningBack"
1080
- ) {
1081
- clearInterval(intervalRef); // Stop sending frames once condition is met
1082
- } else {
1083
- sendCardWasm();
1084
- }
1085
- }, INTERVAL);
1086
- }
1087
- };
1088
1048
 
1089
1049
  const setBlurStatus = (variance) => {
1090
1050
  if (variance >= 9) {
@@ -1102,8 +1062,20 @@ const VeryfiLens = (function () {
1102
1062
  }
1103
1063
  };
1104
1064
 
1065
+ function loadOpenCv(url) {
1066
+ return new Promise((resolve, reject) => {
1067
+ const script = document.createElement("script");
1068
+ script.src = url;
1069
+ script.onload = () => resolve(); // Correctly scoped resolve
1070
+ script.onerror = () => reject(new Error(`Script load error for ${url}`));
1071
+ document.body.appendChild(script);
1072
+ cvReady = true;
1073
+ });
1074
+ }
1075
+
1105
1076
  const isBlurry = async (image) => {
1106
1077
  console.log("[EVENT] Checking for blur");
1078
+
1107
1079
  const src = cv.imread(image);
1108
1080
  let refVariance;
1109
1081
  let whiteCanvas = new cv.Mat(490, 866, cv.CV_8UC3, [255, 255, 255, 0]);
@@ -1251,6 +1223,9 @@ const VeryfiLens = (function () {
1251
1223
  device_uuid = new DeviceUUID(userAgent).get();
1252
1224
  console.log("[EVENT] Device ID", getDeviceID(device_uuid));
1253
1225
  if (session) {
1226
+ if (!cvReady) {
1227
+ loadOpenCv("./wasm/opencv.js");
1228
+ }
1254
1229
  lensSessionKey = session;
1255
1230
  createCanvases();
1256
1231
  videoRef = document.getElementById("veryfi-video-ref");
@@ -1382,17 +1357,6 @@ const VeryfiLens = (function () {
1382
1357
  return;
1383
1358
  }
1384
1359
  },
1385
- testCC: (client_id) => {
1386
- console.log('testing')
1387
- isDocumentProcess = true;
1388
- userAgent = navigator.userAgent;
1389
- device_uuid = new DeviceUUID(userAgent).get();
1390
- ShouldProcessCC = true;
1391
- CCPhase = "ScanningFront";
1392
- cardData = { status: "", number: "", name: "", date: "", cvv: "" };
1393
- testWasmCC(client_id);
1394
- },
1395
-
1396
1360
 
1397
1361
  startCamera: () => {
1398
1362
  console.log("[EVENT] startCamera");
@@ -1438,7 +1402,7 @@ const VeryfiLens = (function () {
1438
1402
  finalImage = await cropWasm();
1439
1403
  setImage && setImage(finalImage);
1440
1404
  stopWasm();
1441
- wasmWrapper.releaseCallback()
1405
+ wasmWrapper.releaseCallback();
1442
1406
  setIsEditing && setIsEditing(true);
1443
1407
  return finalImage;
1444
1408
  },
@@ -1449,16 +1413,17 @@ const VeryfiLens = (function () {
1449
1413
  setImage && setImage(finalImage);
1450
1414
  stopWasm();
1451
1415
  if (hasCoordinates) setIsDocument(true);
1452
- wasmWrapper.releaseCallback()
1416
+ wasmWrapper.releaseCallback();
1453
1417
  setIsEditing && setIsEditing(true);
1454
1418
  return finalImage;
1455
1419
  },
1456
1420
 
1457
1421
  captureUploaded: async (imageData) => {
1458
- return await createImageBitmap(imageData).then((bitmap) => {
1459
- const wasmOutput = wasmWrapper.cropDocument(bitmap);
1422
+ return await createImageBitmap(imageData).then(async (bitmap) => {
1423
+ const wasmOutput = await wasmWrapper.cropDocument(bitmap);
1460
1424
  const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
1461
- // If a document is detected and cropped
1425
+ // console.log(outputWidth, outputHeight, "output")
1426
+
1462
1427
  if (outputWidth > 0 && outputHeight > 0) {
1463
1428
  const width = outputWidth;
1464
1429
  const height = outputHeight;
@@ -1470,17 +1435,15 @@ const VeryfiLens = (function () {
1470
1435
  ctx.putImageData(imageData, 0, 0);
1471
1436
  setBlurStatus(blurLevel);
1472
1437
  wasmWrapper.releaseCallback()
1473
- stopWasm();
1474
-
1475
1438
  const imgString = cropImgCanvas.toDataURL("image/jpeg");
1476
1439
  image = cropImgCanvas;
1477
- releaseCanvas(boxRef);
1478
1440
  setIsDocument(true);
1479
1441
  coordinates = [];
1480
1442
  return imgString.split("data:image/jpeg;base64,")[1];
1481
1443
  } else {
1482
1444
  // Return the original, uncropped image
1483
1445
  return new Promise((resolve, reject) => {
1446
+ setIsDocument(false);
1484
1447
  const reader = new FileReader();
1485
1448
  reader.readAsDataURL(imageData);
1486
1449
  reader.onload = function () {
@@ -1490,6 +1453,7 @@ const VeryfiLens = (function () {
1490
1453
  reader.onerror = function (error) {
1491
1454
  reject(error);
1492
1455
  };
1456
+ // stopWasm()
1493
1457
  });
1494
1458
  }
1495
1459
  });
@@ -1588,6 +1552,7 @@ const VeryfiLens = (function () {
1588
1552
  frameRef && frameRef.remove();
1589
1553
  boxRef && boxRef.remove();
1590
1554
  cropImgRef && cropImgRef.remove();
1555
+ // wasmWrapper && wasmWrapper.release();
1591
1556
  setIsDocument(false);
1592
1557
  },
1593
1558
 
@@ -1608,9 +1573,10 @@ const VeryfiLens = (function () {
1608
1573
 
1609
1574
  getCardPhase: () => {
1610
1575
  return CCPhase;
1576
+ },
1577
+ releaseWasm: () => {
1578
+ wasmWrapper.release();
1611
1579
  }
1612
-
1613
-
1614
1580
  };
1615
1581
  })();
1616
1582