idmission-web-sdk 2.1.86 → 2.1.88

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.
@@ -22,10 +22,11 @@ export type DocumentDetectionModelProviderProps = {
22
22
  autoStart?: boolean;
23
23
  children: ReactNode;
24
24
  throttleMs?: number;
25
+ delayAfterStartMs?: number;
25
26
  documentDetectionModelPath?: string;
26
27
  documentDetectionModelScoreThreshold?: number;
27
28
  documentDetectionModelLoadTimeoutMs?: number;
28
29
  onDocumentDetectionModelError?: (error: Error) => void;
29
30
  };
30
- export declare function DocumentDetectionModelProvider({ autoStart, children, throttleMs, documentDetectionModelPath, documentDetectionModelScoreThreshold, documentDetectionModelLoadTimeoutMs, onDocumentDetectionModelError, }: DocumentDetectionModelProviderProps): ReactElement;
31
+ export declare function DocumentDetectionModelProvider({ autoStart, children, throttleMs, delayAfterStartMs, documentDetectionModelPath, documentDetectionModelScoreThreshold, documentDetectionModelLoadTimeoutMs, onDocumentDetectionModelError, }: DocumentDetectionModelProviderProps): ReactElement;
31
32
  export {};
@@ -1,4 +1,4 @@
1
- export declare function useFrameLoop(fn: (frameId: number) => Promise<void>, { throttleMs, autoStart }: {
1
+ export declare function useFrameLoop(fn: (frameId: number, timeRunning: number) => Promise<void>, { throttleMs, autoStart }: {
2
2
  throttleMs?: number | undefined;
3
3
  autoStart?: boolean | undefined;
4
4
  }): {
@@ -234,7 +234,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
234
234
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
235
235
  };
236
236
 
237
- var webSdkVersion = '2.1.86';
237
+ var webSdkVersion = '2.1.88';
238
238
 
239
239
  function getPlatform() {
240
240
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -5588,13 +5588,25 @@ function makeDocumentDetectorPrediction(detector, frame) {
5588
5588
  });
5589
5589
  });
5590
5590
  }
5591
- var lastNBoxes = [];
5592
5591
  var lastDetectionAt = 0;
5593
5592
  var lastDetectionTime = 0;
5594
5593
  function setLastDetectionAt(time) {
5595
5594
  lastDetectionTime = time - lastDetectionAt;
5596
5595
  lastDetectionAt = time;
5597
5596
  }
