SamilG2's picture
Claude Sonnet 5.5
fix(SEC-F9d): la cuadrícula tiene que parecerse a una planilla (A15)
5195335
Raw History Blame Contribute Delete
9.9 kB
import cv2
import numpy as np
def _detect_lines(gray_img):
h, w = gray_img.shape
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
enhanced = clahe.apply(gray_img)
binary = cv2.adaptiveThreshold(enhanced, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 11, 8)
h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (w // 10, 1))
h_lines = cv2.dilate(cv2.morphologyEx(binary, cv2.MORPH_OPEN, h_kernel, iterations=2),
None, iterations=1)
v_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, h // 15))
v_lines = cv2.dilate(cv2.morphologyEx(binary, cv2.MORPH_OPEN, v_kernel, iterations=2),
None, iterations=1)
return h_lines, v_lines
def _extract_peaks(projection, threshold):
above = projection > threshold
positions = []
in_peak = False
peak_vals = []
for i in range(len(above)):
if above[i] and not in_peak:
in_peak = True
peak_vals = [i]
elif above[i] and in_peak:
peak_vals.append(i)
elif not above[i] and in_peak:
positions.append(int(np.mean(peak_vals)))
in_peak = False
peak_vals = []
if in_peak and peak_vals:
positions.append(int(np.mean(peak_vals)))
return positions
def _get_line_positions(line_mask, axis, expected_range=None):
projection = np.sum(line_mask, axis=(1 if axis == 0 else 0)).astype(float)
if projection.max() == 0:
return []
projection /= projection.max()
best_positions = []
best_score = float('inf')
for threshold in [0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.40, 0.50]:
positions = _extract_peaks(projection, threshold)
if expected_range:
min_exp, max_exp = expected_range
if min_exp <= len(positions) <= max_exp:
return positions
score = abs(len(positions) - (min_exp + max_exp) / 2)
if score < best_score:
best_score = score
best_positions = positions
elif threshold == 0.20:
return positions
return best_positions
def _filter_table_region(h_positions, v_positions):
# Sin verticales (una foto que no es una planilla) no hay cuadrícula.
if len(h_positions) < 3 or not v_positions:
return h_positions, v_positions
clean_v = [v_positions[0]]
for i in range(1, len(v_positions)):
if v_positions[i] - clean_v[-1] > 20:
clean_v.append(v_positions[i])
return h_positions[1:], clean_v
def _extract_cells(img_rect, h_positions, v_positions, content_cols):
cells = []
for i in range(len(h_positions) - 1):
y1, y2 = h_positions[i], h_positions[i + 1]
for j in content_cols:
x1, x2 = v_positions[j], v_positions[j + 1]
pad_x = max(2, int((x2 - x1) * 0.03))
pad_y = max(2, int((y2 - y1) * 0.08))
cell_img = img_rect[y1+pad_y:y2-pad_y, x1+pad_x:x2-pad_x]
if cell_img.size == 0:
continue
cells.append({'image': cell_img, 'row': i, 'col': j})
return cells
def _assign_move_numbers(cells, col_to_content):
for cell in cells:
cell['col_index'] = col_to_content.get(cell['col'], 0)
rows = sorted(set(c['row'] for c in cells))
num_rows = len(rows)
row_to_idx = {r: i for i, r in enumerate(rows)}
for cell in cells:
block = cell['col_index'] // 2
is_black = cell['col_index'] % 2
row_idx = row_to_idx[cell['row']]
cell['move_number'] = row_idx + 1 + (block * num_rows)
cell['color'] = 'black' if is_black else 'white'
cells.sort(key=lambda c: (c['move_number'], 0 if c['color'] == 'white' else 1))
return cells
def preprocess_for_trocr(cell_img: np.ndarray) -> np.ndarray | None:
"""Convierte una celda a escala de grises 64x384px para TrOCR."""
gray = cv2.cvtColor(cell_img, cv2.COLOR_BGR2GRAY) if len(cell_img.shape) == 3 else cell_img.copy()
gray = cv2.normalize(gray, None, 0, 255, cv2.NORM_MINMAX)
h, w = gray.shape
if h == 0 or w == 0:
return None
scale = 64 / h
new_w = max(1, int(w * scale))
resized = cv2.resize(gray, (new_w, 64), interpolation=cv2.INTER_AREA)
canvas = np.full((64, 384), 255, dtype=np.uint8)
canvas[:, :min(new_w, 384)] = resized[:, :384]
return canvas
# Cuántas líneas tiene la cuadrícula de una planilla (A15): 26 horizontales y 7
# verticales en las dos que se leen (25 filas; las dos columnas por bloque), y 12 en
# otra de tres bloques. Una foto de otra cosa (un panel, una pared) da pocas o
# muchas líneas, o a distancias dispares.
_FILAS_ENTRE = (15, 30)
_COLUMNAS_ENTRE = (5, 14)
_FILAS_PAREJAS = 0.6 # parte de las filas con el alto de la mediana (±35 %)
def _parece_planilla(h_positions, v_positions) -> bool:
if not _FILAS_ENTRE[0] <= len(h_positions) <= _FILAS_ENTRE[1]:
return False
if not _COLUMNAS_ENTRE[0] <= len(v_positions) <= _COLUMNAS_ENTRE[1]:
return False
altos = np.diff(h_positions)
mediana = np.median(altos)
return bool(np.mean(np.abs(altos - mediana) <= 0.35 * mediana) >= _FILAS_PAREJAS)
def _grid_lines(img_rect: np.ndarray):
"""Posiciones de las líneas de la cuadrícula de jugadas y la máscara de verticales.
Si las líneas no se parecen a las de una planilla, no hay cuadrícula."""
gray = cv2.GaussianBlur(cv2.cvtColor(img_rect, cv2.COLOR_BGR2GRAY), (3, 3), 0)
h_lines_mask, v_lines_mask = _detect_lines(gray)
h_positions = _get_line_positions(h_lines_mask, axis=0, expected_range=(21, 27))
v_positions = _get_line_positions(v_lines_mask, axis=1, expected_range=(7, 13))
h_positions, v_positions = _filter_table_region(h_positions, v_positions)
if not _parece_planilla(h_positions, v_positions):
return [], [], v_lines_mask
return h_positions, v_positions, v_lines_mask
def _vertical_extent(v_lines_mask, v_positions):
"""Alto que cubren las líneas verticales de las columnas: (arriba, abajo) o None.
Solo la cuadrícula tiene esas verticales. Se usa la mediana de las columnas
para que una línea suelta de la cabecera no mueva el resultado.
"""
tops, bottoms = [], []
for x in v_positions:
band = v_lines_mask[:, max(0, x - 3):x + 4].any(axis=1)
rows = np.flatnonzero(band)
if rows.size:
tops.append(rows[0])
bottoms.append(rows[-1])
if not tops:
return None
return int(np.median(tops)), int(np.median(bottoms))
def crop_moves_grid(img_rect: np.ndarray) -> np.ndarray | None:
"""Recorta solo la cuadrícula de jugadas (los dos bloques), con un margen pequeño.
Es lo único que sale del servidor hacia Gemini: la cabecera (nombres,
institución) y las firmas quedan fuera. Devuelve None si no hay cuadrícula.
"""
h_positions, v_positions, v_lines_mask = _grid_lines(img_rect)
if len(h_positions) < 2 or len(v_positions) < 2:
return None
top, bottom = h_positions[0], h_positions[-1]
# Si alguna línea larga de la cabecera o de las firmas se tomó como fila,
# las verticales de las columnas marcan dónde termina de verdad la cuadrícula.
extent = _vertical_extent(v_lines_mask, v_positions)
if extent:
top, bottom = max(top, extent[0]), min(bottom, extent[1])
if bottom <= top:
return None
row_h = float(np.median(np.diff(h_positions)))
margin = max(2, int(row_h * 0.15))
img_h, img_w = img_rect.shape[:2]
y1, y2 = max(0, top - margin), min(img_h, bottom + margin)
x1, x2 = max(0, v_positions[0] - margin), min(img_w, v_positions[-1] + margin)
return img_rect[y1:y2, x1:x2].copy()
# Escritura en la cuadrícula (A15). Una jugada a mano cubre al menos el 20 % del
# ancho de su celda (medido en planillas reales, a mano y en foto); un punto o una
# mancha, el 9 %. Una cuadrícula sin dos celdas escritas no se lee: ni Gemini ni
# TrOCR, que inventan una jugada donde no hay ninguna.
ESCRITURA_ANCHO_MIN = 0.15
ESCRITURA_CELDAS_MIN = 2
def _ancho_de_tinta(celda: np.ndarray) -> float:
"""Qué parte del ancho de la celda abarca su tinta (0 a 1)."""
gray = cv2.cvtColor(celda, cv2.COLOR_BGR2GRAY) if celda.ndim == 3 else celda
tinta = cv2.adaptiveThreshold(cv2.GaussianBlur(gray, (3, 3), 0), 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 21, 12)
h, w = tinta.shape
margen = max(2, int(h * 0.12)) # donde caen los restos de las líneas
tinta = tinta[margen:h - margen, margen:w - margen]
columnas = np.flatnonzero(tinta.any(axis=0))
if columnas.size == 0:
return 0.0
return (columnas[-1] - columnas[0] + 1) / tinta.shape[1]
def celdas_con_escritura(img_rect: np.ndarray) -> int | None:
"""Cuántas celdas de la cuadrícula tienen escritura, o None si no hay cuadrícula."""
celdas = segment_scoresheet(img_rect)
if not celdas:
return None
return sum(_ancho_de_tinta(c['image']) >= ESCRITURA_ANCHO_MIN for c in celdas)
def segment_scoresheet(img_rect: np.ndarray) -> list[dict]:
"""Detecta la cuadricula y extrae celdas de movimientos ordenadas por jugada."""
h_positions, v_positions, _ = _grid_lines(img_rect)
col_widths = [v_positions[j+1] - v_positions[j] for j in range(len(v_positions) - 1)]
if not col_widths:
return []
median_width = np.median(col_widths)
content_cols = [j for j, w in enumerate(col_widths) if w > median_width * 0.5]
col_to_content = {j: idx for idx, j in enumerate(content_cols)}
cells = _extract_cells(img_rect, h_positions, v_positions, content_cols)
cells = _assign_move_numbers(cells, col_to_content)
return cells