scandoc-ai-components 0.0.24 → 0.0.26

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 +24 -49
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -11170,6 +11170,7 @@ class ExtractorVideo {
11170
11170
  static VALIDATION_BATCH_SIZE = 1;
11171
11171
  static VALIDATION_IMG_WIDTH = 384;
11172
11172
  static VALIDATION_IMG_HEIGHT = 384;
11173
+ static MAX_SCAN_DURATION_MS = 10000;
11173
11174
  static VIDEO_SETTINGS = Object.freeze({
11174
11175
  width: {
11175
11176
  ideal: 1920
@@ -11185,25 +11186,21 @@ class ExtractorVideo {
11185
11186
  this.onExtractedResults = onExtractedResults;
11186
11187
  this.pastBlurValues = [];
11187
11188
  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;
11189
+ this.scanStartTime = null;
11190
+ this.timeoutTimer = null;
11194
11191
  }
11195
11192
  async analyzeVideoStream() {
11196
11193
  if (!this.isRunning) {
11197
11194
  return;
11198
11195
  }
11199
11196
  //
11200
- const elapsedTime = Date.now() - this.sessionStartTime;
11201
- if (elapsedTime > this.MAX_SESSION_DURATION_MS) {
11197
+ const now = Date.now();
11198
+ if (this.scanStartTime && now - this.scanStartTime > ExtractorVideo.MAX_SCAN_DURATION_MS) {
11202
11199
  this.stopVideo();
11203
11200
  this.onExtractedResults({
11204
11201
  success: false,
11205
11202
  code: "TIMEOUT",
11206
- message: "No document detected within allowed time."
11203
+ info: "Document not detected within time limit."
11207
11204
  });
11208
11205
  return;
11209
11206
  }
@@ -11222,21 +11219,6 @@ class ExtractorVideo {
11222
11219
  const images = [...this.candidateImages];
11223
11220
  if (images.length >= ExtractorVideo.VALIDATION_BATCH_SIZE) {
11224
11221
  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;
11240
11222
  if (isValidationOk) {
11241
11223
  this.showMessage(response["Info"]);
11242
11224
  //
@@ -11285,9 +11267,7 @@ class ExtractorVideo {
11285
11267
  }
11286
11268
  }
11287
11269
  }).finally(() => {
11288
- if (this.isRunning) {
11289
- setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11290
- }
11270
+ setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11291
11271
  });
11292
11272
  }
11293
11273
  onExtraction(isExtractionOk, extractionData) {
@@ -11295,21 +11275,21 @@ class ExtractorVideo {
11295
11275
  this.extractionImages = {};
11296
11276
  if (isExtractionOk) {
11297
11277
  this.showMessage("Success - data extracted", "success");
11298
- const shouldStop = this.onExtractedResults(extractionData);
11278
+ const shouldStop = this.onExtractedResults({
11279
+ success: true,
11280
+ code: "EXTRACTION_OK",
11281
+ info: "Document extracted successfully.",
11282
+ data: extractionData
11283
+ });
11299
11284
  if (shouldStop === true) {
11300
11285
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11301
11286
  }
11302
11287
  } 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
- }
11288
+ this.onExtractedResults({
11289
+ success: false,
11290
+ code: "EXTRACTION_FAILED",
11291
+ info: "Document validation passed but extraction failed."
11292
+ });
11313
11293
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11314
11294
  }
11315
11295
  }
@@ -11318,10 +11298,10 @@ class ExtractorVideo {
11318
11298
  if (videoElem !== undefined) {
11319
11299
  this.video = videoElem;
11320
11300
  this.isRunning = true;
11301
+ this.scanStartTime = Date.now();
11321
11302
  navigator.mediaDevices.enumerateDevices().then(devices => {
11322
11303
  const environmentCameras = devices.filter(device => device.kind === "videoinput" && device.label.toLowerCase().includes("back"));
11323
11304
  checkAutofocusSupport(environmentCameras).then(cameras => {
11324
- console.log("Got autofocus camera:", cameras);
11325
11305
  let deviceId = undefined;
11326
11306
  let deviceFound = false;
11327
11307
  if (cameras.length > 0) {
@@ -11334,26 +11314,21 @@ class ExtractorVideo {
11334
11314
  if (deviceFound) {
11335
11315
  userMediaConstraints.deviceId = deviceId;
11336
11316
  }
11337
- //
11338
11317
  navigator.mediaDevices.getUserMedia({
11339
11318
  video: userMediaConstraints
11340
11319
  }).then(stream => {
11341
11320
  this.video.srcObject = stream;
11342
- this.video.onloadedmetadata = () => {
11343
- this.sessionStartTime = Date.now();
11344
- this.analyzeVideoStream();
11345
- };
11346
11321
  this.video.play().catch(e => {
11347
11322
  console.warn(`Error on video play: ${e}`);
11348
11323
  });
11349
- //
11324
+ this.scanStartTime = Date.now(); // Reset timer
11350
11325
  setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11351
11326
  }).catch(error => {
11352
11327
  console.log("Error accessing the camera: " + error);
11353
11328
  this.onExtractedResults({
11354
11329
  success: false,
11355
- code: "CAMERA_ERROR",
11356
- message: error.message || "Unable to access camera"
11330
+ code: "CAMERA_ACCESS_DENIED",
11331
+ info: "Could not access the camera. Permission denied or not found."
11357
11332
  });
11358
11333
  });
11359
11334
  });
@@ -11361,8 +11336,8 @@ class ExtractorVideo {
11361
11336
  console.log("Error accessing the camera: " + error);
11362
11337
  this.onExtractedResults({
11363
11338
  success: false,
11364
- code: "CAMERA_ENUMERATION_ERROR",
11365
- message: error.message || "Unable to enumerate camera devices"
11339
+ code: "CAMERA_ENUMERATION_FAILED",
11340
+ info: "Could not enumerate video input devices."
11366
11341
  });
11367
11342
  });
11368
11343
  this.showMessage("Starting scanning");
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.24",
4
+ "version": "0.0.26",
5
5
  "private": false,
6
6
  "description": "Pure JavaScript package for integrating ScanDoc-AI services.",
7
7
  "keywords": [