-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-ocr-parser.ts
More file actions
526 lines (432 loc) · 23.8 KB
/
Copy pathtest-ocr-parser.ts
File metadata and controls
526 lines (432 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// ALGORITMO FINAL - Usando Bounding Boxes para orden correcto
// Ejecutar con: npx tsx test-ocr-parser.ts
import { readFileSync } from 'fs';
import sharp from 'sharp';
import { googleVisionClient } from './src/lib/clients/google-vision.js';
interface TextBlock {
text: string;
x: number;
y: number;
}
interface Producto {
lineaIndex: number;
codigo: string;
nombre: string;
cantidad: number;
unidad: string;
precioUnitario: number | null;
precioTotal: number | null;
}
/**
* Valida la calidad del OCR basándose en métricas de Google Vision
* Retorna true si la calidad es aceptable, false si es muy mala
*/
function validarCalidadOCR(fullTextAnnotation: any): { esValida: boolean; razon?: string; confianza?: number; numProductos?: number } {
// 1. Verificar que hay texto detectado
if (!fullTextAnnotation || !fullTextAnnotation.text || fullTextAnnotation.text.trim().length < 50) {
return {
esValida: false,
razon: 'No se detectó suficiente texto en la imagen'
};
}
// 2. Verificar confianza de Google Vision (umbral más estricto: 70%)
const confianza = fullTextAnnotation.pages?.[0]?.confidence;
if (confianza !== undefined && confianza < 0.70) {
return {
esValida: false,
razon: 'La calidad de la imagen es muy baja para procesamiento confiable',
confianza: Math.round(confianza * 100)
};
}
// 3. Verificar número de bloques detectados
const numBloques = fullTextAnnotation.pages?.[0]?.blocks?.length || 0;
if (numBloques < 10) {
return {
esValida: false,
razon: 'La imagen está muy borrosa o tiene muy poco contenido legible',
confianza: confianza ? Math.round(confianza * 100) : undefined
};
}
// 4. Verificar que se detectaron códigos de barras (indicador de productos)
const texto = fullTextAnnotation.text;
const codigosBarras = texto.match(/\d{13}/g);
const numProductos = codigosBarras ? codigosBarras.length : 0;
if (numProductos === 0) {
return {
esValida: false,
razon: 'No se detectaron códigos de barras. La imagen puede estar muy borrosa o no es una boleta válida',
confianza: confianza ? Math.round(confianza * 100) : undefined
};
}
// 5. Verificar que hay al menos 2 productos (boletas muy pequeñas pueden ser sospechosas)
if (numProductos < 2) {
return {
esValida: false,
razon: 'Se detectó muy poco contenido. Por favor, asegúrate de capturar toda la boleta',
confianza: confianza ? Math.round(confianza * 100) : undefined,
numProductos
};
}
return {
esValida: true,
confianza: confianza ? Math.round(confianza * 100) : undefined,
numProductos
};
}
/**
* Preprocesa la imagen para mejorar la calidad del OCR (OPTIMIZADO)
* - Operaciones reducidas para mejor rendimiento
* - Mantiene calidad aceptable
*/
async function preprocesarImagen(imageBuffer: Buffer): Promise<Buffer> {
console.log('🔧 Preprocesando imagen (optimizado)...');
try {
const processedBuffer = await sharp(imageBuffer)
// 1. Resize moderado (reducido de 3000 a 2000)
.resize({
width: 2000,
fit: 'inside',
withoutEnlargement: true,
kernel: 'mitchell' // Más rápido que lanczos3
})
// 2. Escala de grises
.grayscale()
// 3. Normalizar (auto-contraste)
.normalize()
// 4. Nitidez moderada (reducido de 2.0 a 1.2)
.sharpen({
sigma: 1.2,
m1: 1.0,
m2: 0.5
})
// Removido: linear() y threshold() para mayor velocidad
.toBuffer();
console.log('✅ Imagen preprocesada\n');
return processedBuffer;
} catch (error) {
console.warn('⚠️ Error en preprocesamiento, usando imagen original');
return imageBuffer;
}
}
async function main() {
console.log('═══════════════════════════════════════════════════════════');
console.log('🎯 ALGORITMO FINAL - MATCHING CON BOUNDING eeBOXES');
console.log('═══════════════════════════════════════════════════════════\n');
const imagePath = './WhatsApp Image 2025-11-07 at 7.06.25 PM(2).jpeg';
const imageBuffer = readFileSync(imagePath);
console.log('📸 Extrayendo texto con Google Vision...\n');
// Preprocesar imagen para mejorar calidad (OPTIMIZADO)
const processedBuffer = await preprocesarImagen(imageBuffer);
const client = googleVisionClient.getClient();
const [result] = await client.documentTextDetection({
image: { content: processedBuffer },
});
const fullTextAnnotation = result.fullTextAnnotation;
if (!fullTextAnnotation?.pages?.[0]) {
console.log('❌ No se pudo extraer texto');
return;
}
// ═══════════════════════════════════════════════════════════
// VALIDACIÓN DE CALIDAD
// ═══════════════════════════════════════════════════════════
const validacion = validarCalidadOCR(fullTextAnnotation);
if (!validacion.esValida) {
console.log('\n❌❌❌ ERROR: CALIDAD DE IMAGEN INSUFICIENTE ❌❌❌\n');
console.log(`📋 Razón: ${validacion.razon}`);
if (validacion.confianza !== undefined) {
console.log(`📊 Confianza del OCR: ${validacion.confianza}%`);
}
console.log('\n💡 RECOMENDACIONES PARA MEJORAR LA FOTO:\n');
console.log(' ✅ Asegúrate de que la boleta esté bien iluminada');
console.log(' ✅ Mantén la boleta completamente plana (sin arrugas)');
console.log(' ✅ Toma la foto perpendicular a la boleta (no en ángulo)');
console.log(' ✅ Asegúrate de que todo el texto esté enfocado');
console.log(' ✅ Usa el flash si es necesario');
console.log(' ✅ Evita sombras sobre la boleta\n');
console.log('Por favor, toma una nueva foto con mejor calidad e intenta nuevamente.\n');
console.log('═══════════════════════════════════════════════════════════\n');
return; // Salir sin procesar
}
console.log(`✅ Calidad de imagen aceptable`);
console.log(` 📊 Confianza OCR: ${validacion.confianza || 'N/A'}%`);
console.log(` 🛒 Productos detectados: ${validacion.numProductos || 'N/A'}\n`);
// ═══════════════════════════════════════════════════════════
// PASO 1: Extraer y ordenar bloques por posición
// ═══════════════════════════════════════════════════════════
const textBlocks: TextBlock[] = [];
const page = fullTextAnnotation.pages[0];
if (page.blocks) {
for (const block of page.blocks) {
if (!block.paragraphs) continue;
for (const paragraph of block.paragraphs) {
if (!paragraph.words) continue;
const paragraphText = paragraph.words
.map((word: any) =>
word.symbols?.map((symbol: any) => symbol.text).join('') || ''
)
.join(' ');
if (paragraph.boundingBox?.vertices) {
const vertices = paragraph.boundingBox.vertices;
const x = vertices.reduce((sum: number, v: any) => sum + (v.x || 0), 0) / vertices.length;
const y = vertices.reduce((sum: number, v: any) => sum + (v.y || 0), 0) / vertices.length;
if (paragraphText.trim()) {
textBlocks.push({ text: paragraphText.trim(), x, y });
}
}
}
}
}
// Ordenar por posición Y, luego X
const TOLERANCE_Y = 10;
textBlocks.sort((a, b) => {
if (Math.abs(a.y - b.y) < TOLERANCE_Y) {
return a.x - b.x;
}
return a.y - b.y;
});
// Agrupar en líneas
const lineas: string[] = [];
let lineaActual = '';
let yActual = textBlocks[0]?.y || 0;
for (const block of textBlocks) {
if (Math.abs(block.y - yActual) < TOLERANCE_Y) {
lineaActual += (lineaActual ? ' ' : '') + block.text;
} else {
if (lineaActual) lineas.push(lineaActual);
lineaActual = block.text;
yActual = block.y;
}
}
if (lineaActual) lineas.push(lineaActual);
console.log(`✅ Total de líneas ordenadas: ${lineas.length}\n`);
// 🔍 DEBUG: Mostrar todas las líneas extraídas
console.log('═══════════════════════════════════════════════════════════');
console.log('🔍 DEBUG: TODAS LAS LÍNEAS EXTRAÍDAS');
console.log('═══════════════════════════════════════════════════════════\n');
lineas.forEach((linea, idx) => {
console.log(`[${idx.toString().padStart(2, '0')}] ${linea}`);
});
console.log('\n');
// ═══════════════════════════════════════════════════════════
// PASO 2: Detectar productos
// ═══════════════════════════════════════════════════════════
console.log('🛒 Detectando productos...\n');
const productos: Producto[] = [];
for (let i = 0; i < lineas.length; i++) {
const linea = lineas[i];
const matchCodigo = linea.match(/^(\d{13})\s+(.+)/);
if (matchCodigo) {
const codigo = matchCodigo[1];
let nombre = matchCodigo[2]
.replace(/\s+\d+[.,]\d{2}$/, '')
.replace(/[^a-zA-ZáéíóúñÁÉÍÓÚÑ\s0-9]/g, ' ')
.replace(/\s{2,}/g, ' ')
.trim()
.substring(0, 40);
if (nombre.length >= 3) {
productos.push({
lineaIndex: i,
codigo,
nombre,
cantidad: 1,
unidad: 'un',
precioUnitario: null,
precioTotal: null
});
console.log(` ✅ [${i.toString().padStart(2, '0')}] ${nombre}`);
}
}
}
console.log(`\n✅ Total: ${productos.length} productos\n`);
// ═══════════════════════════════════════════════════════════
// PASO 3: Buscar cantidades en líneas cercanas (≤3 líneas)
// ═══════════════════════════════════════════════════════════
console.log('📏 Buscando cantidades en líneas cercanas...\n');
const lineasCantidadUsadas = new Set<number>(); // Evitar que dos productos usen la misma línea
for (const producto of productos) {
const lineaProducto = lineas[producto.lineaIndex];
// Primero intentar extraer de la misma línea del producto
const matchEnLinea = lineaProducto.match(/(\d+(?:[.,]\d+)?)\s*(kg|g|ml|l|ko|un)\s+(\d+[.,]\d{2})\s+X\s+/i);
if (matchEnLinea) {
producto.cantidad = parseFloat(matchEnLinea[1].replace(',', '.'));
producto.unidad = (matchEnLinea[2] || 'un').toLowerCase().replace('ko', 'kg');
producto.precioUnitario = parseFloat(matchEnLinea[3].replace(',', '.'));
lineasCantidadUsadas.add(producto.lineaIndex);
console.log(` ✅ ${producto.nombre}`);
console.log(` ${producto.cantidad} ${producto.unidad} @ S/${producto.precioUnitario.toFixed(2)}`);
continue;
}
// Buscar en las siguientes 2 líneas (reducido de 3 a 2 para mayor precisión)
for (let offset = 1; offset <= 2; offset++) {
const nextLineIndex = producto.lineaIndex + offset;
if (nextLineIndex >= lineas.length) break;
if (lineasCantidadUsadas.has(nextLineIndex)) continue; // Ya usada por otro producto
const nextLinea = lineas[nextLineIndex];
const matchCantidad = nextLinea.match(/^(\d+(?:[.,]\d+)?)\s*(kg|g|ml|l|ko|un)?\s+(\d+[.,]\d{2})\s+X\s+/i);
if (matchCantidad) {
producto.cantidad = parseFloat(matchCantidad[1].replace(',', '.'));
producto.unidad = (matchCantidad[2] || 'un').toLowerCase().replace('ko', 'kg');
producto.precioUnitario = parseFloat(matchCantidad[3].replace(',', '.'));
lineasCantidadUsadas.add(nextLineIndex);
console.log(` ✅ ${producto.nombre}`);
console.log(` Línea +${offset}: ${producto.cantidad} ${producto.unidad} @ S/${producto.precioUnitario.toFixed(2)}`);
break;
}
}
}
console.log('');
// ═══════════════════════════════════════════════════════════
// PASO 4: Extraer bloque de precios (MEJORADO)
// ═══════════════════════════════════════════════════════════
console.log('💰 Extrayendo bloque de precios (mejorado)...\n');
const preciosSueltos: number[] = [];
const preciosConLinea: Map<number, number> = new Map(); // línea -> precio
let dentroDeProductos = false;
for (let i = 0; i < lineas.length; i++) {
const linea = lineas[i];
if (/^\d{13}/.test(linea)) {
dentroDeProductos = true;
}
if (dentroDeProductos) {
// Formato 1: Precio suelto en su propia línea (ej: "5.70")
const matchPrecioSuelto = linea.match(/^(\d+[.,]\d{2})$/);
// Formato 2: Precio al final de línea de producto (ej: "POP CORN TOT 1KG 7.60")
const matchPrecioEnProducto = linea.match(/^\d{13}\s+.+\s+(\d+[.,]\d{2})$/);
// Formato 3: Precio al final de línea de cantidad (ej: "2 2.20 X UN 4.40")
const matchPrecioEnCantidad = linea.match(/\d+(?:[.,]\d+)?\s+\d+[.,]\d{2}\s+X\s+(?:UN|KG|G|ML|L)\s+(\d+[.,]\d{2})$/i);
if (matchPrecioSuelto) {
const precio = parseFloat(matchPrecioSuelto[1].replace(',', '.'));
if (precio >= 0.10 && precio < 1000) {
preciosSueltos.push(precio);
preciosConLinea.set(i, precio);
console.log(` [${i}] Precio suelto: ${linea} → S/ ${precio.toFixed(2)}`);
}
} else if (matchPrecioEnProducto) {
const precio = parseFloat(matchPrecioEnProducto[1].replace(',', '.'));
if (precio >= 0.10 && precio < 1000) {
preciosSueltos.push(precio);
preciosConLinea.set(i, precio);
console.log(` [${i}] Precio en producto: ${linea} → S/ ${precio.toFixed(2)}`);
}
} else if (matchPrecioEnCantidad) {
const precio = parseFloat(matchPrecioEnCantidad[1].replace(',', '.'));
if (precio >= 0.10 && precio < 1000) {
preciosSueltos.push(precio);
preciosConLinea.set(i, precio);
console.log(` [${i}] Precio en cantidad: ${linea} → S/ ${precio.toFixed(2)}`);
}
}
if (linea.includes('Descuento') || linea.includes('TOTAL') || linea.includes('SUB TOTAL')) {
console.log(` [${i}] Fin del bloque de productos\n`);
break;
}
}
}
console.log(`✅ Total: ${preciosSueltos.length} precios extraídos\n`);
// ═══════════════════════════════════════════════════════════
// PASO 5: Asignar precios (MEJORADO - Por proximidad)
// ═══════════════════════════════════════════════════════════
console.log('🔗 Asignando precios...\n');
const preciosUsados = new Array(preciosSueltos.length).fill(false);
const preciosArray = Array.from(preciosConLinea.entries()); // [lineaIndex, precio]
// Primero: Asignar precios calculados (cantidad × precio unitario)
for (const producto of productos) {
if (producto.precioUnitario) {
const precioEsperado = producto.cantidad * producto.precioUnitario;
// Buscar el precio más cercano al esperado
let mejorMatch = -1;
let menorDiferencia = Infinity;
for (let j = 0; j < preciosSueltos.length; j++) {
if (preciosUsados[j]) continue;
const diferencia = Math.abs(preciosSueltos[j] - precioEsperado);
if (diferencia < menorDiferencia && diferencia < 0.05) {
menorDiferencia = diferencia;
mejorMatch = j;
}
}
if (mejorMatch !== -1) {
producto.precioTotal = preciosSueltos[mejorMatch];
preciosUsados[mejorMatch] = true;
console.log(` ✅ ${producto.nombre}: S/${preciosSueltos[mejorMatch].toFixed(2)} (calculado)`);
}
}
}
// Segundo: Asignar precios restantes por proximidad de línea
for (const producto of productos) {
if (producto.precioTotal === null) {
// Buscar el precio más cercano en términos de líneas
let mejorMatch = -1;
let menorDistancia = Infinity;
for (let j = 0; j < preciosArray.length; j++) {
if (preciosUsados[j]) continue;
const [lineaPrecio, precio] = preciosArray[j];
const distancia = Math.abs(lineaPrecio - producto.lineaIndex);
// Preferir precios que estén DESPUÉS del producto (máximo 5 líneas)
if (lineaPrecio >= producto.lineaIndex && distancia <= 5 && distancia < menorDistancia) {
menorDistancia = distancia;
mejorMatch = j;
}
}
if (mejorMatch !== -1) {
producto.precioTotal = preciosSueltos[mejorMatch];
preciosUsados[mejorMatch] = true;
const [lineaPrecio] = preciosArray[mejorMatch];
console.log(` 📍 ${producto.nombre}: S/${preciosSueltos[mejorMatch].toFixed(2)} (línea ${lineaPrecio}, distancia: ${Math.abs(lineaPrecio - producto.lineaIndex)})`);
}
}
}
console.log('');
// ═══════════════════════════════════════════════════════════
// RESULTADO FINAL
// ═══════════════════════════════════════════════════════════
console.log('═══════════════════════════════════════════════════════════');
console.log('📊 RESULTADO FINAL');
console.log('═══════════════════════════════════════════════════════════\n');
productos.forEach((p, i) => {
console.log(`${i + 1}. ${p.nombre}`);
if (p.precioUnitario) {
const calculado = p.cantidad * p.precioUnitario;
const valido = p.precioTotal && Math.abs(calculado - p.precioTotal) < 0.05;
const icono = valido ? '✅' : '⚠️';
console.log(` Cantidad: ${p.cantidad} ${p.unidad}`);
console.log(` Precio unitario: S/ ${p.precioUnitario.toFixed(2)}`);
console.log(` Precio total: S/ ${p.precioTotal?.toFixed(2) || 'N/A'}`);
console.log(` ${icono} Validación: ${p.cantidad} × ${p.precioUnitario.toFixed(2)} = ${calculado.toFixed(2)}`);
} else {
console.log(` Cantidad: ${p.cantidad} ${p.unidad}`);
console.log(` Precio total: S/ ${p.precioTotal?.toFixed(2) || 'N/A'}`);
}
console.log('');
});
// ═══════════════════════════════════════════════════════════
// VERIFICACIÓN ESPECÍFICA
// ═══════════════════════════════════════════════════════════
console.log('═══════════════════════════════════════════════════════════');
console.log('🎯 VERIFICACIÓN SPORADE TROPICA');
console.log('═══════════════════════════════════════════════════════════\n');
const sporade = productos.find(p => p.nombre.includes('SPORADE'));
if (sporade) {
console.log('Datos esperados:');
console.log(' Cantidad: 2 un');
console.log(' Precio unit: S/ 2.20');
console.log(' Precio total: S/ 4.40\n');
console.log('Datos obtenidos:');
console.log(` Cantidad: ${sporade.cantidad} ${sporade.unidad}`);
console.log(` Precio unit: S/ ${sporade.precioUnitario?.toFixed(2) || 'N/A'}`);
console.log(` Precio total: S/ ${sporade.precioTotal?.toFixed(2) || 'N/A'}\n`);
const cantidadOK = sporade.cantidad === 2 && sporade.unidad === 'un';
const precioUnitOK = sporade.precioUnitario === 2.20;
const precioTotalOK = sporade.precioTotal === 4.40;
const cantidadIcono = cantidadOK ? '✅' : '❌';
const precioUnitIcono = precioUnitOK ? '✅' : '❌';
const precioTotalIcono = precioTotalOK ? '✅' : '❌';
console.log(`${cantidadIcono} Cantidad: ${cantidadOK ? 'CORRECTO' : 'INCORRECTO'}`);
console.log(`${precioUnitIcono} Precio unitario: ${precioUnitOK ? 'CORRECTO' : 'INCORRECTO'}`);
console.log(`${precioTotalIcono} Precio total: ${precioTotalOK ? 'CORRECTO' : 'INCORRECTO'}`);
if (cantidadOK && precioUnitOK && precioTotalOK) {
console.log('\n🎉 ¡SPORADE TROPICA CORRECTAMENTE EXTRAÍDO!');
}
}
console.log('\n═══════════════════════════════════════════════════════════\n');
}
main().catch(console.error);