Spaces:
Running
Running
File size: 2,289 Bytes
4e07023 52e77bd 4e07023 52e77bd 4e07023 52e77bd 4e07023 52e77bd 4e07023 52e77bd 4e07023 52e77bd 4e07023 52e77bd |
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 |
import re
import cv2
import numpy as np
import pandas as pd
def clean_text(text):
"""Limpieza estándar para BERT."""
if not text or pd.isna(text): return "sin texto"
text = str(text).lower()
# Eliminar URLs y usuarios
text = re.sub(r'http\S+|www\.\S+', '', text)
text = re.sub(r'@\w+', '', text)
# Normalizar risas
text = re.sub(r'(ja|je|ha|he|lo){2,}', 'jaja', text)
# Eliminar basura de OCR
text = re.sub(r'[|_~*^>\[\]]', ' ', text)
# Espacios y saltos
text = text.replace('\n', ' ').replace('\r', ' ')
text = re.sub(r'\s+', ' ', text).strip()
return text if text else "sin texto"
def preprocess_image_for_ocr(file_obj):
"""
Versión optimizada para memes con subtítulos oscuros/complejos.
"""
try:
# Leer el archivo (puede ser UploadedFile de Streamlit o bytes)
if hasattr(file_obj, 'read'):
file_bytes = np.asarray(bytearray(file_obj.read()), dtype=np.uint8)
else:
file_bytes = np.asarray(bytearray(file_obj), dtype=np.uint8)
img = cv2.imdecode(file_bytes, 1)
if img is None:
return None, None
# 1. Upscaling (Mantenemos esto, es vital)
img_resized = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
# 2. Convertir a escala de grises
gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
# 3. CAMBIO CLAVE: Aumentar Contraste (CLAHE) en lugar de Binarizar agresivamente
# CLAHE (Contrast Limited Adaptive Histogram Equalization) mejora el texto
# sin destruir los bordes como lo hace el Threshold puro.
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
contrast_img = clahe.apply(gray)
# 4. Denoising suave (bajamos h de 10 a 5 para no borrar letras finas)
denoised = cv2.fastNlMeansDenoising(contrast_img, None, h=5, templateWindowSize=7, searchWindowSize=21)
# Retornamos la imagen contrastada (gris) en lugar de binarizada (blanco/negro)
# EasyOCR a veces prefiere grises con buen contraste que binarización forzada.
return denoised, img
except Exception as e:
print(f"Error pre-procesamiento: {e}")
return None, None |