idmission-web-sdk 2.1.86 → 2.1.87

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.87';
238
238
 
239
239
  function getPlatform() {
240
240
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -5595,6 +5595,7 @@ function setLastDetectionAt(time) {
5595
5595
  lastDetectionTime = time - lastDetectionAt;
5596
5596
  lastDetectionAt = time;
5597
5597
  }
5598
+ var framesNeededSamples = [];
5598
5599
  var defaultDocumentDetectionBoundaries = {
5599
5600
  top: 20,
5600
5601
  bottom: 20,
@@ -5678,11 +5679,16 @@ function processDocumentDetectorPrediction(prediction, thresholds, boundaries) {
5678
5679
  docWidth = _p[0],
5679
5680
  docHeight = _p[1];
5680
5681
  documentTooClose = docWidth > 0.85 || docHeight > 0.85;
5681
- if (detectionThresholdMet) {
5682
+ if (detectionThresholdMet && documentInBounds && !documentTooClose) {
5682
5683
  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) {
5684
+ framesNeededSamples.unshift(1000 / lastDetectionTime);
5685
+ framesNeededSamples = framesNeededSamples.slice(0, 10);
5686
+ var framesNeeded = Math.ceil(average(framesNeededSamples));
5687
+ lastNBoxes.unshift(bestDocument.box);
5688
+ lastNBoxes = lastNBoxes.slice(0, framesNeeded);
5689
+ var numFramesToConsider = Math.min(framesNeeded, 12);
5690
+ var framesToConsider = lastNBoxes.slice(0, numFramesToConsider);
5691
+ documentIsStable = lastNBoxes.length >= framesNeeded - 1 && !createPairs(framesToConsider).some(function (_a) {
5686
5692
  var a = _a[0],
5687
5693
  b = _a[1];
5688
5694
  return calculateIoU(a, b) < threshold_1;
@@ -5745,6 +5751,12 @@ function calculateIoU(boxA, boxB) {
5745
5751
  if (unionArea === 0) return 0;
5746
5752
  return intersectionArea / unionArea;
5747
5753
  }
5754
+ function average(arr) {
5755
+ var len = arr.length;
5756
+ var sum = 0;
5757
+ for (var i = 0; i < len; i++) sum += arr[i];
5758
+ return sum / len;
5759
+ }
5748
5760
 
5749
5761
  function useFrameLoop(fn, _a) {
5750
5762
  var _b = _a.throttleMs,
@@ -5754,6 +5766,7 @@ function useFrameLoop(fn, _a) {
5754
5766
  var _d = React.useState(false),
5755
5767
  running = _d[0],
5756
5768
  setRunning = _d[1];
5769
+ var startedAtRef = React.useRef(null);
5757
5770
  var loopId = React.useRef(0);
5758
5771
  var frameId = React.useRef(0);
5759
5772
  React.useEffect(function runFrameLoop() {
@@ -5762,15 +5775,17 @@ function useFrameLoop(fn, _a) {
5762
5775
  var currentLoopId = loopId.current;
5763
5776
  function renderPrediction() {
5764
5777
  return __awaiter(this, void 0, void 0, function () {
5765
- var start, took, amountToThrottle;
5766
- return __generator(this, function (_a) {
5767
- switch (_a.label) {
5778
+ var start, timeRunning, took, amountToThrottle;
5779
+ var _a, _b;
5780
+ return __generator(this, function (_c) {
5781
+ switch (_c.label) {
5768
5782
  case 0:
5769
5783
  if (currentLoopId !== loopId.current) return [2 /*return*/];
5770
5784
  start = new Date().getTime();
5771
- return [4 /*yield*/, fn(frameId.current)];
5785
+ timeRunning = start - ((_b = (_a = startedAtRef.current) === null || _a === void 0 ? void 0 : _a.getTime()) !== null && _b !== void 0 ? _b : 0);
5786
+ return [4 /*yield*/, fn(frameId.current, timeRunning)];
5772
5787
  case 1:
5773
- _a.sent();
5788
+ _c.sent();
5774
5789
  took = new Date().getTime() - start;
5775
5790
  amountToThrottle = Math.max((throttleMs !== null && throttleMs !== void 0 ? throttleMs : 0) - took, 0);
5776
5791
  timer = setTimeout(function () {
@@ -5789,11 +5804,13 @@ function useFrameLoop(fn, _a) {
5789
5804
  };
5790
5805
  }, [fn, running, throttleMs]);
5791
5806
  var start = React.useCallback(function () {
5807
+ startedAtRef.current = new Date();
5792
5808
  setRunning(true);
5793
5809
  }, []);
5794
5810
  var stop = React.useCallback(function () {
5795
5811
  loopId.current += 1; // force the loop to stop immediately.
5796
5812
  setRunning(false);
5813
+ startedAtRef.current = null;
5797
5814
  }, []);
5798
5815
  React.useEffect(function startAutomatically() {
5799
5816
  if (autoStart) start();
@@ -5843,44 +5860,46 @@ function DocumentDetectionModelProvider(_a) {
5843
5860
  children = _a.children,
5844
5861
  _c = _a.throttleMs,
5845
5862
  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,
5863
+ _d = _a.delayAfterStartMs,
5864
+ delayAfterStartMs = _d === void 0 ? 0 : _d,
5865
+ _e = _a.documentDetectionModelPath,
5866
+ documentDetectionModelPath = _e === void 0 ? defaultDocumentDetectorModelPath : _e,
5867
+ _f = _a.documentDetectionModelScoreThreshold,
5868
+ documentDetectionModelScoreThreshold = _f === void 0 ? defaultDocumentDetectionScoreThreshold : _f,
5869
+ _g = _a.documentDetectionModelLoadTimeoutMs,
5870
+ documentDetectionModelLoadTimeoutMs = _g === void 0 ? defaultDocumentDetectionModelLoadTimeoutMs : _g,
5852
5871
  onDocumentDetectionModelError = _a.onDocumentDetectionModelError;
5853
- var _g = React.useContext(CameraStateContext),
5854
- videoRef = _g.videoRef,
5855
- videoLoaded = _g.videoLoaded,
5856
- cameraReady = _g.cameraReady;
5872
+ var _h = React.useContext(CameraStateContext),
5873
+ videoRef = _h.videoRef,
5874
+ videoLoaded = _h.videoLoaded,
5875
+ cameraReady = _h.cameraReady;
5857
5876
  var lastPredictionCanvas = React.useRef(null);
5858
5877
  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];
5878
+ var _j = React.useState({}),
5879
+ documentDetectionThresholds = _j[0],
5880
+ setDocumentDetectionThresholds = _j[1];
5881
+ var _k = React.useState(defaultDocumentDetectionBoundaries),
5882
+ documentDetectionBoundaries = _k[0],
5883
+ setDocumentDetectionBoundaries = _k[1];
5868
5884
  var _l = React.useState(0),
5869
- canvasKey = _l[0],
5870
- setCanvasKey = _l[1];
5885
+ timesAllZero = _l[0],
5886
+ setTimesAllZero = _l[1];
5887
+ var _m = React.useState(0),
5888
+ canvasKey = _m[0],
5889
+ setCanvasKey = _m[1];
5871
5890
  var stopDetection = React.useRef(0);
5872
- var _m = useLoadDocumentDetector({
5891
+ var _o = useLoadDocumentDetector({
5873
5892
  modelPath: documentDetectionModelPath,
5874
5893
  modelLoadTimeoutMs: documentDetectionModelLoadTimeoutMs,
5875
5894
  scoreThreshold: documentDetectionModelScoreThreshold,
5876
5895
  onModelError: onDocumentDetectionModelError
5877
5896
  }),
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) {
5897
+ detector = _o.detector,
5898
+ ready = _o.ready,
5899
+ modelDownloadProgress = _o.modelDownloadProgress,
5900
+ modelError = _o.modelError,
5901
+ setModelError = _o.setModelError;
5902
+ var _p = useFrameLoop(React.useCallback(function (frameId, timeRunning) {
5884
5903
  return __awaiter(_this, void 0, void 0, function () {
5885
5904
  var stopDetectionAtStart, vw, vh, ctx, prediction, processedPrediction;
5886
5905
  var _a;
@@ -5909,6 +5928,7 @@ function DocumentDetectionModelProvider(_a) {
5909
5928
  return n + 1;
5910
5929
  });
5911
5930
  if (stopDetectionAtStart !== stopDetection.current) return [2 /*return*/];
5931
+ if (timeRunning < delayAfterStartMs) return [2 /*return*/];
5912
5932
  return [4 /*yield*/, (_a = onPredictionHandler.current) === null || _a === void 0 ? void 0 : _a.call(onPredictionHandler, processedPrediction)];
5913
5933
  case 2:
5914
5934
  _b.sent();
@@ -5918,12 +5938,12 @@ function DocumentDetectionModelProvider(_a) {
5918
5938
  }
5919
5939
  });
5920
5940
  });
5921
- }, [cameraReady, detector, documentDetectionBoundaries, documentDetectionThresholds, ready, videoLoaded, videoRef]), {
5941
+ }, [cameraReady, delayAfterStartMs, detector, documentDetectionBoundaries, documentDetectionThresholds, ready, videoLoaded, videoRef]), {
5922
5942
  throttleMs: throttleMs,
5923
5943
  autoStart: autoStart
5924
5944
  }),
5925
- start = _o.start,
5926
- stop = _o.stop;
5945
+ start = _p.start,
5946
+ stop = _p.stop;
5927
5947
  React.useEffect(function setErrorIfAllZero() {
5928
5948
  if (timesAllZero >= 2) {
5929
5949
  setModelError(new Error('model is returning all zeroes'));