@jaak.ai/stamps 2.0.0-beta.2 → 2.0.0-beta.4
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.
- package/README.md +216 -212
- package/dist/cjs/{index-DGM9-FNg.js → index-BfhtOB0D.js} +5 -2
- package/dist/cjs/index-BfhtOB0D.js.map +1 -0
- package/dist/cjs/jaak-stamps-webcomponent.cjs.js +2 -2
- package/dist/cjs/jaak-stamps.cjs.entry.js +1860 -783
- package/dist/cjs/jaak-stamps.cjs.entry.js.map +1 -1
- package/dist/cjs/jaak-stamps.entry.cjs.js.map +1 -1
- package/dist/cjs/loader.cjs.js +2 -2
- package/dist/collection/components/my-component/my-component.css +629 -72
- package/dist/collection/components/my-component/my-component.js +896 -781
- package/dist/collection/components/my-component/my-component.js.map +1 -1
- package/dist/collection/services/CameraService.js +453 -0
- package/dist/collection/services/CameraService.js.map +1 -0
- package/dist/collection/services/DetectionService.js +369 -0
- package/dist/collection/services/DetectionService.js.map +1 -0
- package/dist/collection/services/EventBusService.js +42 -0
- package/dist/collection/services/EventBusService.js.map +1 -0
- package/dist/collection/services/LoggerService.js +40 -0
- package/dist/collection/services/LoggerService.js.map +1 -0
- package/dist/collection/services/ServiceContainer.js +66 -0
- package/dist/collection/services/ServiceContainer.js.map +1 -0
- package/dist/collection/services/StateManagerService.js +109 -0
- package/dist/collection/services/StateManagerService.js.map +1 -0
- package/dist/collection/services/factories/DeviceStrategyFactory.js +16 -0
- package/dist/collection/services/factories/DeviceStrategyFactory.js.map +1 -0
- package/dist/collection/services/interfaces/ICameraService.js +2 -0
- package/dist/collection/services/interfaces/ICameraService.js.map +1 -0
- package/dist/collection/services/interfaces/IDetectionService.js +2 -0
- package/dist/collection/services/interfaces/IDetectionService.js.map +1 -0
- package/dist/collection/services/interfaces/IEventBus.js +2 -0
- package/dist/collection/services/interfaces/IEventBus.js.map +1 -0
- package/dist/collection/services/interfaces/ILogger.js +2 -0
- package/dist/collection/services/interfaces/ILogger.js.map +1 -0
- package/dist/collection/services/interfaces/IStateManager.js +2 -0
- package/dist/collection/services/interfaces/IStateManager.js.map +1 -0
- package/dist/collection/services/strategies/HighPerformanceDeviceStrategy.js +30 -0
- package/dist/collection/services/strategies/HighPerformanceDeviceStrategy.js.map +1 -0
- package/dist/collection/services/strategies/IDeviceStrategy.js +2 -0
- package/dist/collection/services/strategies/IDeviceStrategy.js.map +1 -0
- package/dist/collection/services/strategies/LowMemoryDeviceStrategy.js +30 -0
- package/dist/collection/services/strategies/LowMemoryDeviceStrategy.js.map +1 -0
- package/dist/collection/types/component-types.js +2 -0
- package/dist/collection/types/component-types.js.map +1 -0
- package/dist/components/index.js +3 -0
- package/dist/components/index.js.map +1 -1
- package/dist/components/jaak-stamps.js +1873 -797
- package/dist/components/jaak-stamps.js.map +1 -1
- package/dist/esm/{index-DqoVMnc7.js → index-BP1Q4KOg.js} +5 -2
- package/dist/esm/index-BP1Q4KOg.js.map +1 -0
- package/dist/esm/jaak-stamps-webcomponent.js +3 -3
- package/dist/esm/jaak-stamps.entry.js +1860 -783
- package/dist/esm/jaak-stamps.entry.js.map +1 -1
- package/dist/esm/loader.js +3 -3
- package/dist/jaak-stamps-webcomponent/jaak-stamps-webcomponent.esm.js +1 -1
- package/dist/jaak-stamps-webcomponent/jaak-stamps.entry.esm.js.map +1 -1
- package/dist/jaak-stamps-webcomponent/p-10ee2dab.entry.js +2 -0
- package/dist/jaak-stamps-webcomponent/p-10ee2dab.entry.js.map +1 -0
- package/dist/jaak-stamps-webcomponent/p-BP1Q4KOg.js +3 -0
- package/dist/jaak-stamps-webcomponent/p-BP1Q4KOg.js.map +1 -0
- package/dist/types/components/my-component/my-component.d.ts +90 -79
- package/dist/types/components.d.ts +32 -8
- package/dist/types/services/CameraService.d.ts +46 -0
- package/dist/types/services/DetectionService.d.ts +35 -0
- package/dist/types/services/EventBusService.d.ts +9 -0
- package/dist/types/services/LoggerService.d.ts +12 -0
- package/dist/types/services/ServiceContainer.d.ts +27 -0
- package/dist/types/services/StateManagerService.d.ts +15 -0
- package/dist/types/services/factories/DeviceStrategyFactory.d.ts +4 -0
- package/dist/types/services/interfaces/ICameraService.d.ts +34 -0
- package/dist/types/services/interfaces/IDetectionService.d.ts +31 -0
- package/dist/types/services/interfaces/IEventBus.d.ts +16 -0
- package/dist/types/services/interfaces/ILogger.d.ts +8 -0
- package/dist/types/services/interfaces/IStateManager.d.ts +35 -0
- package/dist/types/services/strategies/HighPerformanceDeviceStrategy.d.ts +6 -0
- package/dist/types/services/strategies/IDeviceStrategy.d.ts +21 -0
- package/dist/types/services/strategies/LowMemoryDeviceStrategy.d.ts +6 -0
- package/dist/types/types/component-types.d.ts +36 -0
- package/package.json +3 -3
- package/dist/cjs/index-DGM9-FNg.js.map +0 -1
- package/dist/esm/index-DqoVMnc7.js.map +0 -1
- package/dist/jaak-stamps-webcomponent/p-DqoVMnc7.js +0 -3
- package/dist/jaak-stamps-webcomponent/p-DqoVMnc7.js.map +0 -1
- package/dist/jaak-stamps-webcomponent/p-f1172b06.entry.js +0 -2
- package/dist/jaak-stamps-webcomponent/p-f1172b06.entry.js.map +0 -1
|
@@ -1,8 +1,1149 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var index = require('./index-
|
|
3
|
+
var index = require('./index-BfhtOB0D.js');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
class LoggerService {
|
|
6
|
+
debugMode;
|
|
7
|
+
constructor(debug = false) {
|
|
8
|
+
this.debugMode = debug;
|
|
9
|
+
}
|
|
10
|
+
setDebugMode(debug) {
|
|
11
|
+
this.debugMode = debug;
|
|
12
|
+
}
|
|
13
|
+
info(...args) {
|
|
14
|
+
if (this.debugMode) {
|
|
15
|
+
console.log(`[JAAK-STAMPS] [INFO] [${new Date().toLocaleTimeString()}]`, ...args);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
warn(...args) {
|
|
19
|
+
if (this.debugMode) {
|
|
20
|
+
console.warn(`[JAAK-STAMPS] [WARN] [${new Date().toLocaleTimeString()}]`, ...args);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
error(...args) {
|
|
24
|
+
if (this.debugMode) {
|
|
25
|
+
console.error(`[JAAK-STAMPS] [ERROR] [${new Date().toLocaleTimeString()}]`, ...args);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
debug(...args) {
|
|
29
|
+
if (this.debugMode) {
|
|
30
|
+
console.debug(`[JAAK-STAMPS] [DEBUG] [${new Date().toLocaleTimeString()}]`, ...args);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
state(state, data) {
|
|
34
|
+
if (this.debugMode) {
|
|
35
|
+
console.log(`[JAAK-STAMPS] [STATE] [${new Date().toLocaleTimeString()}] ${state}`, data || '');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
performance(operation, duration) {
|
|
39
|
+
if (this.debugMode) {
|
|
40
|
+
console.log(`[JAAK-STAMPS] [PERF] [${new Date().toLocaleTimeString()}] ${operation}: ${duration}ms`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class EventBusService {
|
|
46
|
+
events = new Map();
|
|
47
|
+
on(event, callback) {
|
|
48
|
+
if (!this.events.has(event)) {
|
|
49
|
+
this.events.set(event, []);
|
|
50
|
+
}
|
|
51
|
+
this.events.get(event).push(callback);
|
|
52
|
+
}
|
|
53
|
+
off(event, callback) {
|
|
54
|
+
const callbacks = this.events.get(event);
|
|
55
|
+
if (callbacks) {
|
|
56
|
+
const index = callbacks.indexOf(callback);
|
|
57
|
+
if (index > -1) {
|
|
58
|
+
callbacks.splice(index, 1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
emit(event, data) {
|
|
63
|
+
const callbacks = this.events.get(event);
|
|
64
|
+
if (callbacks) {
|
|
65
|
+
callbacks.forEach(callback => {
|
|
66
|
+
try {
|
|
67
|
+
callback(data);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
console.error(`Error in event callback for ${event}:`, error);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
once(event, callback) {
|
|
76
|
+
const onceCallback = (data) => {
|
|
77
|
+
callback(data);
|
|
78
|
+
this.off(event, onceCallback);
|
|
79
|
+
};
|
|
80
|
+
this.on(event, onceCallback);
|
|
81
|
+
}
|
|
82
|
+
clear() {
|
|
83
|
+
this.events.clear();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
class StateManagerService {
|
|
88
|
+
eventBus;
|
|
89
|
+
captureState = {
|
|
90
|
+
step: 'front',
|
|
91
|
+
isCapturing: false,
|
|
92
|
+
isDetectionPaused: false,
|
|
93
|
+
isVideoActive: false,
|
|
94
|
+
isLoading: false,
|
|
95
|
+
showFlipAnimation: false,
|
|
96
|
+
showSuccessAnimation: false,
|
|
97
|
+
bestScore: 0,
|
|
98
|
+
hasScreenshotTaken: false
|
|
99
|
+
};
|
|
100
|
+
capturedImages = {
|
|
101
|
+
front: {
|
|
102
|
+
fullFrame: null,
|
|
103
|
+
cropped: null
|
|
104
|
+
},
|
|
105
|
+
back: {
|
|
106
|
+
fullFrame: null,
|
|
107
|
+
cropped: null
|
|
108
|
+
},
|
|
109
|
+
metadata: {
|
|
110
|
+
totalImages: 0,
|
|
111
|
+
processCompleted: false,
|
|
112
|
+
backCaptureSkipped: false
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
constructor(eventBus) {
|
|
116
|
+
this.eventBus = eventBus;
|
|
117
|
+
}
|
|
118
|
+
getCaptureState() {
|
|
119
|
+
return { ...this.captureState };
|
|
120
|
+
}
|
|
121
|
+
updateCaptureState(updates) {
|
|
122
|
+
const previousState = { ...this.captureState };
|
|
123
|
+
this.captureState = { ...this.captureState, ...updates };
|
|
124
|
+
// Emit state change event
|
|
125
|
+
this.eventBus.emit('state-changed', {
|
|
126
|
+
previous: previousState,
|
|
127
|
+
current: this.captureState,
|
|
128
|
+
changes: updates
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
getCapturedImages() {
|
|
132
|
+
return JSON.parse(JSON.stringify(this.capturedImages));
|
|
133
|
+
}
|
|
134
|
+
setCapturedImages(images) {
|
|
135
|
+
this.capturedImages = {
|
|
136
|
+
...this.capturedImages,
|
|
137
|
+
...images,
|
|
138
|
+
metadata: {
|
|
139
|
+
...this.capturedImages.metadata,
|
|
140
|
+
...images.metadata
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
// Update total images count
|
|
144
|
+
let totalImages = 0;
|
|
145
|
+
if (this.capturedImages.front.fullFrame && this.capturedImages.front.cropped) {
|
|
146
|
+
totalImages += 2;
|
|
147
|
+
}
|
|
148
|
+
if (this.capturedImages.back.fullFrame && this.capturedImages.back.cropped) {
|
|
149
|
+
totalImages += 2;
|
|
150
|
+
}
|
|
151
|
+
this.capturedImages.metadata.totalImages = totalImages;
|
|
152
|
+
}
|
|
153
|
+
reset() {
|
|
154
|
+
this.captureState = {
|
|
155
|
+
step: 'front',
|
|
156
|
+
isCapturing: false,
|
|
157
|
+
isDetectionPaused: false,
|
|
158
|
+
isVideoActive: false,
|
|
159
|
+
isLoading: false,
|
|
160
|
+
showFlipAnimation: false,
|
|
161
|
+
showSuccessAnimation: false,
|
|
162
|
+
bestScore: 0,
|
|
163
|
+
hasScreenshotTaken: false
|
|
164
|
+
};
|
|
165
|
+
this.capturedImages = {
|
|
166
|
+
front: {
|
|
167
|
+
fullFrame: null,
|
|
168
|
+
cropped: null
|
|
169
|
+
},
|
|
170
|
+
back: {
|
|
171
|
+
fullFrame: null,
|
|
172
|
+
cropped: null
|
|
173
|
+
},
|
|
174
|
+
metadata: {
|
|
175
|
+
totalImages: 0,
|
|
176
|
+
processCompleted: false,
|
|
177
|
+
backCaptureSkipped: false
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
this.eventBus.emit('state-changed', {
|
|
181
|
+
previous: null,
|
|
182
|
+
current: this.captureState,
|
|
183
|
+
changes: { reset: true }
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
isProcessCompleted() {
|
|
187
|
+
return this.captureState.step === 'completed';
|
|
188
|
+
}
|
|
189
|
+
canProceedToBack() {
|
|
190
|
+
return this.captureState.step === 'front' &&
|
|
191
|
+
this.capturedImages.front.fullFrame !== null &&
|
|
192
|
+
this.capturedImages.front.cropped !== null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
class CameraService {
|
|
197
|
+
logger;
|
|
198
|
+
eventBus;
|
|
199
|
+
availableCameras = [];
|
|
200
|
+
selectedCameraId = null;
|
|
201
|
+
deviceType = 'desktop';
|
|
202
|
+
preferredCameraFacing = null;
|
|
203
|
+
preferredCamera = 'auto';
|
|
204
|
+
constructor(logger, eventBus, preferredCamera = 'auto') {
|
|
205
|
+
this.logger = logger;
|
|
206
|
+
this.eventBus = eventBus;
|
|
207
|
+
this.preferredCamera = preferredCamera;
|
|
208
|
+
}
|
|
209
|
+
async detectDeviceType() {
|
|
210
|
+
const userAgent = navigator.userAgent;
|
|
211
|
+
const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent);
|
|
212
|
+
const isTablet = /iPad|Android/i.test(userAgent) && window.innerWidth >= 768;
|
|
213
|
+
if (isTablet) {
|
|
214
|
+
this.deviceType = 'tablet';
|
|
215
|
+
}
|
|
216
|
+
else if (isMobile) {
|
|
217
|
+
this.deviceType = 'mobile';
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
this.deviceType = 'desktop';
|
|
221
|
+
}
|
|
222
|
+
this.logger.state('DISPOSITIVO_DETECTADO', {
|
|
223
|
+
deviceType: this.deviceType,
|
|
224
|
+
userAgent: navigator.userAgent,
|
|
225
|
+
screenDimensions: { width: window.innerWidth, height: window.innerHeight }
|
|
226
|
+
});
|
|
227
|
+
return this.deviceType;
|
|
228
|
+
}
|
|
229
|
+
async enumerateDevices() {
|
|
230
|
+
try {
|
|
231
|
+
const permissionStatus = await this.checkCameraPermission();
|
|
232
|
+
if (permissionStatus === 'denied') {
|
|
233
|
+
this.logger.error('Permiso de cámara denegado por el usuario');
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
if (permissionStatus === 'prompt') {
|
|
237
|
+
const tempStream = await navigator.mediaDevices.getUserMedia({ video: true });
|
|
238
|
+
tempStream.getTracks().forEach(track => track.stop());
|
|
239
|
+
}
|
|
240
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
241
|
+
this.availableCameras = devices.filter(device => device.kind === 'videoinput');
|
|
242
|
+
this.logger.state('CAMARAS_DETECTADAS', {
|
|
243
|
+
count: this.availableCameras.length,
|
|
244
|
+
cameras: this.availableCameras.map(cam => ({
|
|
245
|
+
id: cam.deviceId,
|
|
246
|
+
label: cam.label || 'Unknown Camera'
|
|
247
|
+
}))
|
|
248
|
+
});
|
|
249
|
+
this.setInitialCameraPreference();
|
|
250
|
+
this.eventBus.emit('camera-changed', this.selectedCameraId);
|
|
251
|
+
return this.availableCameras;
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
this.logger.error('Error al enumerar cámaras disponibles:', error);
|
|
255
|
+
this.handleCameraPermissionError(error);
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
getAvailableCameras() {
|
|
260
|
+
return [...this.availableCameras];
|
|
261
|
+
}
|
|
262
|
+
isMultipleCamerasAvailable() {
|
|
263
|
+
return this.availableCameras.length > 1;
|
|
264
|
+
}
|
|
265
|
+
getSelectedCameraId() {
|
|
266
|
+
return this.selectedCameraId;
|
|
267
|
+
}
|
|
268
|
+
async setSelectedCamera(cameraId) {
|
|
269
|
+
const camera = this.availableCameras.find(cam => cam.deviceId === cameraId);
|
|
270
|
+
if (!camera) {
|
|
271
|
+
throw new Error(`Camera with ID ${cameraId} not found`);
|
|
272
|
+
}
|
|
273
|
+
this.selectedCameraId = cameraId;
|
|
274
|
+
this.updatePreferredFacing(camera);
|
|
275
|
+
this.savePreference();
|
|
276
|
+
this.eventBus.emit('camera-changed', cameraId);
|
|
277
|
+
}
|
|
278
|
+
getPreferredFacing() {
|
|
279
|
+
return this.preferredCameraFacing;
|
|
280
|
+
}
|
|
281
|
+
async setupCamera(constraints) {
|
|
282
|
+
const finalConstraints = constraints || await this.getMaxResolution();
|
|
283
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
284
|
+
video: finalConstraints,
|
|
285
|
+
audio: false
|
|
286
|
+
});
|
|
287
|
+
return stream;
|
|
288
|
+
}
|
|
289
|
+
async switchCamera(cameraId) {
|
|
290
|
+
const selectedCamera = this.availableCameras.find(cam => cam.deviceId === cameraId);
|
|
291
|
+
if (!selectedCamera) {
|
|
292
|
+
this.logger.warn('Cámara seleccionada no encontrada, re-enumerando dispositivos...');
|
|
293
|
+
await this.enumerateDevices();
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
await this.setSelectedCamera(cameraId);
|
|
297
|
+
this.logger.state('CAMARA_CAMBIADA', {
|
|
298
|
+
label: selectedCamera.label,
|
|
299
|
+
deviceId: selectedCamera.deviceId
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
async flipToNextCamera() {
|
|
303
|
+
if (!this.isMultipleCamerasAvailable())
|
|
304
|
+
return;
|
|
305
|
+
const currentIndex = this.availableCameras.findIndex(camera => camera.deviceId === this.selectedCameraId);
|
|
306
|
+
const nextIndex = (currentIndex + 1) % this.availableCameras.length;
|
|
307
|
+
const nextCamera = this.availableCameras[nextIndex];
|
|
308
|
+
await this.switchCamera(nextCamera.deviceId);
|
|
309
|
+
}
|
|
310
|
+
savePreference() {
|
|
311
|
+
try {
|
|
312
|
+
const preference = {
|
|
313
|
+
cameraId: this.selectedCameraId,
|
|
314
|
+
facing: this.preferredCameraFacing,
|
|
315
|
+
timestamp: Date.now()
|
|
316
|
+
};
|
|
317
|
+
localStorage.setItem('jaak-stamps-camera-preference', JSON.stringify(preference));
|
|
318
|
+
this.logger.state('PREFERENCIA_CAMARA_GUARDADA', preference);
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
this.logger.warn('Error al guardar preferencia de cámara:', error);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
loadPreference() {
|
|
325
|
+
try {
|
|
326
|
+
const saved = localStorage.getItem('jaak-stamps-camera-preference');
|
|
327
|
+
if (saved) {
|
|
328
|
+
const preference = JSON.parse(saved);
|
|
329
|
+
const isStillAvailable = this.availableCameras.some(camera => camera.deviceId === preference.cameraId);
|
|
330
|
+
if (isStillAvailable) {
|
|
331
|
+
this.selectedCameraId = preference.cameraId;
|
|
332
|
+
this.preferredCameraFacing = preference.facing;
|
|
333
|
+
this.logger.state('PREFERENCIA_CAMARA_CARGADA', preference);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
catch (error) {
|
|
338
|
+
this.logger.warn('Error al cargar preferencia de cámara:', error);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
isRearCamera(stream) {
|
|
342
|
+
const videoTrack = stream.getVideoTracks()[0];
|
|
343
|
+
if (!videoTrack)
|
|
344
|
+
return false;
|
|
345
|
+
const settings = videoTrack.getSettings();
|
|
346
|
+
return settings.facingMode === 'environment';
|
|
347
|
+
}
|
|
348
|
+
getCameraInfo() {
|
|
349
|
+
return {
|
|
350
|
+
availableCameras: this.availableCameras.map(camera => ({
|
|
351
|
+
id: camera.deviceId,
|
|
352
|
+
label: camera.label || 'Unknown Camera',
|
|
353
|
+
selected: camera.deviceId === this.selectedCameraId
|
|
354
|
+
})),
|
|
355
|
+
selectedCameraId: this.selectedCameraId,
|
|
356
|
+
deviceType: this.deviceType,
|
|
357
|
+
isMultipleCamerasAvailable: this.isMultipleCamerasAvailable(),
|
|
358
|
+
preferredFacing: this.preferredCameraFacing
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
async checkCameraPermission() {
|
|
362
|
+
try {
|
|
363
|
+
if (!navigator.permissions) {
|
|
364
|
+
return 'prompt';
|
|
365
|
+
}
|
|
366
|
+
const permission = await navigator.permissions.query({ name: 'camera' });
|
|
367
|
+
return permission.state;
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
this.logger.warn('No se pudo verificar permisos de cámara:', error);
|
|
371
|
+
return 'prompt';
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
handleCameraPermissionError(error) {
|
|
375
|
+
this.availableCameras = [];
|
|
376
|
+
this.eventBus.emit('error', new Error(`Camera permission error: ${error.message}`));
|
|
377
|
+
}
|
|
378
|
+
setInitialCameraPreference() {
|
|
379
|
+
if (this.availableCameras.length === 0)
|
|
380
|
+
return;
|
|
381
|
+
if (this.preferredCamera === 'front') {
|
|
382
|
+
this.selectFrontCamera();
|
|
383
|
+
}
|
|
384
|
+
else if (this.preferredCamera === 'back') {
|
|
385
|
+
this.selectBackCamera();
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
this.selectAutoCamera();
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
selectFrontCamera() {
|
|
392
|
+
this.preferredCameraFacing = 'user';
|
|
393
|
+
const frontCamera = this.availableCameras.find(camera => camera.label.toLowerCase().includes('front') ||
|
|
394
|
+
camera.label.toLowerCase().includes('user') ||
|
|
395
|
+
camera.label.toLowerCase().includes('selfie') ||
|
|
396
|
+
!camera.label.toLowerCase().includes('back') && !camera.label.toLowerCase().includes('rear'));
|
|
397
|
+
this.selectedCameraId = frontCamera ? frontCamera.deviceId : this.availableCameras[0].deviceId;
|
|
398
|
+
this.logger.state('CAMARA_FRONTAL_SELECCIONADA', {
|
|
399
|
+
label: frontCamera?.label || this.availableCameras[0].label,
|
|
400
|
+
deviceId: this.selectedCameraId
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
selectBackCamera() {
|
|
404
|
+
this.preferredCameraFacing = 'environment';
|
|
405
|
+
const backCamera = this.availableCameras.find(camera => camera.label.toLowerCase().includes('back') ||
|
|
406
|
+
camera.label.toLowerCase().includes('rear') ||
|
|
407
|
+
camera.label.toLowerCase().includes('environment'));
|
|
408
|
+
this.selectedCameraId = backCamera ? backCamera.deviceId : this.availableCameras[0].deviceId;
|
|
409
|
+
this.logger.state('CAMARA_TRASERA_SELECCIONADA', {
|
|
410
|
+
label: backCamera?.label || this.availableCameras[0].label,
|
|
411
|
+
deviceId: this.selectedCameraId
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
selectAutoCamera() {
|
|
415
|
+
if (this.deviceType === 'mobile' || this.deviceType === 'tablet') {
|
|
416
|
+
this.selectBackCamera();
|
|
417
|
+
this.logger.state('CAMARA_AUTO_SELECCIONADA_MOBILE', { type: 'rear' });
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
this.selectedCameraId = this.availableCameras[0].deviceId;
|
|
421
|
+
this.logger.state('CAMARA_AUTO_SELECCIONADA_DESKTOP', {
|
|
422
|
+
label: this.availableCameras[0].label,
|
|
423
|
+
deviceId: this.selectedCameraId
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
updatePreferredFacing(camera) {
|
|
428
|
+
const isRearCamera = camera.label.toLowerCase().includes('back') ||
|
|
429
|
+
camera.label.toLowerCase().includes('rear') ||
|
|
430
|
+
camera.label.toLowerCase().includes('environment');
|
|
431
|
+
this.preferredCameraFacing = isRearCamera ? 'environment' : 'user';
|
|
432
|
+
}
|
|
433
|
+
async getMaxResolution() {
|
|
434
|
+
try {
|
|
435
|
+
const videoConstraints = {};
|
|
436
|
+
if (this.selectedCameraId) {
|
|
437
|
+
videoConstraints.deviceId = { exact: this.selectedCameraId };
|
|
438
|
+
}
|
|
439
|
+
else if (this.preferredCameraFacing) {
|
|
440
|
+
videoConstraints.facingMode = this.preferredCameraFacing;
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
videoConstraints.facingMode = "environment";
|
|
444
|
+
}
|
|
445
|
+
const tempStream = await navigator.mediaDevices.getUserMedia({
|
|
446
|
+
video: videoConstraints
|
|
447
|
+
});
|
|
448
|
+
const videoTrack = tempStream.getVideoTracks()[0];
|
|
449
|
+
const capabilities = videoTrack.getCapabilities();
|
|
450
|
+
tempStream.getTracks().forEach(track => track.stop());
|
|
451
|
+
const constraints = { ...videoConstraints };
|
|
452
|
+
// Enhanced focus and image quality settings
|
|
453
|
+
this.applyAdvancedCameraSettings(constraints, capabilities);
|
|
454
|
+
if (capabilities.width && capabilities.height) {
|
|
455
|
+
const maxWidth = Math.min(capabilities.width.max, 1920);
|
|
456
|
+
const maxHeight = Math.min(capabilities.height.max, 1080);
|
|
457
|
+
const isTablet = /iPad|Android/i.test(navigator.userAgent) && window.innerWidth >= 768;
|
|
458
|
+
if (isTablet) {
|
|
459
|
+
constraints.width = { ideal: Math.min(maxWidth, 1280) };
|
|
460
|
+
constraints.height = { ideal: Math.min(maxHeight, 720) };
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
constraints.width = { ideal: maxWidth };
|
|
464
|
+
constraints.height = { ideal: maxHeight };
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
this.logger.state('CONFIGURACION_CAMARA_OPTIMIZADA', {
|
|
468
|
+
constraints,
|
|
469
|
+
capabilities: this.getCapabilitiesSummary(capabilities)
|
|
470
|
+
});
|
|
471
|
+
return constraints;
|
|
472
|
+
}
|
|
473
|
+
catch (err) {
|
|
474
|
+
this.logger.warn('No se pudieron obtener capacidades de cámara, usando configuración de respaldo');
|
|
475
|
+
const isTablet = /iPad|Android/i.test(navigator.userAgent) && window.innerWidth >= 768;
|
|
476
|
+
const fallbackConstraints = {
|
|
477
|
+
width: { ideal: isTablet ? 1280 : 1920 },
|
|
478
|
+
height: { ideal: isTablet ? 720 : 1080 }
|
|
479
|
+
};
|
|
480
|
+
// Apply basic focus settings even for fallback
|
|
481
|
+
this.applyBasicFocusSettings(fallbackConstraints);
|
|
482
|
+
if (this.selectedCameraId) {
|
|
483
|
+
fallbackConstraints.deviceId = { exact: this.selectedCameraId };
|
|
484
|
+
}
|
|
485
|
+
else if (this.preferredCameraFacing) {
|
|
486
|
+
fallbackConstraints.facingMode = this.preferredCameraFacing;
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
fallbackConstraints.facingMode = "environment";
|
|
490
|
+
}
|
|
491
|
+
return fallbackConstraints;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
applyAdvancedCameraSettings(constraints, capabilities) {
|
|
495
|
+
// Apply standard camera settings that are widely supported
|
|
496
|
+
// Frame rate optimization
|
|
497
|
+
if (capabilities.frameRate) {
|
|
498
|
+
constraints.frameRate = {
|
|
499
|
+
ideal: 30,
|
|
500
|
+
min: 15,
|
|
501
|
+
max: 60
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
// Width and height constraints for optimal resolution
|
|
505
|
+
if (capabilities.width && capabilities.height) {
|
|
506
|
+
constraints.width = {
|
|
507
|
+
ideal: Math.min(1920, capabilities.width.max || 1920),
|
|
508
|
+
min: 640
|
|
509
|
+
};
|
|
510
|
+
constraints.height = {
|
|
511
|
+
ideal: Math.min(1080, capabilities.height.max || 1080),
|
|
512
|
+
min: 480
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
// Apply enhanced constraints through advanced array for better browser compatibility
|
|
516
|
+
const advancedConstraints = [];
|
|
517
|
+
// Try to set optimal settings through advanced constraints
|
|
518
|
+
const advanced = {};
|
|
519
|
+
// Focus mode - use 'any' type to bypass TypeScript limitations
|
|
520
|
+
if (capabilities.focusMode) {
|
|
521
|
+
if (capabilities.focusMode.includes('continuous')) {
|
|
522
|
+
advanced.focusMode = 'continuous';
|
|
523
|
+
}
|
|
524
|
+
else if (capabilities.focusMode.includes('single-shot')) {
|
|
525
|
+
advanced.focusMode = 'single-shot';
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
// Focus distance for close objects
|
|
529
|
+
if (capabilities.focusDistance) {
|
|
530
|
+
advanced.focusDistance = {
|
|
531
|
+
ideal: 0.4, // 40cm - optimal for document capture
|
|
532
|
+
min: 0.2,
|
|
533
|
+
max: 1.0
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
// Zoom control
|
|
537
|
+
if (capabilities.zoom) {
|
|
538
|
+
advanced.zoom = {
|
|
539
|
+
ideal: 1.0,
|
|
540
|
+
min: capabilities.zoom.min,
|
|
541
|
+
max: Math.min(capabilities.zoom.max, 3.0)
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
// Add advanced constraints if any were set
|
|
545
|
+
if (Object.keys(advanced).length > 0) {
|
|
546
|
+
advancedConstraints.push(advanced);
|
|
547
|
+
}
|
|
548
|
+
if (advancedConstraints.length > 0) {
|
|
549
|
+
constraints.advanced = advancedConstraints;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
applyBasicFocusSettings(constraints) {
|
|
553
|
+
// Apply basic focus settings when capabilities are not available
|
|
554
|
+
// Use advanced constraints for non-standard properties
|
|
555
|
+
const advanced = {
|
|
556
|
+
focusMode: 'continuous',
|
|
557
|
+
exposureMode: 'continuous',
|
|
558
|
+
whiteBalanceMode: 'continuous'
|
|
559
|
+
};
|
|
560
|
+
if (!constraints.advanced) {
|
|
561
|
+
constraints.advanced = [];
|
|
562
|
+
}
|
|
563
|
+
constraints.advanced.push(advanced);
|
|
564
|
+
constraints.frameRate = { ideal: 30 };
|
|
565
|
+
}
|
|
566
|
+
getCapabilitiesSummary(capabilities) {
|
|
567
|
+
return {
|
|
568
|
+
hasAutoFocus: !!capabilities.focusMode,
|
|
569
|
+
hasFocusDistance: !!capabilities.focusDistance,
|
|
570
|
+
hasExposureControl: !!capabilities.exposureMode,
|
|
571
|
+
hasWhiteBalance: !!capabilities.whiteBalanceMode,
|
|
572
|
+
hasZoom: !!capabilities.zoom,
|
|
573
|
+
hasTorch: capabilities.torch !== undefined,
|
|
574
|
+
hasImageControls: !!(capabilities.brightness || capabilities.contrast || capabilities.saturation || capabilities.sharpness),
|
|
575
|
+
resolutionRange: capabilities.width && capabilities.height ?
|
|
576
|
+
`${capabilities.width.min}x${capabilities.height.min} - ${capabilities.width.max}x${capabilities.height.max}` : 'unknown'
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
// New method to enable torch/flash for better illumination
|
|
580
|
+
async setTorchEnabled(enabled, stream) {
|
|
581
|
+
try {
|
|
582
|
+
if (!stream) {
|
|
583
|
+
this.logger.warn('No hay stream disponible para controlar el flash');
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
const videoTrack = stream.getVideoTracks()[0];
|
|
587
|
+
if (!videoTrack) {
|
|
588
|
+
this.logger.warn('No hay track de video disponible');
|
|
589
|
+
return false;
|
|
590
|
+
}
|
|
591
|
+
const capabilities = videoTrack.getCapabilities();
|
|
592
|
+
if (capabilities.torch === undefined) {
|
|
593
|
+
this.logger.warn('El dispositivo no soporta control de flash');
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
await videoTrack.applyConstraints({
|
|
597
|
+
advanced: [{ torch: enabled }]
|
|
598
|
+
});
|
|
599
|
+
this.logger.state('FLASH_CONTROLADO', { enabled });
|
|
600
|
+
return true;
|
|
601
|
+
}
|
|
602
|
+
catch (error) {
|
|
603
|
+
this.logger.error('Error al controlar el flash:', error);
|
|
604
|
+
return false;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
// Method to manually trigger focus on a specific point
|
|
608
|
+
async focusAtPoint(x, y, stream) {
|
|
609
|
+
try {
|
|
610
|
+
if (!stream) {
|
|
611
|
+
this.logger.warn('No hay stream disponible para enfocar');
|
|
612
|
+
return false;
|
|
613
|
+
}
|
|
614
|
+
const videoTrack = stream.getVideoTracks()[0];
|
|
615
|
+
if (!videoTrack) {
|
|
616
|
+
this.logger.warn('No hay track de video disponible');
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
const capabilities = videoTrack.getCapabilities();
|
|
620
|
+
if (!capabilities.focusMode || !capabilities.focusMode.includes('single-shot')) {
|
|
621
|
+
this.logger.warn('El dispositivo no soporta enfoque manual');
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
// Trigger single-shot focus
|
|
625
|
+
await videoTrack.applyConstraints({
|
|
626
|
+
advanced: [{ focusMode: 'single-shot' }]
|
|
627
|
+
});
|
|
628
|
+
// Return to continuous focus after a short delay
|
|
629
|
+
setTimeout(async () => {
|
|
630
|
+
try {
|
|
631
|
+
await videoTrack.applyConstraints({
|
|
632
|
+
advanced: [{ focusMode: 'continuous' }]
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
catch (error) {
|
|
636
|
+
this.logger.warn('Error al volver a enfoque continuo:', error);
|
|
637
|
+
}
|
|
638
|
+
}, 1000);
|
|
639
|
+
this.logger.state('ENFOQUE_MANUAL_ACTIVADO', { x, y });
|
|
640
|
+
return true;
|
|
641
|
+
}
|
|
642
|
+
catch (error) {
|
|
643
|
+
this.logger.error('Error al enfocar manualmente:', error);
|
|
644
|
+
return false;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
class LowMemoryDeviceStrategy {
|
|
650
|
+
getDeviceInfo() {
|
|
651
|
+
const nav = navigator;
|
|
652
|
+
const memory = nav.deviceMemory || nav.hardwareConcurrency || 4;
|
|
653
|
+
const connection = nav.connection || nav.mozConnection || nav.webkitConnection;
|
|
654
|
+
const isSlowConnection = connection && (connection.effectiveType === 'slow-2g' || connection.effectiveType === '2g');
|
|
655
|
+
return {
|
|
656
|
+
estimatedRAM: memory,
|
|
657
|
+
isLowMemory: true,
|
|
658
|
+
isSlowConnection: isSlowConnection
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
getSessionOptions(_debug) {
|
|
662
|
+
return {
|
|
663
|
+
executionProviders: ['wasm'],
|
|
664
|
+
graphOptimizationLevel: 'basic',
|
|
665
|
+
logSeverityLevel: 4,
|
|
666
|
+
logVerbosityLevel: 0,
|
|
667
|
+
enableCpuMemArena: false,
|
|
668
|
+
enableMemPattern: false,
|
|
669
|
+
executionMode: 'sequential',
|
|
670
|
+
interOpNumThreads: 1,
|
|
671
|
+
intraOpNumThreads: 1,
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
shouldUseSequentialLoading() {
|
|
675
|
+
return true;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
class HighPerformanceDeviceStrategy {
|
|
680
|
+
getDeviceInfo() {
|
|
681
|
+
const nav = navigator;
|
|
682
|
+
const memory = nav.deviceMemory || nav.hardwareConcurrency || 4;
|
|
683
|
+
const connection = nav.connection || nav.mozConnection || nav.webkitConnection;
|
|
684
|
+
const isSlowConnection = connection && (connection.effectiveType === 'slow-2g' || connection.effectiveType === '2g');
|
|
685
|
+
return {
|
|
686
|
+
estimatedRAM: memory,
|
|
687
|
+
isLowMemory: false,
|
|
688
|
+
isSlowConnection: isSlowConnection
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
getSessionOptions(debug) {
|
|
692
|
+
return {
|
|
693
|
+
executionProviders: ['webgl', 'wasm'],
|
|
694
|
+
graphOptimizationLevel: 'all',
|
|
695
|
+
logSeverityLevel: debug ? 2 : 4,
|
|
696
|
+
logVerbosityLevel: 0,
|
|
697
|
+
enableCpuMemArena: true,
|
|
698
|
+
enableMemPattern: true,
|
|
699
|
+
executionMode: 'parallel',
|
|
700
|
+
interOpNumThreads: 2,
|
|
701
|
+
intraOpNumThreads: 2,
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
shouldUseSequentialLoading() {
|
|
705
|
+
return false;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
class DeviceStrategyFactory {
|
|
710
|
+
static createStrategy() {
|
|
711
|
+
const nav = navigator;
|
|
712
|
+
const memory = nav.deviceMemory || nav.hardwareConcurrency || 4;
|
|
713
|
+
const isLowMemory = memory <= 4;
|
|
714
|
+
if (isLowMemory) {
|
|
715
|
+
return new LowMemoryDeviceStrategy();
|
|
716
|
+
}
|
|
717
|
+
else {
|
|
718
|
+
return new HighPerformanceDeviceStrategy();
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
class DetectionService {
|
|
724
|
+
logger;
|
|
725
|
+
debug;
|
|
726
|
+
useDocumentClassification;
|
|
727
|
+
session;
|
|
728
|
+
mobileNetSession;
|
|
729
|
+
mobileNetClassMap;
|
|
730
|
+
modelLoaded = false;
|
|
731
|
+
deviceStrategy;
|
|
732
|
+
MODEL_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/ddmyp-v2.onnx";
|
|
733
|
+
MOBILENET_MODEL_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/cdmmp-v1.onnx";
|
|
734
|
+
MOBILENET_CLASSES_PATH = "https://storage.googleapis.com/jaak-static/web/component/stamps/cdmmp-v1.json";
|
|
735
|
+
INPUT_SIZE = 320;
|
|
736
|
+
CONFIDENCE_THRESHOLD = 0.6;
|
|
737
|
+
// Canvas pool for optimization
|
|
738
|
+
preprocessCanvas;
|
|
739
|
+
preprocessCtx;
|
|
740
|
+
captureCanvas;
|
|
741
|
+
constructor(logger, debug = false, useDocumentClassification = false) {
|
|
742
|
+
this.logger = logger;
|
|
743
|
+
this.debug = debug;
|
|
744
|
+
this.useDocumentClassification = useDocumentClassification;
|
|
745
|
+
this.deviceStrategy = DeviceStrategyFactory.createStrategy();
|
|
746
|
+
this.initializeCanvasPool();
|
|
747
|
+
}
|
|
748
|
+
async loadModel() {
|
|
749
|
+
if (this.modelLoaded || this.session) {
|
|
750
|
+
this.logger.state('MODELO_YA_PRECARGADO', {
|
|
751
|
+
sessionExists: !!this.session,
|
|
752
|
+
modelPreloaded: this.modelLoaded
|
|
753
|
+
});
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
try {
|
|
757
|
+
this.logger.state('PRECARGANDO_MODELO_DETECCION', { modelPath: this.MODEL_PATH });
|
|
758
|
+
const sessionOptions = this.deviceStrategy.getSessionOptions(this.debug);
|
|
759
|
+
try {
|
|
760
|
+
this.session = await window.ort.InferenceSession.create(this.MODEL_PATH, sessionOptions);
|
|
761
|
+
}
|
|
762
|
+
catch (error) {
|
|
763
|
+
if (error.message.includes('failed to allocate a buffer')) {
|
|
764
|
+
this.logger.warn('Fallo en asignación de buffer durante precarga, intentando con configuración mínima');
|
|
765
|
+
const fallbackOptions = {
|
|
766
|
+
executionProviders: ['wasm'],
|
|
767
|
+
graphOptimizationLevel: 'disabled',
|
|
768
|
+
logSeverityLevel: 4,
|
|
769
|
+
enableCpuMemArena: false,
|
|
770
|
+
enableMemPattern: false,
|
|
771
|
+
executionMode: 'sequential',
|
|
772
|
+
interOpNumThreads: 1,
|
|
773
|
+
intraOpNumThreads: 1,
|
|
774
|
+
};
|
|
775
|
+
this.session = await window.ort.InferenceSession.create(this.MODEL_PATH, fallbackOptions);
|
|
776
|
+
}
|
|
777
|
+
else {
|
|
778
|
+
throw error;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
this.modelLoaded = true;
|
|
782
|
+
this.logger.state('MODELO_DETECCION_CARGADO_EXITOSAMENTE', { sessionCreated: !!this.session });
|
|
783
|
+
}
|
|
784
|
+
catch (error) {
|
|
785
|
+
this.logger.error('Error al precargar modelo de detección:', error);
|
|
786
|
+
throw error;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
async loadClassificationModel() {
|
|
790
|
+
if (!this.useDocumentClassification) {
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
try {
|
|
794
|
+
this.logger.state('CARGANDO_MODELO_MOBILENET', { path: this.MOBILENET_MODEL_PATH });
|
|
795
|
+
// Load class map
|
|
796
|
+
const classResponse = await fetch(this.MOBILENET_CLASSES_PATH);
|
|
797
|
+
if (!classResponse.ok) {
|
|
798
|
+
throw new Error(`Failed to load class map: ${this.MOBILENET_CLASSES_PATH}`);
|
|
799
|
+
}
|
|
800
|
+
this.mobileNetClassMap = await classResponse.json();
|
|
801
|
+
this.logger.state('CLASES_MOBILENET_CARGADAS', {
|
|
802
|
+
classCount: Object.keys(this.mobileNetClassMap).length
|
|
803
|
+
});
|
|
804
|
+
// Load model
|
|
805
|
+
const sessionOptions = this.deviceStrategy.getSessionOptions(this.debug);
|
|
806
|
+
try {
|
|
807
|
+
this.mobileNetSession = await window.ort.InferenceSession.create(this.MOBILENET_MODEL_PATH, sessionOptions);
|
|
808
|
+
}
|
|
809
|
+
catch (error) {
|
|
810
|
+
if (error.message.includes('failed to allocate a buffer')) {
|
|
811
|
+
this.logger.warn('Fallo en asignación de buffer de MobileNet, intentando con configuración mínima');
|
|
812
|
+
const fallbackOptions = {
|
|
813
|
+
executionProviders: ['wasm'],
|
|
814
|
+
graphOptimizationLevel: 'disabled',
|
|
815
|
+
logSeverityLevel: 4,
|
|
816
|
+
enableCpuMemArena: false,
|
|
817
|
+
enableMemPattern: false,
|
|
818
|
+
executionMode: 'sequential',
|
|
819
|
+
interOpNumThreads: 1,
|
|
820
|
+
intraOpNumThreads: 1,
|
|
821
|
+
};
|
|
822
|
+
this.mobileNetSession = await window.ort.InferenceSession.create(this.MOBILENET_MODEL_PATH, fallbackOptions);
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
throw error;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
this.logger.state('MODELO_MOBILENET_CARGADO_EXITOSAMENTE', {
|
|
829
|
+
sessionCreated: !!this.mobileNetSession
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
catch (error) {
|
|
833
|
+
this.logger.error('Error al cargar modelo MobileNet:', error);
|
|
834
|
+
throw error;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
isModelLoaded() {
|
|
838
|
+
return this.modelLoaded && !!this.session;
|
|
839
|
+
}
|
|
840
|
+
preprocess(video) {
|
|
841
|
+
if (!this.preprocessCanvas || !this.preprocessCtx) {
|
|
842
|
+
this.initializeCanvasPool();
|
|
843
|
+
}
|
|
844
|
+
this.preprocessCtx.clearRect(0, 0, this.INPUT_SIZE, this.INPUT_SIZE);
|
|
845
|
+
this.preprocessCtx.drawImage(video, 0, 0, this.INPUT_SIZE, this.INPUT_SIZE);
|
|
846
|
+
const imageData = this.preprocessCtx.getImageData(0, 0, this.INPUT_SIZE, this.INPUT_SIZE);
|
|
847
|
+
const [R, G, B] = [[], [], []];
|
|
848
|
+
const { data } = imageData;
|
|
849
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
850
|
+
R.push(data[i] / 255);
|
|
851
|
+
G.push(data[i + 1] / 255);
|
|
852
|
+
B.push(data[i + 2] / 255);
|
|
853
|
+
}
|
|
854
|
+
const transposedData = new Float32Array(R.concat(G, B));
|
|
855
|
+
const float16Data = new Uint16Array(transposedData.length);
|
|
856
|
+
for (let i = 0; i < transposedData.length; i++) {
|
|
857
|
+
float16Data[i] = this.float32ToFloat16(transposedData[i]);
|
|
858
|
+
}
|
|
859
|
+
return new window.ort.Tensor("float16", float16Data, [1, 3, this.INPUT_SIZE, this.INPUT_SIZE]);
|
|
860
|
+
}
|
|
861
|
+
async runInference(inputTensor) {
|
|
862
|
+
if (!this.session) {
|
|
863
|
+
throw new Error('Detection model not loaded');
|
|
864
|
+
}
|
|
865
|
+
const feeds = { [this.session.inputNames[0]]: inputTensor };
|
|
866
|
+
const results = await this.session.run(feeds);
|
|
867
|
+
const output = results[this.session.outputNames[0]].data;
|
|
868
|
+
const detections = [];
|
|
869
|
+
for (let i = 0; i < output.length; i += 6) {
|
|
870
|
+
const [x1, y1, x2, y2, score, classId] = output.slice(i, i + 6);
|
|
871
|
+
if (score > this.CONFIDENCE_THRESHOLD) {
|
|
872
|
+
detections.push({
|
|
873
|
+
x: x1,
|
|
874
|
+
y: y1,
|
|
875
|
+
w: x2 - x1,
|
|
876
|
+
h: y2 - y1,
|
|
877
|
+
score,
|
|
878
|
+
classId
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return detections;
|
|
883
|
+
}
|
|
884
|
+
async classifyDocument(canvas) {
|
|
885
|
+
if (!this.mobileNetSession || !this.mobileNetClassMap) {
|
|
886
|
+
this.logger.warn('Modelo MobileNet no está cargado, saltando clasificación');
|
|
887
|
+
return null;
|
|
888
|
+
}
|
|
889
|
+
try {
|
|
890
|
+
this.logger.state('CLASIFICANDO_DOCUMENTO', { timestamp: Date.now() });
|
|
891
|
+
const inputTensor = this.preprocessMobileNet(canvas);
|
|
892
|
+
const feeds = { input: inputTensor };
|
|
893
|
+
const results = await this.mobileNetSession.run(feeds);
|
|
894
|
+
const output = results[Object.keys(results)[0]].data;
|
|
895
|
+
const maxIdx = output.reduce((bestIdx, val, idx, arr) => val > arr[bestIdx] ? idx : bestIdx, 0);
|
|
896
|
+
const confidence = output[maxIdx];
|
|
897
|
+
const className = this.mobileNetClassMap[maxIdx.toString()] || "unknown";
|
|
898
|
+
this.logger.state('DOCUMENTO_CLASIFICADO', {
|
|
899
|
+
class: className,
|
|
900
|
+
confidence: confidence.toFixed(3),
|
|
901
|
+
classIndex: maxIdx,
|
|
902
|
+
timestamp: Date.now()
|
|
903
|
+
});
|
|
904
|
+
return {
|
|
905
|
+
class: className,
|
|
906
|
+
confidence: confidence,
|
|
907
|
+
classIndex: maxIdx
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
catch (error) {
|
|
911
|
+
this.logger.error('Error al clasificar documento:', error);
|
|
912
|
+
return null;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
checkSideAlignment(box, maskConfig) {
|
|
916
|
+
const { INPUT_SIZE, ID1_ASPECT_RATIO, shouldMirrorVideo, alignmentTolerance, maskSize, videoRef } = maskConfig;
|
|
917
|
+
if (!videoRef) {
|
|
918
|
+
return { top: false, right: false, bottom: false, left: false };
|
|
919
|
+
}
|
|
920
|
+
// Get video dimensions to calculate actual mask boundaries in model space
|
|
921
|
+
const videoWidth = videoRef.videoWidth;
|
|
922
|
+
const videoHeight = videoRef.videoHeight;
|
|
923
|
+
if (videoWidth === 0 || videoHeight === 0) {
|
|
924
|
+
return { top: false, right: false, bottom: false, left: false };
|
|
925
|
+
}
|
|
926
|
+
// Calculate video aspect ratio
|
|
927
|
+
const videoAspectRatio = videoWidth / videoHeight;
|
|
928
|
+
// The model sees video stretched to 320x320, but we need to calculate where
|
|
929
|
+
// the mask should be in this distorted space to match the visual mask
|
|
930
|
+
// In the visual display, we calculate mask size based on the limiting dimension
|
|
931
|
+
// We need to replicate this logic but account for the distortion in model space
|
|
932
|
+
// Calculate the "display" dimensions (what the visual mask calculations use)
|
|
933
|
+
const containerAspectRatio = 1; // Assume square container for simplicity
|
|
934
|
+
let displayedVideoWidth, displayedVideoHeight;
|
|
935
|
+
if (videoAspectRatio > containerAspectRatio) {
|
|
936
|
+
// Video is wider - letterboxed in display
|
|
937
|
+
displayedVideoWidth = INPUT_SIZE;
|
|
938
|
+
displayedVideoHeight = INPUT_SIZE / videoAspectRatio;
|
|
939
|
+
}
|
|
940
|
+
else {
|
|
941
|
+
// Video is taller - pillarboxed in display
|
|
942
|
+
displayedVideoHeight = INPUT_SIZE;
|
|
943
|
+
displayedVideoWidth = INPUT_SIZE * videoAspectRatio;
|
|
944
|
+
}
|
|
945
|
+
// Calculate mask dimensions using the same logic as updateMaskDimensions
|
|
946
|
+
const maxWidthByHeight = displayedVideoHeight * ID1_ASPECT_RATIO;
|
|
947
|
+
let visualMaskWidth, visualMaskHeight;
|
|
948
|
+
const sizeRatio = maskSize / 100;
|
|
949
|
+
if (maxWidthByHeight <= displayedVideoWidth) {
|
|
950
|
+
// Video height is the limiting factor
|
|
951
|
+
visualMaskHeight = displayedVideoHeight * sizeRatio;
|
|
952
|
+
visualMaskWidth = visualMaskHeight * ID1_ASPECT_RATIO;
|
|
953
|
+
}
|
|
954
|
+
else {
|
|
955
|
+
// Video width is the limiting factor
|
|
956
|
+
visualMaskWidth = displayedVideoWidth * sizeRatio;
|
|
957
|
+
visualMaskHeight = visualMaskWidth / ID1_ASPECT_RATIO;
|
|
958
|
+
}
|
|
959
|
+
// Now map these visual mask dimensions to the distorted model space
|
|
960
|
+
// The model stretches video to 320x320, so we need to apply the inverse transformation
|
|
961
|
+
const modelMaskWidth = visualMaskWidth * (INPUT_SIZE / displayedVideoWidth);
|
|
962
|
+
const modelMaskHeight = visualMaskHeight * (INPUT_SIZE / displayedVideoHeight);
|
|
963
|
+
// Calculate mask boundaries in model coordinate system (always centered in 320x320)
|
|
964
|
+
const maskCenterX = INPUT_SIZE / 2;
|
|
965
|
+
const maskCenterY = INPUT_SIZE / 2;
|
|
966
|
+
const maskLeft = maskCenterX - (modelMaskWidth / 2);
|
|
967
|
+
const maskRight = maskCenterX + (modelMaskWidth / 2);
|
|
968
|
+
const maskTop = maskCenterY - (modelMaskHeight / 2);
|
|
969
|
+
const maskBottom = maskCenterY + (modelMaskHeight / 2);
|
|
970
|
+
// Obtener las coordenadas del documento detectado
|
|
971
|
+
let docLeft = box.x;
|
|
972
|
+
let docRight = box.x + box.w;
|
|
973
|
+
const docTop = box.y;
|
|
974
|
+
const docBottom = box.y + box.h;
|
|
975
|
+
// Si el video está en modo mirror, invertir las coordenadas horizontales
|
|
976
|
+
if (shouldMirrorVideo) {
|
|
977
|
+
const originalDocLeft = docLeft;
|
|
978
|
+
const originalDocRight = docRight;
|
|
979
|
+
docLeft = INPUT_SIZE - originalDocRight;
|
|
980
|
+
docRight = INPUT_SIZE - originalDocLeft;
|
|
981
|
+
}
|
|
982
|
+
// Tolerancia para considerar que un lado está alineado (en píxeles)
|
|
983
|
+
const tolerance = alignmentTolerance;
|
|
984
|
+
// Verificar alineación de esquinas más estricta
|
|
985
|
+
// Una esquina está alineada solo si AMBOS bordes que la forman están alineados
|
|
986
|
+
const topAligned = Math.abs(docTop - maskTop) <= tolerance;
|
|
987
|
+
const rightAligned = Math.abs(docRight - maskRight) <= tolerance;
|
|
988
|
+
const bottomAligned = Math.abs(docBottom - maskBottom) <= tolerance;
|
|
989
|
+
const leftAligned = Math.abs(docLeft - maskLeft) <= tolerance;
|
|
990
|
+
return {
|
|
991
|
+
top: topAligned && leftAligned, // Esquina superior izquierda: borde top Y left alineados
|
|
992
|
+
right: topAligned && rightAligned, // Esquina superior derecha: borde top Y right alineados
|
|
993
|
+
bottom: bottomAligned && leftAligned, // Esquina inferior izquierda: borde bottom Y left alineados
|
|
994
|
+
left: bottomAligned && rightAligned // Esquina inferior derecha: borde bottom Y right alineados
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
isCardInFrame(box) {
|
|
998
|
+
const cardCenterX = box.x + box.w / 2;
|
|
999
|
+
const cardCenterY = box.y + box.h / 2;
|
|
1000
|
+
const frameCenterX = this.INPUT_SIZE / 2;
|
|
1001
|
+
const frameCenterY = this.INPUT_SIZE / 2;
|
|
1002
|
+
const toleranceX = 40;
|
|
1003
|
+
const toleranceY = 30;
|
|
1004
|
+
const isCentered = Math.abs(cardCenterX - frameCenterX) < toleranceX &&
|
|
1005
|
+
Math.abs(cardCenterY - frameCenterY) < toleranceY;
|
|
1006
|
+
const isGoodSize = box.w > 150 && box.w < 300 && box.h > 90 && box.h < 200;
|
|
1007
|
+
return isCentered && isGoodSize;
|
|
1008
|
+
}
|
|
1009
|
+
areAllSidesAligned(alignment) {
|
|
1010
|
+
return alignment.top && alignment.right && alignment.bottom && alignment.left;
|
|
1011
|
+
}
|
|
1012
|
+
cleanup() {
|
|
1013
|
+
this.cleanupCanvasPool();
|
|
1014
|
+
if (this.session) {
|
|
1015
|
+
this.session.release?.();
|
|
1016
|
+
this.session = undefined;
|
|
1017
|
+
}
|
|
1018
|
+
if (this.mobileNetSession) {
|
|
1019
|
+
this.mobileNetSession.release?.();
|
|
1020
|
+
this.mobileNetSession = undefined;
|
|
1021
|
+
}
|
|
1022
|
+
this.mobileNetClassMap = undefined;
|
|
1023
|
+
this.modelLoaded = false;
|
|
1024
|
+
this.logger.state('DETECCION_SERVICE_LIMPIADO', { timestamp: Date.now() });
|
|
1025
|
+
}
|
|
1026
|
+
initializeCanvasPool() {
|
|
1027
|
+
this.preprocessCanvas = document.createElement('canvas');
|
|
1028
|
+
this.preprocessCanvas.width = this.INPUT_SIZE;
|
|
1029
|
+
this.preprocessCanvas.height = this.INPUT_SIZE;
|
|
1030
|
+
this.preprocessCtx = this.preprocessCanvas.getContext('2d', {
|
|
1031
|
+
alpha: false,
|
|
1032
|
+
willReadFrequently: true
|
|
1033
|
+
});
|
|
1034
|
+
this.captureCanvas = document.createElement('canvas');
|
|
1035
|
+
this.logger.state('CANVAS_POOL_INICIALIZADO', {
|
|
1036
|
+
preprocessCanvasSize: this.INPUT_SIZE
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
cleanupCanvasPool() {
|
|
1040
|
+
if (this.preprocessCanvas) {
|
|
1041
|
+
this.preprocessCtx = undefined;
|
|
1042
|
+
this.preprocessCanvas = undefined;
|
|
1043
|
+
}
|
|
1044
|
+
if (this.captureCanvas) {
|
|
1045
|
+
this.captureCanvas = undefined;
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
float32ToFloat16(value) {
|
|
1049
|
+
const buffer = new ArrayBuffer(4);
|
|
1050
|
+
const view = new DataView(buffer);
|
|
1051
|
+
view.setFloat32(0, value, true);
|
|
1052
|
+
const f = view.getUint32(0, true);
|
|
1053
|
+
const sign = (f >> 31) & 0x1;
|
|
1054
|
+
const exp = (f >> 23) & 0xFF;
|
|
1055
|
+
const frac = f & 0x7FFFFF;
|
|
1056
|
+
let newExp = exp - 127 + 15;
|
|
1057
|
+
if (exp === 0) {
|
|
1058
|
+
newExp = 0;
|
|
1059
|
+
}
|
|
1060
|
+
else if (exp === 0xFF) {
|
|
1061
|
+
newExp = 0x1F;
|
|
1062
|
+
}
|
|
1063
|
+
else if (newExp >= 0x1F) {
|
|
1064
|
+
newExp = 0x1F;
|
|
1065
|
+
return (sign << 15) | (newExp << 10);
|
|
1066
|
+
}
|
|
1067
|
+
else if (newExp <= 0) {
|
|
1068
|
+
return (sign << 15);
|
|
1069
|
+
}
|
|
1070
|
+
return (sign << 15) | (newExp << 10) | (frac >> 13);
|
|
1071
|
+
}
|
|
1072
|
+
preprocessMobileNet(canvas) {
|
|
1073
|
+
const tempCanvas = document.createElement('canvas');
|
|
1074
|
+
tempCanvas.width = 224;
|
|
1075
|
+
tempCanvas.height = 224;
|
|
1076
|
+
const tempCtx = tempCanvas.getContext('2d');
|
|
1077
|
+
tempCtx.drawImage(canvas, 0, 0, 224, 224);
|
|
1078
|
+
const imageData = tempCtx.getImageData(0, 0, 224, 224);
|
|
1079
|
+
const data = imageData.data;
|
|
1080
|
+
const hw = 224 * 224;
|
|
1081
|
+
const arr = new Float32Array(3 * hw);
|
|
1082
|
+
for (let i = 0; i < hw; i++) {
|
|
1083
|
+
arr[i] = (data[i * 4 + 0] / 255 - 0.5) / 0.5;
|
|
1084
|
+
arr[hw + i] = (data[i * 4 + 1] / 255 - 0.5) / 0.5;
|
|
1085
|
+
arr[2 * hw + i] = (data[i * 4 + 2] / 255 - 0.5) / 0.5;
|
|
1086
|
+
}
|
|
1087
|
+
return new window.ort.Tensor('float32', arr, [1, 3, 224, 224]);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
class ServiceContainer {
|
|
1092
|
+
services = new Map();
|
|
1093
|
+
constructor(config) {
|
|
1094
|
+
this.initializeServices(config);
|
|
1095
|
+
}
|
|
1096
|
+
initializeServices(config) {
|
|
1097
|
+
// Initialize services in dependency order
|
|
1098
|
+
const eventBus = new EventBusService();
|
|
1099
|
+
const logger = new LoggerService(config.debug);
|
|
1100
|
+
const stateManager = new StateManagerService(eventBus);
|
|
1101
|
+
const cameraService = new CameraService(logger, eventBus, config.preferredCamera);
|
|
1102
|
+
const detectionService = new DetectionService(logger, config.debug, config.useDocumentClassification);
|
|
1103
|
+
// Register services
|
|
1104
|
+
this.services.set('eventBus', eventBus);
|
|
1105
|
+
this.services.set('logger', logger);
|
|
1106
|
+
this.services.set('stateManager', stateManager);
|
|
1107
|
+
this.services.set('cameraService', cameraService);
|
|
1108
|
+
this.services.set('detectionService', detectionService);
|
|
1109
|
+
}
|
|
1110
|
+
get(serviceName) {
|
|
1111
|
+
const service = this.services.get(serviceName);
|
|
1112
|
+
if (!service) {
|
|
1113
|
+
throw new Error(`Service ${serviceName} not found`);
|
|
1114
|
+
}
|
|
1115
|
+
return service;
|
|
1116
|
+
}
|
|
1117
|
+
getLogger() {
|
|
1118
|
+
return this.get('logger');
|
|
1119
|
+
}
|
|
1120
|
+
getEventBus() {
|
|
1121
|
+
return this.get('eventBus');
|
|
1122
|
+
}
|
|
1123
|
+
getStateManager() {
|
|
1124
|
+
return this.get('stateManager');
|
|
1125
|
+
}
|
|
1126
|
+
getCameraService() {
|
|
1127
|
+
return this.get('cameraService');
|
|
1128
|
+
}
|
|
1129
|
+
getDetectionService() {
|
|
1130
|
+
return this.get('detectionService');
|
|
1131
|
+
}
|
|
1132
|
+
updateConfig(config) {
|
|
1133
|
+
if (config.debug !== undefined) {
|
|
1134
|
+
const logger = this.services.get('logger');
|
|
1135
|
+
logger.setDebugMode(config.debug);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
cleanup() {
|
|
1139
|
+
// Cleanup services in reverse order
|
|
1140
|
+
this.getDetectionService().cleanup();
|
|
1141
|
+
this.getEventBus().clear();
|
|
1142
|
+
this.services.clear();
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
const myComponentCss = ":host{display:block;width:100%;height:100%;font-family:system-ui, -apple-system, sans-serif;color:#1a1a1a}.detector-container{display:flex;flex-direction:column;align-items:center;width:100%;height:100%}h1{font-size:24px;font-weight:500;color:#333;margin:0 0 24px 0}.video-container{position:relative;width:100%;height:100%;background:#333;border:1px solid #e0e0e0;border-radius:8px;overflow:hidden}video,.detection-overlay{position:absolute;width:100%;height:100%;border-radius:8px}video.mirror,.detection-overlay.mirror{transform:rotateY(180deg)}.detection-overlay{z-index:1;pointer-events:none}video{z-index:0}.detection-box{transition:opacity 0.2s ease}.status{margin-top:16px;font-size:12px;color:#666}.overlay-mask{position:absolute;top:0;left:0;width:100%;height:100%;z-index:10;display:block;pointer-events:none}.card-outline{position:absolute;top:var(--mask-center-y, 50%);left:var(--mask-center-x, 50%);transform:translate(-50%, -50%);width:var(--mask-width, 88%);height:var(--mask-height, 55%);border:none;border-radius:4px;background:transparent;opacity:0.8}.side{position:absolute;background:#999;transition:background-color 0.3s ease;border-radius:1px}.side-top.aligned{border-left-color:#28a745;border-top-color:#28a745}.side-right.aligned{border-right-color:#28a745;border-top-color:#28a745}.side-bottom.aligned{border-left-color:#28a745;border-bottom-color:#28a745}.side-left.aligned{border-right-color:#28a745;border-bottom-color:#28a745}.side-top{top:-3px;left:-3px;width:30px;height:30px;border-left:6px solid #ffffff;border-top:6px solid #ffffff;background:transparent}.side-right{top:-3px;right:-3px;width:30px;height:30px;border-right:6px solid #ffffff;border-top:6px solid #ffffff;background:transparent}.side-bottom{bottom:-3px;left:-3px;width:30px;height:30px;border-left:6px solid #ffffff;border-bottom:6px solid #ffffff;background:transparent}.side-left{bottom:-3px;right:-3px;width:30px;height:30px;border-right:6px solid #ffffff;border-bottom:6px solid #ffffff;background:transparent}.guide-text{position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);color:#fff;font-size:14px;font-weight:600;text-align:center;white-space:normal;background:rgba(128, 128, 128, 0.8);padding:12px 20px;border-radius:20px;max-width:300px;z-index:20}.quality-indicator{position:absolute;top:20px;right:20px;display:flex;align-items:center;gap:8px;background:rgba(0, 0, 0, 0.8);padding:8px 12px;border-radius:20px;z-index:25;transition:all 0.3s ease}.quality-indicator.warning{background:rgba(255, 152, 0, 0.9)}.quality-indicator.good{background:rgba(76, 175, 80, 0.9)}.quality-score{color:#fff;font-size:12px;font-weight:600;min-width:30px;text-align:center}.quality-status{width:20px;height:20px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:14px;font-weight:bold;color:#fff;transition:all 0.3s ease}.quality-status.ready{background:#4CAF50}.quality-status.not-ready{background:#f44336}.card-outline.quality-warning{animation:qualityWarning 1s ease-in-out infinite alternate}@keyframes qualityWarning{0%{box-shadow:0 0 0 3px rgba(255, 152, 0, 0.6)}100%{box-shadow:0 0 0 6px rgba(255, 152, 0, 0.3)}}.capture-animation{position:absolute;top:0;left:0;width:100%;height:100%;background:#fff;opacity:0;z-index:30;pointer-events:none;animation:captureFlash 0.6s ease-out}@keyframes captureFlash{0%{opacity:0}15%{opacity:0.8}30%{opacity:0}45%{opacity:0.4}60%{opacity:0}100%{opacity:0}}.card-outline.capturing{animation:pulseGreen 0.6s ease-out}@keyframes pulseGreen{0%{border-color:#28a745;box-shadow:0 0 0 4px rgba(40, 167, 69, 0)}50%{border-color:#28a745;box-shadow:0 0 0 8px rgba(40, 167, 69, 0.6)}100%{border-color:#28a745;box-shadow:0 0 0 4px rgba(40, 167, 69, 0)}}.flip-animation{position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);z-index:35;pointer-events:none;opacity:0;animation:showFlipInstruction 3s ease-in-out;display:flex;flex-direction:column;align-items:center;justify-content:center}.id-card-icon{width:80px;height:50px;background:linear-gradient(135deg, #6c757d 0%, #495057 100%);border-radius:8px;position:relative;animation:flipCard 2s ease-in-out infinite;box-shadow:0 4px 12px rgba(0, 0, 0, 0.3)}.id-card-icon::before{content:'';position:absolute;top:8px;left:8px;width:16px;height:12px;background:rgba(255, 255, 255, 0.9);border-radius:2px}.id-card-icon::after{content:'';position:absolute;top:25px;left:8px;width:64px;height:3px;background:rgba(255, 255, 255, 0.7);border-radius:1px;box-shadow:0 6px 0 rgba(255, 255, 255, 0.7), 0 12px 0 rgba(255, 255, 255, 0.7)}.flip-text{margin-top:12px;color:#fff;font-size:14px;font-weight:600;text-align:center;background:rgba(128, 128, 128, 0.8);padding:12px 20px;border-radius:20px;max-width:300px}@keyframes showFlipInstruction{0%{opacity:0;transform:translate(-50%, -50%) scale(0.8)}15%{opacity:1;transform:translate(-50%, -50%) scale(1)}85%{opacity:1;transform:translate(-50%, -50%) scale(1)}100%{opacity:0;transform:translate(-50%, -50%) scale(0.8)}}@keyframes flipCard{0%{transform:rotateY(0deg)}50%{transform:rotateY(180deg)}100%{transform:rotateY(360deg)}}.success-animation{position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);z-index:35;pointer-events:none;opacity:0;animation:showSuccessMessage 3s ease-in-out;display:flex;flex-direction:column;align-items:center;justify-content:center}.check-icon{width:80px;height:80px;background:linear-gradient(135deg, #6c757d 0%, #495057 100%);border-radius:50%;position:relative;animation:bounceSuccess 0.6s ease-out;box-shadow:0 4px 16px rgba(108, 117, 125, 0.4);display:flex;align-items:center;justify-content:center}.check-icon::before{content:'';position:absolute;width:20px;height:35px;border:4px solid #fff;border-top:none;border-left:none;transform:rotate(45deg);animation:drawCheck 0.4s ease-out 0.2s both}.success-text{margin-top:16px;color:#fff;font-size:14px;font-weight:600;text-align:center;background:rgba(128, 128, 128, 0.8);padding:12px 20px;border-radius:20px;max-width:300px;animation:fadeInUp 0.5s ease-out 0.4s both}@keyframes showSuccessMessage{0%{opacity:0;transform:translate(-50%, -50%) scale(0.5)}15%{opacity:1;transform:translate(-50%, -50%) scale(1)}85%{opacity:1;transform:translate(-50%, -50%) scale(1)}100%{opacity:0;transform:translate(-50%, -50%) scale(0.9)}}@keyframes bounceSuccess{0%{transform:scale(0)}50%{transform:scale(1.1)}100%{transform:scale(1)}}@keyframes drawCheck{0%{width:0;height:0}50%{width:20px;height:0}100%{width:20px;height:35px}}@keyframes fadeInUp{0%{opacity:0;transform:translateY(20px)}100%{opacity:1;transform:translateY(0)}}.skip-button{position:absolute;bottom:20px;left:50%;transform:translateX(-50%);z-index:25;pointer-events:auto;background:#fff;color:#333;border:none;border-radius:25px;padding:12px 24px;font-size:14px;font-weight:600;cursor:pointer;transition:all 0.3s ease}.skip-button:hover{background:#f8f9fa}.skip-button:active{background:#e9ecef;transform:translateX(-50%) translateY(0)}.camera-controls{position:absolute;top:16px;right:16px;z-index:25;display:flex;gap:8px;pointer-events:auto}.camera-selector-button{height:32px;padding:0 10px;border:none;border-radius:8px;background:rgba(0, 0, 0, 0.25);backdrop-filter:blur(12px);color:#ffffff;font-size:12px;font-weight:500;cursor:pointer;transition:all 0.2s ease;display:flex;align-items:center;justify-content:center;border:1px solid rgba(255, 255, 255, 0.1);white-space:nowrap;min-width:fit-content}.camera-selector-button:hover{background:rgba(0, 0, 0, 0.8);border-color:rgba(255, 255, 255, 0.2);transform:translateY(-1px)}.camera-selector-button:active{transform:translateY(0);background:rgba(0, 0, 0, 0.9)}.camera-selector-button:disabled,.camera-selector-button.loading{opacity:0.7;cursor:not-allowed;pointer-events:none}.camera-selector-button:disabled:hover,.camera-selector-button.loading:hover{transform:none;background:rgba(0, 0, 0, 0.6);border-color:rgba(255, 255, 255, 0.1)}.button-spinner{width:16px;height:16px;border:2px solid rgba(255, 255, 255, 0.3);border-top:2px solid #ffffff;border-radius:50%;animation:spin 1s linear infinite}@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}.camera-selector-dropdown{position:absolute;top:56px;right:16px;z-index:30;background:rgba(0, 0, 0, 0.25);backdrop-filter:blur(20px);border-radius:12px;min-width:260px;max-width:300px;border:1px solid rgba(255, 255, 255, 0.1);overflow:hidden;pointer-events:auto;animation:slideInFromTop 0.3s ease-out}@keyframes slideInFromTop{0%{opacity:0;transform:translateY(-8px) scale(0.95)}100%{opacity:1;transform:translateY(0) scale(1)}}.camera-selector-header{display:flex;justify-content:space-between;align-items:center;padding:8px 12px;border-bottom:1px solid rgba(255, 255, 255, 0.1)}.camera-selector-header span{font-weight:500;color:#ffffff;font-size:13px;opacity:0.9}.close-selector{width:20px;height:20px;border:none;background:none;color:rgba(255, 255, 255, 0.7);font-size:16px;cursor:pointer;display:flex;align-items:center;justify-content:center;border-radius:4px;transition:all 0.2s ease}.close-selector:hover{background:rgba(255, 255, 255, 0.1);color:#ffffff}.camera-list{padding:4px 0;max-height:240px;overflow-y:auto}.camera-option{width:100%;padding:8px 12px;border:none;background:none;text-align:left;cursor:pointer;transition:all 0.2s ease;display:flex;justify-content:space-between;align-items:center;color:rgba(255, 255, 255, 0.9)}.camera-option:hover{background:rgba(255, 255, 255, 0.08)}.camera-option.selected{background:rgba(255, 255, 255, 0.12);color:#ffffff}.camera-label{font-size:13px;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin-right:8px;font-weight:400}.selected-indicator{font-size:14px;color:#ffffff;opacity:0.9}.device-info{padding:6px 12px;border-top:1px solid rgba(255, 255, 255, 0.1);background:rgba(255, 255, 255, 0.05)}.device-info small{color:rgba(255, 255, 255, 0.6);font-size:11px;text-transform:capitalize;font-weight:400}@media (max-width: 480px){.camera-controls{top:12px;right:12px;gap:6px}.camera-selector-button{height:36px;padding:0 12px;font-size:11px;border-radius:6px}.camera-selector-dropdown{right:12px;top:48px;min-width:240px;max-width:calc(100vw - 24px)}.camera-selector-header{padding:6px 10px}.camera-option{padding:6px 10px}.device-info{padding:4px 10px}}.watermark{position:absolute;bottom:12px;right:12px;z-index:15;pointer-events:none;opacity:0.7}.watermark img{height:24px;width:auto;filter:drop-shadow(0 1px 2px rgba(0, 0, 0, 0.3))}.component-status{position:absolute;top:16px;left:16px;background:rgba(0, 0, 0, 0.25);backdrop-filter:blur(20px);border-radius:12px;border:1px solid rgba(255, 255, 255, 0.1);display:flex;align-items:center;gap:6px;padding:6px 10px;z-index:40;height:32px;width:fit-content;animation:slideInFromTop 0.3s ease-out}.status-spinner{width:16px;height:16px;border:1px solid rgba(255, 255, 255, 0.3);border-top:1px solid #ffffff;border-radius:50%;animation:statusSpin 1s linear infinite;flex-shrink:0}.status-content{display:flex;flex-direction:column;gap:1px}.status-message{color:#ffffff;font-size:12px;font-weight:500;margin:0}.status-description{color:rgba(255, 255, 255, 0.7);font-size:10px;line-height:1.2;margin:0}@keyframes statusSpin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}@media (max-width: 480px){.component-status{top:12px;left:12px;padding:4px 8px;gap:4px;height:28px;border-radius:8px}.status-spinner{width:14px;height:14px}.status-message{font-size:11px}.status-description{font-size:9px}}.loading-overlay{position:absolute;top:0;left:0;width:100%;height:100%;background:rgba(0, 0, 0, 0.85);backdrop-filter:blur(4px);display:flex;flex-direction:column;align-items:center;justify-content:center;z-index:30;border-radius:8px}.loading-spinner{width:50px;height:50px;border:3px solid rgba(255, 255, 255, 0.15);border-top:3px solid #28a745;border-radius:50%;animation:spin 1s ease-in-out infinite;margin-bottom:16px}.loading-text{color:#ffffff;font-size:14px;font-weight:500;text-align:center;opacity:0.9;margin-bottom:4px}.loading-description{color:rgba(255, 255, 255, 0.7);font-size:12px;text-align:center;opacity:0.8}@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}.performance-monitor{position:absolute;top:70px;left:16px;background:rgba(0, 0, 0, 0.25);backdrop-filter:blur(20px);border-radius:12px;border:1px solid rgba(255, 255, 255, 0.1);z-index:35;width:fit-content;animation:slideInFromLeft 0.3s ease-out;transition:all 0.3s ease}@keyframes slideInFromLeft{0%{opacity:0;transform:translateX(-20px) scale(0.95)}100%{opacity:1;transform:translateX(0) scale(1)}}.performance-expanded{padding:6px 10px}.metrics-row{display:flex;gap:8px;margin-bottom:4px}.metrics-row:last-child{margin-bottom:0}.metric-compact{display:flex;flex-direction:column;align-items:center;gap:2px;min-width:50px}.metric-label{font-size:9px;color:rgba(255, 255, 255, 0.6);font-weight:500;text-transform:uppercase;letter-spacing:0.3px}.metric-value{font-size:10px;font-weight:600;font-family:'Monaco', 'Menlo', 'Consolas', monospace;padding:2px 4px;border-radius:3px;text-align:center;white-space:nowrap}.metric-value.good{color:#4ade80;background:rgba(74, 222, 128, 0.1);border:1px solid rgba(74, 222, 128, 0.2)}.metric-value.warning{color:#fbbf24;background:rgba(251, 191, 36, 0.1);border:1px solid rgba(251, 191, 36, 0.2)}.metric-value.danger{color:#f87171;background:rgba(248, 113, 113, 0.1);border:1px solid rgba(248, 113, 113, 0.2)}@media (max-width: 480px){.performance-monitor{top:60px;left:12px;width:fit-content}.performance-expanded{padding:4px 8px}.metrics-row{gap:6px;margin-bottom:3px}.metric-compact{min-width:45px;gap:1px}.metric-label{font-size:8px}.metric-value{font-size:9px;padding:1px 3px}}.quality-monitor{position:absolute;top:200px;left:16px;background:rgba(0, 0, 0, 0.25);backdrop-filter:blur(20px);border-radius:12px;border:1px solid rgba(255, 255, 255, 0.1);z-index:35;width:fit-content;animation:slideInFromLeft 0.3s ease-out;transition:all 0.3s ease}.quality-text{padding:6px 10px;font-size:11px;color:white;font-family:'Monaco', 'Menlo', 'Consolas', monospace;line-height:1.4}.quality-fail{color:#ff9999}@keyframes pulse{0%,100%{opacity:1}50%{opacity:0.7}}.metric-value.danger{animation:pulse 2s infinite}@media (max-width: 480px){.quality-monitor{top:160px;left:12px}.quality-text{padding:4px 8px;font-size:10px}}";
|
|
6
1147
|
|
|
7
1148
|
const JaakStamps = class {
|
|
8
1149
|
constructor(hostRef) {
|
|
@@ -12,131 +1153,223 @@ const JaakStamps = class {
|
|
|
12
1153
|
}
|
|
13
1154
|
get el() { return index.getElement(this); }
|
|
14
1155
|
debug = false;
|
|
15
|
-
alignmentTolerance =
|
|
16
|
-
maskSize =
|
|
17
|
-
cropMargin =
|
|
18
|
-
useDocumentClassification = false;
|
|
1156
|
+
alignmentTolerance = 15;
|
|
1157
|
+
maskSize = 80;
|
|
1158
|
+
cropMargin = 20;
|
|
1159
|
+
useDocumentClassification = false;
|
|
1160
|
+
preferredCamera = 'auto';
|
|
1161
|
+
captureDelay = 1500;
|
|
19
1162
|
captureCompleted;
|
|
20
1163
|
isReady;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
statusColor = '#aaa';
|
|
24
|
-
bestScore = 0;
|
|
25
|
-
capturedFullFrame = null;
|
|
26
|
-
capturedCroppedId = null;
|
|
27
|
-
capturedBackFullFrame = null;
|
|
28
|
-
capturedBackCroppedId = null;
|
|
29
|
-
captureStep = 'front';
|
|
30
|
-
isCapturing = false;
|
|
31
|
-
showFlipAnimation = false;
|
|
32
|
-
showSuccessAnimation = false;
|
|
33
|
-
shouldMirrorVideo = true;
|
|
1164
|
+
// State derived from services
|
|
1165
|
+
detectionBoxes = [];
|
|
34
1166
|
sideAlignment = {
|
|
35
|
-
top: false,
|
|
36
|
-
right: false,
|
|
37
|
-
bottom: false,
|
|
38
|
-
left: false
|
|
1167
|
+
top: false, right: false, bottom: false, left: false
|
|
39
1168
|
};
|
|
40
|
-
isDetectionPaused = false;
|
|
41
|
-
isLoading = false;
|
|
42
|
-
isModelPreloaded = false;
|
|
43
1169
|
isMaskReady = false;
|
|
1170
|
+
shouldMirrorVideo = true;
|
|
1171
|
+
showCameraSelector = false;
|
|
1172
|
+
isSwitchingCamera = false;
|
|
1173
|
+
hasDocumentDetected = false;
|
|
1174
|
+
currentStatus = {
|
|
1175
|
+
message: 'Inicializando componente...',
|
|
1176
|
+
description: 'Configurando servicios y cargando recursos',
|
|
1177
|
+
type: 'initializing',
|
|
1178
|
+
isInitialized: false
|
|
1179
|
+
};
|
|
1180
|
+
performanceData = {
|
|
1181
|
+
fps: 0,
|
|
1182
|
+
inferenceTime: 0,
|
|
1183
|
+
memoryUsage: 0,
|
|
1184
|
+
onnxLoadTime: 0,
|
|
1185
|
+
frameProcessingTime: 0,
|
|
1186
|
+
totalDetections: 0,
|
|
1187
|
+
successfulDetections: 0,
|
|
1188
|
+
detectionRate: 0
|
|
1189
|
+
};
|
|
1190
|
+
// Services
|
|
1191
|
+
serviceContainer;
|
|
1192
|
+
logger;
|
|
1193
|
+
eventBus;
|
|
1194
|
+
stateManager;
|
|
1195
|
+
cameraService;
|
|
1196
|
+
detectionService;
|
|
1197
|
+
// UI References
|
|
44
1198
|
videoRef;
|
|
45
|
-
|
|
46
|
-
session;
|
|
47
|
-
startTime;
|
|
1199
|
+
detectionContainer;
|
|
48
1200
|
videoStream;
|
|
49
1201
|
animationId;
|
|
50
|
-
|
|
1202
|
+
// Detection state
|
|
51
1203
|
lastDetectedBox;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
1204
|
+
startTime;
|
|
1205
|
+
hasScreenshotTaken = false;
|
|
1206
|
+
alignmentStartTime;
|
|
1207
|
+
alignmentTimer;
|
|
1208
|
+
// Performance monitoring
|
|
1209
|
+
performanceMetrics = {
|
|
1210
|
+
fps: 0,
|
|
1211
|
+
inferenceTime: 0,
|
|
1212
|
+
memoryUsage: 0,
|
|
1213
|
+
cpuUsage: 0,
|
|
1214
|
+
onnxLoadTime: 0,
|
|
1215
|
+
frameProcessingTime: 0,
|
|
1216
|
+
totalDetections: 0,
|
|
1217
|
+
successfulDetections: 0,
|
|
1218
|
+
detectionRate: 0,
|
|
1219
|
+
lastUpdateTime: 0
|
|
1220
|
+
};
|
|
1221
|
+
performanceUpdateInterval;
|
|
1222
|
+
// Performance optimization
|
|
55
1223
|
frameSkipCounter = 0;
|
|
56
|
-
FRAME_SKIP = 2;
|
|
1224
|
+
FRAME_SKIP = 2;
|
|
57
1225
|
consecutiveFailures = 0;
|
|
58
|
-
MAX_FAILURES = 30;
|
|
1226
|
+
MAX_FAILURES = 30;
|
|
59
1227
|
lastInferenceTime = 0;
|
|
60
|
-
MIN_INFERENCE_INTERVAL = 50;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
1228
|
+
MIN_INFERENCE_INTERVAL = 50;
|
|
1229
|
+
async componentDidLoad() {
|
|
1230
|
+
this.updateStatus('Iniciando servicios...', 'Configurando módulos internos', 'initializing');
|
|
1231
|
+
await this.initializeServices();
|
|
1232
|
+
this.updateStatus('Configurando eventos...', 'Preparando comunicación entre servicios', 'initializing');
|
|
1233
|
+
await this.setupEventListeners();
|
|
1234
|
+
this.updateStatus('Inicializando cámara...', 'Detectando dispositivos disponibles', 'initializing');
|
|
1235
|
+
await this.initializeComponent();
|
|
1236
|
+
if (this.debug) {
|
|
1237
|
+
this.initializePerformanceMonitor();
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
async initializeServices() {
|
|
1241
|
+
const config = {
|
|
1242
|
+
debug: this.debug,
|
|
1243
|
+
alignmentTolerance: this.alignmentTolerance,
|
|
1244
|
+
maskSize: this.maskSize,
|
|
1245
|
+
cropMargin: this.cropMargin,
|
|
1246
|
+
useDocumentClassification: this.useDocumentClassification,
|
|
1247
|
+
preferredCamera: this.preferredCamera,
|
|
1248
|
+
captureDelay: this.captureDelay
|
|
1249
|
+
};
|
|
1250
|
+
this.serviceContainer = new ServiceContainer(config);
|
|
1251
|
+
this.logger = this.serviceContainer.getLogger();
|
|
1252
|
+
this.eventBus = this.serviceContainer.getEventBus();
|
|
1253
|
+
this.stateManager = this.serviceContainer.getStateManager();
|
|
1254
|
+
this.cameraService = this.serviceContainer.getCameraService();
|
|
1255
|
+
this.detectionService = this.serviceContainer.getDetectionService();
|
|
1256
|
+
this.logger.state('SERVICIOS_INICIALIZADOS', { timestamp: Date.now() });
|
|
1257
|
+
}
|
|
1258
|
+
async setupEventListeners() {
|
|
1259
|
+
this.eventBus.on('state-changed', (data) => {
|
|
1260
|
+
this.handleStateChange(data);
|
|
1261
|
+
});
|
|
1262
|
+
this.eventBus.on('camera-changed', (cameraId) => {
|
|
1263
|
+
this.logger.state('CAMARA_CAMBIADA_EVENT', { cameraId });
|
|
1264
|
+
});
|
|
1265
|
+
this.eventBus.on('error', (error) => {
|
|
1266
|
+
this.logger.error('Error en servicio:', error);
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
async initializeComponent() {
|
|
1270
|
+
this.logger.state('COMPONENTE_INICIALIZANDO', {
|
|
1271
|
+
debug: this.debug,
|
|
1272
|
+
maskSize: this.maskSize,
|
|
1273
|
+
cropMargin: this.cropMargin,
|
|
1274
|
+
useDocumentClassification: this.useDocumentClassification,
|
|
1275
|
+
preferredCamera: this.preferredCamera,
|
|
1276
|
+
captureDelay: this.captureDelay
|
|
1277
|
+
});
|
|
1278
|
+
this.validateProps();
|
|
74
1279
|
if (this.debug) {
|
|
75
|
-
|
|
1280
|
+
this.stateManager.updateCaptureState({ isLoading: true });
|
|
1281
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
76
1282
|
}
|
|
1283
|
+
this.updateStatus('Detectando cámaras...', 'Buscando dispositivos de captura', 'initializing');
|
|
1284
|
+
await this.cameraService.detectDeviceType();
|
|
1285
|
+
await this.cameraService.enumerateDevices();
|
|
1286
|
+
this.updateStatus('Cargando modelo IA...', 'Preparando reconocimiento de documentos', 'initializing');
|
|
1287
|
+
await this.loadOnnxRuntime();
|
|
1288
|
+
this.initializeResizeObserver();
|
|
77
1289
|
}
|
|
78
|
-
|
|
1290
|
+
validateProps() {
|
|
79
1291
|
if (this.maskSize < 50 || this.maskSize > 100) {
|
|
80
|
-
|
|
1292
|
+
this.logger.warn(`Propiedad maskSize inválida. Valor: ${this.maskSize}, esperado: 50-100. Usando valor por defecto: 90`);
|
|
81
1293
|
this.maskSize = 90;
|
|
82
1294
|
}
|
|
83
|
-
}
|
|
84
|
-
validateCropMargin() {
|
|
85
1295
|
if (this.cropMargin < 0 || this.cropMargin > 100) {
|
|
86
|
-
|
|
1296
|
+
this.logger.warn(`Propiedad cropMargin inválida. Valor: ${this.cropMargin}, esperado: 0-100. Usando valor por defecto: 0`);
|
|
87
1297
|
this.cropMargin = 0;
|
|
88
1298
|
}
|
|
1299
|
+
const validOptions = ['auto', 'front', 'back'];
|
|
1300
|
+
if (!validOptions.includes(this.preferredCamera)) {
|
|
1301
|
+
this.logger.warn(`Propiedad preferredCamera inválida. Valor: ${this.preferredCamera}, esperado: ${validOptions.join(', ')}. Usando valor por defecto: 'auto'`);
|
|
1302
|
+
this.preferredCamera = 'auto';
|
|
1303
|
+
}
|
|
1304
|
+
if (this.captureDelay < 0 || this.captureDelay > 10000) {
|
|
1305
|
+
this.logger.warn(`Propiedad captureDelay inválida. Valor: ${this.captureDelay}, esperado: 0-10000. Usando valor por defecto: 1500`);
|
|
1306
|
+
this.captureDelay = 1500;
|
|
1307
|
+
}
|
|
89
1308
|
}
|
|
90
|
-
|
|
91
|
-
const isDocumentReady = !!window.ort && this.isModelPreloaded;
|
|
92
|
-
this.isReady.emit(isDocumentReady);
|
|
93
|
-
this.debugLog('🟢 isReady event emitted:', isDocumentReady);
|
|
94
|
-
}
|
|
95
|
-
isRearCamera(stream) {
|
|
96
|
-
const videoTrack = stream.getVideoTracks()[0];
|
|
97
|
-
if (!videoTrack)
|
|
98
|
-
return false;
|
|
99
|
-
const settings = videoTrack.getSettings();
|
|
100
|
-
return settings.facingMode === 'environment';
|
|
101
|
-
}
|
|
102
|
-
async componentDidLoad() {
|
|
103
|
-
this.validateMaskSize();
|
|
104
|
-
this.validateCropMargin();
|
|
1309
|
+
async loadOnnxRuntime() {
|
|
105
1310
|
if (!window.ort) {
|
|
1311
|
+
this.updateStatus('Descargando librerías...', 'Obteniendo recursos de reconocimiento', 'initializing');
|
|
106
1312
|
const script = document.createElement('script');
|
|
107
1313
|
script.src = 'https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js';
|
|
108
1314
|
document.head.appendChild(script);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
1315
|
+
await new Promise((resolve) => {
|
|
1316
|
+
script.onload = () => {
|
|
1317
|
+
setTimeout(() => {
|
|
1318
|
+
this.finalizeInitialization();
|
|
1319
|
+
resolve(undefined);
|
|
1320
|
+
}, 300);
|
|
1321
|
+
};
|
|
1322
|
+
});
|
|
113
1323
|
}
|
|
114
1324
|
else {
|
|
115
|
-
|
|
116
|
-
|
|
1325
|
+
setTimeout(() => {
|
|
1326
|
+
this.finalizeInitialization();
|
|
1327
|
+
}, 300);
|
|
117
1328
|
}
|
|
118
|
-
// Initialize canvas pool for better performance
|
|
119
|
-
this.initializeCanvasPool();
|
|
120
|
-
this.setupResizeObserver();
|
|
121
1329
|
}
|
|
122
|
-
|
|
123
|
-
|
|
1330
|
+
finalizeInitialization() {
|
|
1331
|
+
this.stateManager.updateCaptureState({ isLoading: false });
|
|
1332
|
+
this.currentStatus = {
|
|
1333
|
+
message: 'Listo para capturar',
|
|
1334
|
+
description: '',
|
|
1335
|
+
type: 'ready',
|
|
1336
|
+
isInitialized: true
|
|
1337
|
+
};
|
|
1338
|
+
this.emitReadyEvent();
|
|
1339
|
+
}
|
|
1340
|
+
updateStatus(message, description, type = 'loading') {
|
|
1341
|
+
this.currentStatus = {
|
|
1342
|
+
message,
|
|
1343
|
+
description,
|
|
1344
|
+
type,
|
|
1345
|
+
isInitialized: this.currentStatus.isInitialized
|
|
1346
|
+
};
|
|
1347
|
+
}
|
|
1348
|
+
emitReadyEvent() {
|
|
1349
|
+
const isDocumentReady = !!window.ort && this.detectionService.isModelLoaded();
|
|
1350
|
+
this.isReady.emit(isDocumentReady);
|
|
1351
|
+
this.logger.state('COMPONENTE_LISTO', {
|
|
1352
|
+
ortLibraryLoaded: !!window.ort,
|
|
1353
|
+
modelPreloaded: this.detectionService.isModelLoaded(),
|
|
1354
|
+
isReady: isDocumentReady
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
handleStateChange(data) {
|
|
1358
|
+
}
|
|
1359
|
+
initializeResizeObserver() {
|
|
1360
|
+
if ('ResizeObserver' in window && this.detectionContainer) {
|
|
124
1361
|
const resizeObserver = new ResizeObserver(() => {
|
|
125
1362
|
this.handleResize();
|
|
126
1363
|
});
|
|
127
|
-
resizeObserver.observe(this.
|
|
1364
|
+
resizeObserver.observe(this.detectionContainer.parentElement);
|
|
128
1365
|
}
|
|
129
1366
|
}
|
|
130
1367
|
handleResize() {
|
|
131
|
-
if (this.
|
|
132
|
-
const container = this.
|
|
1368
|
+
if (this.detectionContainer) {
|
|
1369
|
+
const container = this.detectionContainer.parentElement;
|
|
133
1370
|
const rect = container.getBoundingClientRect();
|
|
134
|
-
// Set canvas size to match container
|
|
135
|
-
this.canvasRef.width = rect.width;
|
|
136
|
-
this.canvasRef.height = rect.height;
|
|
137
|
-
// Update mask positioning based on container and video dimensions
|
|
138
1371
|
this.updateMaskDimensions(rect);
|
|
139
|
-
this.
|
|
1372
|
+
this.logger.debug('Container redimensionado:', { width: rect.width, height: rect.height });
|
|
140
1373
|
}
|
|
141
1374
|
}
|
|
142
1375
|
updateMaskDimensions(containerRect) {
|
|
@@ -153,32 +1386,27 @@ const JaakStamps = class {
|
|
|
153
1386
|
let displayedVideoWidth, displayedVideoHeight;
|
|
154
1387
|
let videoOffsetX = 0, videoOffsetY = 0;
|
|
155
1388
|
if (videoAspectRatio > containerAspectRatio) {
|
|
156
|
-
// Video is wider - letterboxed (black bars top/bottom)
|
|
157
1389
|
displayedVideoWidth = containerRect.width;
|
|
158
1390
|
displayedVideoHeight = containerRect.width / videoAspectRatio;
|
|
159
1391
|
videoOffsetY = (containerRect.height - displayedVideoHeight) / 2;
|
|
160
1392
|
}
|
|
161
1393
|
else {
|
|
162
|
-
// Video is taller - pillarboxed (black bars left/right)
|
|
163
1394
|
displayedVideoHeight = containerRect.height;
|
|
164
1395
|
displayedVideoWidth = containerRect.height * videoAspectRatio;
|
|
165
1396
|
videoOffsetX = (containerRect.width - displayedVideoWidth) / 2;
|
|
166
1397
|
}
|
|
167
|
-
// Calculate
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const maxWidthByHeight = displayedVideoHeight * this.ID1_ASPECT_RATIO;
|
|
1398
|
+
// Calculate mask dimensions with ID-1 aspect ratio
|
|
1399
|
+
const ID1_ASPECT_RATIO = 85.60 / 53.98;
|
|
1400
|
+
const maxWidthByHeight = displayedVideoHeight * ID1_ASPECT_RATIO;
|
|
171
1401
|
let maskWidthInVideo, maskHeightInVideo;
|
|
172
1402
|
const sizeRatio = this.maskSize / 100;
|
|
173
1403
|
if (maxWidthByHeight <= displayedVideoWidth) {
|
|
174
|
-
// Video height is the limiting factor
|
|
175
1404
|
maskHeightInVideo = displayedVideoHeight * sizeRatio;
|
|
176
|
-
maskWidthInVideo = maskHeightInVideo *
|
|
1405
|
+
maskWidthInVideo = maskHeightInVideo * ID1_ASPECT_RATIO;
|
|
177
1406
|
}
|
|
178
1407
|
else {
|
|
179
|
-
// Video width is the limiting factor
|
|
180
1408
|
maskWidthInVideo = displayedVideoWidth * sizeRatio;
|
|
181
|
-
maskHeightInVideo = maskWidthInVideo /
|
|
1409
|
+
maskHeightInVideo = maskWidthInVideo / ID1_ASPECT_RATIO;
|
|
182
1410
|
}
|
|
183
1411
|
// Convert to percentages of the container
|
|
184
1412
|
const maskWidthPercent = (maskWidthInVideo / containerRect.width) * 100;
|
|
@@ -194,9 +1422,8 @@ const JaakStamps = class {
|
|
|
194
1422
|
this.el.style.setProperty('--mask-height', `${maskHeightPercent}%`);
|
|
195
1423
|
this.el.style.setProperty('--mask-center-x', `${videoCenterXPercent}%`);
|
|
196
1424
|
this.el.style.setProperty('--mask-center-y', `${videoCenterYPercent}%`);
|
|
197
|
-
// Mark mask as ready now that dimensions are calculated
|
|
198
1425
|
this.isMaskReady = true;
|
|
199
|
-
this.
|
|
1426
|
+
this.logger.state('DIMENSIONES_MASCARA_ACTUALIZADAS', {
|
|
200
1427
|
video: { width: videoWidth, height: videoHeight },
|
|
201
1428
|
displayed: { width: displayedVideoWidth, height: displayedVideoHeight },
|
|
202
1429
|
mask: { widthPercent: maskWidthPercent, heightPercent: maskHeightPercent },
|
|
@@ -204,445 +1431,270 @@ const JaakStamps = class {
|
|
|
204
1431
|
offset: { x: videoOffsetX, y: videoOffsetY }
|
|
205
1432
|
});
|
|
206
1433
|
}
|
|
207
|
-
|
|
208
|
-
// Preprocess canvas - reused for every inference
|
|
209
|
-
this.preprocessCanvas = document.createElement('canvas');
|
|
210
|
-
this.preprocessCanvas.width = this.INPUT_SIZE;
|
|
211
|
-
this.preprocessCanvas.height = this.INPUT_SIZE;
|
|
212
|
-
this.preprocessCtx = this.preprocessCanvas.getContext('2d', {
|
|
213
|
-
alpha: false, // No transparency needed
|
|
214
|
-
willReadFrequently: true // Optimize for frequent getImageData calls
|
|
215
|
-
});
|
|
216
|
-
// Capture canvas - reused for screenshots
|
|
217
|
-
this.captureCanvas = document.createElement('canvas');
|
|
218
|
-
this.captureCtx = this.captureCanvas.getContext('2d', {
|
|
219
|
-
alpha: false
|
|
220
|
-
});
|
|
221
|
-
this.debugLog('🎨 Canvas pool initialized for performance');
|
|
222
|
-
}
|
|
223
|
-
disconnectedCallback() {
|
|
224
|
-
this.cleanup();
|
|
225
|
-
}
|
|
1434
|
+
// PUBLIC METHODS
|
|
226
1435
|
async getCapturedImages() {
|
|
227
|
-
|
|
1436
|
+
const state = this.stateManager.getCaptureState();
|
|
1437
|
+
if (state.step !== 'completed') {
|
|
228
1438
|
throw new Error('El proceso de captura no ha sido completado');
|
|
229
1439
|
}
|
|
230
|
-
return
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
await this.startDetection();
|
|
250
|
-
}
|
|
251
|
-
async stopCapture() {
|
|
252
|
-
this.exitSession();
|
|
253
|
-
}
|
|
254
|
-
async resetCapture() {
|
|
255
|
-
this.resetDetection();
|
|
256
|
-
}
|
|
257
|
-
async getStatus() {
|
|
258
|
-
return {
|
|
259
|
-
isVideoActive: this.isVideoActive,
|
|
260
|
-
captureStep: this.captureStep,
|
|
261
|
-
statusMessage: this.statusMessage,
|
|
262
|
-
hasImages: !!(this.capturedFullFrame || this.capturedBackFullFrame),
|
|
263
|
-
isProcessCompleted: this.captureStep === 'completed',
|
|
264
|
-
isModelPreloaded: this.isModelPreloaded
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
async preloadModel() {
|
|
268
|
-
if (this.isModelPreloaded || this.session) {
|
|
269
|
-
this.debugLog('🚀 Model already preloaded or session exists');
|
|
270
|
-
return { success: true, message: 'Model already loaded' };
|
|
271
|
-
}
|
|
272
|
-
try {
|
|
273
|
-
this.isLoading = true;
|
|
274
|
-
this.statusMessage = "Precargando modelos...";
|
|
275
|
-
this.statusColor = "#007bff";
|
|
276
|
-
const modelPath = this.MODEL_PATH;
|
|
277
|
-
this.debugLog('🤖 Preloading detection model:', modelPath);
|
|
278
|
-
// Configure ONNX Runtime with device-specific optimizations
|
|
279
|
-
const sessionOptions = this.getSessionOptions();
|
|
280
|
-
this.session = await window.ort.InferenceSession.create(modelPath, sessionOptions);
|
|
281
|
-
// Preload MobileNet model and classes only if classification is enabled
|
|
282
|
-
if (this.useDocumentClassification) {
|
|
283
|
-
await this.loadMobileNetModel();
|
|
284
|
-
}
|
|
285
|
-
this.isModelPreloaded = true;
|
|
286
|
-
this.isLoading = false;
|
|
287
|
-
this.statusMessage = "Modelos precargados. Listo para comenzar detección";
|
|
288
|
-
this.statusColor = "#28a745";
|
|
289
|
-
this.emitReadyEvent();
|
|
290
|
-
this.debugLog('✅ Models preloaded successfully');
|
|
291
|
-
return { success: true, message: 'Models preloaded successfully' };
|
|
292
|
-
}
|
|
293
|
-
catch (error) {
|
|
294
|
-
this.debugLog('❌ Error preloading models:', error);
|
|
295
|
-
this.isLoading = false;
|
|
296
|
-
this.statusMessage = "Error al precargar los modelos";
|
|
297
|
-
this.statusColor = "#ff6b6b";
|
|
298
|
-
return { success: false, error: error.message };
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
async skipBackCapture() {
|
|
302
|
-
if (this.captureStep === 'back' && this.capturedFullFrame) {
|
|
303
|
-
this.completeProcess(true);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
async loadMobileNetModel() {
|
|
307
|
-
try {
|
|
308
|
-
this.debugLog('🤖 Loading MobileNet model...');
|
|
309
|
-
// Load class map
|
|
310
|
-
const classResponse = await fetch(this.MOBILENET_CLASSES_PATH);
|
|
311
|
-
if (!classResponse.ok) {
|
|
312
|
-
throw new Error(`Failed to load class map: ${this.MOBILENET_CLASSES_PATH}`);
|
|
313
|
-
}
|
|
314
|
-
this.mobileNetClassMap = await classResponse.json();
|
|
315
|
-
this.debugLog('📋 MobileNet classes loaded:', this.mobileNetClassMap);
|
|
316
|
-
// Load model
|
|
317
|
-
const sessionOptions = this.getSessionOptions();
|
|
318
|
-
this.mobileNetSession = await window.ort.InferenceSession.create(this.MOBILENET_MODEL_PATH, sessionOptions);
|
|
319
|
-
this.debugLog('✅ MobileNet model loaded successfully');
|
|
320
|
-
}
|
|
321
|
-
catch (error) {
|
|
322
|
-
this.debugLog('❌ Error loading MobileNet model:', error);
|
|
323
|
-
throw error;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
preprocessMobileNet(canvas) {
|
|
327
|
-
// Create a temporary canvas for MobileNet preprocessing (224x224)
|
|
328
|
-
const tempCanvas = document.createElement('canvas');
|
|
329
|
-
tempCanvas.width = 224;
|
|
330
|
-
tempCanvas.height = 224;
|
|
331
|
-
const tempCtx = tempCanvas.getContext('2d');
|
|
332
|
-
// Draw the cropped image onto the 224x224 canvas
|
|
333
|
-
tempCtx.drawImage(canvas, 0, 0, 224, 224);
|
|
334
|
-
// Get image data and preprocess for MobileNet
|
|
335
|
-
const imageData = tempCtx.getImageData(0, 0, 224, 224);
|
|
336
|
-
const data = imageData.data;
|
|
337
|
-
const hw = 224 * 224;
|
|
338
|
-
const arr = new Float32Array(3 * hw);
|
|
339
|
-
// Normalize pixels: (pixel/255 - 0.5) / 0.5 = (pixel - 127.5) / 127.5
|
|
340
|
-
for (let i = 0; i < hw; i++) {
|
|
341
|
-
arr[i] = (data[i * 4 + 0] / 255 - 0.5) / 0.5; // R
|
|
342
|
-
arr[hw + i] = (data[i * 4 + 1] / 255 - 0.5) / 0.5; // G
|
|
343
|
-
arr[2 * hw + i] = (data[i * 4 + 2] / 255 - 0.5) / 0.5; // B
|
|
344
|
-
}
|
|
345
|
-
return new window.ort.Tensor('float32', arr, [1, 3, 224, 224]);
|
|
346
|
-
}
|
|
347
|
-
async classifyDocument(canvas) {
|
|
348
|
-
if (!this.mobileNetSession || !this.mobileNetClassMap) {
|
|
349
|
-
this.debugLog('⚠️ MobileNet model not loaded');
|
|
350
|
-
return null;
|
|
351
|
-
}
|
|
352
|
-
try {
|
|
353
|
-
this.debugLog('🔍 Classifying document...');
|
|
354
|
-
// Preprocess image for MobileNet
|
|
355
|
-
const inputTensor = this.preprocessMobileNet(canvas);
|
|
356
|
-
// Run inference
|
|
357
|
-
const feeds = { input: inputTensor };
|
|
358
|
-
const results = await this.mobileNetSession.run(feeds);
|
|
359
|
-
const output = results[Object.keys(results)[0]].data;
|
|
360
|
-
// Find the class with highest confidence
|
|
361
|
-
const maxIdx = output.reduce((bestIdx, val, idx, arr) => val > arr[bestIdx] ? idx : bestIdx, 0);
|
|
362
|
-
const confidence = output[maxIdx];
|
|
363
|
-
const className = this.mobileNetClassMap[maxIdx.toString()] || "unknown";
|
|
364
|
-
this.debugLog('📄 Document classification result:', {
|
|
365
|
-
class: className,
|
|
366
|
-
confidence: confidence.toFixed(3),
|
|
367
|
-
classIndex: maxIdx
|
|
368
|
-
});
|
|
369
|
-
return {
|
|
370
|
-
class: className,
|
|
371
|
-
confidence: confidence,
|
|
372
|
-
classIndex: maxIdx
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
catch (error) {
|
|
376
|
-
this.debugLog('❌ Error classifying document:', error);
|
|
377
|
-
return null;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
cleanup() {
|
|
381
|
-
if (this.animationId) {
|
|
382
|
-
cancelAnimationFrame(this.animationId);
|
|
383
|
-
}
|
|
384
|
-
if (this.videoStream) {
|
|
385
|
-
this.videoStream.getTracks().forEach(track => track.stop());
|
|
386
|
-
}
|
|
387
|
-
// Limpiar canvas en cleanup general
|
|
388
|
-
if (this.canvasRef) {
|
|
389
|
-
const ctx = this.canvasRef.getContext("2d");
|
|
390
|
-
if (ctx) {
|
|
391
|
-
ctx.clearRect(0, 0, this.canvasRef.width, this.canvasRef.height);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
// OPTIMIZATION: Clean up canvas pool
|
|
395
|
-
this.cleanupCanvasPool();
|
|
396
|
-
}
|
|
397
|
-
cleanupCanvasPool() {
|
|
398
|
-
// Release canvas references for garbage collection
|
|
399
|
-
if (this.preprocessCanvas) {
|
|
400
|
-
this.preprocessCtx = undefined;
|
|
401
|
-
this.preprocessCanvas = undefined;
|
|
402
|
-
}
|
|
403
|
-
if (this.captureCanvas) {
|
|
404
|
-
this.captureCtx = undefined;
|
|
405
|
-
this.captureCanvas = undefined;
|
|
406
|
-
}
|
|
407
|
-
// Disposed ONNX sessions
|
|
408
|
-
if (this.session) {
|
|
409
|
-
this.session.release?.();
|
|
410
|
-
this.session = undefined;
|
|
411
|
-
}
|
|
412
|
-
if (this.mobileNetSession) {
|
|
413
|
-
this.mobileNetSession.release?.();
|
|
414
|
-
this.mobileNetSession = undefined;
|
|
415
|
-
}
|
|
416
|
-
this.mobileNetClassMap = undefined;
|
|
417
|
-
this.isModelPreloaded = false;
|
|
418
|
-
this.debugLog('🧹 Canvas pool cleaned up');
|
|
419
|
-
}
|
|
420
|
-
async getMaxResolution() {
|
|
421
|
-
try {
|
|
422
|
-
// Primero obtén un stream básico para acceder a las capacidades
|
|
423
|
-
const tempStream = await navigator.mediaDevices.getUserMedia({
|
|
424
|
-
video: { facingMode: "environment" }
|
|
425
|
-
});
|
|
426
|
-
const videoTrack = tempStream.getVideoTracks()[0];
|
|
427
|
-
const capabilities = videoTrack.getCapabilities();
|
|
428
|
-
// Detener el stream temporal
|
|
429
|
-
tempStream.getTracks().forEach(track => track.stop());
|
|
430
|
-
// Construir restricciones con resolución optimizada para tablets
|
|
431
|
-
const constraints = {
|
|
432
|
-
facingMode: "environment"
|
|
433
|
-
};
|
|
434
|
-
if (capabilities.width && capabilities.height) {
|
|
435
|
-
// Limitar resolución máxima para tablets para evitar problemas de rendimiento
|
|
436
|
-
const maxWidth = Math.min(capabilities.width.max, 1920);
|
|
437
|
-
const maxHeight = Math.min(capabilities.height.max, 1080);
|
|
438
|
-
// Para tablets, usar resolución moderada en lugar de máxima
|
|
439
|
-
const isTablet = /iPad|Android/i.test(navigator.userAgent) && window.innerWidth >= 768;
|
|
440
|
-
if (isTablet) {
|
|
441
|
-
constraints.width = { ideal: Math.min(maxWidth, 1280) };
|
|
442
|
-
constraints.height = { ideal: Math.min(maxHeight, 720) };
|
|
443
|
-
}
|
|
444
|
-
else {
|
|
445
|
-
constraints.width = { ideal: maxWidth };
|
|
446
|
-
constraints.height = { ideal: maxHeight };
|
|
447
|
-
}
|
|
448
|
-
this.debugLog('📐 Resolution capabilities:', {
|
|
449
|
-
maxWidth: capabilities.width.max,
|
|
450
|
-
maxHeight: capabilities.height.max,
|
|
451
|
-
selectedWidth: constraints.width.ideal,
|
|
452
|
-
selectedHeight: constraints.height.ideal,
|
|
453
|
-
isTablet
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
return constraints;
|
|
457
|
-
}
|
|
458
|
-
catch (err) {
|
|
459
|
-
this.debugLog('⚠️ Could not get capabilities, using fallback');
|
|
460
|
-
// Fallback optimizado para tablets
|
|
461
|
-
const isTablet = /iPad|Android/i.test(navigator.userAgent) && window.innerWidth >= 768;
|
|
462
|
-
return {
|
|
463
|
-
facingMode: "environment",
|
|
464
|
-
width: { ideal: isTablet ? 1280 : 1920 },
|
|
465
|
-
height: { ideal: isTablet ? 720 : 1080 }
|
|
466
|
-
};
|
|
1440
|
+
return this.stateManager.getCapturedImages();
|
|
1441
|
+
}
|
|
1442
|
+
async isProcessCompleted() {
|
|
1443
|
+
return this.stateManager.isProcessCompleted();
|
|
1444
|
+
}
|
|
1445
|
+
async startCapture() {
|
|
1446
|
+
await this.startDetection();
|
|
1447
|
+
}
|
|
1448
|
+
async stopCapture() {
|
|
1449
|
+
this.exitSession();
|
|
1450
|
+
}
|
|
1451
|
+
async resetCapture() {
|
|
1452
|
+
this.resetDetection();
|
|
1453
|
+
}
|
|
1454
|
+
async skipBackCapture() {
|
|
1455
|
+
const captureState = this.stateManager.getCaptureState();
|
|
1456
|
+
const capturedImages = this.stateManager.getCapturedImages();
|
|
1457
|
+
if (captureState.step === 'back' && capturedImages.front.fullFrame) {
|
|
1458
|
+
this.completeProcess(true);
|
|
467
1459
|
}
|
|
468
1460
|
}
|
|
469
|
-
async
|
|
1461
|
+
async getStatus() {
|
|
1462
|
+
const captureState = this.stateManager.getCaptureState();
|
|
1463
|
+
const capturedImages = this.stateManager.getCapturedImages();
|
|
1464
|
+
return {
|
|
1465
|
+
isVideoActive: captureState.isVideoActive,
|
|
1466
|
+
captureStep: captureState.step,
|
|
1467
|
+
hasImages: !!(capturedImages.front.fullFrame || capturedImages.back.fullFrame),
|
|
1468
|
+
isProcessCompleted: this.stateManager.isProcessCompleted(),
|
|
1469
|
+
isModelPreloaded: this.detectionService.isModelLoaded()
|
|
1470
|
+
};
|
|
1471
|
+
}
|
|
1472
|
+
async preloadModel() {
|
|
1473
|
+
if (this.detectionService.isModelLoaded()) {
|
|
1474
|
+
this.logger.state('MODELO_YA_PRECARGADO');
|
|
1475
|
+
this.updateStatus('Modelos ya cargados', '', 'ready');
|
|
1476
|
+
return { success: true, message: 'Model already loaded' };
|
|
1477
|
+
}
|
|
470
1478
|
try {
|
|
471
|
-
const
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
this.
|
|
482
|
-
this.debugLog('📹 Rear camera:', isRear);
|
|
483
|
-
this.debugLog('🪞 Should mirror video:', this.shouldMirrorVideo);
|
|
484
|
-
return new Promise((resolve) => {
|
|
485
|
-
this.videoRef.onloadedmetadata = async () => {
|
|
486
|
-
await this.videoRef.play();
|
|
487
|
-
this.isVideoActive = true;
|
|
488
|
-
// Update mask dimensions once video is loaded
|
|
489
|
-
if (this.canvasRef) {
|
|
490
|
-
const container = this.canvasRef.parentElement;
|
|
491
|
-
const rect = container.getBoundingClientRect();
|
|
492
|
-
this.updateMaskDimensions(rect);
|
|
493
|
-
}
|
|
494
|
-
resolve();
|
|
495
|
-
};
|
|
496
|
-
});
|
|
1479
|
+
const loadStartTime = performance.now();
|
|
1480
|
+
this.updateStatus('Descargando modelo de detección...', 'Obteniendo red neuronal para reconocimiento de documentos', 'loading');
|
|
1481
|
+
this.stateManager.updateCaptureState({ isLoading: true });
|
|
1482
|
+
await this.detectionService.loadModel();
|
|
1483
|
+
this.updateStatus('Cargando clasificador...', 'Preparando modelo de clasificación de tipos de documento', 'loading');
|
|
1484
|
+
await this.detectionService.loadClassificationModel();
|
|
1485
|
+
const loadEndTime = performance.now();
|
|
1486
|
+
const loadTime = loadEndTime - loadStartTime;
|
|
1487
|
+
// Record ONNX load time
|
|
1488
|
+
if (this.debug) {
|
|
1489
|
+
this.recordOnnxPerformance(loadTime, 0);
|
|
497
1490
|
}
|
|
1491
|
+
this.updateStatus('Optimizando modelos...', 'Configurando parámetros de rendimiento', 'loading');
|
|
1492
|
+
await new Promise(resolve => setTimeout(resolve, 300));
|
|
1493
|
+
this.updateStatus('Modelos precargados', '', 'ready');
|
|
1494
|
+
this.stateManager.updateCaptureState({ isLoading: false });
|
|
1495
|
+
this.emitReadyEvent();
|
|
1496
|
+
this.logger.state('MODELOS_PRECARGADOS_EXITOSAMENTE', { loadTime: Math.round(loadTime) });
|
|
1497
|
+
return { success: true, message: 'Models preloaded successfully' };
|
|
498
1498
|
}
|
|
499
|
-
catch (
|
|
500
|
-
this.
|
|
501
|
-
this.
|
|
502
|
-
this.
|
|
1499
|
+
catch (error) {
|
|
1500
|
+
this.logger.error('Error al precargar modelos:', error);
|
|
1501
|
+
this.updateStatus('Error al cargar modelos', 'No se pudieron descargar los recursos necesarios', 'error');
|
|
1502
|
+
this.stateManager.updateCaptureState({ isLoading: false });
|
|
1503
|
+
return { success: false, error: error.message };
|
|
503
1504
|
}
|
|
504
1505
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
1506
|
+
async getCameraInfo() {
|
|
1507
|
+
return this.cameraService.getCameraInfo();
|
|
1508
|
+
}
|
|
1509
|
+
async setPreferredCamera(camera) {
|
|
1510
|
+
this.preferredCamera = camera;
|
|
1511
|
+
this.serviceContainer.updateConfig({ preferredCamera: camera });
|
|
1512
|
+
await this.cameraService.enumerateDevices();
|
|
1513
|
+
const captureState = this.stateManager.getCaptureState();
|
|
1514
|
+
if (captureState.isVideoActive) {
|
|
1515
|
+
const selectedCameraId = this.cameraService.getSelectedCameraId();
|
|
1516
|
+
if (selectedCameraId) {
|
|
1517
|
+
await this.cameraService.switchCamera(selectedCameraId);
|
|
1518
|
+
}
|
|
509
1519
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
G.push(data[i + 1] / 255);
|
|
520
|
-
B.push(data[i + 2] / 255);
|
|
1520
|
+
return {
|
|
1521
|
+
success: true,
|
|
1522
|
+
selectedCamera: this.cameraService.getSelectedCameraId(),
|
|
1523
|
+
availableCameras: this.cameraService.getAvailableCameras().length
|
|
1524
|
+
};
|
|
1525
|
+
}
|
|
1526
|
+
async setCaptureDelay(delay) {
|
|
1527
|
+
if (delay < 0 || delay > 10000) {
|
|
1528
|
+
throw new Error('Capture delay must be between 0 and 10000 milliseconds');
|
|
521
1529
|
}
|
|
522
|
-
|
|
523
|
-
|
|
1530
|
+
this.captureDelay = delay;
|
|
1531
|
+
this.serviceContainer.updateConfig({ captureDelay: delay });
|
|
1532
|
+
return {
|
|
1533
|
+
success: true,
|
|
1534
|
+
captureDelay: this.captureDelay
|
|
1535
|
+
};
|
|
524
1536
|
}
|
|
525
|
-
|
|
1537
|
+
async getCaptureDelay() {
|
|
1538
|
+
return this.captureDelay;
|
|
1539
|
+
}
|
|
1540
|
+
async setTorchEnabled(enabled) {
|
|
1541
|
+
const torchEnabled = await this.cameraService.setTorchEnabled(enabled, this.videoStream);
|
|
526
1542
|
return {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
enableMemPattern: true,
|
|
537
|
-
executionMode: 'parallel',
|
|
538
|
-
interOpNumThreads: 2,
|
|
539
|
-
intraOpNumThreads: 2,
|
|
1543
|
+
success: torchEnabled,
|
|
1544
|
+
enabled: torchEnabled ? enabled : false
|
|
1545
|
+
};
|
|
1546
|
+
}
|
|
1547
|
+
async focusAtPoint(x, y) {
|
|
1548
|
+
const focused = await this.cameraService.focusAtPoint(x, y, this.videoStream);
|
|
1549
|
+
return {
|
|
1550
|
+
success: focused,
|
|
1551
|
+
coordinates: { x, y }
|
|
540
1552
|
};
|
|
541
1553
|
}
|
|
1554
|
+
// DETECTION METHODS
|
|
542
1555
|
async startDetection() {
|
|
1556
|
+
this.logger.state('INICIANDO_DETECCION');
|
|
543
1557
|
try {
|
|
544
|
-
//
|
|
545
|
-
if (!this.
|
|
546
|
-
|
|
547
|
-
this.
|
|
548
|
-
this.
|
|
549
|
-
|
|
550
|
-
this.
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
1558
|
+
// Paso 1: Verificar y cargar modelos si es necesario
|
|
1559
|
+
if (!this.detectionService.isModelLoaded()) {
|
|
1560
|
+
const loadStartTime = performance.now();
|
|
1561
|
+
this.updateStatus('Cargando modelo de detección...', 'Descargando red neuronal para reconocimiento', 'loading');
|
|
1562
|
+
this.stateManager.updateCaptureState({ isLoading: true });
|
|
1563
|
+
await this.detectionService.loadModel();
|
|
1564
|
+
this.updateStatus('Cargando clasificador...', 'Preparando modelo de clasificación de documentos', 'loading');
|
|
1565
|
+
await this.detectionService.loadClassificationModel();
|
|
1566
|
+
const loadEndTime = performance.now();
|
|
1567
|
+
const loadTime = loadEndTime - loadStartTime;
|
|
1568
|
+
// Record ONNX load time
|
|
1569
|
+
if (this.debug) {
|
|
1570
|
+
this.recordOnnxPerformance(loadTime, 0);
|
|
556
1571
|
}
|
|
557
|
-
this.
|
|
558
|
-
this.emitReadyEvent();
|
|
1572
|
+
this.logger.state('MODELOS_CARGADOS_EN_DETECCION', { loadTime: Math.round(loadTime) });
|
|
559
1573
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
1574
|
+
// Paso 2: Detectar y configurar dispositivos
|
|
1575
|
+
this.updateStatus('Detectando cámaras...', 'Buscando dispositivos de captura disponibles', 'loading');
|
|
1576
|
+
await this.cameraService.enumerateDevices();
|
|
1577
|
+
// Paso 3: Configurar cámara seleccionada
|
|
1578
|
+
this.updateStatus('Configurando cámara...', 'Estableciendo resolución y parámetros óptimos', 'loading');
|
|
1579
|
+
const stream = await this.cameraService.setupCamera();
|
|
1580
|
+
// Paso 4: Inicializar video y ocultar estado inmediatamente
|
|
1581
|
+
this.updateStatus('Captura activa', 'Buscando documento en el marco de captura', 'active');
|
|
1582
|
+
await this.initializeVideoStream(stream);
|
|
1583
|
+
// Paso 5: Calibrar máscara
|
|
1584
|
+
if (this.detectionContainer) {
|
|
1585
|
+
const container = this.detectionContainer.parentElement;
|
|
1586
|
+
const rect = container.getBoundingClientRect();
|
|
1587
|
+
this.updateMaskDimensions(rect);
|
|
564
1588
|
}
|
|
565
|
-
|
|
566
|
-
await this.setupCamera();
|
|
1589
|
+
// Finalizar e iniciar captura
|
|
567
1590
|
this.startTime = Date.now();
|
|
568
|
-
this.isLoading
|
|
1591
|
+
this.stateManager.updateCaptureState({ isLoading: false });
|
|
569
1592
|
this.detectFrame();
|
|
570
1593
|
}
|
|
571
1594
|
catch (err) {
|
|
572
|
-
this.
|
|
573
|
-
this.
|
|
574
|
-
this.
|
|
575
|
-
|
|
1595
|
+
this.logger.error('Error al inicializar detección:', err);
|
|
1596
|
+
this.updateStatus('Error al iniciar captura', 'No se pudo completar la inicialización', 'error');
|
|
1597
|
+
this.stateManager.updateCaptureState({ isLoading: false });
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
async initializeVideoStream(stream) {
|
|
1601
|
+
if (this.videoRef) {
|
|
1602
|
+
this.videoRef.srcObject = stream;
|
|
1603
|
+
this.videoStream = stream;
|
|
1604
|
+
const isRear = this.cameraService.isRearCamera(stream);
|
|
1605
|
+
this.shouldMirrorVideo = !isRear;
|
|
1606
|
+
this.logger.state('CAMARA_CONFIGURADA', {
|
|
1607
|
+
isRearCamera: isRear,
|
|
1608
|
+
shouldMirrorVideo: this.shouldMirrorVideo
|
|
1609
|
+
});
|
|
1610
|
+
return new Promise((resolve) => {
|
|
1611
|
+
this.videoRef.onloadedmetadata = async () => {
|
|
1612
|
+
await this.videoRef.play();
|
|
1613
|
+
this.stateManager.updateCaptureState({ isVideoActive: true });
|
|
1614
|
+
resolve();
|
|
1615
|
+
};
|
|
1616
|
+
});
|
|
1617
|
+
}
|
|
1618
|
+
else {
|
|
1619
|
+
throw new Error('Video element not available');
|
|
576
1620
|
}
|
|
577
1621
|
}
|
|
578
1622
|
async detectFrame() {
|
|
579
1623
|
try {
|
|
580
|
-
|
|
1624
|
+
const frameStartTime = performance.now();
|
|
1625
|
+
const captureState = this.stateManager.getCaptureState();
|
|
1626
|
+
if (!this.videoRef || !this.detectionContainer || !this.detectionService.isModelLoaded())
|
|
581
1627
|
return;
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
// Solo continuar el bucle sin procesar detección
|
|
585
|
-
if (this.captureStep !== 'completed') {
|
|
1628
|
+
if (captureState.isDetectionPaused) {
|
|
1629
|
+
if (captureState.step !== 'completed') {
|
|
586
1630
|
this.animationId = requestAnimationFrame(() => this.detectFrame());
|
|
587
1631
|
}
|
|
588
1632
|
return;
|
|
589
1633
|
}
|
|
590
|
-
//
|
|
1634
|
+
// Frame skipping for performance
|
|
591
1635
|
this.frameSkipCounter++;
|
|
592
1636
|
if (this.frameSkipCounter <= this.FRAME_SKIP) {
|
|
593
|
-
if (
|
|
1637
|
+
if (captureState.step !== 'completed') {
|
|
594
1638
|
this.animationId = requestAnimationFrame(() => this.detectFrame());
|
|
595
1639
|
}
|
|
596
1640
|
return;
|
|
597
1641
|
}
|
|
598
1642
|
this.frameSkipCounter = 0;
|
|
599
|
-
//
|
|
1643
|
+
// Throttle inference
|
|
600
1644
|
const currentTime = Date.now();
|
|
601
1645
|
if (currentTime - this.lastInferenceTime < this.MIN_INFERENCE_INTERVAL) {
|
|
602
|
-
if (
|
|
1646
|
+
if (captureState.step !== 'completed') {
|
|
603
1647
|
this.animationId = requestAnimationFrame(() => this.detectFrame());
|
|
604
1648
|
}
|
|
605
1649
|
return;
|
|
606
1650
|
}
|
|
607
1651
|
this.lastInferenceTime = currentTime;
|
|
608
|
-
|
|
609
|
-
const
|
|
610
|
-
const
|
|
611
|
-
|
|
612
|
-
const
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
}
|
|
1652
|
+
// Measure preprocessing and inference time
|
|
1653
|
+
const inferenceStartTime = performance.now();
|
|
1654
|
+
const inputTensor = this.detectionService.preprocess(this.videoRef);
|
|
1655
|
+
// Standard detection without quality validation
|
|
1656
|
+
const detections = await this.detectionService.runInference(inputTensor);
|
|
1657
|
+
const inferenceTime = performance.now() - inferenceStartTime;
|
|
1658
|
+
// Record ONNX performance metrics
|
|
1659
|
+
if (this.debug) {
|
|
1660
|
+
this.recordOnnxPerformance(0, inferenceTime);
|
|
1661
|
+
}
|
|
1662
|
+
// Update best score logic
|
|
1663
|
+
if (this.startTime && Date.now() - this.startTime < 5000) {
|
|
1664
|
+
detections.forEach((detection) => {
|
|
1665
|
+
const isWellPositioned = this.detectionService.isCardInFrame(detection);
|
|
1666
|
+
const effectiveScore = isWellPositioned ? detection.score * 1.2 : detection.score;
|
|
1667
|
+
if (effectiveScore > captureState.bestScore) {
|
|
1668
|
+
this.stateManager.updateCaptureState({ bestScore: effectiveScore });
|
|
625
1669
|
}
|
|
626
|
-
}
|
|
1670
|
+
});
|
|
627
1671
|
}
|
|
628
|
-
//
|
|
629
|
-
|
|
1672
|
+
// Update document detection state
|
|
1673
|
+
this.hasDocumentDetected = detections.length > 0;
|
|
1674
|
+
// Adaptive frame rate
|
|
1675
|
+
if (detections.length === 0) {
|
|
630
1676
|
this.consecutiveFailures++;
|
|
631
1677
|
}
|
|
632
1678
|
else {
|
|
633
1679
|
this.consecutiveFailures = 0;
|
|
634
1680
|
}
|
|
635
|
-
|
|
1681
|
+
// Update detection boxes for debug
|
|
1682
|
+
if (this.debug) {
|
|
1683
|
+
this.updateDetectionBoxes(detections);
|
|
1684
|
+
}
|
|
1685
|
+
else {
|
|
1686
|
+
this.detectionBoxes = [];
|
|
1687
|
+
}
|
|
1688
|
+
this.updateMaskColor(detections);
|
|
1689
|
+
// Record frame processing metrics
|
|
636
1690
|
if (this.debug) {
|
|
637
|
-
|
|
1691
|
+
const frameEndTime = performance.now();
|
|
1692
|
+
const totalFrameTime = frameEndTime - frameStartTime;
|
|
1693
|
+
this.recordFrameProcessing(totalFrameTime, detections.length);
|
|
638
1694
|
}
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
// Solo continuar si no hemos completado el proceso
|
|
642
|
-
if (this.captureStep !== 'completed') {
|
|
643
|
-
// OPTIMIZATION 4: Reduce frame rate when no detection for better battery life
|
|
1695
|
+
// Continue detection loop
|
|
1696
|
+
if (captureState.step !== 'completed') {
|
|
644
1697
|
if (this.consecutiveFailures > this.MAX_FAILURES) {
|
|
645
|
-
// Reduce to ~5fps when no detection for 0.5 seconds
|
|
646
1698
|
setTimeout(() => this.detectFrame(), 200);
|
|
647
1699
|
}
|
|
648
1700
|
else {
|
|
@@ -651,102 +1703,97 @@ const JaakStamps = class {
|
|
|
651
1703
|
}
|
|
652
1704
|
}
|
|
653
1705
|
catch (e) {
|
|
654
|
-
this.
|
|
655
|
-
|
|
656
|
-
if (
|
|
657
|
-
// On error, wait longer before retrying
|
|
1706
|
+
this.logger.error('Error en inferencia de modelo:', e);
|
|
1707
|
+
const captureState = this.stateManager.getCaptureState();
|
|
1708
|
+
if (captureState.step !== 'completed') {
|
|
658
1709
|
setTimeout(() => this.detectFrame(), 100);
|
|
659
1710
|
}
|
|
660
1711
|
}
|
|
661
1712
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
const frameCenterY = this.INPUT_SIZE / 2;
|
|
667
|
-
const toleranceX = 40;
|
|
668
|
-
const toleranceY = 30;
|
|
669
|
-
const isCentered = Math.abs(cardCenterX - frameCenterX) < toleranceX &&
|
|
670
|
-
Math.abs(cardCenterY - frameCenterY) < toleranceY;
|
|
671
|
-
const isGoodSize = box.w > 150 && box.w < 300 && box.h > 90 && box.h < 200;
|
|
672
|
-
return isCentered && isGoodSize;
|
|
1713
|
+
// ... (continuing with remaining methods)
|
|
1714
|
+
// Due to length constraints, I'll provide the key architectural changes
|
|
1715
|
+
disconnectedCallback() {
|
|
1716
|
+
this.cleanup();
|
|
673
1717
|
}
|
|
674
|
-
|
|
675
|
-
if (
|
|
676
|
-
|
|
677
|
-
|
|
1718
|
+
cleanup() {
|
|
1719
|
+
if (this.animationId) {
|
|
1720
|
+
cancelAnimationFrame(this.animationId);
|
|
1721
|
+
}
|
|
1722
|
+
if (this.videoStream) {
|
|
1723
|
+
this.videoStream.getTracks().forEach(track => track.stop());
|
|
1724
|
+
}
|
|
1725
|
+
if (this.alignmentTimer) {
|
|
1726
|
+
clearTimeout(this.alignmentTimer);
|
|
1727
|
+
this.alignmentTimer = undefined;
|
|
1728
|
+
}
|
|
1729
|
+
if (this.performanceUpdateInterval) {
|
|
1730
|
+
clearInterval(this.performanceUpdateInterval);
|
|
1731
|
+
this.performanceUpdateInterval = undefined;
|
|
1732
|
+
}
|
|
1733
|
+
this.detectionBoxes = [];
|
|
1734
|
+
this.alignmentStartTime = undefined;
|
|
1735
|
+
this.hasDocumentDetected = false;
|
|
1736
|
+
this.serviceContainer?.cleanup();
|
|
1737
|
+
}
|
|
1738
|
+
render() {
|
|
1739
|
+
const captureState = this.stateManager?.getCaptureState() || {
|
|
1740
|
+
isVideoActive: false,
|
|
1741
|
+
showFlipAnimation: false,
|
|
1742
|
+
showSuccessAnimation: false,
|
|
1743
|
+
step: 'front',
|
|
1744
|
+
isCapturing: false
|
|
1745
|
+
};
|
|
1746
|
+
const cameraInfo = this.cameraService?.getCameraInfo() || {
|
|
1747
|
+
availableCameras: [],
|
|
1748
|
+
selectedCameraId: null,
|
|
1749
|
+
deviceType: 'desktop'};
|
|
1750
|
+
return (index.h("div", { key: '7af79a0ff25b51790a9477cd7b338a0584172fc2', class: "detector-container" }, index.h("div", { key: 'ebf6a3811e0e599f29e98325b975a645e3bfb5e0', class: "video-container" }, index.h("video", { key: '5208fc9574fa78b1149be5839bc26d04bf9bcdfc', ref: el => this.videoRef = el, autoplay: true, muted: true, playsinline: true, class: this.shouldMirrorVideo ? 'mirror' : '', style: { display: captureState.isVideoActive ? 'block' : 'none' } }), index.h("div", { key: 'b507c786eccbd21acdd884ef50bc6ec280b9d437', ref: el => this.detectionContainer = el, class: `detection-overlay ${this.shouldMirrorVideo ? 'mirror' : ''}` }, this.debug && this.detectionBoxes.map((box, index$1) => (index.h("div", { key: index$1, class: "detection-box", style: {
|
|
1751
|
+
position: 'absolute',
|
|
1752
|
+
left: `${box.x}px`,
|
|
1753
|
+
top: `${box.y}px`,
|
|
1754
|
+
width: `${box.w}px`,
|
|
1755
|
+
height: `${box.h}px`,
|
|
1756
|
+
border: '2px solid #32406C',
|
|
1757
|
+
pointerEvents: 'none',
|
|
1758
|
+
boxSizing: 'border-box'
|
|
1759
|
+
} })))), this.isMaskReady && (index.h("div", { key: '53066c626b23daae351ea9a80409e8882434b0dd', class: "overlay-mask" }, index.h("div", { key: '77bb4048d43e328e57a5c7e1909e0a59c8957217', class: "card-outline" }, index.h("div", { key: 'ea0f2610d903f985286856ab69f8be24fd42148e', class: "side side-top" }), index.h("div", { key: 'dc671111ef4adf33aa41f4457fdf344ede23eb80', class: "side side-right" }), index.h("div", { key: '2475ec41577dca92b425542c8861c570ed7ecd3b', class: "side side-bottom" }), index.h("div", { key: 'a06171c8eb1bd6353a6464485604560d73ac04b7', class: "side side-left" }), !captureState.showFlipAnimation && !captureState.showSuccessAnimation && (index.h("div", { key: 'ecfd98f36d457fe7282b26f6a0efef5361380c76', class: "guide-text" }, "Alinee su identificaci\u00F3n con el marco"))), captureState.step === 'back' && !captureState.showFlipAnimation && !captureState.showSuccessAnimation && (index.h("button", { key: 'f3c7b66500110437e9e4a7442cb327f2fdc6d71f', class: "skip-button", onClick: () => this.skipBackCapture(), type: "button" }, "Saltar reverso")), captureState.isVideoActive && (index.h("div", { key: 'b5b2a8dfc6dbcf381c32c4caa562c27ced2e3e56', class: "camera-controls" }, index.h("button", { key: '6b40d4649dfa22c5ea8ca90cacce35cd0ca53ff3', class: `camera-selector-button ${this.isSwitchingCamera ? 'loading' : ''}`, onClick: () => this.toggleCameraSelector(), type: "button", title: "Seleccionar c\u00E1mara", disabled: this.isSwitchingCamera }, this.isSwitchingCamera ? (index.h("div", { class: "button-spinner" })) : ('Cámaras')))), this.showCameraSelector && cameraInfo.availableCameras.length > 0 && (index.h("div", { key: '41feec90287510b08bd1090ad15586b323007c30', class: "camera-selector-dropdown" }, index.h("div", { key: '8310ebe3db842c463ad428b973f841053315a76f', class: "camera-selector-header" }, index.h("span", { key: '6693bdfa33643dfe73798d4f25b0c2d9de7aac7c' }, "Seleccionar C\u00E1mara"), index.h("button", { key: '8552d45e1482c570d7ff85588d4404eec7c369d7', class: "close-selector", onClick: () => this.toggleCameraSelector(), type: "button" }, "\u00D7")), index.h("div", { key: '5333ec0e5fb2c50a07a7c0b7d4dfb1bb4ee0df1c', class: "camera-list" }, cameraInfo.availableCameras.map((camera) => (index.h("button", { key: camera.id, class: `camera-option ${cameraInfo.selectedCameraId === camera.id ? 'selected' : ''}`, onClick: () => this.handleCameraSwitch(camera.id), type: "button" }, index.h("span", { class: "camera-label" }, camera.label || `Cámara ${cameraInfo.availableCameras.indexOf(camera) + 1}`), cameraInfo.selectedCameraId === camera.id && (index.h("span", { class: "selected-indicator" }, "\u2713")))))), index.h("div", { key: '8adf70b0e632c56a4784dec44621688809a3ff56', class: "device-info" }, index.h("small", { key: '2ea23922386ecda4a9011b50faba5efe4b4e60bb' }, "Dispositivo: ", cameraInfo.deviceType)))))), captureState.isCapturing && (index.h("div", { key: '8e693db67fe1e6ae8f11082b8993682b6d19b11d', class: "capture-animation" })), captureState.showFlipAnimation && (index.h("div", { key: '474489ed7848cdc163c492a5e19f5f13231e17b3', class: "flip-animation" }, index.h("div", { key: 'a9bd24ca21330882f001b7811077630eb1c30f50', class: "id-card-icon" }), index.h("div", { key: 'ffadb660a61e0f5e3e29bdc473d987e87dc4ad8a', class: "flip-text" }, "\u00A1Voltea tu identificaci\u00F3n!"))), captureState.showSuccessAnimation && (index.h("div", { key: 'd2522ee33aa159d49f040a293775892fb1cb81b5', class: "success-animation" }, index.h("div", { key: 'e7f899fac58bc9a3d51cf2cb89ee33bb320b241f', class: "check-icon" }), index.h("div", { key: 'd5d0ee1f37dbc659ca625ad03cbce325be0cf0a6', class: "success-text" }, "\u00A1Proceso completado!"))), index.h("div", { key: '425d347666032c3982a660ebee13b47670b996cb', class: `component-status status-${this.currentStatus.type}` }, (this.currentStatus.type === 'loading' || this.currentStatus.type === 'initializing') && (index.h("div", { key: '4d396fa107872f9a1e526fd59dc4edf279a05792', class: "status-spinner" })), index.h("div", { key: '232e9ae24c8cf5ef8ac0e3442e45a920799c724d', class: "status-content" }, index.h("div", { key: 'fe9dc0ca6d8e8276052bac7d2d44f1c7c9730364', class: "status-message" }, this.currentStatus.message), this.currentStatus.description && (index.h("div", { key: '6910c7ac78fd89e6fa1165446ec6d46fb356035c', class: "status-description" }, this.currentStatus.description)))), this.debug && (index.h("div", { key: '7282b8afaf03da53b01a23d04babd772528c3c14', class: "performance-monitor" }, index.h("div", { key: 'b582b3124fed13387cd70a02a34ab2bb541f3bfc', class: "performance-expanded" }, index.h("div", { key: '764bac2d61224b9a5f306f50934d410e1bd27787', class: "metrics-row" }, index.h("div", { key: '261563f8314a9f6069b1ac6362eb9c38936a9dc7', class: "metric-compact" }, index.h("span", { key: '4feea98eff86620c1bc90eb65d9799102423109d', class: "metric-label" }, "FPS"), index.h("span", { key: 'eb15f13b27b8aa50701ad3d11c4cf87200010b67', class: `metric-value ${this.performanceData.fps < 15 ? 'warning' : this.performanceData.fps < 10 ? 'danger' : 'good'}` }, this.performanceData.fps)), index.h("div", { key: '0813c35c2c33286f2276bfb5adce59b32c7bb597', class: "metric-compact" }, index.h("span", { key: '201e7bcb556e63b141e42a3ffc3419eb4284bfa8', class: "metric-label" }, "MEM"), index.h("span", { key: '4f6703cbe0128440d0f2f22f631035ab85422c64', class: `metric-value ${this.performanceData.memoryUsage > 100 ? 'warning' : this.performanceData.memoryUsage > 200 ? 'danger' : 'good'}` }, this.performanceData.memoryUsage, "MB"))), index.h("div", { key: '6343c361c93138b9ea66e8ae8acd361b93333f8c', class: "metrics-row" }, index.h("div", { key: 'd3c81fe597004bf380fae2d32f3a8314317e7769', class: "metric-compact" }, index.h("span", { key: 'fce572af3e200a611974dceef4da02f6aecedffc', class: "metric-label" }, "INF"), index.h("span", { key: '18dd78d54dcecfb70ddf59b782f87f8c6f208b0f', class: `metric-value ${this.performanceData.inferenceTime > 100 ? 'warning' : this.performanceData.inferenceTime > 200 ? 'danger' : 'good'}` }, this.performanceData.inferenceTime, "ms")), index.h("div", { key: '4c31ebfd835b017fd3e41b23d6bd9b19add511aa', class: "metric-compact" }, index.h("span", { key: '66b1a380b9479a8fc1e8c2385cb72aa4158dc74e', class: "metric-label" }, "FRAME"), index.h("span", { key: '3c6e41f7f11123c524d3a9df409345929d63f352', class: `metric-value ${this.performanceData.frameProcessingTime > 50 ? 'warning' : this.performanceData.frameProcessingTime > 100 ? 'danger' : 'good'}` }, this.performanceData.frameProcessingTime, "ms"))), index.h("div", { key: '3b522e2cdfe4bd9f1dd6eb0f91ee3577dc43e490', class: "metrics-row" }, index.h("div", { key: 'fa1e6f687b43aa2582b8b3426feb03a16c6493fc', class: "metric-compact" }, index.h("span", { key: 'a9355091e71a33a6a0d7cc6c11171dcda2e51b22', class: "metric-label" }, "DET"), index.h("span", { key: 'beca5a7ccf4853675cf8366bcf577ebfbd46c4f0', class: "metric-value good" }, this.performanceData.successfulDetections, "/", this.performanceData.totalDetections)), index.h("div", { key: '4b07fc9b3d35f99bc6e77c8726192cde0476a787', class: "metric-compact" }, index.h("span", { key: 'a1674b538f45328ee0afb00e582c07e0b4ccf553', class: "metric-label" }, "RATE"), index.h("span", { key: 'fd384a5137bc3df6c939d3d698a3ef80e9087030', class: `metric-value ${this.performanceData.detectionRate < 30 ? 'danger' : this.performanceData.detectionRate < 60 ? 'warning' : 'good'}` }, this.performanceData.detectionRate, "%")))))), index.h("div", { key: 'ebbaf15a0ad96e4da5bbf746c32d7a4abb51b973', class: "watermark" }, index.h("img", { key: '8e7d274b529d1f7020ec4a7225579792763f871a', src: "https://storage.googleapis.com/jaak-static/commons/powered-by-jaak.png", alt: "Powered by Jaak" })))));
|
|
1760
|
+
}
|
|
1761
|
+
// Utility methods
|
|
1762
|
+
updateDetectionBoxes(boxes) {
|
|
1763
|
+
if (!this.videoRef || !this.detectionContainer)
|
|
1764
|
+
return;
|
|
678
1765
|
const videoWidth = this.videoRef.videoWidth;
|
|
679
1766
|
const videoHeight = this.videoRef.videoHeight;
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
1767
|
+
const container = this.detectionContainer.parentElement;
|
|
1768
|
+
const containerRect = container.getBoundingClientRect();
|
|
1769
|
+
const containerWidth = containerRect.width;
|
|
1770
|
+
const containerHeight = containerRect.height;
|
|
1771
|
+
if (videoWidth === 0 || videoHeight === 0)
|
|
1772
|
+
return;
|
|
684
1773
|
const videoAspectRatio = videoWidth / videoHeight;
|
|
685
|
-
|
|
686
|
-
// the mask should be in this distorted space to match the visual mask
|
|
687
|
-
// In the visual display, we calculate mask size based on the limiting dimension
|
|
688
|
-
// We need to replicate this logic but account for the distortion in model space
|
|
689
|
-
// Calculate the "display" dimensions (what the visual mask calculations use)
|
|
690
|
-
const containerAspectRatio = 1; // Assume square container for simplicity
|
|
1774
|
+
const containerAspectRatio = containerWidth / containerHeight;
|
|
691
1775
|
let displayedVideoWidth, displayedVideoHeight;
|
|
1776
|
+
let videoOffsetX = 0, videoOffsetY = 0;
|
|
692
1777
|
if (videoAspectRatio > containerAspectRatio) {
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
}
|
|
697
|
-
else {
|
|
698
|
-
// Video is taller - pillarboxed in display
|
|
699
|
-
displayedVideoHeight = this.INPUT_SIZE;
|
|
700
|
-
displayedVideoWidth = this.INPUT_SIZE * videoAspectRatio;
|
|
701
|
-
}
|
|
702
|
-
// Calculate mask dimensions using the same logic as updateMaskDimensions
|
|
703
|
-
const maxWidthByHeight = displayedVideoHeight * this.ID1_ASPECT_RATIO;
|
|
704
|
-
let visualMaskWidth, visualMaskHeight;
|
|
705
|
-
const sizeRatio = this.maskSize / 100;
|
|
706
|
-
if (maxWidthByHeight <= displayedVideoWidth) {
|
|
707
|
-
// Video height is the limiting factor
|
|
708
|
-
visualMaskHeight = displayedVideoHeight * sizeRatio;
|
|
709
|
-
visualMaskWidth = visualMaskHeight * this.ID1_ASPECT_RATIO;
|
|
1778
|
+
displayedVideoWidth = containerWidth;
|
|
1779
|
+
displayedVideoHeight = containerWidth / videoAspectRatio;
|
|
1780
|
+
videoOffsetY = (containerHeight - displayedVideoHeight) / 2;
|
|
710
1781
|
}
|
|
711
1782
|
else {
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
}
|
|
716
|
-
// Now map these visual mask dimensions to the distorted model space
|
|
717
|
-
// The model stretches video to 320x320, so we need to apply the inverse transformation
|
|
718
|
-
const modelMaskWidth = visualMaskWidth * (this.INPUT_SIZE / displayedVideoWidth);
|
|
719
|
-
const modelMaskHeight = visualMaskHeight * (this.INPUT_SIZE / displayedVideoHeight);
|
|
720
|
-
// Calculate mask boundaries in model coordinate system (always centered in 320x320)
|
|
721
|
-
const maskCenterX = this.INPUT_SIZE / 2;
|
|
722
|
-
const maskCenterY = this.INPUT_SIZE / 2;
|
|
723
|
-
const maskLeft = maskCenterX - (modelMaskWidth / 2);
|
|
724
|
-
const maskRight = maskCenterX + (modelMaskWidth / 2);
|
|
725
|
-
const maskTop = maskCenterY - (modelMaskHeight / 2);
|
|
726
|
-
const maskBottom = maskCenterY + (modelMaskHeight / 2);
|
|
727
|
-
// Obtener las coordenadas del documento detectado
|
|
728
|
-
let docLeft = box.x;
|
|
729
|
-
let docRight = box.x + box.w;
|
|
730
|
-
const docTop = box.y;
|
|
731
|
-
const docBottom = box.y + box.h;
|
|
732
|
-
// Si el video está en modo mirror, invertir las coordenadas horizontales
|
|
733
|
-
if (this.shouldMirrorVideo) {
|
|
734
|
-
const originalDocLeft = docLeft;
|
|
735
|
-
const originalDocRight = docRight;
|
|
736
|
-
docLeft = this.INPUT_SIZE - originalDocRight;
|
|
737
|
-
docRight = this.INPUT_SIZE - originalDocLeft;
|
|
1783
|
+
displayedVideoHeight = containerHeight;
|
|
1784
|
+
displayedVideoWidth = containerHeight * videoAspectRatio;
|
|
1785
|
+
videoOffsetX = (containerWidth - displayedVideoWidth) / 2;
|
|
738
1786
|
}
|
|
739
|
-
|
|
740
|
-
const
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
return alignment.top && alignment.right && alignment.bottom && alignment.left;
|
|
1787
|
+
const INPUT_SIZE = 320;
|
|
1788
|
+
const scaleX = displayedVideoWidth / INPUT_SIZE;
|
|
1789
|
+
const scaleY = displayedVideoHeight / INPUT_SIZE;
|
|
1790
|
+
this.detectionBoxes = boxes.map(det => ({
|
|
1791
|
+
x: det.x * scaleX + videoOffsetX,
|
|
1792
|
+
y: det.y * scaleY + videoOffsetY,
|
|
1793
|
+
w: det.w * scaleX,
|
|
1794
|
+
h: det.h * scaleY,
|
|
1795
|
+
score: det.score
|
|
1796
|
+
}));
|
|
750
1797
|
}
|
|
751
1798
|
updateMaskColor(boxes) {
|
|
752
1799
|
const cardOutline = this.el.shadowRoot?.querySelector('.card-outline');
|
|
@@ -759,297 +1806,327 @@ const JaakStamps = class {
|
|
|
759
1806
|
let currentAlignment = { top: false, right: false, bottom: false, left: false };
|
|
760
1807
|
if (boxes.length > 0) {
|
|
761
1808
|
bestBox = boxes.reduce((best, current) => current.score > best.score ? current : best);
|
|
762
|
-
|
|
1809
|
+
const maskConfig = {
|
|
1810
|
+
INPUT_SIZE: 320,
|
|
1811
|
+
ID1_ASPECT_RATIO: 85.60 / 53.98,
|
|
1812
|
+
shouldMirrorVideo: this.shouldMirrorVideo,
|
|
1813
|
+
alignmentTolerance: this.alignmentTolerance,
|
|
1814
|
+
maskSize: this.maskSize,
|
|
1815
|
+
videoRef: this.videoRef
|
|
1816
|
+
};
|
|
1817
|
+
currentAlignment = this.detectionService.checkSideAlignment(bestBox, maskConfig);
|
|
763
1818
|
this.sideAlignment = currentAlignment;
|
|
764
1819
|
}
|
|
765
1820
|
else {
|
|
766
|
-
// Reset alignment when no detection
|
|
767
1821
|
this.sideAlignment = { top: false, right: false, bottom: false, left: false };
|
|
768
1822
|
}
|
|
769
|
-
// Actualizar colores de cada lado individualmente
|
|
770
1823
|
topSide?.classList.toggle('aligned', currentAlignment.top);
|
|
771
1824
|
rightSide?.classList.toggle('aligned', currentAlignment.right);
|
|
772
1825
|
bottomSide?.classList.toggle('aligned', currentAlignment.bottom);
|
|
773
1826
|
leftSide?.classList.toggle('aligned', currentAlignment.left);
|
|
774
|
-
|
|
775
|
-
const allSidesAligned = this.areAllSidesAligned(currentAlignment);
|
|
1827
|
+
const allSidesAligned = this.detectionService.areAllSidesAligned(currentAlignment);
|
|
776
1828
|
if (allSidesAligned && bestBox) {
|
|
777
1829
|
cardOutline?.classList.add('perfect-match');
|
|
778
1830
|
corners?.forEach(corner => corner.classList.add('perfect-match'));
|
|
1831
|
+
// Debug logging
|
|
1832
|
+
this.logger.state('CAPTURE_EVALUATION', {
|
|
1833
|
+
allSidesAligned: true,
|
|
1834
|
+
hasScreenshotTaken: this.hasScreenshotTaken,
|
|
1835
|
+
captureDelay: this.captureDelay,
|
|
1836
|
+
alignmentStartTime: this.alignmentStartTime
|
|
1837
|
+
});
|
|
779
1838
|
if (!this.hasScreenshotTaken) {
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
1839
|
+
const currentTime = Date.now();
|
|
1840
|
+
// Initialize alignment start time if not set
|
|
1841
|
+
if (!this.alignmentStartTime) {
|
|
1842
|
+
this.alignmentStartTime = currentTime;
|
|
1843
|
+
this.logger.state('ALIGNMENT_TIMER_STARTED', { startTime: currentTime });
|
|
1844
|
+
}
|
|
1845
|
+
// Check if document has been aligned for the configured delay
|
|
1846
|
+
const alignmentDuration = currentTime - this.alignmentStartTime;
|
|
1847
|
+
this.logger.state('ALIGNMENT_DURATION_CHECK', {
|
|
1848
|
+
alignmentDuration,
|
|
1849
|
+
captureDelay: this.captureDelay,
|
|
1850
|
+
readyToCapture: alignmentDuration >= this.captureDelay
|
|
783
1851
|
});
|
|
784
|
-
this.
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1852
|
+
if (alignmentDuration >= this.captureDelay) {
|
|
1853
|
+
this.logger.state('TRIGGERING_CAPTURE', {
|
|
1854
|
+
alignmentDuration,
|
|
1855
|
+
captureDelay: this.captureDelay
|
|
1856
|
+
});
|
|
1857
|
+
this.lastDetectedBox = bestBox;
|
|
1858
|
+
this.takeScreenshot().catch(error => {
|
|
1859
|
+
this.logger.error('Error al tomar captura de pantalla:', error);
|
|
1860
|
+
});
|
|
1861
|
+
this.hasScreenshotTaken = true;
|
|
1862
|
+
this.alignmentStartTime = undefined;
|
|
1863
|
+
setTimeout(() => {
|
|
1864
|
+
this.hasScreenshotTaken = false;
|
|
1865
|
+
}, 2000);
|
|
1866
|
+
}
|
|
789
1867
|
}
|
|
790
1868
|
}
|
|
791
1869
|
else {
|
|
792
1870
|
cardOutline?.classList.remove('perfect-match');
|
|
793
1871
|
corners?.forEach(corner => corner.classList.remove('perfect-match'));
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
if (boxes.length === 0) {
|
|
798
|
-
this.statusMessage = "Posicione la identificación dentro del marco";
|
|
799
|
-
this.statusColor = "#ff6b6b";
|
|
800
|
-
}
|
|
801
|
-
else {
|
|
802
|
-
const bestBox = boxes.reduce((best, current) => current.score > best.score ? current : best);
|
|
803
|
-
const alignment = this.checkSideAlignment(bestBox);
|
|
804
|
-
const alignedSides = [alignment.top, alignment.right, alignment.bottom, alignment.left].filter(Boolean).length;
|
|
805
|
-
const allSidesAligned = this.areAllSidesAligned(alignment);
|
|
806
|
-
if (allSidesAligned) {
|
|
807
|
-
this.statusMessage = "Identificación perfectamente alineada. Mantenga inmóvil";
|
|
808
|
-
this.statusColor = "#00ff00";
|
|
1872
|
+
// Reset alignment timer when document moves out of position
|
|
1873
|
+
if (this.alignmentStartTime) {
|
|
1874
|
+
this.alignmentStartTime = undefined;
|
|
809
1875
|
}
|
|
810
|
-
|
|
811
|
-
this.
|
|
812
|
-
this.
|
|
1876
|
+
if (this.alignmentTimer) {
|
|
1877
|
+
clearTimeout(this.alignmentTimer);
|
|
1878
|
+
this.alignmentTimer = undefined;
|
|
813
1879
|
}
|
|
814
|
-
else if (bestBox.w < 150) {
|
|
815
|
-
this.statusMessage = "Identificación detectada. Acerque al marco";
|
|
816
|
-
this.statusColor = "#ffb366";
|
|
817
|
-
}
|
|
818
|
-
else {
|
|
819
|
-
this.statusMessage = "Identificación detectada. Alinee con el marco";
|
|
820
|
-
this.statusColor = "#ffb366";
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
drawDetections(ctx, boxes) {
|
|
825
|
-
if (!this.videoRef)
|
|
826
|
-
return;
|
|
827
|
-
// Get video and container dimensions
|
|
828
|
-
const videoWidth = this.videoRef.videoWidth;
|
|
829
|
-
const videoHeight = this.videoRef.videoHeight;
|
|
830
|
-
const containerWidth = this.canvasRef.width;
|
|
831
|
-
const containerHeight = this.canvasRef.height;
|
|
832
|
-
if (videoWidth === 0 || videoHeight === 0)
|
|
833
|
-
return;
|
|
834
|
-
// Calculate video aspect ratio and container aspect ratio
|
|
835
|
-
const videoAspectRatio = videoWidth / videoHeight;
|
|
836
|
-
const containerAspectRatio = containerWidth / containerHeight;
|
|
837
|
-
// Determine how video fits in container (same logic as updateMaskDimensions)
|
|
838
|
-
let displayedVideoWidth, displayedVideoHeight;
|
|
839
|
-
let videoOffsetX = 0, videoOffsetY = 0;
|
|
840
|
-
if (videoAspectRatio > containerAspectRatio) {
|
|
841
|
-
// Video is wider - letterboxed (black bars top/bottom)
|
|
842
|
-
displayedVideoWidth = containerWidth;
|
|
843
|
-
displayedVideoHeight = containerWidth / videoAspectRatio;
|
|
844
|
-
videoOffsetY = (containerHeight - displayedVideoHeight) / 2;
|
|
845
|
-
}
|
|
846
|
-
else {
|
|
847
|
-
// Video is taller - pillarboxed (black bars left/right)
|
|
848
|
-
displayedVideoHeight = containerHeight;
|
|
849
|
-
displayedVideoWidth = containerHeight * videoAspectRatio;
|
|
850
|
-
videoOffsetX = (containerWidth - displayedVideoWidth) / 2;
|
|
851
1880
|
}
|
|
852
|
-
// Scale factor from model coordinates (320x320) to displayed video area
|
|
853
|
-
const scaleX = displayedVideoWidth / this.INPUT_SIZE;
|
|
854
|
-
const scaleY = displayedVideoHeight / this.INPUT_SIZE;
|
|
855
|
-
boxes.forEach(det => {
|
|
856
|
-
// Convert model coordinates to displayed video coordinates
|
|
857
|
-
const x = det.x * scaleX + videoOffsetX;
|
|
858
|
-
const y = det.y * scaleY + videoOffsetY;
|
|
859
|
-
const w = det.w * scaleX;
|
|
860
|
-
const h = det.h * scaleY;
|
|
861
|
-
ctx.strokeStyle = "#32406C";
|
|
862
|
-
ctx.lineWidth = 2;
|
|
863
|
-
ctx.strokeRect(x, y, w, h);
|
|
864
|
-
});
|
|
865
1881
|
}
|
|
866
1882
|
async takeScreenshot() {
|
|
867
1883
|
if (!this.videoRef || !this.lastDetectedBox)
|
|
868
1884
|
return;
|
|
869
|
-
|
|
1885
|
+
this.logger.state('INICIANDO_CAPTURA', {
|
|
1886
|
+
captureStep: this.stateManager.getCaptureState().step,
|
|
1887
|
+
detectedBox: this.lastDetectedBox,
|
|
1888
|
+
videoResolution: {
|
|
1889
|
+
width: this.videoRef.videoWidth,
|
|
1890
|
+
height: this.videoRef.videoHeight
|
|
1891
|
+
}
|
|
1892
|
+
});
|
|
1893
|
+
this.stateManager.updateCaptureState({ isCapturing: true });
|
|
870
1894
|
this.triggerCaptureAnimation();
|
|
871
|
-
//
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
this.captureCanvas.width
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
const
|
|
881
|
-
const scaleY = this.videoRef.videoHeight / this.INPUT_SIZE;
|
|
1895
|
+
// Create capture canvas
|
|
1896
|
+
const captureCanvas = document.createElement('canvas');
|
|
1897
|
+
captureCanvas.width = this.videoRef.videoWidth;
|
|
1898
|
+
captureCanvas.height = this.videoRef.videoHeight;
|
|
1899
|
+
const captureCtx = captureCanvas.getContext('2d', { alpha: false });
|
|
1900
|
+
captureCtx.drawImage(this.videoRef, 0, 0, captureCanvas.width, captureCanvas.height);
|
|
1901
|
+
// Calculate crop coordinates
|
|
1902
|
+
const INPUT_SIZE = 320;
|
|
1903
|
+
const scaleX = this.videoRef.videoWidth / INPUT_SIZE;
|
|
1904
|
+
const scaleY = this.videoRef.videoHeight / INPUT_SIZE;
|
|
882
1905
|
const cropX = Math.max(0, (this.lastDetectedBox.x * scaleX) - this.cropMargin);
|
|
883
1906
|
const cropY = Math.max(0, (this.lastDetectedBox.y * scaleY) - this.cropMargin);
|
|
884
1907
|
const cropWidth = Math.min((this.lastDetectedBox.w * scaleX) + (2 * this.cropMargin), this.videoRef.videoWidth - cropX);
|
|
885
1908
|
const cropHeight = Math.min((this.lastDetectedBox.h * scaleY) + (2 * this.cropMargin), this.videoRef.videoHeight - cropY);
|
|
886
|
-
//
|
|
887
|
-
// (We reuse main capture canvas for full frame)
|
|
1909
|
+
// Create cropped version
|
|
888
1910
|
const croppedCanvas = document.createElement('canvas');
|
|
889
1911
|
croppedCanvas.width = cropWidth;
|
|
890
1912
|
croppedCanvas.height = cropHeight;
|
|
891
1913
|
const croppedCtx = croppedCanvas.getContext('2d', { alpha: false });
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
1914
|
+
croppedCtx.drawImage(this.videoRef, cropX, cropY, cropWidth, cropHeight, 0, 0, cropWidth, cropHeight);
|
|
1915
|
+
const captureState = this.stateManager.getCaptureState();
|
|
1916
|
+
if (captureState.step === 'front') {
|
|
1917
|
+
this.stateManager.setCapturedImages({
|
|
1918
|
+
front: {
|
|
1919
|
+
fullFrame: captureCanvas.toDataURL('image/png'),
|
|
1920
|
+
cropped: croppedCanvas.toDataURL('image/png')
|
|
1921
|
+
}
|
|
1922
|
+
});
|
|
900
1923
|
// Check if document classification is enabled
|
|
901
1924
|
if (this.useDocumentClassification) {
|
|
902
|
-
|
|
903
|
-
if (
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
catch (error) {
|
|
908
|
-
this.debugLog('⚠️ Failed to load classification model, continuing without classification:', error);
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
// Classify the cropped document if model is available
|
|
912
|
-
if (this.mobileNetSession) {
|
|
913
|
-
const classification = await this.classifyDocument(croppedCanvas);
|
|
914
|
-
if (classification && classification.class === 'passport') {
|
|
915
|
-
// If it's a passport, skip back capture since passports don't have a back side
|
|
916
|
-
this.debugLog('📄 Passport detected - skipping back capture');
|
|
917
|
-
this.completeProcess(true);
|
|
918
|
-
return;
|
|
919
|
-
}
|
|
1925
|
+
const classification = await this.detectionService.classifyDocument(croppedCanvas);
|
|
1926
|
+
if (classification && classification.class === 'passport') {
|
|
1927
|
+
this.logger.state('PASAPORTE_DETECTADO_SALTANDO_REVERSO', { classification: classification?.class });
|
|
1928
|
+
this.completeProcess(true);
|
|
1929
|
+
return;
|
|
920
1930
|
}
|
|
921
1931
|
}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
this.isDetectionPaused = true;
|
|
928
|
-
// Mostrar animación de giro después de la captura
|
|
1932
|
+
this.stateManager.updateCaptureState({
|
|
1933
|
+
step: 'back',
|
|
1934
|
+
isDetectionPaused: true,
|
|
1935
|
+
showFlipAnimation: true
|
|
1936
|
+
});
|
|
929
1937
|
setTimeout(() => {
|
|
930
|
-
this.
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
this.
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
1938
|
+
this.stateManager.updateCaptureState({
|
|
1939
|
+
showFlipAnimation: false,
|
|
1940
|
+
isDetectionPaused: false
|
|
1941
|
+
});
|
|
1942
|
+
}, 3000);
|
|
1943
|
+
}
|
|
1944
|
+
else if (captureState.step === 'back') {
|
|
1945
|
+
this.stateManager.setCapturedImages({
|
|
1946
|
+
back: {
|
|
1947
|
+
fullFrame: captureCanvas.toDataURL('image/png'),
|
|
1948
|
+
cropped: croppedCanvas.toDataURL('image/png')
|
|
1949
|
+
}
|
|
1950
|
+
});
|
|
943
1951
|
this.completeProcess(false);
|
|
944
|
-
this.debugLog('📸 TRASERA capturada. Proceso completado. Detector detenido. Imágenes emitidas.');
|
|
945
1952
|
}
|
|
946
1953
|
}
|
|
947
1954
|
triggerCaptureAnimation() {
|
|
948
|
-
this.isCapturing = true;
|
|
949
|
-
// Agregar clase de animación al marco
|
|
950
1955
|
const cardOutline = this.el.shadowRoot?.querySelector('.card-outline');
|
|
951
1956
|
cardOutline?.classList.add('capturing');
|
|
952
|
-
// Limpiar animación después de que termine
|
|
953
1957
|
setTimeout(() => {
|
|
954
|
-
this.isCapturing
|
|
1958
|
+
this.stateManager.updateCaptureState({ isCapturing: false });
|
|
955
1959
|
cardOutline?.classList.remove('capturing');
|
|
956
1960
|
}, 600);
|
|
957
1961
|
}
|
|
1962
|
+
completeProcess(skippedBack = false) {
|
|
1963
|
+
this.stateManager.updateCaptureState({
|
|
1964
|
+
step: 'completed',
|
|
1965
|
+
showSuccessAnimation: true
|
|
1966
|
+
});
|
|
1967
|
+
const capturedImages = this.stateManager.getCapturedImages();
|
|
1968
|
+
capturedImages.metadata.processCompleted = true;
|
|
1969
|
+
capturedImages.metadata.backCaptureSkipped = skippedBack;
|
|
1970
|
+
this.stateManager.setCapturedImages(capturedImages);
|
|
1971
|
+
this.stopDetection();
|
|
1972
|
+
this.updateStatus('Proceso completado', `${capturedImages.metadata.totalImages} imágenes capturadas exitosamente`, 'ready');
|
|
1973
|
+
const finalImages = {
|
|
1974
|
+
...capturedImages,
|
|
1975
|
+
timestamp: new Date().toISOString()
|
|
1976
|
+
};
|
|
1977
|
+
this.captureCompleted.emit(finalImages);
|
|
1978
|
+
setTimeout(() => {
|
|
1979
|
+
this.stateManager.updateCaptureState({ showSuccessAnimation: false });
|
|
1980
|
+
}, 3000);
|
|
1981
|
+
this.logger.state('PROCESO_COMPLETADO', {
|
|
1982
|
+
skippedBack,
|
|
1983
|
+
totalImages: capturedImages.metadata.totalImages,
|
|
1984
|
+
timestamp: new Date().toISOString()
|
|
1985
|
+
});
|
|
1986
|
+
}
|
|
958
1987
|
stopDetection() {
|
|
959
1988
|
if (this.animationId) {
|
|
960
1989
|
cancelAnimationFrame(this.animationId);
|
|
961
1990
|
this.animationId = undefined;
|
|
962
1991
|
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
1992
|
+
this.detectionBoxes = [];
|
|
1993
|
+
this.logger.state('DETECTOR_DETENIDO', { timestamp: Date.now() });
|
|
1994
|
+
}
|
|
1995
|
+
toggleCameraSelector() {
|
|
1996
|
+
if (this.isSwitchingCamera)
|
|
1997
|
+
return; // Don't toggle if switching camera
|
|
1998
|
+
this.showCameraSelector = !this.showCameraSelector;
|
|
1999
|
+
}
|
|
2000
|
+
async handleCameraSwitch(cameraId) {
|
|
2001
|
+
if (this.isSwitchingCamera)
|
|
2002
|
+
return; // Prevent multiple simultaneous switches
|
|
2003
|
+
try {
|
|
2004
|
+
// Close the selector immediately when user selects a camera
|
|
2005
|
+
this.showCameraSelector = false;
|
|
2006
|
+
this.isSwitchingCamera = true;
|
|
2007
|
+
this.logger.state('INICIANDO_CAMBIO_CAMARA', {
|
|
2008
|
+
from: this.cameraService.getSelectedCameraId(),
|
|
2009
|
+
to: cameraId
|
|
2010
|
+
});
|
|
2011
|
+
// Stop current video stream
|
|
2012
|
+
if (this.videoStream) {
|
|
2013
|
+
this.videoStream.getTracks().forEach(track => track.stop());
|
|
2014
|
+
}
|
|
2015
|
+
// Switch camera in service
|
|
2016
|
+
await this.cameraService.switchCamera(cameraId);
|
|
2017
|
+
// Setup new camera stream
|
|
2018
|
+
const newStream = await this.cameraService.setupCamera();
|
|
2019
|
+
// Update video element
|
|
2020
|
+
await this.initializeVideoStream(newStream);
|
|
2021
|
+
this.logger.state('CAMBIO_CAMARA_EXITOSO', {
|
|
2022
|
+
newCameraId: cameraId,
|
|
2023
|
+
isRearCamera: this.cameraService.isRearCamera(newStream)
|
|
2024
|
+
});
|
|
2025
|
+
}
|
|
2026
|
+
catch (error) {
|
|
2027
|
+
this.logger.error('Error al cambiar cámara:', error);
|
|
2028
|
+
// Try to restore previous camera if switch failed
|
|
2029
|
+
try {
|
|
2030
|
+
const fallbackStream = await this.cameraService.setupCamera();
|
|
2031
|
+
await this.initializeVideoStream(fallbackStream);
|
|
2032
|
+
}
|
|
2033
|
+
catch (fallbackError) {
|
|
2034
|
+
this.logger.error('Error al restaurar cámara anterior:', fallbackError);
|
|
2035
|
+
this.updateStatus('Error al cambiar cámara', 'No se pudo completar el cambio de dispositivo', 'error');
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
finally {
|
|
2039
|
+
this.isSwitchingCamera = false;
|
|
967
2040
|
}
|
|
968
|
-
this.debugLog('🛑 Detector de identificación detenido');
|
|
969
2041
|
}
|
|
970
2042
|
resetDetection() {
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
this.
|
|
974
|
-
|
|
2043
|
+
const currentCaptureState = this.stateManager.getCaptureState();
|
|
2044
|
+
const wasVideoActive = currentCaptureState.isVideoActive;
|
|
2045
|
+
this.stateManager.reset();
|
|
2046
|
+
if (wasVideoActive) {
|
|
2047
|
+
this.stateManager.updateCaptureState({ isVideoActive: true });
|
|
2048
|
+
}
|
|
975
2049
|
this.hasScreenshotTaken = false;
|
|
976
|
-
this.
|
|
977
|
-
this.capturedCroppedId = null;
|
|
978
|
-
this.capturedBackFullFrame = null;
|
|
979
|
-
this.capturedBackCroppedId = null;
|
|
980
|
-
this.captureStep = 'front';
|
|
981
|
-
this.isCapturing = false;
|
|
982
|
-
this.showFlipAnimation = false;
|
|
983
|
-
this.showSuccessAnimation = false;
|
|
984
|
-
this.isDetectionPaused = false;
|
|
985
|
-
this.isLoading = false;
|
|
986
|
-
this.lastDetectedBox = undefined;
|
|
987
|
-
this.isModelPreloaded = false;
|
|
988
|
-
// Reset performance counters
|
|
2050
|
+
this.startTime = Date.now();
|
|
989
2051
|
this.frameSkipCounter = 0;
|
|
990
2052
|
this.consecutiveFailures = 0;
|
|
991
2053
|
this.lastInferenceTime = 0;
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
2054
|
+
this.detectionBoxes = [];
|
|
2055
|
+
this.alignmentStartTime = undefined;
|
|
2056
|
+
this.hasDocumentDetected = false;
|
|
2057
|
+
if (this.alignmentTimer) {
|
|
2058
|
+
clearTimeout(this.alignmentTimer);
|
|
2059
|
+
this.alignmentTimer = undefined;
|
|
995
2060
|
}
|
|
996
|
-
|
|
997
|
-
|
|
2061
|
+
if (wasVideoActive && this.detectionService.isModelLoaded()) {
|
|
2062
|
+
this.updateStatus('Captura reiniciada', 'Buscando documento en el marco de captura', 'active');
|
|
998
2063
|
this.detectFrame();
|
|
999
2064
|
}
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
this.statusMessage = skippedBack ?
|
|
1004
|
-
"Proceso completado (solo frente capturado)" :
|
|
1005
|
-
"Proceso de captura completado exitosamente";
|
|
1006
|
-
this.statusColor = "#28a745";
|
|
1007
|
-
// Detener el detector
|
|
1008
|
-
this.stopDetection();
|
|
1009
|
-
// Emitir evento con las imágenes capturadas
|
|
1010
|
-
const capturedImages = {
|
|
1011
|
-
front: {
|
|
1012
|
-
fullFrame: this.capturedFullFrame,
|
|
1013
|
-
cropped: this.capturedCroppedId
|
|
1014
|
-
},
|
|
1015
|
-
back: {
|
|
1016
|
-
fullFrame: this.capturedBackFullFrame,
|
|
1017
|
-
cropped: this.capturedBackCroppedId
|
|
1018
|
-
},
|
|
1019
|
-
timestamp: new Date().toISOString(),
|
|
1020
|
-
metadata: {
|
|
1021
|
-
totalImages: skippedBack ? 2 : 4,
|
|
1022
|
-
processCompleted: true,
|
|
1023
|
-
backCaptureSkipped: skippedBack
|
|
1024
|
-
}
|
|
1025
|
-
};
|
|
1026
|
-
this.captureCompleted.emit(capturedImages);
|
|
1027
|
-
// Mostrar animación de éxito después de la captura
|
|
1028
|
-
setTimeout(() => {
|
|
1029
|
-
this.showSuccessAnimation = true;
|
|
1030
|
-
setTimeout(() => {
|
|
1031
|
-
this.showSuccessAnimation = false;
|
|
1032
|
-
}, 3000);
|
|
1033
|
-
}, 800);
|
|
2065
|
+
else {
|
|
2066
|
+
this.updateStatus('Listo para capturar', '', 'ready');
|
|
2067
|
+
}
|
|
1034
2068
|
}
|
|
1035
2069
|
exitSession() {
|
|
1036
2070
|
if (this.videoStream) {
|
|
1037
2071
|
this.videoStream.getTracks().forEach(track => track.stop());
|
|
1038
2072
|
this.videoStream = undefined;
|
|
1039
|
-
this.isVideoActive
|
|
1040
|
-
this.statusMessage = "Sesión finalizada.";
|
|
1041
|
-
this.statusColor = "#aaa";
|
|
1042
|
-
}
|
|
1043
|
-
this.isLoading = false;
|
|
1044
|
-
// Limpiar canvas al finalizar sesión
|
|
1045
|
-
if (this.canvasRef) {
|
|
1046
|
-
const ctx = this.canvasRef.getContext("2d");
|
|
1047
|
-
ctx.clearRect(0, 0, this.canvasRef.width, this.canvasRef.height);
|
|
2073
|
+
this.stateManager.updateCaptureState({ isVideoActive: false, isLoading: false });
|
|
1048
2074
|
}
|
|
2075
|
+
this.isMaskReady = false;
|
|
2076
|
+
this.updateStatus('Sesión finalizada', '', 'ready');
|
|
2077
|
+
this.detectionBoxes = [];
|
|
1049
2078
|
this.cleanup();
|
|
1050
2079
|
}
|
|
1051
|
-
|
|
1052
|
-
|
|
2080
|
+
// PERFORMANCE MONITORING METHODS
|
|
2081
|
+
initializePerformanceMonitor() {
|
|
2082
|
+
this.performanceMetrics.lastUpdateTime = performance.now();
|
|
2083
|
+
// Update performance metrics every 500ms
|
|
2084
|
+
this.performanceUpdateInterval = window.setInterval(() => {
|
|
2085
|
+
this.updatePerformanceMetrics();
|
|
2086
|
+
}, 500);
|
|
2087
|
+
this.logger.debug('Monitor de performance inicializado');
|
|
2088
|
+
}
|
|
2089
|
+
updatePerformanceMetrics() {
|
|
2090
|
+
const currentTime = performance.now();
|
|
2091
|
+
const deltaTime = currentTime - this.performanceMetrics.lastUpdateTime;
|
|
2092
|
+
// Calculate FPS
|
|
2093
|
+
if (deltaTime > 0) {
|
|
2094
|
+
this.performanceMetrics.fps = Math.round(1000 / (deltaTime / this.frameSkipCounter || 1));
|
|
2095
|
+
}
|
|
2096
|
+
// Get memory usage if available
|
|
2097
|
+
if ('memory' in performance) {
|
|
2098
|
+
const memInfo = performance.memory;
|
|
2099
|
+
this.performanceMetrics.memoryUsage = Math.round(memInfo.usedJSHeapSize / 1048576); // MB
|
|
2100
|
+
}
|
|
2101
|
+
// Calculate detection success rate
|
|
2102
|
+
if (this.performanceMetrics.totalDetections > 0) {
|
|
2103
|
+
this.performanceMetrics.successfulDetections = this.performanceMetrics.successfulDetections;
|
|
2104
|
+
const detectionRate = (this.performanceMetrics.successfulDetections / this.performanceMetrics.totalDetections) * 100;
|
|
2105
|
+
this.performanceMetrics.detectionRate = Math.round(detectionRate);
|
|
2106
|
+
}
|
|
2107
|
+
// Update state for rendering
|
|
2108
|
+
this.performanceData = {
|
|
2109
|
+
fps: this.performanceMetrics.fps,
|
|
2110
|
+
inferenceTime: this.performanceMetrics.inferenceTime,
|
|
2111
|
+
memoryUsage: this.performanceMetrics.memoryUsage,
|
|
2112
|
+
onnxLoadTime: this.performanceMetrics.onnxLoadTime,
|
|
2113
|
+
frameProcessingTime: this.performanceMetrics.frameProcessingTime,
|
|
2114
|
+
totalDetections: this.performanceMetrics.totalDetections,
|
|
2115
|
+
successfulDetections: this.performanceMetrics.successfulDetections,
|
|
2116
|
+
detectionRate: this.performanceMetrics.detectionRate
|
|
2117
|
+
};
|
|
2118
|
+
this.performanceMetrics.lastUpdateTime = currentTime;
|
|
2119
|
+
}
|
|
2120
|
+
recordOnnxPerformance(loadTime, inferenceTime) {
|
|
2121
|
+
this.performanceMetrics.onnxLoadTime = Math.round(loadTime);
|
|
2122
|
+
this.performanceMetrics.inferenceTime = Math.round(inferenceTime);
|
|
2123
|
+
}
|
|
2124
|
+
recordFrameProcessing(processingTime, detectionsFound) {
|
|
2125
|
+
this.performanceMetrics.frameProcessingTime = Math.round(processingTime);
|
|
2126
|
+
this.performanceMetrics.totalDetections++;
|
|
2127
|
+
if (detectionsFound > 0) {
|
|
2128
|
+
this.performanceMetrics.successfulDetections++;
|
|
2129
|
+
}
|
|
1053
2130
|
}
|
|
1054
2131
|
};
|
|
1055
2132
|
JaakStamps.style = myComponentCss;
|