File size: 5,409 Bytes
37d841f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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):
    if len(h_positions) < 3:
        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


def segment_scoresheet(img_rect: np.ndarray) -> list[dict]:
    """Detecta la cuadricula y extrae celdas de movimientos ordenadas por jugada."""
    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)

    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