@jaak.ai/stamps 2.0.0-dev.19 → 2.0.0-dev.20

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.
@@ -27,6 +27,7 @@ export class JaakStamps {
27
27
  isDetectionPaused = false;
28
28
  isLoading = false;
29
29
  isModelPreloaded = false;
30
+ isMaskReady = false;
30
31
  videoRef;
31
32
  canvasRef;
32
33
  session;
@@ -50,8 +51,8 @@ export class JaakStamps {
50
51
  MODEL_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/detector-nano.onnx";
51
52
  INPUT_SIZE = 320;
52
53
  CONFIDENCE_THRESHOLD = 0.6;
53
- MASK_WIDTH = 280;
54
- MASK_HEIGHT = 176;
54
+ // ISO/IEC 7810 ID-1 standard dimensions (85.60mm x 53.98mm)
55
+ ID1_ASPECT_RATIO = 85.60 / 53.98; // 1.5863320574...
55
56
  debugLog(...args) {
56
57
  if (this.debug) {
57
58
  console.log(...args);
@@ -85,6 +86,91 @@ export class JaakStamps {
85
86
  }
86
87
  // Initialize canvas pool for better performance
87
88
  this.initializeCanvasPool();
89
+ this.setupResizeObserver();
90
+ }
91
+ setupResizeObserver() {
92
+ if ('ResizeObserver' in window && this.canvasRef) {
93
+ const resizeObserver = new ResizeObserver(() => {
94
+ this.handleResize();
95
+ });
96
+ resizeObserver.observe(this.canvasRef.parentElement);
97
+ }
98
+ }
99
+ handleResize() {
100
+ if (this.canvasRef) {
101
+ const container = this.canvasRef.parentElement;
102
+ const rect = container.getBoundingClientRect();
103
+ // Set canvas size to match container
104
+ this.canvasRef.width = rect.width;
105
+ this.canvasRef.height = rect.height;
106
+ // Update mask positioning based on container and video dimensions
107
+ this.updateMaskDimensions(rect);
108
+ this.debugLog('📐 Canvas resized:', { width: rect.width, height: rect.height });
109
+ }
110
+ }
111
+ updateMaskDimensions(containerRect) {
112
+ if (!this.videoRef)
113
+ return;
114
+ const videoWidth = this.videoRef.videoWidth;
115
+ const videoHeight = this.videoRef.videoHeight;
116
+ if (videoWidth === 0 || videoHeight === 0)
117
+ return;
118
+ // Calculate video aspect ratio and container aspect ratio
119
+ const videoAspectRatio = videoWidth / videoHeight;
120
+ const containerAspectRatio = containerRect.width / containerRect.height;
121
+ // Determine how video fits in container (letterboxed or pillarboxed)
122
+ let displayedVideoWidth, displayedVideoHeight;
123
+ let videoOffsetX = 0, videoOffsetY = 0;
124
+ if (videoAspectRatio > containerAspectRatio) {
125
+ // Video is wider - letterboxed (black bars top/bottom)
126
+ displayedVideoWidth = containerRect.width;
127
+ displayedVideoHeight = containerRect.width / videoAspectRatio;
128
+ videoOffsetY = (containerRect.height - displayedVideoHeight) / 2;
129
+ }
130
+ else {
131
+ // Video is taller - pillarboxed (black bars left/right)
132
+ displayedVideoHeight = containerRect.height;
133
+ displayedVideoWidth = containerRect.height * videoAspectRatio;
134
+ videoOffsetX = (containerRect.width - displayedVideoWidth) / 2;
135
+ }
136
+ // Calculate maximum possible mask size while preserving exact ID-1 aspect ratio
137
+ // Determine the limiting dimension based on video orientation and ID-1 proportions
138
+ // Calculate what would be the max width if limited by video height
139
+ const maxWidthByHeight = displayedVideoHeight * this.ID1_ASPECT_RATIO;
140
+ let maskWidthInVideo, maskHeightInVideo;
141
+ if (maxWidthByHeight <= displayedVideoWidth) {
142
+ // Video height is the limiting factor
143
+ maskHeightInVideo = displayedVideoHeight * 0.9; // Use 90% of available height
144
+ maskWidthInVideo = maskHeightInVideo * this.ID1_ASPECT_RATIO;
145
+ }
146
+ else {
147
+ // Video width is the limiting factor
148
+ maskWidthInVideo = displayedVideoWidth * 0.9; // Use 90% of available width
149
+ maskHeightInVideo = maskWidthInVideo / this.ID1_ASPECT_RATIO;
150
+ }
151
+ // Convert to percentages of the container
152
+ const maskWidthPercent = (maskWidthInVideo / containerRect.width) * 100;
153
+ const maskHeightPercent = (maskHeightInVideo / containerRect.height) * 100;
154
+ // Calculate the center position of the displayed video area
155
+ const videoCenterX = videoOffsetX + (displayedVideoWidth / 2);
156
+ const videoCenterY = videoOffsetY + (displayedVideoHeight / 2);
157
+ // Convert to percentages of the container
158
+ const videoCenterXPercent = (videoCenterX / containerRect.width) * 100;
159
+ const videoCenterYPercent = (videoCenterY / containerRect.height) * 100;
160
+ // Update CSS custom properties for mask sizing and positioning
161
+ this.el.style.setProperty('--mask-width', `${maskWidthPercent}%`);
162
+ this.el.style.setProperty('--mask-height', `${maskHeightPercent}%`);
163
+ this.el.style.setProperty('--mask-center-x', `${videoCenterXPercent}%`);
164
+ this.el.style.setProperty('--mask-center-y', `${videoCenterYPercent}%`);
165
+ // Mark mask as ready now that dimensions are calculated
166
+ this.isMaskReady = true;
167
+ this.debugLog('🎯 Mask dimensions updated:', {
168
+ video: { width: videoWidth, height: videoHeight },
169
+ displayed: { width: displayedVideoWidth, height: displayedVideoHeight },
170
+ mask: { widthPercent: maskWidthPercent, heightPercent: maskHeightPercent },
171
+ center: { x: videoCenterXPercent, y: videoCenterYPercent },
172
+ offset: { x: videoOffsetX, y: videoOffsetY }
173
+ });
88
174
  }
89
175
  initializeCanvasPool() {
90
176
  // Preprocess canvas - reused for every inference
@@ -216,14 +302,45 @@ export class JaakStamps {
216
302
  this.isModelPreloaded = false;
217
303
  this.debugLog('🧹 Canvas pool cleaned up');
218
304
  }
305
+ async getMaxResolution() {
306
+ try {
307
+ // Primero obtén un stream básico para acceder a las capacidades
308
+ const tempStream = await navigator.mediaDevices.getUserMedia({
309
+ video: { facingMode: "environment" }
310
+ });
311
+ const videoTrack = tempStream.getVideoTracks()[0];
312
+ const capabilities = videoTrack.getCapabilities();
313
+ // Detener el stream temporal
314
+ tempStream.getTracks().forEach(track => track.stop());
315
+ // Construir restricciones con la resolución máxima
316
+ const constraints = {
317
+ facingMode: "environment"
318
+ };
319
+ if (capabilities.width && capabilities.height) {
320
+ constraints.width = { ideal: capabilities.width.max };
321
+ constraints.height = { ideal: capabilities.height.max };
322
+ this.debugLog('📐 Max resolution capabilities:', {
323
+ width: capabilities.width.max,
324
+ height: capabilities.height.max
325
+ });
326
+ }
327
+ return constraints;
328
+ }
329
+ catch (err) {
330
+ this.debugLog('⚠️ Could not get capabilities, using fallback');
331
+ // Fallback para dispositivos que no soportan getCapabilities
332
+ return {
333
+ facingMode: "environment",
334
+ width: { ideal: 1920 },
335
+ height: { ideal: 1080 }
336
+ };
337
+ }
338
+ }
219
339
  async setupCamera() {
220
340
  try {
341
+ const constraints = await this.getMaxResolution();
221
342
  const stream = await navigator.mediaDevices.getUserMedia({
222
- video: {
223
- width: { exact: 640 },
224
- height: { exact: 640 },
225
- facingMode: "environment"
226
- },
343
+ video: constraints,
227
344
  audio: false
228
345
  });
229
346
  if (this.videoRef) {
@@ -238,6 +355,12 @@ export class JaakStamps {
238
355
  this.videoRef.onloadedmetadata = async () => {
239
356
  await this.videoRef.play();
240
357
  this.isVideoActive = true;
358
+ // Update mask dimensions once video is loaded
359
+ if (this.canvasRef) {
360
+ const container = this.canvasRef.parentElement;
361
+ const rect = container.getBoundingClientRect();
362
+ this.updateMaskDimensions(rect);
363
+ }
241
364
  resolve();
242
365
  };
243
366
  });
@@ -415,13 +538,57 @@ export class JaakStamps {
415
538
  return isCentered && isGoodSize;
416
539
  }
417
540
  checkSideAlignment(box) {
418
- // Obtener las coordenadas del marco de referencia (máscara)
541
+ if (!this.videoRef)
542
+ return { top: false, right: false, bottom: false, left: false };
543
+ // Get video dimensions to calculate actual mask boundaries in model space
544
+ const videoWidth = this.videoRef.videoWidth;
545
+ const videoHeight = this.videoRef.videoHeight;
546
+ if (videoWidth === 0 || videoHeight === 0) {
547
+ return { top: false, right: false, bottom: false, left: false };
548
+ }
549
+ // Calculate video aspect ratio
550
+ const videoAspectRatio = videoWidth / videoHeight;
551
+ // The model sees video stretched to 320x320, but we need to calculate where
552
+ // the mask should be in this distorted space to match the visual mask
553
+ // In the visual display, we calculate mask size based on the limiting dimension
554
+ // We need to replicate this logic but account for the distortion in model space
555
+ // Calculate the "display" dimensions (what the visual mask calculations use)
556
+ const containerAspectRatio = 1; // Assume square container for simplicity
557
+ let displayedVideoWidth, displayedVideoHeight;
558
+ if (videoAspectRatio > containerAspectRatio) {
559
+ // Video is wider - letterboxed in display
560
+ displayedVideoWidth = this.INPUT_SIZE;
561
+ displayedVideoHeight = this.INPUT_SIZE / videoAspectRatio;
562
+ }
563
+ else {
564
+ // Video is taller - pillarboxed in display
565
+ displayedVideoHeight = this.INPUT_SIZE;
566
+ displayedVideoWidth = this.INPUT_SIZE * videoAspectRatio;
567
+ }
568
+ // Calculate mask dimensions using the same logic as updateMaskDimensions
569
+ const maxWidthByHeight = displayedVideoHeight * this.ID1_ASPECT_RATIO;
570
+ let visualMaskWidth, visualMaskHeight;
571
+ if (maxWidthByHeight <= displayedVideoWidth) {
572
+ // Video height is the limiting factor
573
+ visualMaskHeight = displayedVideoHeight * 0.9;
574
+ visualMaskWidth = visualMaskHeight * this.ID1_ASPECT_RATIO;
575
+ }
576
+ else {
577
+ // Video width is the limiting factor
578
+ visualMaskWidth = displayedVideoWidth * 0.9;
579
+ visualMaskHeight = visualMaskWidth / this.ID1_ASPECT_RATIO;
580
+ }
581
+ // Now map these visual mask dimensions to the distorted model space
582
+ // The model stretches video to 320x320, so we need to apply the inverse transformation
583
+ const modelMaskWidth = visualMaskWidth * (this.INPUT_SIZE / displayedVideoWidth);
584
+ const modelMaskHeight = visualMaskHeight * (this.INPUT_SIZE / displayedVideoHeight);
585
+ // Calculate mask boundaries in model coordinate system (always centered in 320x320)
419
586
  const maskCenterX = this.INPUT_SIZE / 2;
420
587
  const maskCenterY = this.INPUT_SIZE / 2;
421
- const maskLeft = maskCenterX - (this.MASK_WIDTH / 2);
422
- const maskRight = maskCenterX + (this.MASK_WIDTH / 2);
423
- const maskTop = maskCenterY - (this.MASK_HEIGHT / 2);
424
- const maskBottom = maskCenterY + (this.MASK_HEIGHT / 2);
588
+ const maskLeft = maskCenterX - (modelMaskWidth / 2);
589
+ const maskRight = maskCenterX + (modelMaskWidth / 2);
590
+ const maskTop = maskCenterY - (modelMaskHeight / 2);
591
+ const maskBottom = maskCenterY + (modelMaskHeight / 2);
425
592
  // Obtener las coordenadas del documento detectado
426
593
  let docLeft = box.x;
427
594
  let docRight = box.x + box.w;
@@ -518,12 +685,45 @@ export class JaakStamps {
518
685
  }
519
686
  }
520
687
  drawDetections(ctx, boxes) {
521
- // Scale factor to convert from model coordinates (320x320) to canvas coordinates (640x640)
522
- const scale = this.canvasRef.width / this.INPUT_SIZE;
688
+ if (!this.videoRef)
689
+ return;
690
+ // Get video and container dimensions
691
+ const videoWidth = this.videoRef.videoWidth;
692
+ const videoHeight = this.videoRef.videoHeight;
693
+ const containerWidth = this.canvasRef.width;
694
+ const containerHeight = this.canvasRef.height;
695
+ if (videoWidth === 0 || videoHeight === 0)
696
+ return;
697
+ // Calculate video aspect ratio and container aspect ratio
698
+ const videoAspectRatio = videoWidth / videoHeight;
699
+ const containerAspectRatio = containerWidth / containerHeight;
700
+ // Determine how video fits in container (same logic as updateMaskDimensions)
701
+ let displayedVideoWidth, displayedVideoHeight;
702
+ let videoOffsetX = 0, videoOffsetY = 0;
703
+ if (videoAspectRatio > containerAspectRatio) {
704
+ // Video is wider - letterboxed (black bars top/bottom)
705
+ displayedVideoWidth = containerWidth;
706
+ displayedVideoHeight = containerWidth / videoAspectRatio;
707
+ videoOffsetY = (containerHeight - displayedVideoHeight) / 2;
708
+ }
709
+ else {
710
+ // Video is taller - pillarboxed (black bars left/right)
711
+ displayedVideoHeight = containerHeight;
712
+ displayedVideoWidth = containerHeight * videoAspectRatio;
713
+ videoOffsetX = (containerWidth - displayedVideoWidth) / 2;
714
+ }
715
+ // Scale factor from model coordinates (320x320) to displayed video area
716
+ const scaleX = displayedVideoWidth / this.INPUT_SIZE;
717
+ const scaleY = displayedVideoHeight / this.INPUT_SIZE;
523
718
  boxes.forEach(det => {
719
+ // Convert model coordinates to displayed video coordinates
720
+ const x = det.x * scaleX + videoOffsetX;
721
+ const y = det.y * scaleY + videoOffsetY;
722
+ const w = det.w * scaleX;
723
+ const h = det.h * scaleY;
524
724
  ctx.strokeStyle = "#32406C";
525
725
  ctx.lineWidth = 2;
526
- ctx.strokeRect(det.x * scale, det.y * scale, det.w * scale, det.h * scale);
726
+ ctx.strokeRect(x, y, w, h);
527
727
  });
528
728
  }
529
729
  takeScreenshot() {
@@ -689,7 +889,7 @@ export class JaakStamps {
689
889
  this.cleanup();
690
890
  }
691
891
  render() {
692
- return (h("div", { key: '788a458497c2303875f8accea1e0cc579379ff94', class: "detector-container" }, h("div", { key: 'ed491d2fb8399f7236ab492a100932518455be79', class: "video-container" }, h("video", { key: '8187f838c75776432dc48db4cd9bfe4ee350830d', ref: el => this.videoRef = el, width: "640", height: "640", autoplay: true, muted: true, playsinline: true, class: this.shouldMirrorVideo ? 'mirror' : '', style: { display: this.isVideoActive ? 'block' : 'none' } }), h("canvas", { key: '8cc31c45881d4f23c56c5ed246c9e59e30e0a782', ref: el => this.canvasRef = el, width: "640", height: "640", class: this.shouldMirrorVideo ? 'mirror' : '' }), h("div", { key: '54441a535098860601a7ef4f1b31a0d2a40a911b', class: "overlay-mask" }, h("div", { key: 'f9272bcb85c849e3b3170a0062c43e64359f112d', class: "card-outline" }, h("div", { key: '8a260667206ce2fecac262f2b5e81d0ebdccbb3c', class: "side side-top" }), h("div", { key: 'c9c0d25087aa8332027815829fc7bbd937f0596b', class: "side side-right" }), h("div", { key: '39394f06936ff01a525c83343ec05e6161611899', class: "side side-bottom" }), h("div", { key: '11799336a4b6f72d6042c02206626d947422ef0f', class: "side side-left" }), h("div", { key: '93d5636f85514d3fff76415ed806e6845c6e7fe3', class: "corner corner-tl" }), h("div", { key: '3bfc09f919744d1d04dcb0c08fb923879dc217c9', class: "corner corner-tr" }), h("div", { key: '6d9fa1395a606960597bed65301679dcd12fc8bb', class: "corner corner-bl" }), h("div", { key: '4819402bd891c4a8d581e1e10449850b3a580e96', class: "corner corner-br" }), !this.showFlipAnimation && !this.showSuccessAnimation && (h("div", { key: '26a57343cfccea003c3edc65c5c53ac97b16de53', class: "guide-text" }, this.statusMessage))), this.captureStep === 'back' && !this.showFlipAnimation && !this.showSuccessAnimation && (h("button", { key: '641d63e9f9e76fa81b2f770a1036b337f54a2c23', class: "skip-button", onClick: () => this.skipBackCapture(), type: "button" }, "Saltar reverso"))), this.isCapturing && (h("div", { key: 'e7ab12f7ad092d233e19de5bc7a3e424b6bc70c3', class: "capture-animation" })), this.showFlipAnimation && (h("div", { key: 'dddbe58a92172e9ab1e7002a3b4310d8e552926f', class: "flip-animation" }, h("div", { key: 'c2f49ee888573d873d67e8d4871bb6b1d8f2bc8d', class: "id-card-icon" }), h("div", { key: '6e71e7a9a3a85bb4f9252955ea220e64a25ae479', class: "flip-text" }, "\u00A1Voltea tu identificaci\u00F3n!"))), this.showSuccessAnimation && (h("div", { key: 'c9d8fd42711764ea197c7d13ef3d90e34b559e64', class: "success-animation" }, h("div", { key: '50dd978c2c501b53591346d75dac018006f42992', class: "check-icon" }), h("div", { key: 'ad25bc5811d435ceb7a1e9791cd3fc5dbd5c5227', class: "success-text" }, "\u00A1Proceso completado!"))), this.isLoading && (h("div", { key: '9ac52c0754d165f8141989b17398a89b21790976', class: "loading-overlay" }, h("div", { key: 'ccffac3b53db3ac4beddbf43ee8cff693e29fd73', class: "loading-spinner" }), h("div", { key: '708c4aad55e18be3fc77fe3f169430475dce7ee1', class: "loading-text" }, this.statusMessage))), h("div", { key: 'e0addbcea4b176772de10a608c6de4242da2a7f3', class: "watermark" }, h("img", { key: 'd3d2a989a01e2a64a100ccdc43a027f969d4b8b1', src: "https://storage.googleapis.com/jaak-static/commons/powered-by-jaak.png", alt: "Powered by Jaak" })))));
892
+ return (h("div", { key: 'c95e9dee4b79561384fe1777b99a4cc4fa92bdbe', class: "detector-container" }, h("div", { key: 'a002fed79f5fd475a636818535a4a65324ec3fbf', class: "video-container" }, h("video", { key: '1a2a79193aac789c53b899da06e24bc4fe71891e', ref: el => this.videoRef = el, autoplay: true, muted: true, playsinline: true, class: this.shouldMirrorVideo ? 'mirror' : '', style: { display: this.isVideoActive ? 'block' : 'none' } }), h("canvas", { key: '1771841686ff849a70d3b8e23034a44880494c7e', ref: el => this.canvasRef = el, class: this.shouldMirrorVideo ? 'mirror' : '' }), this.isMaskReady && (h("div", { key: '29563822f3ec6c0d00ef20fb2da346b2d9c24175', class: "overlay-mask" }, h("div", { key: 'e70ec55cfc25697e62981ffbfb5c78dc32821daa', class: "card-outline" }, h("div", { key: '1ae7dae4facd6971a3031cec4992f657192855da', class: "side side-top" }), h("div", { key: 'efec73ad8e754a279443a93f05c7e416476540c3', class: "side side-right" }), h("div", { key: '42dca6ffed2ac93bffd48877b3ef05e11aa16bac', class: "side side-bottom" }), h("div", { key: 'e872078e654987be4d02600b866f39767bff6c91', class: "side side-left" }), h("div", { key: '71dc7e353e6a11ddc6e300ddd6a96c483dc8e43d', class: "corner corner-tl" }), h("div", { key: 'e380c3c0526f8c6c5f6caefc2ea1feabc17b0b4a', class: "corner corner-tr" }), h("div", { key: 'de4720a6418ec86931c24a0542cffdbba8211b22', class: "corner corner-bl" }), h("div", { key: '338a487a89c9c4d1662428fdc73cdb355766149c', class: "corner corner-br" }), !this.showFlipAnimation && !this.showSuccessAnimation && (h("div", { key: '34190a47c348dd00d48467c4edfd80efaf0494cd', class: "guide-text" }, this.statusMessage))), this.captureStep === 'back' && !this.showFlipAnimation && !this.showSuccessAnimation && (h("button", { key: 'c585163a9db511b461d575594b2c8f5a79cbc1d2', class: "skip-button", onClick: () => this.skipBackCapture(), type: "button" }, "Saltar reverso")))), this.isCapturing && (h("div", { key: 'bc56a29c90d7177cad35c930ed54304bb20f700e', class: "capture-animation" })), this.showFlipAnimation && (h("div", { key: '1d636a6dde80ec90f6db7868a0abd163af8a8836', class: "flip-animation" }, h("div", { key: '95a78fbba1f340d8a56b24a9dda4a4c888cc2d44', class: "id-card-icon" }), h("div", { key: 'bb8e11d34b31cb0d28c2b1f2d02a1cc85b8dd1b3', class: "flip-text" }, "\u00A1Voltea tu identificaci\u00F3n!"))), this.showSuccessAnimation && (h("div", { key: 'ae2ce6604dccccb27995cbcc0ba0635c36125e9f', class: "success-animation" }, h("div", { key: '713cdfca5618225281106ed9da9c30c7f00d2f96', class: "check-icon" }), h("div", { key: '9ac255f66474d6a0d546c4f28dd1464dc1db199c', class: "success-text" }, "\u00A1Proceso completado!"))), this.isLoading && (h("div", { key: 'f3d08c1ba7596c70b3184b6023e4c330cadf395e', class: "loading-overlay" }, h("div", { key: '989ebafd27b84d6e86483569fb28a2df3dc587d5', class: "loading-spinner" }), h("div", { key: 'b14a356fde939b9bd52733bafaba762e5c5b2503', class: "loading-text" }, this.statusMessage))), h("div", { key: '3ff6690dc129f0981eb21de785c631c1221dbd5b', class: "watermark" }, h("img", { key: '67ad3a5d54c9dd1b36fae6f5857695b8dd1a2447', src: "https://storage.googleapis.com/jaak-static/commons/powered-by-jaak.png", alt: "Powered by Jaak" })))));
693
893
  }
694
894
  static get is() { return "jaak-stamps"; }
695
895
  static get encapsulation() { return "shadow"; }
@@ -765,7 +965,8 @@ export class JaakStamps {
765
965
  "sideAlignment": {},
766
966
  "isDetectionPaused": {},
767
967
  "isLoading": {},
768
- "isModelPreloaded": {}
968
+ "isModelPreloaded": {},
969
+ "isMaskReady": {}
769
970
  };
770
971
  }
771
972
  static get events() {