scandoc-ai-components 0.0.41 → 0.0.43

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 +69 -71
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -11188,7 +11188,20 @@ class ExtractorVideo {
11188
11188
  this.pastBlurValues = [];
11189
11189
  this.isRunning = false;
11190
11190
  this.scanStartTime = null;
11191
- this.documentDetectionStartTime = null;
11191
+ this.unsupportedDocCount = 0;
11192
+ this.video = null;
11193
+ }
11194
+ reset() {
11195
+ this.stopVideo();
11196
+ this.candidateImages = [];
11197
+ this.extractionImages = {};
11198
+ this.pastBlurValues = [];
11199
+ this.scanStartTime = null;
11200
+ this.timeoutTimer = null;
11201
+ this.isRunning = false;
11202
+ this.unsupportedDocStartTime = null;
11203
+ this.unsupportedDocCount = 0;
11204
+ this.video = null;
11192
11205
  }
11193
11206
  async analyzeVideoStream() {
11194
11207
  if (!this.isRunning) {
@@ -11214,28 +11227,30 @@ class ExtractorVideo {
11214
11227
  const validationImage = (0,_utils__WEBPACK_IMPORTED_MODULE_4__.videoImgToBase64)(video, canvas, ExtractorVideo.VALIDATION_IMG_WIDTH, ExtractorVideo.VALIDATION_IMG_HEIGHT, true);
11215
11228
  DOCUMENT_DETECTOR.isDocumentPresent(video).then(async isPresent => {
11216
11229
  if (isPresent) {
11217
- if (!this.documentDetectionStartTime) {
11218
- console.log('here');
11219
- this.documentDetectionStartTime = Date.now();
11220
- }
11221
11230
  this.candidateImages.push({
11222
11231
  fullImg: fullImage,
11223
11232
  validationImg: validationImage
11224
11233
  });
11225
11234
  const images = [...this.candidateImages];
11226
- const timeSinceFirstDetection = Date.now() - this.documentDetectionStartTime;
11227
- console.log(timeSinceFirstDetection);
11228
- if (timeSinceFirstDetection > cfgValues.VALIDATION_TIMEOUT_MS) {
11229
- this.showMessage("Validation failed: timeout after 5 seconds.");
11230
- this.documentDetectionStartTime = null;
11231
- this.candidateImages = [];
11232
- return;
11233
- }
11234
11235
  if (images.length >= ExtractorVideo.VALIDATION_BATCH_SIZE) {
11235
11236
  const [isValidationOk, response] = await (0,_requests_validation__WEBPACK_IMPORTED_MODULE_3__["default"])(images.map(e => e["validationImg"]), this.pastBlurValues, {});
11236
11237
  if (isValidationOk) {
11237
- console.log('validation is ok');
11238
- console.log(response);
11238
+ if (response["InfoCode"] === "1006") {
11239
+ this.unsupportedDocCount += 1;
11240
+ if (this.unsupportedDocCount >= cfgValues.MAX_UNSUPPORTED_ATTEMPTS) {
11241
+ this.stopVideo();
11242
+ this.onExtractedResults({
11243
+ success: false,
11244
+ code: "002",
11245
+ info: "Document template not supported."
11246
+ });
11247
+ this.unsupportedDocCount = 0;
11248
+ this.candidateImages = [];
11249
+ return;
11250
+ }
11251
+ } else {
11252
+ this.unsupportedDocCount = 0;
11253
+ }
11239
11254
  this.showMessage(response["Info"]);
11240
11255
  //
11241
11256
  if ("DetectedBlurValue" in response) {
@@ -11314,66 +11329,47 @@ class ExtractorVideo {
11314
11329
  try {
11315
11330
  const serviceConfig = (0,_config__WEBPACK_IMPORTED_MODULE_1__.getScanDocAIConfig)();
11316
11331
  await serviceConfig.getAccessToken(true);
11317
- } catch (authError) {
11318
- this.isRunning = false;
11319
- this.stopVideo();
11320
- this.onExtractedResults({
11321
- success: false,
11322
- code: "004",
11323
- info: "Authentication failed."
11324
- });
11325
- return false;
11326
- }
11327
- const videoElem = document.getElementById("ScanDocAIVideoElement");
11328
- if (videoElem !== undefined) {
11332
+ const videoElem = document.getElementById("ScanDocAIVideoElement");
11333
+ if (!videoElem) {
11334
+ throw new Error("Video element not found.");
11335
+ }
11329
11336
  this.video = videoElem;
11330
11337
  this.isRunning = true;
11331
11338
  this.scanStartTime = Date.now();
11332
- navigator.mediaDevices.enumerateDevices().then(devices => {
11333
- const environmentCameras = devices.filter(device => device.kind === "videoinput" && device.label.toLowerCase().includes("back"));
11334
- checkAutofocusSupport(environmentCameras).then(cameras => {
11335
- let deviceId = undefined;
11336
- let deviceFound = false;
11337
- if (cameras.length > 0) {
11338
- deviceId = cameras[0].deviceId;
11339
- deviceFound = true;
11340
- }
11341
- const userMediaConstraints = {
11342
- ...ExtractorVideo.VIDEO_SETTINGS
11343
- };
11344
- if (deviceFound) {
11345
- userMediaConstraints.deviceId = deviceId;
11346
- }
11347
- navigator.mediaDevices.getUserMedia({
11348
- video: userMediaConstraints
11349
- }).then(stream => {
11350
- this.video.srcObject = stream;
11351
- this.video.play().catch(e => {
11352
- console.warn(`Error on video play: ${e}`);
11353
- });
11354
- this.scanStartTime = Date.now(); // Reset timer
11355
- setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11356
- }).catch(error => {
11357
- console.log("Error accessing the camera: " + error);
11358
- this.onExtractedResults({
11359
- success: false,
11360
- code: "005",
11361
- info: "Could not access the camera. Permission denied or not found."
11362
- });
11363
- });
11364
- });
11365
- }).catch(error => {
11366
- console.log("Error accessing the camera: " + error);
11367
- this.onExtractedResults({
11368
- success: false,
11369
- code: "005",
11370
- info: "Could not enumerate video input devices."
11371
- });
11339
+ const devices = await navigator.mediaDevices.enumerateDevices();
11340
+ const environmentCameras = devices.filter(device => device.kind === "videoinput" && device.label.toLowerCase().includes("back"));
11341
+ const cameras = await checkAutofocusSupport(environmentCameras);
11342
+ let deviceId;
11343
+ if (cameras.length > 0) {
11344
+ deviceId = cameras[0].deviceId;
11345
+ }
11346
+ const userMediaConstraints = {
11347
+ ...ExtractorVideo.VIDEO_SETTINGS
11348
+ };
11349
+ if (deviceId) {
11350
+ userMediaConstraints.deviceId = deviceId;
11351
+ }
11352
+ const stream = await navigator.mediaDevices.getUserMedia({
11353
+ video: userMediaConstraints
11372
11354
  });
11355
+ this.video.srcObject = stream;
11356
+ await this.video.play().catch(e => {
11357
+ console.warn(`Error on video play: ${e}`);
11358
+ });
11359
+ this.scanStartTime = Date.now(); // Reset timer
11360
+ setTimeout(() => this.analyzeVideoStream(), ExtractorVideo.FREQUENCY_MS);
11373
11361
  this.showMessage("Starting scanning");
11374
11362
  return true;
11363
+ } catch (error) {
11364
+ this.isRunning = false;
11365
+ this.stopVideo();
11366
+ this.onExtractedResults({
11367
+ success: false,
11368
+ code: error.status === 401 ? "004" : "005",
11369
+ info: error.status === 401 ? "Authentication failed." : error.message || "Error"
11370
+ });
11371
+ return false;
11375
11372
  }
11376
- return false;
11377
11373
  }
11378
11374
  stopVideo() {
11379
11375
  this.isRunning = false;
@@ -11609,7 +11605,7 @@ let internalConfig = {
11609
11605
  FACE_IMAGE_RESIZE: null,
11610
11606
  // timeout settings:
11611
11607
  MAX_SCAN_DURATION_MS: 10000,
11612
- VALIDATION_TIMEOUT_MS: 5000
11608
+ MAX_UNSUPPORTED_ATTEMPTS: 5
11613
11609
  };
11614
11610
  //
11615
11611
  function setScanDocAIConfig(newConfig = {}) {
@@ -11633,11 +11629,13 @@ class ServiceConfig {
11633
11629
  if (isOk) {
11634
11630
  this.accessToken = data["access_token"];
11635
11631
  this.refreshToken = data["refresh_token"];
11632
+ return this.accessToken;
11636
11633
  } else {
11637
- console.error("Could not authenticate! Check your static key!");
11634
+ const error = new Error("Authentication failed");
11635
+ error.status = 401;
11636
+ throw error;
11638
11637
  }
11639
11638
  }
11640
- return this.accessToken;
11641
11639
  }
11642
11640
  async refreshTokens() {
11643
11641
  if (this.refreshToken !== undefined) {
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.41",
4
+ "version": "0.0.43",
5
5
  "private": false,
6
6
  "description": "Pure JavaScript package for integrating ScanDoc-AI services.",
7
7
  "keywords": [