scandoc-ai-components 0.0.21 → 0.0.24

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.
Files changed (2) hide show
  1. package/dist/index.js +65 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -11185,12 +11185,29 @@ class ExtractorVideo {
11185
11185
  this.onExtractedResults = onExtractedResults;
11186
11186
  this.pastBlurValues = [];
11187
11187
  this.isRunning = false;
11188
+ this.sessionStartTime = null;
11189
+ this.MAX_SESSION_DURATION_MS = 10000;
11190
+ this.failedValidations = 0;
11191
+ this.MAX_FAILED_VALIDATIONS = 5;
11192
+ this.failedExtractionAttempts = 0;
11193
+ this.MAX_EXTRACTION_ATTEMPTS = 3;
11188
11194
  }
11189
11195
  async analyzeVideoStream() {
11190
11196
  if (!this.isRunning) {
11191
11197
  return;
11192
11198
  }
11193
11199
  //
11200
+ const elapsedTime = Date.now() - this.sessionStartTime;
11201
+ if (elapsedTime > this.MAX_SESSION_DURATION_MS) {
11202
+ this.stopVideo();
11203
+ this.onExtractedResults({
11204
+ success: false,
11205
+ code: "TIMEOUT",
11206
+ message: "No document detected within allowed time."
11207
+ });
11208
+ return;
11209
+ }
11210
+ //
11194
11211
  const canvas = document.createElement("canvas");
11195
11212
  let video = document.getElementById("ScanDocAIVideoElement");
11196
11213
  //
@@ -11205,6 +11222,21 @@ class ExtractorVideo {
11205
11222
  const images = [...this.candidateImages];
11206
11223
  if (images.length >= ExtractorVideo.VALIDATION_BATCH_SIZE) {
11207
11224
  const [isValidationOk, response] = await (0,_requests_validation__WEBPACK_IMPORTED_MODULE_2__["default"])(images.map(e => e["validationImg"]), this.pastBlurValues, {});
11225
+ if (!isValidationOk) {
11226
+ this.failedValidations++;
11227
+ if (this.failedValidations >= this.MAX_FAILED_VALIDATIONS) {
11228
+ this.stopVideo();
11229
+ this.onExtractedResults({
11230
+ success: false,
11231
+ code: "VALIDATION_FAILED",
11232
+ message: "Document validation failed repeatedly."
11233
+ });
11234
+ return;
11235
+ }
11236
+ this.candidateImages = [];
11237
+ return;
11238
+ }
11239
+ this.failedValidations = 0;
11208
11240
  if (isValidationOk) {
11209
11241
  this.showMessage(response["Info"]);
11210
11242
  //
@@ -11253,13 +11285,14 @@ class ExtractorVideo {
11253
11285
  }
11254
11286
  }
11255
11287
  }).finally(() => {
11256
- setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11288
+ if (this.isRunning) {
11289
+ setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11290
+ }
11257
11291
  });
11258
11292
  }
11259
11293
  onExtraction(isExtractionOk, extractionData) {
11260
11294
  this.candidateImages = [];
11261
11295
  this.extractionImages = {};
11262
- //
11263
11296
  if (isExtractionOk) {
11264
11297
  this.showMessage("Success - data extracted", "success");
11265
11298
  const shouldStop = this.onExtractedResults(extractionData);
@@ -11267,6 +11300,16 @@ class ExtractorVideo {
11267
11300
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11268
11301
  }
11269
11302
  } else {
11303
+ this.failedExtractionAttempts++;
11304
+ if (this.failedExtractionAttempts >= this.MAX_EXTRACTION_ATTEMPTS) {
11305
+ this.stopVideo();
11306
+ this.onExtractedResults({
11307
+ success: false,
11308
+ code: "EXTRACTION_FAILED",
11309
+ message: "Document extraction failed."
11310
+ });
11311
+ return;
11312
+ }
11270
11313
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11271
11314
  }
11272
11315
  }
@@ -11296,14 +11339,32 @@ class ExtractorVideo {
11296
11339
  video: userMediaConstraints
11297
11340
  }).then(stream => {
11298
11341
  this.video.srcObject = stream;
11342
+ this.video.onloadedmetadata = () => {
11343
+ this.sessionStartTime = Date.now();
11344
+ this.analyzeVideoStream();
11345
+ };
11299
11346
  this.video.play().catch(e => {
11300
11347
  console.warn(`Error on video play: ${e}`);
11301
11348
  });
11302
11349
  //
11303
11350
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11304
- }).catch(error => console.log("Error accessing the camera: " + error));
11351
+ }).catch(error => {
11352
+ console.log("Error accessing the camera: " + error);
11353
+ this.onExtractedResults({
11354
+ success: false,
11355
+ code: "CAMERA_ERROR",
11356
+ message: error.message || "Unable to access camera"
11357
+ });
11358
+ });
11305
11359
  });
11306
- }).catch(() => console.log("Error accessing the camera: " + error));
11360
+ }).catch(error => {
11361
+ console.log("Error accessing the camera: " + error);
11362
+ this.onExtractedResults({
11363
+ success: false,
11364
+ code: "CAMERA_ENUMERATION_ERROR",
11365
+ message: error.message || "Unable to enumerate camera devices"
11366
+ });
11367
+ });
11307
11368
  this.showMessage("Starting scanning");
11308
11369
  return true;
11309
11370
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "scandoc-ai-components",
3
3
  "author": "ScanDoc-AI",
4
- "version": "0.0.21",
4
+ "version": "0.0.24",
5
5
  "private": false,
6
6
  "description": "Pure JavaScript package for integrating ScanDoc-AI services.",
7
7
  "keywords": [