julianzrmrz commited on
Commit
52e77bd
verified
1 Parent(s): aa1492c

Update src/utils.py

Browse files
Files changed (1) hide show
  1. src/utils.py +29 -21
src/utils.py CHANGED
@@ -19,33 +19,41 @@ def clean_text(text):
19
  text = re.sub(r'\s+', ' ', text).strip()
20
  return text if text else "sin texto"
21
 
22
- def preprocess_image_for_ocr(file_bytes):
23
  """
24
- Recibe bytes (desde Streamlit) y aplica filtros de OpenCV.
25
- Retorna: (imagen_binarizada, imagen_original_cv2)
26
  """
27
- # Convertir bytes a array numpy para OpenCV
28
- file_bytes = np.asarray(bytearray(file_bytes.read()), dtype=np.uint8)
29
- img = cv2.imdecode(file_bytes, 1) # 1 = Color BGR
30
-
31
- if img is None: return None, None
32
-
33
- # Pipeline de Mejora (Igual al benchmark)
34
  try:
35
- # 1. Upscaling (2x)
 
 
 
 
 
 
 
 
 
 
 
36
  img_resized = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
37
 
38
- # 2. Escala de Grises
39
  gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
40
 
41
- # 3. Denoising
42
- denoised = cv2.fastNlMeansDenoising(gray, None, h=10, templateWindowSize=7, searchWindowSize=21)
 
 
 
 
 
 
43
 
44
- # 4. Binarizaci贸n Adaptativa
45
- binary = cv2.adaptiveThreshold(
46
- denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2
47
- )
48
- return binary, img
49
  except Exception as e:
50
- print(f"Error en pre-procesamiento: {e}")
51
- return None, img
 
19
  text = re.sub(r'\s+', ' ', text).strip()
20
  return text if text else "sin texto"
21
 
22
+ def preprocess_image_for_ocr(file_obj):
23
  """
24
+ Versi贸n optimizada para memes con subt铆tulos oscuros/complejos.
 
25
  """
 
 
 
 
 
 
 
26
  try:
27
+ # Leer el archivo (puede ser UploadedFile de Streamlit o bytes)
28
+ if hasattr(file_obj, 'read'):
29
+ file_bytes = np.asarray(bytearray(file_obj.read()), dtype=np.uint8)
30
+ else:
31
+ file_bytes = np.asarray(bytearray(file_obj), dtype=np.uint8)
32
+
33
+ img = cv2.imdecode(file_bytes, 1)
34
+
35
+ if img is None:
36
+ return None, None
37
+
38
+ # 1. Upscaling (Mantenemos esto, es vital)
39
  img_resized = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
40
 
41
+ # 2. Convertir a escala de grises
42
  gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
43
 
44
+ # 3. CAMBIO CLAVE: Aumentar Contraste (CLAHE) en lugar de Binarizar agresivamente
45
+ # CLAHE (Contrast Limited Adaptive Histogram Equalization) mejora el texto
46
+ # sin destruir los bordes como lo hace el Threshold puro.
47
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
48
+ contrast_img = clahe.apply(gray)
49
+
50
+ # 4. Denoising suave (bajamos h de 10 a 5 para no borrar letras finas)
51
+ denoised = cv2.fastNlMeansDenoising(contrast_img, None, h=5, templateWindowSize=7, searchWindowSize=21)
52
 
53
+ # Retornamos la imagen contrastada (gris) en lugar de binarizada (blanco/negro)
54
+ # EasyOCR a veces prefiere grises con buen contraste que binarizaci贸n forzada.
55
+ return denoised, img
56
+
 
57
  except Exception as e:
58
+ print(f"Error pre-procesamiento: {e}")
59
+ return None, None