File size: 5,150 Bytes
188f4d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
"""字符检测(Detection / End-to-End Spotting)打分。

  - Detection:仅看 bbox,IoU > 0.75 视为 TP,包含 [UNK]
  - Spotting:IoU > 0.75 且字符匹配视为 TP,排除 [UNK]
均输出 per-sample F1。GT bbox 像素单位会被归一化到 0-1000 与模型输出对齐。
"""

from __future__ import annotations

from ..utils.unk import is_unk_char

IOU_THRESH = 0.75


def _iou(a, b) -> float:
    ax1, ay1, ax2, ay2 = a
    bx1, by1, bx2, by2 = b
    if ax2 <= ax1 or ay2 <= ay1 or bx2 <= bx1 or by2 <= by1:
        return 0.0
    ix1, iy1 = max(ax1, bx1), max(ay1, by1)
    ix2, iy2 = min(ax2, bx2), min(ay2, by2)
    iw, ih = ix2 - ix1, iy2 - iy1
    if iw <= 0 or ih <= 0:
        return 0.0
    inter = iw * ih
    union = (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - inter
    return inter / union if union > 0 else 0.0


def _parse_gt(row: dict) -> tuple[list[dict], int, int]:
    """解析 GT spotting,返回 [{'bbox':[x1,y1,x2,y2],'char':str}, ...] + 图像 W/H。"""
    sp = row.get("spotting") or []
    W = int(row.get("width") or 0)
    H = int(row.get("height") or 0)
    items: list[dict] = []
    for it in sp:
        if not isinstance(it, dict):
            continue
        ch = it.get("modern_char")
        if ch is None:
            ch = it.get("text", "")
        ch = str(ch or "").strip()

        bbox = it.get("bbox")
        if bbox is None:
            continue
        x1 = y1 = x2 = y2 = None
        if isinstance(bbox, dict):
            try:
                x1, y1, x2, y2 = (float(bbox[k]) for k in ("x1", "y1", "x2", "y2"))
            except (KeyError, TypeError, ValueError):
                continue
        elif isinstance(bbox, (list, tuple)) and len(bbox) == 4:
            try:
                bx = [float(v) for v in bbox]
            except (TypeError, ValueError):
                continue
            if bx[2] < bx[0] or bx[3] < bx[1]:
                x1, y1, x2, y2 = bx[0], bx[1], bx[0] + bx[2], bx[1] + bx[3]
            else:
                if "modern_char" in it:
                    x1, y1, x2, y2 = bx[0], bx[1], bx[0] + bx[2], bx[1] + bx[3]
                else:
                    x1, y1, x2, y2 = bx
        else:
            continue
        if x2 <= x1 or y2 <= y1:
            continue
        items.append({"bbox": [float(x1), float(y1), float(x2), float(y2)], "char": ch})
    return items, W, H


def _scale_to_1000(items: list[dict], W: int, H: int) -> list[dict]:
    if W <= 0 or H <= 0:
        return []
    sx, sy = 1000.0 / W, 1000.0 / H
    return [
        {"bbox": [it["bbox"][0] * sx, it["bbox"][1] * sy, it["bbox"][2] * sx, it["bbox"][3] * sy], "char": it["char"]}
        for it in items
    ]


def _match(preds: list[dict], gts: list[dict], iou_thresh: float, require_char: bool) -> tuple[int, int, int]:
    if not preds or not gts:
        return 0, len(preds), len(gts)
    pairs: list[tuple[float, int, int]] = []
    for pi, p in enumerate(preds):
        for gi, g in enumerate(gts):
            if require_char and p.get("char", "") != g.get("char", ""):
                continue
            iou = _iou(p["bbox"], g["bbox"])
            if iou >= iou_thresh:
                pairs.append((iou, pi, gi))
    pairs.sort(key=lambda x: x[0], reverse=True)
    used_p, used_g, tp = set(), set(), 0
    for _, pi, gi in pairs:
        if pi in used_p or gi in used_g:
            continue
        used_p.add(pi)
        used_g.add(gi)
        tp += 1
    return tp, len(preds) - tp, len(gts) - tp


def _f1(tp: int, fp: int, fn: int) -> float:
    if tp == 0:
        return 0.0
    p = tp / (tp + fp) if (tp + fp) else 0.0
    r = tp / (tp + fn) if (tp + fn) else 0.0
    return 2 * p * r / (p + r) if (p + r) else 0.0


def judge(extract: dict, row: dict, iou_thresh: float = IOU_THRESH) -> dict:
    gt_raw, W, H = _parse_gt(row)
    gts = _scale_to_1000(gt_raw, W, H)

    preds_src = (extract or {}).get("items") or []
    preds: list[dict] = []
    for it in preds_src:
        if not isinstance(it, dict):
            continue
        bb = it.get("bbox")
        if not isinstance(bb, (list, tuple)) or len(bb) != 4:
            continue
        try:
            bb = [float(v) for v in bb]
        except (TypeError, ValueError):
            continue
        if bb[2] <= bb[0] or bb[3] <= bb[1]:
            continue
        preds.append({"bbox": bb, "char": str(it.get("char", "") or "").strip()})

    det_tp, det_fp, det_fn = _match(preds, gts, iou_thresh, require_char=False)
    det_f1 = _f1(det_tp, det_fp, det_fn)

    spot_gts = [it for it in gts if not is_unk_char(it["char"])]
    spot_preds = [it for it in preds if not is_unk_char(it["char"])]
    spot_tp, spot_fp, spot_fn = _match(spot_preds, spot_gts, iou_thresh, require_char=True)
    spot_f1 = _f1(spot_tp, spot_fp, spot_fn)

    return {
        "score": spot_f1,
        "iou_thresh": iou_thresh,
        "detection_f1": det_f1,
        "spotting_f1": spot_f1,
        "detection": {"tp": det_tp, "fp": det_fp, "fn": det_fn},
        "spotting": {"tp": spot_tp, "fp": spot_fp, "fn": spot_fn},
        "n_pred": len(preds),
        "n_gt": len(gts),
    }