5597
+ var framesNeededSamples = [];
5598
+ function trackFramesNeeded(value) {
5599
+ framesNeededSamples.unshift(value);
5600
+ if (framesNeededSamples.length > 10) framesNeededSamples.length = 10;
5601
+ }
5602
+ var lastNBoxes = [];
5603
+ function trackBox(box, framesNeeded) {
5604
+ if (framesNeeded === void 0) {
5605
+ framesNeeded = 12;
5606
+ }
5607
+ lastNBoxes.unshift(box);
5608
+ if (lastNBoxes.length > framesNeeded) lastNBoxes.length = framesNeeded;
5609
+ }
5598
5610
  var defaultDocumentDetectionBoundaries = {
5599
5611
  top: 20,
5600
5612
  bottom: 20,
@@ -5678,11 +5690,12 @@ function processDocumentDetectorPrediction(prediction, thresholds, boundaries) {
5678
5690
  docWidth = _p[0],
5679
5691
  docHeight = _p[1];
5680
5692
  documentTooClose = docWidth > 0.85 || docHeight > 0.85;
5681
- if (detectionThresholdMet) {
5693
+ if (detectionThresholdMet && documentInBounds && !documentTooClose) {
5682
5694
  var threshold_1 = (_m = thresholds.stability) !== null && _m !== void 0 ? _m : defaultDocumentDetectionThresholds.stability;
5683
- var framesNeeded = Math.ceil(Math.min(1000 / lastDetectionTime, 12));
5684
- lastNBoxes = __spreadArray([bestDocument.box], lastNBoxes, true).slice(0, framesNeeded);
5685
- documentIsStable = lastNBoxes.length >= framesNeeded && !createPairs(lastNBoxes).some(function (_a) {
5695
+ trackFramesNeeded(1000 / lastDetectionTime);
5696
+ var framesNeeded = Math.ceil(average(framesNeededSamples));
5697
+ trackBox(bestDocument.box, framesNeeded);
5698
+ documentIsStable = lastNBoxes.length >= framesNeeded && !createPairs(lastNBoxes, 12).some(function (_a) {
5686
5699
  var a = _a[0],
5687
5700
  b = _a[1];
5688
5701
  return calculateIoU(a, b) < threshold_1;
@@ -5724,9 +5737,10 @@ function applyNonMaxSuppression(detectedObjects) {
5724
5737
  return !!obj;
5725
5738
  });
5726
5739
  }
5727
- function createPairs(arr) {
5740
+ function createPairs(arr, maxPairs) {
5728
5741
  var pairs = [];
5729
- for (var i = 0; i < arr.length - 1; i++) {
5742
+ var end = maxPairs ;
5743
+ for (var i = 0; i < end; i++) {
5730
5744
  pairs.push([arr[i], arr[i + 1]]);
5731
5745
  }
5732
5746
  return pairs;
@@ -5745,6 +5759,13 @@ function calculateIoU(boxA, boxB) {
5745
5759
  if (unionArea === 0) return 0;
5746
5760
  return intersectionArea / unionArea;
5747
5761
  }
5762
+ function average(arr) {
5763
+ var len = arr.length;
5764
+ if (len === 0) return 0;
5765
+ var sum = 0;
5766
+ for (var i = 0; i < len; i++) sum += arr[i];
5767
+ return sum / len;
5768
+ }
5748
5769
 
5749
5770
  function useFrameLoop(fn, _a) {
5750
5771
  var _b = _a.throttleMs,
@@ -5754,6 +5775,7 @@ function useFrameLoop(fn, _a) {
5754
5775
  var _d = React.useState(false),
5755
5776
  running = _d[0],
5756
5777
  setRunning = _d[1];
5778
+ var startedAtRef = React.useRef(null);
5757
5779
  var loopId = React.useRef(0);
5758
5780
  var frameId = React.useRef(0);
5759
5781
  React.useEffect(function runFrameLoop() {
@@ -5762,15 +5784,17 @@ function useFrameLoop(fn, _a) {
5762
5784
  var currentLoopId = loopId.current;
5763
5785
  function renderPrediction() {
5764
5786
  return __awaiter(this, void 0, void 0, function () {
5765
- var start, took, amountToThrottle;
5766
- return __generator(this, function (_a) {
5767
- switch (_a.label) {
5787
+ var start, timeRunning, took, amountToThrottle;
5788
+ var _a, _b;
5789
+ return __generator(this, function (_c) {
5790
+ switch (_c.label) {
5768
5791
  case 0:
5769
5792
  if (currentLoopId !== loopId.current) return [2 /*return*/];
5770
5793
  start = new Date().getTime();
5771
- return [4 /*yield*/, fn(frameId.current)];
5794
+ timeRunning = start - ((_b = (_a = startedAtRef.current) === null || _a === void 0 ? void 0 : _a.getTime()) !== null && _b !== void 0 ? _b : 0);
5795
+ return [4 /*yield*/, fn(frameId.current, timeRunning)];
5772
5796
  case 1:
5773
- _a.sent();
5797
+ _c.sent();
5774
5798
  took = new Date().getTime() - start;
5775
5799
  amountToThrottle = Math.max((throttleMs !== null && throttleMs !== void 0 ? throttleMs : 0) - took, 0);
5776
5800
  timer = setTimeout(function () {
@@ -5789,11 +5813,13 @@ function useFrameLoop(fn, _a) {
5789
5813
  };
5790
5814
  }, [fn, running, throttleMs]);
5791
5815
  var start = React.useCallback(function () {
5816
+ startedAtRef.current = new Date();
5792
5817
  setRunning(true);
5793
5818
  }, []);
5794
5819
  var stop = React.useCallback(function () {
5795
5820
  loopId.current += 1; // force the loop to stop immediately.
5796
5821
  setRunning(false);
5822
+ startedAtRef.current = null;
5797
5823
  }, []);
5798
5824
  React.useEffect(function startAutomatically() {
5799
5825
  if (autoStart) start();
@@ -5843,44 +5869,46 @@ function DocumentDetectionModelProvider(_a) {
5843
5869
  children = _a.children,
5844
5870
  _c = _a.throttleMs,
5845
5871
  throttleMs = _c === void 0 ? 16 : _c,
5846
- _d = _a.documentDetectionModelPath,
5847
- documentDetectionModelPath = _d === void 0 ? defaultDocumentDetectorModelPath : _d,
5848
- _e = _a.documentDetectionModelScoreThreshold,
5849
- documentDetectionModelScoreThreshold = _e === void 0 ? defaultDocumentDetectionScoreThreshold : _e,
5850
- _f = _a.documentDetectionModelLoadTimeoutMs,
5851
- documentDetectionModelLoadTimeoutMs = _f === void 0 ? defaultDocumentDetectionModelLoadTimeoutMs : _f,
5872
+ _d = _a.delayAfterStartMs,
5873
+ delayAfterStartMs = _d === void 0 ? 0 : _d,
5874
+ _e = _a.documentDetectionModelPath,
5875
+ documentDetectionModelPath = _e === void 0 ? defaultDocumentDetectorModelPath : _e,
5876
+ _f = _a.documentDetectionModelScoreThreshold,
5877
+ documentDetectionModelScoreThreshold = _f === void 0 ? defaultDocumentDetectionScoreThreshold : _f,
5878
+ _g = _a.documentDetectionModelLoadTimeoutMs,
5879
+ documentDetectionModelLoadTimeoutMs = _g === void 0 ? defaultDocumentDetectionModelLoadTimeoutMs : _g,
5852
5880
  onDocumentDetectionModelError = _a.onDocumentDetectionModelError;
5853
- var _g = React.useContext(CameraStateContext),
5854
- videoRef = _g.videoRef,
5855
- videoLoaded = _g.videoLoaded,
5856
- cameraReady = _g.cameraReady;
5881
+ var _h = React.useContext(CameraStateContext),
5882
+ videoRef = _h.videoRef,
5883
+ videoLoaded = _h.videoLoaded,
5884
+ cameraReady = _h.cameraReady;
5857
5885
  var lastPredictionCanvas = React.useRef(null);
5858
5886
  var onPredictionHandler = React.useRef();
5859
- var _h = React.useState({}),
5860
- documentDetectionThresholds = _h[0],
5861
- setDocumentDetectionThresholds = _h[1];
5862
- var _j = React.useState(defaultDocumentDetectionBoundaries),
5863
- documentDetectionBoundaries = _j[0],
5864
- setDocumentDetectionBoundaries = _j[1];
5865
- var _k = React.useState(0),
5866
- timesAllZero = _k[0],
5867
- setTimesAllZero = _k[1];
5887
+ var _j = React.useState({}),
5888
+ documentDetectionThresholds = _j[0],
5889
+ setDocumentDetectionThresholds = _j[1];
5890
+ var _k = React.useState(defaultDocumentDetectionBoundaries),
5891
+ documentDetectionBoundaries = _k[0],
5892
+ setDocumentDetectionBoundaries = _k[1];
5868
5893
  var _l = React.useState(0),
5869
- canvasKey = _l[0],
5870
- setCanvasKey = _l[1];
5894
+ timesAllZero = _l[0],
5895
+ setTimesAllZero = _l[1];
5896
+ var _m = React.useState(0),
5897
+ canvasKey = _m[0],
5898
+ setCanvasKey = _m[1];
5871
5899
  var stopDetection = React.useRef(0);
5872
- var _m = useLoadDocumentDetector({
5900
+ var _o = useLoadDocumentDetector({
5873
5901
  modelPath: documentDetectionModelPath,
5874
5902
  modelLoadTimeoutMs: documentDetectionModelLoadTimeoutMs,
5875
5903
  scoreThreshold: documentDetectionModelScoreThreshold,
5876
5904
  onModelError: onDocumentDetectionModelError
5877
5905
  }),
5878
- detector = _m.detector,
5879
- ready = _m.ready,
5880
- modelDownloadProgress = _m.modelDownloadProgress,
5881
- modelError = _m.modelError,
5882
- setModelError = _m.setModelError;
5883
- var _o = useFrameLoop(React.useCallback(function (frameId) {
5906
+ detector = _o.detector,
5907
+ ready = _o.ready,
5908
+ modelDownloadProgress = _o.modelDownloadProgress,
5909
+ modelError = _o.modelError,
5910
+ setModelError = _o.setModelError;
5911
+ var _p = useFrameLoop(React.useCallback(function (frameId, timeRunning) {
5884
5912
  return __awaiter(_this, void 0, void 0, function () {
5885
5913
  var stopDetectionAtStart, vw, vh, ctx, prediction, processedPrediction;
5886
5914
  var _a;
@@ -5909,6 +5937,7 @@ function DocumentDetectionModelProvider(_a) {
5909
5937
  return n + 1;
5910
5938
  });
5911
5939
  if (stopDetectionAtStart !== stopDetection.current) return [2 /*return*/];
5940
+ if (timeRunning < delayAfterStartMs) return [2 /*return*/];
5912
5941
  return [4 /*yield*/, (_a = onPredictionHandler.current) === null || _a === void 0 ? void 0 : _a.call(onPredictionHandler, processedPrediction)];
5913
5942
  case 2:
5914
5943
  _b.sent();
@@ -5918,12 +5947,12 @@ function DocumentDetectionModelProvider(_a) {
5918
5947
  }
5919
5948
  });
5920
5949
  });
5921
- }, [cameraReady, detector, documentDetectionBoundaries, documentDetectionThresholds, ready, videoLoaded, videoRef]), {
5950
+ }, [cameraReady, delayAfterStartMs, detector, documentDetectionBoundaries, documentDetectionThresholds, ready, videoLoaded, videoRef]), {
5922
5951
  throttleMs: throttleMs,
5923
5952
  autoStart: autoStart
5924
5953
  }),
5925
- start = _o.start,
5926
- stop = _o.stop;
5954
+ start = _p.start,
5955
+ stop = _p.stop;
5927
5956
  React.useEffect(function setErrorIfAllZero() {
5928
5957
  if (timesAllZero >= 2) {
5929
5958
  setModelError(new Error('model is returning all zeroes'));