fix: probar OCR con imagen que tiene texto real, no pixel en blanco
Un pixel 1x1 transparente siempre da "sin_texto_detectado" aunque el servicio funcione perfecto, mostrando un falso ❌. Ahora manda un PNG con texto renderizado y distingue 3 casos: conectado y leyo texto (✅), conectado pero no reconocio nada (⚠️, indica problema real del OCR), y no se pudo conectar (❌). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
978c7c1492
commit
38f0986aad
@@ -190,26 +190,35 @@ class ShowConfiguracionChat extends Component
|
||||
return;
|
||||
}
|
||||
|
||||
// 1x1 png transparente, solo para verificar que el servicio responde.
|
||||
$pixel = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=');
|
||||
// PNG 240x80 con el texto "TEST OCR 123" renderizado, para que la prueba
|
||||
// sea real: un pixel en blanco siempre da "sin_texto_detectado" aunque
|
||||
// todo funcione bien.
|
||||
$imagenPrueba = 'iVBORw0KGgoAAAANSUhEUgAAAPAAAABQCAIAAACoK28rAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB6ElEQVR4nO3c3W6iUBhA0WHS939l5sLENPxpPRCd3bXuWgSPZVc/MXGa5/kPVPx99wLgTIImRdCkCJoUQZMiaFIETYqgSRE0KYImRdCkCJoUQZMiaFIETYqgSRE0KYImRdCkCJoUQZMiaFIETYqgSRE0KYImRdCkCJoUQZMiaFIETYqgSfka2Xmaps3fz/O8uen+5errrbdNBwf80WIWt18c9uC+nv/699vu69vvLWNzDZxuOuUvuz67e+f7yRsf7P5wAYt9934cudN7nZv/OetlHC+JE33EyDHP88jZXfTxvaF1OuMZ7b2MHCxj8QCPX44YMTRy/KcGm97LcXxcYdy1QW8OlLeX+w+ZKa9bxma1e7MKZ7k26L3Ttj7N03TONP+yc+997zn4/uz+9sdb9REz9Bvdp9sTJ9qHE4UZ+jq/MeifXjR84eCefd/lDUGvB+hBixy/J7Uu9fhS9+DCDi5Or9cg+itcex164eEHKwcHfHINB0dbbH35OvRrnwp9yJvgPG9NSPmNMzRhgiZF0KQImhRBkyJoUgRNiqBJETQpgiZF0KQImhRBkyJoUgRNiqBJETQpgiZF0KQImhRBkyJoUgRNiqBJETQpgiZF0KQImhRBkyJoUgRNiqBJETQpgiZF0KQImpR/3UrzkDoP+sMAAAAASUVORK5CYII=';
|
||||
|
||||
try {
|
||||
$inicio = microtime(true);
|
||||
$response = Http::withToken($token)
|
||||
->timeout(15)
|
||||
->post("{$url}/extract", [
|
||||
'image_base64' => base64_encode($pixel),
|
||||
'image_base64' => $imagenPrueba,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
$ms = (int) ((microtime(true) - $inicio) * 1000);
|
||||
$ms = (int) ((microtime(true) - $inicio) * 1000);
|
||||
$data = $response->json();
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->ocrResult = "✅ Servicio OCR responde OK ({$ms}ms). " .
|
||||
"Respuesta: " . substr($response->body(), 0, 200);
|
||||
} else {
|
||||
$this->ocrResult = "❌ El servicio respondió con error " . $response->status() . ": " .
|
||||
if ($data === null) {
|
||||
// No respondio JSON valido: la conexion en si fallo (ruta mal, host caido, etc).
|
||||
$this->ocrResult = "❌ El servicio respondió HTTP " . $response->status() . " sin JSON válido: " .
|
||||
substr($response->body(), 0, 200);
|
||||
} elseif ($data['success'] ?? false) {
|
||||
$texto = $data['text'] ?? '';
|
||||
$this->ocrResult = "✅ Servicio OCR responde OK ({$ms}ms). Texto reconocido: \"" . substr($texto, 0, 150) . "\"";
|
||||
} else {
|
||||
// Respondio con JSON estructurado pero success=false: SI esta conectado,
|
||||
// solo que no reconocio el texto de la imagen de prueba.
|
||||
$this->ocrResult = "⚠️ El servicio conectó pero no reconoció el texto de prueba (HTTP {$response->status()}): " .
|
||||
($data['error'] ?? 'sin detalle');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->ocrResult = '❌ No se pudo conectar con el servicio OCR: ' . $e->getMessage();
|
||||
|
||||
@@ -182,7 +182,7 @@
|
||||
<span wire:loading wire:target="probarOcr">Probando...</span>
|
||||
</button>
|
||||
@if($ocrResult)
|
||||
<p class="mt-2 text-sm {{ str_contains($ocrResult, '✅') ? 'text-emerald-600' : 'text-red-600' }}">
|
||||
<p class="mt-2 text-sm {{ str_contains($ocrResult, '✅') ? 'text-emerald-600' : (str_contains($ocrResult, '⚠️') ? 'text-amber-600' : 'text-red-600') }}">
|
||||
{{ $ocrResult }}
|
||||
</p>
|
||||
@endif
|
||||
|
||||
Reference in New Issue
Block a user