veryfi-lens-wasm 1.1.2 → 1.1.4

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.4",
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;
@@ -256,7 +236,6 @@ const VeryfiLens = (function () {
256
236
  const camHeight = 1920;
257
237
  const isDesktop = window.screen.width > window.screen.height;
258
238
  if (navigator) {
259
- console.log("User agent is present");
260
239
  navigator.mediaDevices
261
240
  .getUserMedia({
262
241
  video: {
@@ -282,7 +261,6 @@ const VeryfiLens = (function () {
282
261
  const camHeight = 4096;
283
262
  const isDesktop = window.screen.width > window.screen.height;
284
263
  if (navigator) {
285
- console.log("User agent is present");
286
264
  navigator.mediaDevices
287
265
  .getUserMedia({
288
266
  video: {
@@ -316,7 +294,9 @@ const VeryfiLens = (function () {
316
294
  if (isDocumentProcess) {
317
295
  mode = "Document";
318
296
  } else
319
- isStitchingProcess ? (mode = "Stitcher") : (mode = "StitcherProcess");
297
+ if (isStitchingProcess) {
298
+ (mode = "Stitcher")
299
+ } else (mode = "StitcherProcess");
320
300
  if (videoRef) {
321
301
  let rCorners;
322
302
  const video = videoRef;
@@ -367,6 +347,7 @@ const VeryfiLens = (function () {
367
347
  }
368
348
  };
369
349
 
350
+
370
351
  const getLongImage = async () => {
371
352
  let wasmOutput;
372
353
  wasmOutput = await wasmWrapper.getStitchedImage();
@@ -426,10 +407,11 @@ const VeryfiLens = (function () {
426
407
  width,
427
408
  height
428
409
  );
429
-
430
- while (container?.firstChild) {
431
- container.removeChild(container.firstChild);
432
- }
410
+ if (container) {
411
+ while (container.firstChild) {
412
+ container.removeChild(container.firstChild);
413
+ }
414
+ }
433
415
  container.appendChild(canvas);
434
416
  }
435
417
 
@@ -440,7 +422,7 @@ const VeryfiLens = (function () {
440
422
  [x2, y2],
441
423
  [x3, y3],
442
424
  ];
443
- // console.log(coordinates)
425
+ // console.log('coordinates',coordinates)
444
426
  }
445
427
  function logLongDocument(
446
428
  status,
@@ -514,9 +496,7 @@ const VeryfiLens = (function () {
514
496
  outputHeight: fullSizeImage.height,
515
497
  };
516
498
  }
517
- console.log(wasmOutput);
518
499
  const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
519
- console.log(outputWidth, outputHeight);
520
500
  const width = outputWidth;
521
501
  const height = outputHeight;
522
502
  const cropImgCanvas = cropImgRef;
@@ -546,6 +526,7 @@ const VeryfiLens = (function () {
546
526
  );
547
527
  } else {
548
528
  // If there are no coordinates, use the fullSizeImage as it is.
529
+ console.log('[Event] Using full size image')
549
530
  const canvas = new OffscreenCanvas(
550
531
  fullSizeImage.width,
551
532
  fullSizeImage.height
@@ -565,9 +546,8 @@ const VeryfiLens = (function () {
565
546
  outputHeight: fullSizeImage.height,
566
547
  };
567
548
  }
568
- console.log(wasmOutput);
549
+
569
550
  const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
570
- console.log(outputWidth, outputHeight);
571
551
  const width = outputWidth;
572
552
  const height = outputHeight;
573
553
  const cropImgCanvas = cropImgRef;
@@ -587,75 +567,6 @@ const VeryfiLens = (function () {
587
567
  return imgString.split("data:image/jpeg;base64,")[1];
588
568
  };
589
569
 
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
570
  const getCropLimits = (coordinates) => {
660
571
  const sx = Math.min(
661
572
  coordinates[0][0],
@@ -719,7 +630,7 @@ const VeryfiLens = (function () {
719
630
  clearInterval(intervalRef);
720
631
  };
721
632
  const stopWasm = () => {
722
- videoRef.srcObject.getTracks().forEach((track) => track.stop());
633
+ videoRef && videoRef.srcObject.getTracks().forEach((track) => track.stop());
723
634
  clearInterval(intervalRef);
724
635
  };
725
636
 
@@ -777,6 +688,14 @@ const VeryfiLens = (function () {
777
688
  };
778
689
  };
779
690
 
691
+ const startUploadWasm = async () => {
692
+ wasmWrapper = new WasmWrapper();
693
+ await wasmWrapper.initialize();
694
+ if (wasmWrapper) {
695
+ wasmWrapper.setDocumentCallback(logDocument);
696
+ }
697
+ }
698
+
780
699
  const setBlurStatus = (variance) => {
781
700
  if (variance >= 10) {
782
701
  blurStatus = false;
@@ -793,59 +712,6 @@ const VeryfiLens = (function () {
793
712
  }
794
713
  };
795
714
 
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
715
  return {
850
716
  init: async (session) => {
851
717
  const fp = await FingerprintID.load();
@@ -986,6 +852,39 @@ const VeryfiLens = (function () {
986
852
  cropImgRef = document.getElementById("veryfi-crop-img-ref");
987
853
  startWasmLong();
988
854
  },
855
+ initUploadWasm: async (session, imageData) => {
856
+ const fp = await FingerprintID.load();
857
+ device_fingerprint = (await fp.get()).visitorId;
858
+ userAgent = navigator.userAgent;
859
+ device_uuid = new DeviceUUID(userAgent).get();
860
+ console.log(
861
+ "[EVENT] Device ID",
862
+ getDeviceID(device_uuid, device_fingerprint)
863
+ );
864
+ await startUploadWasm();
865
+ },
866
+
867
+ processDocument: async (
868
+ image,
869
+ username,
870
+ clientId,
871
+ apiKey,
872
+ ) => {
873
+ console.log('[EVENT] Processing document upload')
874
+ const processDocumentUrl = PROCESS_URL;
875
+ const requestOptions = {
876
+ method: 'POST',
877
+ headers: { 'Content-Type': 'application/json' },
878
+ body: JSON.stringify({
879
+ image: image,
880
+ username,
881
+ api_key: apiKey,
882
+ client_id: clientId,
883
+ }),
884
+ };
885
+ return fetch(processDocumentUrl, requestOptions).then((response) => response.json());
886
+ },
887
+
989
888
  startCamera: () => {
990
889
  console.log("[EVENT] startCamera");
991
890
  startLens();
@@ -1034,6 +933,47 @@ const VeryfiLens = (function () {
1034
933
  setIsEditing && setIsEditing(true);
1035
934
  return finalImage;
1036
935
  },
936
+
937
+ captureUploaded: async (imageData) => {
938
+ return await createImageBitmap(imageData).then((bitmap) => {
939
+ const wasmOutput = wasmWrapper.cropDocument(bitmap);
940
+ const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
941
+ // If a document is detected and cropped
942
+ if (outputWidth > 0 && outputHeight > 0) {
943
+ const width = outputWidth;
944
+ const height = outputHeight;
945
+ const cropImgCanvas = document.createElement("canvas");
946
+ cropImgCanvas.height = height;
947
+ cropImgCanvas.width = width;
948
+ const ctx = cropImgCanvas.getContext("2d");
949
+ const imageData = new ImageData(data, width, height);
950
+ ctx.putImageData(imageData, 0, 0);
951
+ setBlurStatus(blurLevel);
952
+ stopWasm();
953
+
954
+ const imgString = cropImgCanvas.toDataURL("image/jpeg");
955
+ image = cropImgCanvas;
956
+ releaseCanvas(boxRef);
957
+ setIsDocument(true);
958
+ coordinates = [];
959
+ return imgString.split("data:image/jpeg;base64,")[1];
960
+ } else {
961
+ // Return the original, uncropped image
962
+ return new Promise((resolve, reject) => {
963
+ const reader = new FileReader();
964
+ reader.readAsDataURL(imageData);
965
+ reader.onload = function() {
966
+ const base64Image = reader.result.split(',')[1];
967
+ resolve(base64Image);
968
+ };
969
+ reader.onerror = function(error) {
970
+ reject(error);
971
+ };
972
+ });
973
+ }
974
+ });
975
+ },
976
+
1037
977
  startStitching: async () => {
1038
978
  isStitchingProcess = true;
1039
979
  },
@@ -1072,7 +1012,7 @@ const VeryfiLens = (function () {
1072
1012
  lensSessionKey = key;
1073
1013
  },
1074
1014
  getSocketStatus: () => {
1075
- const value = ws?.readyState || 4;
1015
+ const value = ws.readyState || 4;
1076
1016
  return SOCKET_STATUSES[value];
1077
1017
  },
1078
1018
  getSocketStatusColor: () => {
@@ -1083,7 +1023,8 @@ const VeryfiLens = (function () {
1083
1023
  "red",
1084
1024
  "purple",
1085
1025
  ];
1086
- const value = ws?.readyState || 4;
1026
+
1027
+ const value = ws.readyState || 4;
1087
1028
  return socketStatusesColors[value];
1088
1029
  },
1089
1030
  getBlurStatus: (setIsBlurry) => {
@@ -1100,17 +1041,31 @@ const VeryfiLens = (function () {
1100
1041
  const frame = frameRef;
1101
1042
  const crop = cropImgRef;
1102
1043
  crop && releaseCanvas(crop);
1103
- video?.pause();
1104
- video?.removeAttribute("src");
1105
- video?.load();
1044
+ if (video) {
1045
+ video.pause();
1046
+ video.removeAttribute("src");
1047
+ video.load();
1048
+ }
1106
1049
  box && releaseCanvas(box);
1107
1050
  frame && releaseCanvas(frame);
1108
- videoRef?.remove();
1109
- frameRef?.remove();
1110
- boxRef?.remove();
1111
- cropImgRef?.remove();
1051
+ videoRef && videoRef.remove();
1052
+ frameRef && frameRef.remove();
1053
+ boxRef && boxRef.remove();
1054
+ cropImgRef && cropImgRef.remove();
1112
1055
  setIsDocument(false);
1113
1056
  },
1057
+ getDeviceData: () => {
1058
+ if (!device_uuid) {
1059
+ userAgent = navigator.userAgent;
1060
+ device_uuid = new DeviceUUID(userAgent).get();
1061
+ }
1062
+ return {
1063
+ uuid: device_uuid,
1064
+ // fp: device_fingerprint,
1065
+ // source: "lens.web",
1066
+ // data: navigator
1067
+ }
1068
+ },
1114
1069
  };
1115
1070
  })();
1116
1071