veryfi-lens-wasm 1.1.1 → 1.1.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/flow.png ADDED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "repository": {
package/src/wasm/wasm.js CHANGED
@@ -101,7 +101,7 @@ export class WasmWrapper {
101
101
  let outputBuffer = this.wasmModule.ccall("getStitchedImage", "int");
102
102
  let { data, blurLevel, outputHeight, outputWidth } =
103
103
  this.getResultFromBuffer(outputBuffer);
104
- return { data, outputHeight, outputWidth };
104
+ return { data, blurLevel, outputHeight, outputWidth };
105
105
  }
106
106
 
107
107
  processDocument(bitmap, mode = "Document") {
@@ -121,6 +121,7 @@ export class WasmWrapper {
121
121
 
122
122
  cropWasm(bitmap, corners) {
123
123
  if (!this.loaded) return;
124
+ console.log('corners', corners)
124
125
  const buffer = this.setBitmapOnWASMMemory_(bitmap);
125
126
  let cornersPtr = this.wasmModule._malloc(4 * 2 * 4);
126
127
  let startIndex = cornersPtr / 4;
package/veryfi-lens.js CHANGED
@@ -145,6 +145,7 @@ const VeryfiLens = (function () {
145
145
  corner.map((cord) => cord / scale)
146
146
  );
147
147
  drawContours(rCorners);
148
+ coordinates = rCorners;
148
149
  break;
149
150
  default:
150
151
  isSocketBusy = false;
@@ -366,57 +367,11 @@ const VeryfiLens = (function () {
366
367
  }
367
368
  };
368
369
 
369
- const cropWasm = async () => {
370
- let wasmOutput;
371
- if (hasCoordinates) {
372
- wasmOutput = await wasmWrapper.cropDocument(fullSizeImage);
373
- } else {
374
- // If there are no coordinates, use the fullSizeImage as it is.
375
- const canvas = new OffscreenCanvas(
376
- fullSizeImage.width,
377
- fullSizeImage.height
378
- );
379
- const ctx = canvas.getContext("2d");
380
- ctx.drawImage(fullSizeImage, 0, 0);
381
- const imageData = ctx.getImageData(
382
- 0,
383
- 0,
384
- fullSizeImage.width,
385
- fullSizeImage.height
386
- );
387
- wasmOutput = {
388
- data: new Uint8ClampedArray(imageData.data.buffer),
389
- blurLevel: -1.0,
390
- outputWidth: fullSizeImage.width,
391
- outputHeight: fullSizeImage.height,
392
- };
393
- }
394
- console.log(wasmOutput);
395
- const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
396
- console.log(outputWidth, outputHeight);
397
- const width = outputWidth;
398
- const height = outputHeight;
399
- const cropImgCanvas = cropImgRef;
400
- cropImgRef.height = height;
401
- cropImgRef.width = width;
402
- const ctx = cropImgCanvas.getContext("2d");
403
- const imageData = new ImageData(data, width, height);
404
- ctx.putImageData(imageData, 0, 0);
405
-
406
- stopWasm();
407
-
408
- const imgString = cropImgCanvas.toDataURL("image/jpeg");
409
- image = cropImgCanvas;
410
- console.log(blurLevel);
411
- isBlurry(image);
412
- releaseCanvas(boxRef);
413
- coordinates = [];
414
- return imgString.split("data:image/jpeg;base64,")[1];
415
- };
416
370
  const getLongImage = async () => {
417
371
  let wasmOutput;
418
372
  wasmOutput = await wasmWrapper.getStitchedImage();
419
- const { data, outputHeight, outputWidth } = wasmOutput;
373
+ const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
374
+ // console.log(blurLevel)
420
375
  const width = outputWidth;
421
376
  const height = outputHeight;
422
377
  const cropImgCanvas = cropImgRef;
@@ -425,6 +380,7 @@ const VeryfiLens = (function () {
425
380
  const ctx = cropImgCanvas.getContext("2d");
426
381
  const imageData = new ImageData(data, width, height);
427
382
  ctx.putImageData(imageData, 0, 0);
383
+ // setBlurStatus(blurLevel); gives 0 all the time
428
384
  stopWasm();
429
385
  const imgString = cropImgCanvas.toDataURL("image/jpeg");
430
386
  image = cropImgCanvas;
@@ -533,75 +489,173 @@ const VeryfiLens = (function () {
533
489
  }
534
490
  };
535
491
 
536
- const cropImage = async () => {
537
- const video = videoRef;
492
+ const cropWasm = async () => {
493
+ let wasmOutput;
494
+ if (hasCoordinates) {
495
+ wasmOutput = await wasmWrapper.cropDocument(fullSizeImage);
496
+ } else {
497
+ // If there are no coordinates, use the fullSizeImage as it is.
498
+ const canvas = new OffscreenCanvas(
499
+ fullSizeImage.width,
500
+ fullSizeImage.height
501
+ );
502
+ const ctx = canvas.getContext("2d");
503
+ ctx.drawImage(fullSizeImage, 0, 0);
504
+ const imageData = ctx.getImageData(
505
+ 0,
506
+ 0,
507
+ fullSizeImage.width,
508
+ fullSizeImage.height
509
+ );
510
+ wasmOutput = {
511
+ data: new Uint8ClampedArray(imageData.data.buffer),
512
+ blurLevel: -1.0,
513
+ outputWidth: fullSizeImage.width,
514
+ outputHeight: fullSizeImage.height,
515
+ };
516
+ }
517
+ console.log(wasmOutput);
518
+ const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
519
+ console.log(outputWidth, outputHeight);
520
+ const width = outputWidth;
521
+ const height = outputHeight;
538
522
  const cropImgCanvas = cropImgRef;
539
- if (fullSizeImage) {
540
- console.log("[Event] Full size image is set");
541
- if (hasCoordinates) {
542
- setIsDocument(true);
543
- let { sx, sy, sw, sh } = getCropLimits(coordinates);
544
- const scaleWidth = fullSizeImage.width / video.videoWidth;
545
- const scaleHeight = fullSizeImage.height / video.videoHeight;
546
-
547
- sx = sx * scaleWidth;
548
- sy = sy * scaleHeight;
549
- sw = sw * scaleWidth;
550
- sh = sh * scaleHeight;
551
-
552
- cropImgCanvas.width = sw;
553
- cropImgCanvas.height = sh;
554
- const ctx = cropImgCanvas.getContext("2d");
555
-
556
- if (ctx) {
557
- ctx.save();
558
- ctx.drawImage(fullSizeImage, sx, sy, sw, sh, 0, 0, sw, sh);
559
- ctx.restore();
560
- }
561
- } else {
562
- cropImgCanvas.width = video.videoWidth;
563
- cropImgCanvas.height = video.videoHeight;
564
- const ctx = cropImgCanvas.getContext("2d");
565
- if (ctx) {
566
- ctx.save();
567
- ctx.drawImage(fullSizeImage, 0, 0);
568
- ctx.restore();
569
- }
570
- }
523
+ cropImgRef.height = height;
524
+ cropImgRef.width = width;
525
+ const ctx = cropImgCanvas.getContext("2d");
526
+ const imageData = new ImageData(data, width, height);
527
+ ctx.putImageData(imageData, 0, 0);
528
+ setBlurStatus(blurLevel);
529
+ stopWasm();
530
+
531
+ const imgString = cropImgCanvas.toDataURL("image/jpeg");
532
+ image = cropImgCanvas;
533
+ releaseCanvas(boxRef);
534
+ coordinates = [];
535
+ return imgString.split("data:image/jpeg;base64,")[1];
536
+ };
537
+
538
+ const socketCropWasm = async () => {
539
+ let wasmOutput;
540
+ if (hasCoordinates) {
541
+ let [topLeft, topRight, bottomLeft, bottomRight] =
542
+ getCropLimits(coordinates);
543
+ wasmOutput = await wasmWrapper.cropWasm(
544
+ fullSizeImage,
545
+ [topLeft, topRight, bottomLeft, bottomRight].flat()
546
+ );
571
547
  } else {
572
- if (hasCoordinates) {
573
- setIsDocument(true);
574
- let { sx, sy, sw, sh } = getCropLimits(coordinates);
575
- cropImgCanvas.width = sw;
576
- cropImgCanvas.height = sh;
577
- const ctx = cropImgCanvas.getContext("2d");
578
- if (ctx) {
579
- ctx.save();
580
- ctx.drawImage(video, sx, sy, sw, sh, 0, 0, sw, sh);
581
- ctx.restore();
582
- }
583
- } else {
584
- cropImgCanvas.width = video.videoWidth;
585
- cropImgCanvas.height = video.videoHeight;
586
- const ctx = cropImgCanvas.getContext("2d");
587
- if (ctx) {
588
- ctx.save();
589
- ctx.drawImage(video, 0, 0);
590
- ctx.restore();
591
- }
592
- }
548
+ // If there are no coordinates, use the fullSizeImage as it is.
549
+ const canvas = new OffscreenCanvas(
550
+ fullSizeImage.width,
551
+ fullSizeImage.height
552
+ );
553
+ const ctx = canvas.getContext("2d");
554
+ ctx.drawImage(fullSizeImage, 0, 0);
555
+ const imageData = ctx.getImageData(
556
+ 0,
557
+ 0,
558
+ fullSizeImage.width,
559
+ fullSizeImage.height
560
+ );
561
+ wasmOutput = {
562
+ data: new Uint8ClampedArray(imageData.data.buffer),
563
+ blurLevel: -1.0,
564
+ outputWidth: fullSizeImage.width,
565
+ outputHeight: fullSizeImage.height,
566
+ };
593
567
  }
568
+ console.log(wasmOutput);
569
+ const { data, blurLevel, outputHeight, outputWidth } = wasmOutput;
570
+ console.log(outputWidth, outputHeight);
571
+ const width = outputWidth;
572
+ const height = outputHeight;
573
+ const cropImgCanvas = cropImgRef;
574
+ cropImgRef.height = height;
575
+ cropImgRef.width = width;
576
+ const ctx = cropImgCanvas.getContext("2d");
577
+ const imageData = new ImageData(data, width, height);
578
+ ctx.putImageData(imageData, 0, 0);
579
+ setBlurStatus(blurLevel);
594
580
 
595
- waitForElement("#blur-detector").then(() => {
596
- isBlurry(cropImgCanvas);
597
- });
581
+ stopWasm();
598
582
 
599
- image = cropImgCanvas;
600
583
  const imgString = cropImgCanvas.toDataURL("image/jpeg");
601
-
584
+ image = cropImgCanvas;
585
+ releaseCanvas(boxRef);
586
+ coordinates = [];
602
587
  return imgString.split("data:image/jpeg;base64,")[1];
603
588
  };
604
589
 
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
+
605
659
  const getCropLimits = (coordinates) => {
606
660
  const sx = Math.min(
607
661
  coordinates[0][0],
@@ -629,7 +683,13 @@ const VeryfiLens = (function () {
629
683
  coordinates[2][1],
630
684
  coordinates[3][1]
631
685
  ) - sy;
632
- return { sx, sy, sw, sh };
686
+
687
+ const topLeft = [sx, sy];
688
+ const topRight = [sx + sw, sy];
689
+ const bottomLeft = [sx, sy + sh];
690
+ const bottomRight = [sx + sw, sy + sh];
691
+ // return { sx, sy, sw, sh }; uncomment to use non wasm crop function (cropImage())
692
+ return [topLeft, topRight, bottomLeft, bottomRight];
633
693
  };
634
694
 
635
695
  const fetchSessionId = async (clientId) => {
@@ -716,66 +776,76 @@ const VeryfiLens = (function () {
716
776
  clearInterval(intervalRef);
717
777
  };
718
778
  };
719
- const setBlurStatus = (blur, variance) => {
720
- return {
721
- blur,
722
- variance,
723
- };
724
- };
725
-
726
- const isBlurry = async (image) => {
727
- console.log("[EVENT] Checking for blur");
728
- const src = cv.imread(image);
729
- let refVariance;
730
- let whiteCanvas = new cv.Mat(490, 866, cv.CV_8UC3, [255, 255, 255, 0]);
731
-
732
- const grayscale = new cv.Mat();
733
- const refGrayscale = new cv.Mat();
734
-
735
- cv.cvtColor(src, grayscale, cv.COLOR_RGBA2GRAY);
736
- cv.cvtColor(whiteCanvas, refGrayscale, cv.COLOR_RGBA2GRAY);
737
-
738
- const laplacian = new cv.Mat();
739
- const refLaplacian = new cv.Mat();
740
-
741
- cv.Laplacian(grayscale, laplacian, cv.CV_8U);
742
- cv.Laplacian(refGrayscale, refLaplacian, cv.CV_8U);
743
-
744
- const meanStdDev = new cv.Mat();
745
- const laplacianMean = new cv.Mat();
746
- const refMeanStdDev = new cv.Mat();
747
- const refLaplacianMean = new cv.Mat();
748
-
749
- cv.meanStdDev(laplacian, laplacianMean, meanStdDev);
750
- cv.meanStdDev(refLaplacian, refLaplacianMean, refMeanStdDev);
751
-
752
- variance = meanStdDev.data64F[0] * 10;
753
- refVariance = refMeanStdDev.data64F[0] * 10;
754
779
 
755
- console.log("variance", variance);
756
- console.log("reference variance", refVariance);
757
-
758
- grayscale.delete();
759
- laplacian.delete();
760
- meanStdDev.delete();
761
- laplacianMean.delete();
762
- refGrayscale.delete();
763
- refLaplacian.delete();
764
- refMeanStdDev.delete();
765
- refLaplacianMean.delete();
766
- whiteCanvas.delete();
767
-
768
- if (variance > 95) {
780
+ const setBlurStatus = (variance) => {
781
+ if (variance >= 10) {
782
+ blurStatus = false;
783
+ return { blurStatus, variance };
784
+ } else if (variance < 0) {
769
785
  blurStatus = false;
770
- setBlurStatus(blurStatus, variance);
771
- return;
786
+ console.log(
787
+ "Variance is lower than 0, Which means image was not cropped and did not pass through blur detection"
788
+ );
789
+ return { blurStatus, variance };
772
790
  } else {
773
791
  blurStatus = true;
774
- setBlurStatus(blurStatus, variance);
775
- return;
792
+ return { blurStatus, variance };
776
793
  }
777
794
  };
778
795
 
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
+
779
849
  return {
780
850
  init: async (session) => {
781
851
  const fp = await FingerprintID.load();
@@ -821,6 +891,8 @@ const VeryfiLens = (function () {
821
891
  boxRef = document.getElementById("veryfi-box-ref");
822
892
  cropImgRef = document.getElementById("veryfi-crop-img-ref");
823
893
  startLens();
894
+ wasmWrapper = new WasmWrapper();
895
+ await wasmWrapper.initialize();
824
896
  },
825
897
  initWasm: async (session) => {
826
898
  isDocumentProcess = true;
@@ -868,7 +940,6 @@ const VeryfiLens = (function () {
868
940
  cropImgRef = document.getElementById("veryfi-crop-img-ref");
869
941
  startWasm();
870
942
  },
871
-
872
943
  initWasmLong: async (session) => {
873
944
  isStitchingProcess = false;
874
945
  const fp = await FingerprintID.load();
@@ -915,7 +986,6 @@ const VeryfiLens = (function () {
915
986
  cropImgRef = document.getElementById("veryfi-crop-img-ref");
916
987
  startWasmLong();
917
988
  },
918
-
919
989
  startCamera: () => {
920
990
  console.log("[EVENT] startCamera");
921
991
  startLens();
@@ -937,7 +1007,7 @@ const VeryfiLens = (function () {
937
1007
  },
938
1008
  capture: async (setImage, setIsEditing) => {
939
1009
  console.log("[EVENT] capture");
940
- const finalImage = await cropImage();
1010
+ const finalImage = await socketCropWasm();
941
1011
  setImage && setImage(finalImage);
942
1012
  console.log("[EVENT] hasCoordinates: ", hasCoordinates);
943
1013
  if (hasCoordinates) setIsDocument(true);
@@ -964,11 +1034,9 @@ const VeryfiLens = (function () {
964
1034
  setIsEditing && setIsEditing(true);
965
1035
  return finalImage;
966
1036
  },
967
-
968
1037
  startStitching: async () => {
969
1038
  isStitchingProcess = true;
970
1039
  },
971
-
972
1040
  createNewSession: async (clientId) => {
973
1041
  await fetchSessionId(clientId);
974
1042
  setClientId(clientId);
@@ -1019,7 +1087,7 @@ const VeryfiLens = (function () {
1019
1087
  return socketStatusesColors[value];
1020
1088
  },
1021
1089
  getBlurStatus: (setIsBlurry) => {
1022
- setIsBlurry(blurStatus);
1090
+ setIsBlurry && setIsBlurry(blurStatus);
1023
1091
  return {
1024
1092
  blurStatus,
1025
1093
  variance,
@@ -1029,7 +1097,7 @@ const VeryfiLens = (function () {
1029
1097
  console.log("Cleaning");
1030
1098
  const box = boxRef;
1031
1099
  const video = videoRef;
1032
- const frame = frameRef
1100
+ const frame = frameRef;
1033
1101
  const crop = cropImgRef;
1034
1102
  crop && releaseCanvas(crop);
1035
1103
  video?.pause();