veryfi-lens-wasm 1.1.2 → 1.1.3

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.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "repository": {
package/src/wasm/wasm.js CHANGED
@@ -121,7 +121,6 @@ export class WasmWrapper {
121
121
 
122
122
  cropWasm(bitmap, corners) {
123
123
  if (!this.loaded) return;
124
- console.log('corners', corners)
125
124
  const buffer = this.setBitmapOnWASMMemory_(bitmap);
126
125
  let cornersPtr = this.wasmModule._malloc(4 * 2 * 4);
127
126
  let startIndex = cornersPtr / 4;
@@ -132,7 +131,6 @@ export class WasmWrapper {
132
131
  ["number", "number", "number", "number", "boolean"],
133
132
  [buffer, bitmap.width, bitmap.height, cornersPtr, true]
134
133
  );
135
-
136
134
  this.freeBuffer_(buffer);
137
135
  this.freeBuffer_(cornersPtr);
138
136
  return this.getResultFromBuffer(outputBuffer);
package/veryfi-lens.js CHANGED
@@ -1,9 +1,6 @@
1
1
  import DeviceUUID from "./src/device-uuid.js";
2
2
  import FingerprintID from "./src/fingerprint-id.js";
3
3
  import { WasmWrapper } from "./src/wasm/wasm.js";
4
- let script = document.createElement("script");
5
- script.src = "./wasm/opencv.js";
6
- document.body.appendChild(script);
7
4
 
8
5
  const VeryfiLens = (function () {
9
6
  const DEFAULT_BOX_COlOR = "rgba(84, 192, 139, 0.6)";
@@ -14,6 +11,7 @@ const VeryfiLens = (function () {
14
11
  const MAX_SHAPE = 512.0;
15
12
  const SOCKET_URL = "wss://lens.veryfi.com/ws/crop";
16
13
  const VALIDATE_URL = "https://lens.veryfi.com/rest/validate_partner";
14
+ const PROCESS_URL = "https://lens.veryfi.com/rest/process"
17
15
  const SOCKET_STATUSES = [
18
16
  {
19
17
  value: 0,
@@ -70,31 +68,13 @@ const VeryfiLens = (function () {
70
68
  let shouldUpdatePreview = false;
71
69
 
72
70
  const releaseCanvas = (canvas) => {
71
+ if (canvas) {
73
72
  canvas.width = 1;
74
73
  canvas.height = 1;
75
74
  const ctx = canvas.getContext("2d");
76
- ctx && ctx.clearRect(0, 0, 1, 1);
75
+ ctx && ctx.clearRect(0, 0, 1, 1);}
77
76
  };
78
77
 
79
- function waitForElement(selector) {
80
- return new Promise((resolve) => {
81
- if (document.querySelector(selector)) {
82
- return resolve(document.querySelector(selector));
83
- }
84
-
85
- const observer = new MutationObserver((mutations) => {
86
- if (document.querySelector(selector)) {
87
- resolve(document.querySelector(selector));
88
- observer.disconnect();
89
- }
90
- });
91
-
92
- observer.observe(document.body, {
93
- childList: true,
94
- subtree: true,
95
- });
96
- });
97
- }
98
78
 
99
79
  const setClientId = (key) => {
100
80
  clientId = key;
@@ -316,7 +296,9 @@ const VeryfiLens = (function () {
316
296
  if (isDocumentProcess) {
317
297
  mode = "Document";
318
298
  } else
319
- isStitchingProcess ? (mode = "Stitcher") : (mode = "StitcherProcess");
299
+ if (isStitchingProcess) {
300
+ (mode = "Stitcher")
301
+ } else (mode = "StitcherProcess");
320
302
  if (videoRef) {
321
303
  let rCorners;
322
304
  const video = videoRef;
@@ -367,6 +349,7 @@ const VeryfiLens = (function () {
367
349
  }
368
350
  };
369
351
 
352
+
370
353
  const getLongImage = async () => {
371
354
  let wasmOutput;
372
355
  wasmOutput = await wasmWrapper.getStitchedImage();
@@ -426,10 +409,11 @@ const VeryfiLens = (function () {
426
409
  width,
427
410
  height
428
411
  );
429
-
430
- while (container?.firstChild) {
431
- container.removeChild(container.firstChild);
432
- }
412
+ if (container) {
413
+ while (container.firstChild) {
414
+ container.removeChild(container.firstChild);
415
+ }
416
+ }
433
417
  container.appendChild(canvas);
434
418
  }
435
419
 
@@ -440,7 +424,7 @@ const VeryfiLens = (function () {
440
424
  [x2, y2],
441
425
  [x3, y3],
442
426
  ];
443
- // console.log(coordinates)
427
+ console.log('coordinates',coordinates)
444
428
  }
445
429
  function logLongDocument(
446
430
  status,
@@ -546,6 +530,7 @@ const VeryfiLens = (function () {
546
530
  );
547
531
  } else {
548
532
  // If there are no coordinates, use the fullSizeImage as it is.
533
+ console.log('[Event] Using full size image')
549
534
  const canvas = new OffscreenCanvas(
550
535
  fullSizeImage.width,
551
536
  fullSizeImage.height
@@ -565,6 +550,7 @@ const VeryfiLens = (function () {
565
550
  outputHeight: fullSizeImage.height,
566
551
  };
567
552
  }
553
+
568
554
  console.log(wasmOutput);
569
555
  const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
570
556
  console.log(outputWidth, outputHeight);
@@ -587,75 +573,6 @@ const VeryfiLens = (function () {
587
573
  return imgString.split("data:image/jpeg;base64,")[1];
588
574
  };
589
575
 
590
- // const cropImage = async () => {
591
- // const video = videoRef;
592
- // const cropImgCanvas = cropImgRef;
593
- // if (fullSizeImage) {
594
- // console.log("[Event] Full size image is set");
595
- // if (hasCoordinates) {
596
- // setIsDocument(true);
597
- // let { sx, sy, sw, sh } = getCropLimits(coordinates);
598
- // const scaleWidth = fullSizeImage.width / video.videoWidth;
599
- // const scaleHeight = fullSizeImage.height / video.videoHeight;
600
-
601
- // sx = sx * scaleWidth;
602
- // sy = sy * scaleHeight;
603
- // sw = sw * scaleWidth;
604
- // sh = sh * scaleHeight;
605
-
606
- // cropImgCanvas.width = sw;
607
- // cropImgCanvas.height = sh;
608
- // const ctx = cropImgCanvas.getContext("2d");
609
-
610
- // if (ctx) {
611
- // ctx.save();
612
- // ctx.drawImage(fullSizeImage, sx, sy, sw, sh, 0, 0, sw, sh);
613
- // ctx.restore();
614
- // }
615
- // } else {
616
- // cropImgCanvas.width = video.videoWidth;
617
- // cropImgCanvas.height = video.videoHeight;
618
- // const ctx = cropImgCanvas.getContext("2d");
619
- // if (ctx) {
620
- // ctx.save();
621
- // ctx.drawImage(fullSizeImage, 0, 0);
622
- // ctx.restore();
623
- // }
624
- // }
625
- // } else {
626
- // if (hasCoordinates) {
627
- // setIsDocument(true);
628
- // let { sx, sy, sw, sh } = getCropLimits(coordinates);
629
- // cropImgCanvas.width = sw;
630
- // cropImgCanvas.height = sh;
631
- // const ctx = cropImgCanvas.getContext("2d");
632
- // if (ctx) {
633
- // ctx.save();
634
- // ctx.drawImage(video, sx, sy, sw, sh, 0, 0, sw, sh);
635
- // ctx.restore();
636
- // }
637
- // } else {
638
- // cropImgCanvas.width = video.videoWidth;
639
- // cropImgCanvas.height = video.videoHeight;
640
- // const ctx = cropImgCanvas.getContext("2d");
641
- // if (ctx) {
642
- // ctx.save();
643
- // ctx.drawImage(video, 0, 0);
644
- // ctx.restore();
645
- // }
646
- // }
647
- // }
648
-
649
- // waitForElement("#blur-detector").then(() => {
650
- // isBlurry(cropImgCanvas);
651
- // });
652
-
653
- // image = cropImgCanvas;
654
- // const imgString = cropImgCanvas.toDataURL("image/jpeg");
655
-
656
- // return imgString.split("data:image/jpeg;base64,")[1];
657
- // };
658
-
659
576
  const getCropLimits = (coordinates) => {
660
577
  const sx = Math.min(
661
578
  coordinates[0][0],
@@ -719,7 +636,7 @@ const VeryfiLens = (function () {
719
636
  clearInterval(intervalRef);
720
637
  };
721
638
  const stopWasm = () => {
722
- videoRef.srcObject.getTracks().forEach((track) => track.stop());
639
+ videoRef && videoRef.srcObject.getTracks().forEach((track) => track.stop());
723
640
  clearInterval(intervalRef);
724
641
  };
725
642
 
@@ -777,6 +694,14 @@ const VeryfiLens = (function () {
777
694
  };
778
695
  };
779
696
 
697
+ const startUploadWasm = async () => {
698
+ wasmWrapper = new WasmWrapper();
699
+ await wasmWrapper.initialize();
700
+ if (wasmWrapper) {
701
+ wasmWrapper.setDocumentCallback(logDocument);
702
+ }
703
+ }
704
+
780
705
  const setBlurStatus = (variance) => {
781
706
  if (variance >= 10) {
782
707
  blurStatus = false;
@@ -793,59 +718,6 @@ const VeryfiLens = (function () {
793
718
  }
794
719
  };
795
720
 
796
- // const isBlurry = async (image) => {
797
- // console.log("[EVENT] Checking for blur");
798
- // const src = cv.imread(image);
799
- // let refVariance;
800
- // let whiteCanvas = new cv.Mat(490, 866, cv.CV_8UC3, [255, 255, 255, 0]);
801
-
802
- // const grayscale = new cv.Mat();
803
- // const refGrayscale = new cv.Mat();
804
-
805
- // cv.cvtColor(src, grayscale, cv.COLOR_RGBA2GRAY);
806
- // cv.cvtColor(whiteCanvas, refGrayscale, cv.COLOR_RGBA2GRAY);
807
-
808
- // const laplacian = new cv.Mat();
809
- // const refLaplacian = new cv.Mat();
810
-
811
- // cv.Laplacian(grayscale, laplacian, cv.CV_8U);
812
- // cv.Laplacian(refGrayscale, refLaplacian, cv.CV_8U);
813
-
814
- // const meanStdDev = new cv.Mat();
815
- // const laplacianMean = new cv.Mat();
816
- // const refMeanStdDev = new cv.Mat();
817
- // const refLaplacianMean = new cv.Mat();
818
-
819
- // cv.meanStdDev(laplacian, laplacianMean, meanStdDev);
820
- // cv.meanStdDev(refLaplacian, refLaplacianMean, refMeanStdDev);
821
-
822
- // variance = meanStdDev.data64F[0] * 10;
823
- // refVariance = refMeanStdDev.data64F[0] * 10;
824
-
825
- // console.log("variance", variance);
826
- // console.log("reference variance", refVariance);
827
-
828
- // grayscale.delete();
829
- // laplacian.delete();
830
- // meanStdDev.delete();
831
- // laplacianMean.delete();
832
- // refGrayscale.delete();
833
- // refLaplacian.delete();
834
- // refMeanStdDev.delete();
835
- // refLaplacianMean.delete();
836
- // whiteCanvas.delete();
837
-
838
- // if (variance > 88) {
839
- // blurStatus = false;
840
- // setBlurStatus(blurStatus, variance);
841
- // return;
842
- // } else {
843
- // blurStatus = true;
844
- // setBlurStatus(blurStatus, variance);
845
- // return;
846
- // }
847
- // };
848
-
849
721
  return {
850
722
  init: async (session) => {
851
723
  const fp = await FingerprintID.load();
@@ -986,6 +858,44 @@ const VeryfiLens = (function () {
986
858
  cropImgRef = document.getElementById("veryfi-crop-img-ref");
987
859
  startWasmLong();
988
860
  },
861
+ initUploadWasm: async (session, imageData) => {
862
+ const fp = await FingerprintID.load();
863
+ device_fingerprint = (await fp.get()).visitorId;
864
+ userAgent = navigator.userAgent;
865
+ device_uuid = new DeviceUUID(userAgent).get();
866
+ console.log(
867
+ "[EVENT] Device ID",
868
+ getDeviceID(device_uuid, device_fingerprint)
869
+ );
870
+ await startUploadWasm();
871
+ },
872
+ // finalImage = await cropWasm();
873
+ // setImage && setImage(finalImage);
874
+ // stopWasm();
875
+ // setIsEditing && setIsEditing(true);
876
+ // return finalImage;
877
+
878
+ processDocument: async (
879
+ image,
880
+ username,
881
+ clientId,
882
+ apiKey,
883
+ ) => {
884
+ console.log('[EVENT] Processing document upload')
885
+ const processDocumentUrl = PROCESS_URL;
886
+ const requestOptions = {
887
+ method: 'POST',
888
+ headers: { 'Content-Type': 'application/json' },
889
+ body: JSON.stringify({
890
+ image: image,
891
+ username,
892
+ api_key: apiKey,
893
+ client_id: clientId,
894
+ }),
895
+ };
896
+ return fetch(processDocumentUrl, requestOptions).then((response) => response.json());
897
+ },
898
+
989
899
  startCamera: () => {
990
900
  console.log("[EVENT] startCamera");
991
901
  startLens();
@@ -1034,6 +944,48 @@ const VeryfiLens = (function () {
1034
944
  setIsEditing && setIsEditing(true);
1035
945
  return finalImage;
1036
946
  },
947
+
948
+ captureUploaded: async (imageData) => {
949
+ return await createImageBitmap(imageData).then((bitmap) => {
950
+ const wasmOutput = wasmWrapper.cropDocument(bitmap);
951
+ const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
952
+
953
+ // If a document is detected and cropped
954
+ if (outputWidth > 0 && outputHeight > 0) {
955
+ const width = outputWidth;
956
+ const height = outputHeight;
957
+ const cropImgCanvas = document.createElement("canvas");
958
+ cropImgCanvas.height = height;
959
+ cropImgCanvas.width = width;
960
+ const ctx = cropImgCanvas.getContext("2d");
961
+ const imageData = new ImageData(data, width, height);
962
+ ctx.putImageData(imageData, 0, 0);
963
+ setBlurStatus(blurLevel);
964
+ stopWasm();
965
+
966
+ const imgString = cropImgCanvas.toDataURL("image/jpeg");
967
+ image = cropImgCanvas;
968
+ releaseCanvas(boxRef);
969
+ setIsDocument(true);
970
+ coordinates = [];
971
+ return imgString.split("data:image/jpeg;base64,")[1];
972
+ } else {
973
+ // Return the original, uncropped image
974
+ return new Promise((resolve, reject) => {
975
+ const reader = new FileReader();
976
+ reader.readAsDataURL(imageData);
977
+ reader.onload = function() {
978
+ const base64Image = reader.result.split(',')[1];
979
+ resolve(base64Image);
980
+ };
981
+ reader.onerror = function(error) {
982
+ reject(error);
983
+ };
984
+ });
985
+ }
986
+ });
987
+ },
988
+
1037
989
  startStitching: async () => {
1038
990
  isStitchingProcess = true;
1039
991
  },
@@ -1072,7 +1024,7 @@ const VeryfiLens = (function () {
1072
1024
  lensSessionKey = key;
1073
1025
  },
1074
1026
  getSocketStatus: () => {
1075
- const value = ws?.readyState || 4;
1027
+ const value = ws.readyState || 4;
1076
1028
  return SOCKET_STATUSES[value];
1077
1029
  },
1078
1030
  getSocketStatusColor: () => {
@@ -1083,7 +1035,8 @@ const VeryfiLens = (function () {
1083
1035
  "red",
1084
1036
  "purple",
1085
1037
  ];
1086
- const value = ws?.readyState || 4;
1038
+
1039
+ const value = ws.readyState || 4;
1087
1040
  return socketStatusesColors[value];
1088
1041
  },
1089
1042
  getBlurStatus: (setIsBlurry) => {
@@ -1100,17 +1053,27 @@ const VeryfiLens = (function () {
1100
1053
  const frame = frameRef;
1101
1054
  const crop = cropImgRef;
1102
1055
  crop && releaseCanvas(crop);
1103
- video?.pause();
1104
- video?.removeAttribute("src");
1105
- video?.load();
1056
+ if (video) {
1057
+ video.pause();
1058
+ video.removeAttribute("src");
1059
+ video.load();
1060
+ }
1106
1061
  box && releaseCanvas(box);
1107
1062
  frame && releaseCanvas(frame);
1108
- videoRef?.remove();
1109
- frameRef?.remove();
1110
- boxRef?.remove();
1111
- cropImgRef?.remove();
1063
+ videoRef && videoRef.remove();
1064
+ frameRef && frameRef.remove();
1065
+ boxRef && boxRef.remove();
1066
+ cropImgRef && cropImgRef.remove();
1112
1067
  setIsDocument(false);
1113
1068
  },
1069
+ getDeviceData: () => {
1070
+ return {
1071
+ uuid: device_uuid,
1072
+ // fp: device_fingerprint,
1073
+ // source: "lens.web",
1074
+ // data: navigator
1075
+ }
1076
+ },
1114
1077
  };
1115
1078
  })();
1116
1079