@jaak.ai/stamps 2.0.0-dev.23 → 2.0.0-dev.25

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.
@@ -5,6 +5,7 @@ export class JaakStamps {
5
5
  alignmentTolerance = 10; // Tolerancia en píxeles para considerar un lado alineado (escalado para 320x320)
6
6
  maskSize = 90; // Porcentaje del video que ocupará la máscara (default 90%)
7
7
  cropMargin = 0; // Margen en píxeles para el recorte del documento (default 0)
8
+ useDocumentClassification = false; // Habilita la clasificación automática de documentos para determinar si solicitar reverso
8
9
  captureCompleted;
9
10
  isReady;
10
11
  isVideoActive = false;
@@ -38,6 +39,8 @@ export class JaakStamps {
38
39
  animationId;
39
40
  hasScreenshotTaken = false;
40
41
  lastDetectedBox;
42
+ mobileNetSession;
43
+ mobileNetClassMap;
41
44
  // Performance optimization properties
42
45
  frameSkipCounter = 0;
43
46
  FRAME_SKIP = 2; // Process every 3rd frame (reduces CPU by ~66%)
@@ -50,7 +53,9 @@ export class JaakStamps {
50
53
  preprocessCtx;
51
54
  captureCanvas;
52
55
  captureCtx;
53
- MODEL_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/detector-nano.onnx";
56
+ MODEL_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/ddmyp-v1.onnx";
57
+ MOBILENET_MODEL_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/cdmmp-v1.onnx";
58
+ MOBILENET_CLASSES_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/cdmmp-v1.json";
54
59
  INPUT_SIZE = 320;
55
60
  CONFIDENCE_THRESHOLD = 0.6;
56
61
  // ISO/IEC 7810 ID-1 standard dimensions (85.60mm x 53.98mm)
@@ -256,25 +261,29 @@ export class JaakStamps {
256
261
  }
257
262
  try {
258
263
  this.isLoading = true;
259
- this.statusMessage = "Precargando modelo de detección...";
264
+ this.statusMessage = "Precargando modelos...";
260
265
  this.statusColor = "#007bff";
261
266
  const modelPath = this.MODEL_PATH;
262
- this.debugLog('🤖 Preloading model:', modelPath);
267
+ this.debugLog('🤖 Preloading detection model:', modelPath);
263
268
  // Configure ONNX Runtime with device-specific optimizations
264
269
  const sessionOptions = this.getSessionOptions();
265
270
  this.session = await window.ort.InferenceSession.create(modelPath, sessionOptions);
271
+ // Preload MobileNet model and classes only if classification is enabled
272
+ if (this.useDocumentClassification) {
273
+ await this.loadMobileNetModel();
274
+ }
266
275
  this.isModelPreloaded = true;
267
276
  this.isLoading = false;
268
- this.statusMessage = "Modelo precargado. Listo para comenzar detección";
277
+ this.statusMessage = "Modelos precargados. Listo para comenzar detección";
269
278
  this.statusColor = "#28a745";
270
279
  this.emitReadyEvent();
271
- this.debugLog('✅ Model preloaded successfully');
272
- return { success: true, message: 'Model preloaded successfully' };
280
+ this.debugLog('✅ Models preloaded successfully');
281
+ return { success: true, message: 'Models preloaded successfully' };
273
282
  }
274
283
  catch (error) {
275
- this.debugLog('❌ Error preloading model:', error);
284
+ this.debugLog('❌ Error preloading models:', error);
276
285
  this.isLoading = false;
277
- this.statusMessage = "Error al precargar el modelo";
286
+ this.statusMessage = "Error al precargar los modelos";
278
287
  this.statusColor = "#ff6b6b";
279
288
  return { success: false, error: error.message };
280
289
  }
@@ -284,6 +293,80 @@ export class JaakStamps {
284
293
  this.completeProcess(true);
285
294
  }
286
295
  }
296
+ async loadMobileNetModel() {
297
+ try {
298
+ this.debugLog('🤖 Loading MobileNet model...');
299
+ // Load class map
300
+ const classResponse = await fetch(this.MOBILENET_CLASSES_PATH);
301
+ if (!classResponse.ok) {
302
+ throw new Error(`Failed to load class map: ${this.MOBILENET_CLASSES_PATH}`);
303
+ }
304
+ this.mobileNetClassMap = await classResponse.json();
305
+ this.debugLog('📋 MobileNet classes loaded:', this.mobileNetClassMap);
306
+ // Load model
307
+ const sessionOptions = this.getSessionOptions();
308
+ this.mobileNetSession = await window.ort.InferenceSession.create(this.MOBILENET_MODEL_PATH, sessionOptions);
309
+ this.debugLog('✅ MobileNet model loaded successfully');
310
+ }
311
+ catch (error) {
312
+ this.debugLog('❌ Error loading MobileNet model:', error);
313
+ throw error;
314
+ }
315
+ }
316
+ preprocessMobileNet(canvas) {
317
+ // Create a temporary canvas for MobileNet preprocessing (224x224)
318
+ const tempCanvas = document.createElement('canvas');
319
+ tempCanvas.width = 224;
320
+ tempCanvas.height = 224;
321
+ const tempCtx = tempCanvas.getContext('2d');
322
+ // Draw the cropped image onto the 224x224 canvas
323
+ tempCtx.drawImage(canvas, 0, 0, 224, 224);
324
+ // Get image data and preprocess for MobileNet
325
+ const imageData = tempCtx.getImageData(0, 0, 224, 224);
326
+ const data = imageData.data;
327
+ const hw = 224 * 224;
328
+ const arr = new Float32Array(3 * hw);
329
+ // Normalize pixels: (pixel/255 - 0.5) / 0.5 = (pixel - 127.5) / 127.5
330
+ for (let i = 0; i < hw; i++) {
331
+ arr[i] = (data[i * 4 + 0] / 255 - 0.5) / 0.5; // R
332
+ arr[hw + i] = (data[i * 4 + 1] / 255 - 0.5) / 0.5; // G
333
+ arr[2 * hw + i] = (data[i * 4 + 2] / 255 - 0.5) / 0.5; // B
334
+ }
335
+ return new window.ort.Tensor('float32', arr, [1, 3, 224, 224]);
336
+ }
337
+ async classifyDocument(canvas) {
338
+ if (!this.mobileNetSession || !this.mobileNetClassMap) {
339
+ this.debugLog('⚠️ MobileNet model not loaded');
340
+ return null;
341
+ }
342
+ try {
343
+ this.debugLog('🔍 Classifying document...');
344
+ // Preprocess image for MobileNet
345
+ const inputTensor = this.preprocessMobileNet(canvas);
346
+ // Run inference
347
+ const feeds = { input: inputTensor };
348
+ const results = await this.mobileNetSession.run(feeds);
349
+ const output = results[Object.keys(results)[0]].data;
350
+ // Find the class with highest confidence
351
+ const maxIdx = output.reduce((bestIdx, val, idx, arr) => val > arr[bestIdx] ? idx : bestIdx, 0);
352
+ const confidence = output[maxIdx];
353
+ const className = this.mobileNetClassMap[maxIdx.toString()] || "unknown";
354
+ this.debugLog('📄 Document classification result:', {
355
+ class: className,
356
+ confidence: confidence.toFixed(3),
357
+ classIndex: maxIdx
358
+ });
359
+ return {
360
+ class: className,
361
+ confidence: confidence,
362
+ classIndex: maxIdx
363
+ };
364
+ }
365
+ catch (error) {
366
+ this.debugLog('❌ Error classifying document:', error);
367
+ return null;
368
+ }
369
+ }
287
370
  cleanup() {
288
371
  if (this.animationId) {
289
372
  cancelAnimationFrame(this.animationId);
@@ -311,11 +394,16 @@ export class JaakStamps {
311
394
  this.captureCtx = undefined;
312
395
  this.captureCanvas = undefined;
313
396
  }
314
- // Disposed ONNX session
397
+ // Disposed ONNX sessions
315
398
  if (this.session) {
316
399
  this.session.release?.();
317
400
  this.session = undefined;
318
401
  }
402
+ if (this.mobileNetSession) {
403
+ this.mobileNetSession.release?.();
404
+ this.mobileNetSession = undefined;
405
+ }
406
+ this.mobileNetClassMap = undefined;
319
407
  this.isModelPreloaded = false;
320
408
  this.debugLog('🧹 Canvas pool cleaned up');
321
409
  }
@@ -446,18 +534,22 @@ export class JaakStamps {
446
534
  // Check if model is already preloaded
447
535
  if (!this.session) {
448
536
  this.isLoading = true;
449
- this.statusMessage = "Cargando modelo de detección...";
537
+ this.statusMessage = "Cargando modelos...";
450
538
  this.statusColor = "#007bff";
451
539
  const modelPath = this.MODEL_PATH;
452
- this.debugLog('🤖 Loading model:', modelPath);
540
+ this.debugLog('🤖 Loading detection model:', modelPath);
453
541
  const sessionOptions = this.getSessionOptions();
454
542
  this.session = await window.ort.InferenceSession.create(modelPath, sessionOptions);
543
+ // Load MobileNet model if classification is enabled and not already loaded
544
+ if (this.useDocumentClassification && !this.mobileNetSession) {
545
+ await this.loadMobileNetModel();
546
+ }
455
547
  this.isModelPreloaded = true;
456
548
  this.emitReadyEvent();
457
549
  }
458
550
  else {
459
- this.debugLog('🚀 Using preloaded model');
460
- this.statusMessage = "Usando modelo precargado...";
551
+ this.debugLog('🚀 Using preloaded models');
552
+ this.statusMessage = "Usando modelos precargados...";
461
553
  this.statusColor = "#007bff";
462
554
  }
463
555
  this.statusMessage = "Configurando cámara...";
@@ -676,7 +768,9 @@ export class JaakStamps {
676
768
  corners?.forEach(corner => corner.classList.add('perfect-match'));
677
769
  if (!this.hasScreenshotTaken) {
678
770
  this.lastDetectedBox = bestBox;
679
- this.takeScreenshot();
771
+ this.takeScreenshot().catch(error => {
772
+ this.debugLog('❌ Error taking screenshot:', error);
773
+ });
680
774
  this.hasScreenshotTaken = true;
681
775
  // Reset para permitir segunda captura
682
776
  setTimeout(() => {
@@ -759,7 +853,7 @@ export class JaakStamps {
759
853
  ctx.strokeRect(x, y, w, h);
760
854
  });
761
855
  }
762
- takeScreenshot() {
856
+ async takeScreenshot() {
763
857
  if (!this.videoRef || !this.lastDetectedBox)
764
858
  return;
765
859
  // Activar animación
@@ -793,6 +887,29 @@ export class JaakStamps {
793
887
  // Captura del frente usando canvas reutilizado
794
888
  this.capturedFullFrame = this.captureCanvas.toDataURL('image/png');
795
889
  this.capturedCroppedId = croppedCanvas.toDataURL('image/png');
890
+ // Check if document classification is enabled
891
+ if (this.useDocumentClassification) {
892
+ // Load MobileNet model if not loaded yet (lazy loading)
893
+ if (!this.mobileNetSession) {
894
+ try {
895
+ await this.loadMobileNetModel();
896
+ }
897
+ catch (error) {
898
+ this.debugLog('⚠️ Failed to load classification model, continuing without classification:', error);
899
+ }
900
+ }
901
+ // Classify the cropped document if model is available
902
+ if (this.mobileNetSession) {
903
+ const classification = await this.classifyDocument(croppedCanvas);
904
+ if (classification && classification.class === 'passport') {
905
+ // If it's a passport, skip back capture since passports don't have a back side
906
+ this.debugLog('📄 Passport detected - skipping back capture');
907
+ this.completeProcess(true);
908
+ return;
909
+ }
910
+ }
911
+ }
912
+ // For other IDs, continue with normal flow (back capture)
796
913
  this.captureStep = 'back';
797
914
  this.statusMessage = "Voltee la identificación y muestre la parte trasera";
798
915
  this.statusColor = "#007bff";
@@ -922,7 +1039,7 @@ export class JaakStamps {
922
1039
  this.cleanup();
923
1040
  }
924
1041
  render() {
925
- return (h("div", { key: '7cd80970b12ba60949ff34b5a9405b9782267918', class: "detector-container" }, h("div", { key: '0e66e46a524bd37f60f30504a270f72b64a97fc1', class: "video-container" }, h("video", { key: '0c1b0719c626659e54bec8ce3383f50d990c32c1', ref: el => this.videoRef = el, autoplay: true, muted: true, playsinline: true, class: this.shouldMirrorVideo ? 'mirror' : '', style: { display: this.isVideoActive ? 'block' : 'none' } }), h("canvas", { key: '09fc3a2d511e48186099a0ff5d3d6378f15fe1c4', ref: el => this.canvasRef = el, class: this.shouldMirrorVideo ? 'mirror' : '' }), this.isMaskReady && (h("div", { key: '70a31b04c9543fc0834a848013daf06c654e4194', class: "overlay-mask" }, h("div", { key: '050dcdaa59b028b5c379de310a80ca7191bf8af8', class: "card-outline" }, h("div", { key: 'adef93f2e6b16b308dc5d67469f35c2025ca160f', class: "side side-top" }), h("div", { key: '4900bed8cc2607f92163a25bbf3c0d3a272b9840', class: "side side-right" }), h("div", { key: '11fc9da6cfe04b993bf4a72413da2344122b21e4', class: "side side-bottom" }), h("div", { key: 'ed95ac2578ba045e003d61144aecd09ef4f01561', class: "side side-left" }), h("div", { key: '37eec8aad0a0d449174d52caab7aec5a25d11731', class: "corner corner-tl" }), h("div", { key: 'c7247e71d8936d0a30f415f73377713a8c457420', class: "corner corner-tr" }), h("div", { key: 'e131e0c3bd66068ae1d33cd7211edafd92dfbe62', class: "corner corner-bl" }), h("div", { key: '89234ddcde735501907276e10287af983033ec85', class: "corner corner-br" }), !this.showFlipAnimation && !this.showSuccessAnimation && (h("div", { key: '4b2543dd05f173b3ee4e3053ed7b62d4e7b75e63', class: "guide-text" }, this.statusMessage))), this.captureStep === 'back' && !this.showFlipAnimation && !this.showSuccessAnimation && (h("button", { key: 'e7e2b2b1486f7024fc2d42073313833bca88e315', class: "skip-button", onClick: () => this.skipBackCapture(), type: "button" }, "Saltar reverso")))), this.isCapturing && (h("div", { key: '6ca8283fe3eb1c5b2700c0d313afb51c2db3ea7f', class: "capture-animation" })), this.showFlipAnimation && (h("div", { key: 'eaed1200385a98a8750cdaef2dad9f8ff63914f3', class: "flip-animation" }, h("div", { key: 'dbe9bfc929fa8b13cadc60cdece603c48dbcbd4c', class: "id-card-icon" }), h("div", { key: '82b9de0910a993b6cf3af7ecdf43de41d495c181', class: "flip-text" }, "\u00A1Voltea tu identificaci\u00F3n!"))), this.showSuccessAnimation && (h("div", { key: '92ef47686b0fbf91f6a3f8c3c3f23a78a6fc76f8', class: "success-animation" }, h("div", { key: '35aeb41dce144fc844d7447a958473834f6b0843', class: "check-icon" }), h("div", { key: '2ba06161a8d665394803ce8cc90320b154048c0b', class: "success-text" }, "\u00A1Proceso completado!"))), this.isLoading && (h("div", { key: '8abc4f5724ca6543698f7ca9116ccfc9c042edbd', class: "loading-overlay" }, h("div", { key: '5c8f73bb7c30c6a4c304e96e4db2bf75dc89b89e', class: "loading-spinner" }), h("div", { key: '8f5665f21be98ddccb9d9d2c6412e4edf4571a51', class: "loading-text" }, this.statusMessage))), h("div", { key: '00316bee3c56f07b9771c400f5b84b7e14dca4af', class: "watermark" }, h("img", { key: 'd7f58ffa993405912f8aa26386f39caa72e7a000', src: "https://storage.googleapis.com/jaak-static/commons/powered-by-jaak.png", alt: "Powered by Jaak" })))));
1042
+ return (h("div", { key: '42591479495df221b101555198a23288d7d765b0', class: "detector-container" }, h("div", { key: '2b9ab48572aa2fb5542b8a8dc1b70a0f2a6eca89', class: "video-container" }, h("video", { key: 'b9e84f2eb67fa6f7f158e19b90871a1eebee624b', ref: el => this.videoRef = el, autoplay: true, muted: true, playsinline: true, class: this.shouldMirrorVideo ? 'mirror' : '', style: { display: this.isVideoActive ? 'block' : 'none' } }), h("canvas", { key: 'a82fc4826e220e696799b07b3510456f174a6628', ref: el => this.canvasRef = el, class: this.shouldMirrorVideo ? 'mirror' : '' }), this.isMaskReady && (h("div", { key: '9b3b838f99030e3dfa760fa0dd7c89ff24f26ed9', class: "overlay-mask" }, h("div", { key: 'f6a3b1ac2f12c1fb081f6b5eb853e627040396f4', class: "card-outline" }, h("div", { key: '6626259249ac5e0fd6ba665ab802af1ac2f9fc17', class: "side side-top" }), h("div", { key: '7a3195c96b2406b40b122f4267c2a251614cdede', class: "side side-right" }), h("div", { key: 'ece78305cfb083ada1d44de1d7d5226730879cc3', class: "side side-bottom" }), h("div", { key: '107468c583bbb01dd46c36c096e49be8e0b574ba', class: "side side-left" }), h("div", { key: 'c590f13b80c5e37c0ac0c68cfc2e2904b4345ebc', class: "corner corner-tl" }), h("div", { key: 'ab1e5aab179068ad1bc7b58ef3628d9618a877a8', class: "corner corner-tr" }), h("div", { key: '3a1db6cf1105bd3a2dfe7d4b998e79f6e40625ec', class: "corner corner-bl" }), h("div", { key: '5f9bc4e4557e5539335894160dccbaa2a6d6ac2d', class: "corner corner-br" }), !this.showFlipAnimation && !this.showSuccessAnimation && (h("div", { key: '8226d6721b0ae800f935e978a93113bd3b99656a', class: "guide-text" }, this.statusMessage))), this.captureStep === 'back' && !this.showFlipAnimation && !this.showSuccessAnimation && (h("button", { key: '16cec7ca4a80ba0db1ace768826da17063eddb3e', class: "skip-button", onClick: () => this.skipBackCapture(), type: "button" }, "Saltar reverso")))), this.isCapturing && (h("div", { key: 'bfdca0059eb50d37a77e8e43ef963ba7ed953edf', class: "capture-animation" })), this.showFlipAnimation && (h("div", { key: 'a1ad4b4f884d7d9f6090f148ddd6df19250073dd', class: "flip-animation" }, h("div", { key: 'e159ce385824dc049a6e9409d3747105c6f0a08e', class: "id-card-icon" }), h("div", { key: '6fcff650a3c7fdb16f8d327750e06055b9a93d0d', class: "flip-text" }, "\u00A1Voltea tu identificaci\u00F3n!"))), this.showSuccessAnimation && (h("div", { key: 'c4b76ecb19c65969676e5156689c2c0b1c9a584e', class: "success-animation" }, h("div", { key: '12203a8fe7ee878089b3dda1d1142cde5489eed0', class: "check-icon" }), h("div", { key: 'a2d41c8944873d33106994b8d56c3ee1ee1ae9fc', class: "success-text" }, "\u00A1Proceso completado!"))), this.isLoading && (h("div", { key: 'a155cd999fa40c35fa7b391cde7c1026edb250a7', class: "loading-overlay" }, h("div", { key: 'ccba76c5d4a472158c28bc564ba95bd65829b58d', class: "loading-spinner" }), h("div", { key: '23168f9bdcf4e993539288ff5703acaed13bb16a', class: "loading-text" }, this.statusMessage))), h("div", { key: '0107b82ad19726c1bab131e17892a46b04e08cfd', class: "watermark" }, h("img", { key: 'ad8831a6f77fa61805f22e3a27f5c5619ac1cc0e', src: "https://storage.googleapis.com/jaak-static/commons/powered-by-jaak.png", alt: "Powered by Jaak" })))));
926
1043
  }
927
1044
  static get is() { return "jaak-stamps"; }
928
1045
  static get encapsulation() { return "shadow"; }
@@ -1017,6 +1134,26 @@ export class JaakStamps {
1017
1134
  "setter": false,
1018
1135
  "reflect": false,
1019
1136
  "defaultValue": "0"
1137
+ },
1138
+ "useDocumentClassification": {
1139
+ "type": "boolean",
1140
+ "attribute": "use-document-classification",
1141
+ "mutable": false,
1142
+ "complexType": {
1143
+ "original": "boolean",
1144
+ "resolved": "boolean",
1145
+ "references": {}
1146
+ },
1147
+ "required": false,
1148
+ "optional": false,
1149
+ "docs": {
1150
+ "tags": [],
1151
+ "text": ""
1152
+ },
1153
+ "getter": false,
1154
+ "setter": false,
1155
+ "reflect": false,
1156
+ "defaultValue": "false"
1020
1157
  }
1021
1158
  };
1022
1159
  }