File size: 7,234 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""单字识别(Fine-grained Archaic Character Recognition)任务。

工作流:
  1. ``prepare_referring_sample`` 从 spotting GT 中按 (seed + sample_key) 确定性采样一个非 [UNK] 字符;
  2. 在原图上绘制红色矩形框,落盘到稳定缓存目录;
  3. 用渲染图调用模型,模型只识别红框内字符;
  4. 评分使用 Exact Match Accuracy。
"""

from __future__ import annotations

import hashlib
import json
import os
import random
import re
import tempfile
import threading
from pathlib import Path
from typing import Optional

from PIL import Image, ImageDraw

from ..utils.unk import is_unk_char
from ._text import clean_value, extract_by_prefix, strip_thinking

# ============================================================
# Prompt / Extract
# ============================================================
PROMPT = """你是一名精通中国古文字学的专家。

任务:
图中有一个**红色矩形框**,框内是一个古文字。请识别该红框内的古文字对应的**现代汉字**。

要求:
- 只识别红框内的单个字符,不要识别图片中其他位置的字符
- 输出必须是**一个**现代汉字(不要输出多个字,不要加拼音、标点、解释或其他任何字符)
- 如果无法识别,则输出:[UNK]

输出格式(必须严格遵守):
现代汉字:<单个汉字>
"""

_CJK_RE = re.compile(r"[\u4e00-\u9fff\u3400-\u4dbf\U00020000-\U0002a6df\U0002a700-\U0002ebef\U00030000-\U0003134f]")


def extract(answer: str) -> tuple[bool, dict[str, str]]:
    text = strip_thinking(answer)
    raw = extract_by_prefix(text, ["现代汉字", "汉字", "识别结果", "识别", "答案", "char"])
    if not raw:
        raw = clean_value(text)
    if not raw:
        return False, {}

    stripped = raw.strip()
    if stripped.upper() in {"[UNK]", "<UNK>", "UNK"}:
        return True, {"char": "[UNK]"}

    m = _CJK_RE.search(stripped)
    if m:
        return True, {"char": m.group(0)}

    first = stripped.split()[0] if stripped.split() else ""
    if first:
        return True, {"char": first[:1]}
    return False, {}


# ============================================================
# 红框采样 + 渲染
# ============================================================
DEFAULT_SEED = 42
RED_BOX_COLOR = (255, 0, 0)
MIN_BOX_WIDTH = 3
MAX_BOX_WIDTH = 12
BOX_WIDTH_RATIO = 0.006  # 0.6% of min(W,H)


def _bbox_to_xyxy(item: dict) -> Optional[tuple[float, float, float, float]]:
    """规范化 spotting item 的 bbox 为像素坐标 (x1,y1,x2,y2)。

    支持:
      - {"bbox":[x,y,w,h], "modern_char": ...}(甲骨文)
      - {"bbox":{"x1","y1","x2","y2"}, "text": ...}(金文/篆文)
      - {"bbox":[x1,y1,x2,y2], "text": ...}(备用格式)
    """
    bbox = item.get("bbox")
    if bbox is None:
        return None

    if isinstance(bbox, dict):
        try:
            x1 = float(bbox.get("x1"))
            y1 = float(bbox.get("y1"))
            x2 = float(bbox.get("x2"))
            y2 = float(bbox.get("y2"))
        except (TypeError, ValueError):
            return None
        return (x1, y1, x2, y2) if (x2 > x1 and y2 > y1) else None

    if isinstance(bbox, (list, tuple)) and len(bbox) == 4:
        try:
            bx = [float(v) for v in bbox]
        except (TypeError, ValueError):
            return None
        if "modern_char" in item:
            x, y, w, h = bx
            x1, y1, x2, y2 = x, y, x + w, y + h
        else:
            if bx[2] < bx[0] or bx[3] < bx[1]:
                x, y, w, h = bx
                x1, y1, x2, y2 = x, y, x + w, y + h
            else:
                x1, y1, x2, y2 = bx
        return (x1, y1, x2, y2) if (x2 > x1 and y2 > y1) else None

    return None


def _item_char(item: dict) -> str:
    ch = item.get("modern_char")
    if ch is None:
        ch = item.get("text", "")
    return str(ch or "").strip()


def _build_sample_key(row: dict) -> str:
    parts: list[str] = []
    for k in ("image_path", "img_path", "image"):
        v = row.get(k)
        if v:
            parts.append(f"{k}={v}")
    sp = row.get("spotting")
    if isinstance(sp, list) and sp:
        fp = hashlib.md5(json.dumps(sp, ensure_ascii=False, sort_keys=True).encode("utf-8")).hexdigest()[:12]
        parts.append(f"sp_fp={fp}")
    return "|".join(parts)


def _seeded_rng(key: str, seed: int) -> random.Random:
    h = hashlib.md5(f"{seed}::{key}".encode("utf-8")).hexdigest()
    return random.Random(int(h[:16], 16))


def _pick_target(row: dict, seed: int) -> Optional[dict]:
    sp = row.get("spotting") or []
    if not isinstance(sp, list) or not sp:
        return None

    candidates: list[tuple[int, str, tuple[float, float, float, float]]] = []
    for idx, it in enumerate(sp):
        if not isinstance(it, dict):
            continue
        ch = _item_char(it)
        if is_unk_char(ch):
            continue
        bb = _bbox_to_xyxy(it)
        if bb is None:
            continue
        candidates.append((idx, ch, bb))
    if not candidates:
        return None

    sample_key = _build_sample_key(row) or "no-key"
    rng = _seeded_rng(sample_key, seed)
    idx, ch, bb = rng.choice(candidates)
    return {"char": ch, "bbox_xyxy": bb, "index": idx, "sample_key": sample_key}


def _box_width(W: int, H: int) -> int:
    short = max(1, min(W, H))
    w = int(round(short * BOX_WIDTH_RATIO))
    return max(MIN_BOX_WIDTH, min(MAX_BOX_WIDTH, w))


def _draw_box(img_path: str, bbox_xyxy, out_dir: Optional[str]) -> str:
    with Image.open(img_path) as im:
        im = im.convert("RGB")
        W, H = im.size
        x1, y1, x2, y2 = [
            max(0.0, min(float(W - 1) if i % 2 == 0 else float(H - 1), float(v))) for i, v in enumerate(bbox_xyxy)
        ]
        bw = _box_width(W, H)
        ImageDraw.Draw(im).rectangle([x1, y1, x2, y2], outline=RED_BOX_COLOR, width=bw)

        if out_dir is None:
            out_dir = os.path.join(tempfile.gettempdir(), "chronotext_referring")
        os.makedirs(out_dir, exist_ok=True)
        stem = Path(img_path).stem
        tag = hashlib.md5(f"{img_path}|{x1:.2f},{y1:.2f},{x2:.2f},{y2:.2f}".encode("utf-8")).hexdigest()[:8]
        out_path = os.path.join(out_dir, f"{stem}_redbox_{tag}_{os.getpid()}_{threading.get_ident()}.png")

        tmp = f"{out_path}.tmp"
        im.save(tmp, format="PNG")
        os.replace(tmp, out_path)
        return out_path


def prepare_referring_sample(
    row: dict,
    img_path: str,
    seed: int = DEFAULT_SEED,
    out_dir: Optional[str] = None,
) -> Optional[dict]:
    """采样 + 画框 + 落盘。任一步失败返回 None。"""
    picked = _pick_target(row, seed=seed)
    if picked is None:
        return None
    if not img_path or not os.path.exists(img_path):
        return None

    rendered = _draw_box(img_path, picked["bbox_xyxy"], out_dir)
    if not (rendered and os.path.exists(rendered)):
        rendered = _draw_box(img_path, picked["bbox_xyxy"], out_dir)
    return {
        "rendered_img_path": rendered,
        "target_char": picked["char"],
        "target_bbox_xyxy": [float(v) for v in picked["bbox_xyxy"]],
        "index": picked["index"],
        "sample_key": picked["sample_key"],
    }