aram-mayhem-dsh 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/settings.json +70 -70
- package/package.json +1 -1
- package/scripts/desktop_overlay.py +1 -1
- package/scripts/dsh_service.py +1 -1
- package/src/__init__.py +1 -1
- package/src/vision/debug_export.py +66 -66
- package/src/vision/title_strip.py +127 -127
package/config/settings.json
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 1,
|
|
3
|
-
"window": {
|
|
4
|
-
"x": null,
|
|
5
|
-
"y": null,
|
|
6
|
-
"width": 420,
|
|
7
|
-
"height": 680,
|
|
8
|
-
"min_width": 360,
|
|
9
|
-
"opacity": 0.97,
|
|
10
|
-
"always_on_top": true,
|
|
11
|
-
"mini": false,
|
|
12
|
-
"click_through": false,
|
|
13
|
-
"edge_hide": true,
|
|
14
|
-
"edge_hide_ratio": 0.55,
|
|
15
|
-
"edge_hover_delay_ms": 400
|
|
16
|
-
},
|
|
17
|
-
"hotkeys": {
|
|
18
|
-
"enabled": true,
|
|
19
|
-
"toggle_visible": "Ctrl+Shift+H",
|
|
20
|
-
"toggle_mini": "Ctrl+Shift+M",
|
|
21
|
-
"rescan_augments": "Ctrl+Shift+R",
|
|
22
|
-
"toggle_click_through": "Ctrl+Shift+L",
|
|
23
|
-
"debug_vision": "F9"
|
|
24
|
-
},
|
|
25
|
-
"lcu": {
|
|
26
|
-
"enabled": true,
|
|
27
|
-
"poll_interval": 1.0,
|
|
28
|
-
"use_websocket": true,
|
|
29
|
-
"reconnect_interval": 5.0,
|
|
30
|
-
"request_timeout": 3.0
|
|
31
|
-
},
|
|
32
|
-
"vision": {
|
|
33
|
-
"enabled": true,
|
|
34
|
-
"interval": 0.25,
|
|
35
|
-
"region": [
|
|
36
|
-
0.06,
|
|
37
|
-
0.12,
|
|
38
|
-
0.88,
|
|
39
|
-
0.76
|
|
40
|
-
],
|
|
41
|
-
"augment_title_strip": [
|
|
42
|
-
0.08,
|
|
43
|
-
0.38,
|
|
44
|
-
0.84,
|
|
45
|
-
0.055
|
|
46
|
-
],
|
|
47
|
-
"card_merge_gap_ratio": 0.025,
|
|
48
|
-
"ocr_closed_threshold": 0.65,
|
|
49
|
-
"debug_vision": false,
|
|
50
|
-
"ocr_min_confidence": 0.45,
|
|
51
|
-
"match_threshold": 0.72,
|
|
52
|
-
"min_matches": 2,
|
|
53
|
-
"cooldown": 8.0
|
|
54
|
-
},
|
|
55
|
-
"data": {
|
|
56
|
-
"seed_file": "data/seed_16.23.json",
|
|
57
|
-
"cache_file": "data/cache/stats.sqlite3",
|
|
58
|
-
"stats_url": "",
|
|
59
|
-
"auto_refresh": false,
|
|
60
|
-
"refresh_hours": 24.0
|
|
61
|
-
},
|
|
62
|
-
"ui": {
|
|
63
|
-
"language": "zh_CN",
|
|
64
|
-
"font_family": "Microsoft YaHei UI",
|
|
65
|
-
"exclude_from_capture": true
|
|
66
|
-
},
|
|
67
|
-
"demo": {
|
|
68
|
-
"enabled": false
|
|
69
|
-
}
|
|
70
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"window": {
|
|
4
|
+
"x": null,
|
|
5
|
+
"y": null,
|
|
6
|
+
"width": 420,
|
|
7
|
+
"height": 680,
|
|
8
|
+
"min_width": 360,
|
|
9
|
+
"opacity": 0.97,
|
|
10
|
+
"always_on_top": true,
|
|
11
|
+
"mini": false,
|
|
12
|
+
"click_through": false,
|
|
13
|
+
"edge_hide": true,
|
|
14
|
+
"edge_hide_ratio": 0.55,
|
|
15
|
+
"edge_hover_delay_ms": 400
|
|
16
|
+
},
|
|
17
|
+
"hotkeys": {
|
|
18
|
+
"enabled": true,
|
|
19
|
+
"toggle_visible": "Ctrl+Shift+H",
|
|
20
|
+
"toggle_mini": "Ctrl+Shift+M",
|
|
21
|
+
"rescan_augments": "Ctrl+Shift+R",
|
|
22
|
+
"toggle_click_through": "Ctrl+Shift+L",
|
|
23
|
+
"debug_vision": "F9"
|
|
24
|
+
},
|
|
25
|
+
"lcu": {
|
|
26
|
+
"enabled": true,
|
|
27
|
+
"poll_interval": 1.0,
|
|
28
|
+
"use_websocket": true,
|
|
29
|
+
"reconnect_interval": 5.0,
|
|
30
|
+
"request_timeout": 3.0
|
|
31
|
+
},
|
|
32
|
+
"vision": {
|
|
33
|
+
"enabled": true,
|
|
34
|
+
"interval": 0.25,
|
|
35
|
+
"region": [
|
|
36
|
+
0.06,
|
|
37
|
+
0.12,
|
|
38
|
+
0.88,
|
|
39
|
+
0.76
|
|
40
|
+
],
|
|
41
|
+
"augment_title_strip": [
|
|
42
|
+
0.08,
|
|
43
|
+
0.38,
|
|
44
|
+
0.84,
|
|
45
|
+
0.055
|
|
46
|
+
],
|
|
47
|
+
"card_merge_gap_ratio": 0.025,
|
|
48
|
+
"ocr_closed_threshold": 0.65,
|
|
49
|
+
"debug_vision": false,
|
|
50
|
+
"ocr_min_confidence": 0.45,
|
|
51
|
+
"match_threshold": 0.72,
|
|
52
|
+
"min_matches": 2,
|
|
53
|
+
"cooldown": 8.0
|
|
54
|
+
},
|
|
55
|
+
"data": {
|
|
56
|
+
"seed_file": "data/seed_16.23.json",
|
|
57
|
+
"cache_file": "data/cache/stats.sqlite3",
|
|
58
|
+
"stats_url": "",
|
|
59
|
+
"auto_refresh": false,
|
|
60
|
+
"refresh_hours": 24.0
|
|
61
|
+
},
|
|
62
|
+
"ui": {
|
|
63
|
+
"language": "zh_CN",
|
|
64
|
+
"font_family": "Microsoft YaHei UI",
|
|
65
|
+
"exclude_from_capture": true
|
|
66
|
+
},
|
|
67
|
+
"demo": {
|
|
68
|
+
"enabled": false
|
|
69
|
+
}
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aram-mayhem-dsh",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "ARAM: Mayhem companion for DSH: an on-demand local service, screen-top desktop icon, sidebar page with LCU state, champion/augment statistics, builds and screen OCR, with nine agent tools.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -648,7 +648,7 @@ class FloatingOverlay(QWidget):
|
|
|
648
648
|
ratio = 0.55
|
|
649
649
|
visible = max(16, round(self.width() * (1.0 - ratio)))
|
|
650
650
|
return area.right() - visible
|
|
651
|
-
return area.right() - self.width()
|
|
651
|
+
return area.right() - self.width() + 1
|
|
652
652
|
|
|
653
653
|
def _snap_right(self) -> None:
|
|
654
654
|
"""Backward-compatible alias for the half-hidden right edge."""
|
package/scripts/dsh_service.py
CHANGED
|
@@ -65,7 +65,7 @@ from src.lcu.listener import parse_champ_select, parse_queue_info
|
|
|
65
65
|
log = logging.getLogger("aram-dsh-service")
|
|
66
66
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
67
67
|
|
|
68
|
-
VERSION = "0.4.
|
|
68
|
+
VERSION = "0.4.1"
|
|
69
69
|
IN_GAME_PHASES = {"InProgress", "Reconnect"}
|
|
70
70
|
|
|
71
71
|
|
package/src/__init__.py
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
"""F9 / debug-vision export for the augment title strip."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import time
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
|
|
8
|
-
from PIL import Image, ImageDraw
|
|
9
|
-
|
|
10
|
-
from ..core.paths import PROJECT_ROOT
|
|
11
|
-
from ..data.models import AugmentMatch, OCRLine
|
|
12
|
-
from .title_strip import CardCluster
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def save_vision_debug(
|
|
16
|
-
strip: Image.Image,
|
|
17
|
-
processed: Image.Image,
|
|
18
|
-
clusters: list[CardCluster],
|
|
19
|
-
lines: list[OCRLine],
|
|
20
|
-
matches: list[AugmentMatch],
|
|
21
|
-
*,
|
|
22
|
-
output_dir: Path | None = None,
|
|
23
|
-
fullscreen: Image.Image | None = None,
|
|
24
|
-
) -> Path:
|
|
25
|
-
target = output_dir or (PROJECT_ROOT / "debug_output")
|
|
26
|
-
target.mkdir(parents=True, exist_ok=True)
|
|
27
|
-
stamp = time.strftime("%Y%m%d-%H%M%S")
|
|
28
|
-
run_dir = target / stamp
|
|
29
|
-
run_dir.mkdir(parents=True, exist_ok=True)
|
|
30
|
-
|
|
31
|
-
strip.save(run_dir / "title_strip_raw.png")
|
|
32
|
-
processed.save(run_dir / "title_strip_processed.png")
|
|
33
|
-
|
|
34
|
-
annotated = strip.convert("RGB").copy()
|
|
35
|
-
draw = ImageDraw.Draw(annotated)
|
|
36
|
-
draw.rectangle((0, 0, annotated.width - 1, annotated.height - 1), outline="#2F80ED", width=3)
|
|
37
|
-
for index, cluster in enumerate(clusters, 1):
|
|
38
|
-
box = (
|
|
39
|
-
max(0, int(cluster.x_min)),
|
|
40
|
-
max(0, int(cluster.y_min)),
|
|
41
|
-
min(annotated.width - 1, int(cluster.x_max)),
|
|
42
|
-
min(annotated.height - 1, int(cluster.y_max)),
|
|
43
|
-
)
|
|
44
|
-
draw.rectangle(box, outline="#3DDC97", width=2)
|
|
45
|
-
draw.text((box[0] + 3, max(0, box[1] - 12)), str(index), fill="#3DDC97")
|
|
46
|
-
annotated.save(run_dir / "annotated_strip.png")
|
|
47
|
-
if fullscreen is not None:
|
|
48
|
-
fullscreen.convert("RGB").save(run_dir / "game_fullscreen.png")
|
|
49
|
-
|
|
50
|
-
lines_out = [
|
|
51
|
-
f"cards={len(matches)} clusters={len(clusters)} ocr_lines={len(lines)}",
|
|
52
|
-
"matches=" + ", ".join(f"{m.augment.name}:{m.raw_text}:{m.match_score:.3f}" for m in matches),
|
|
53
|
-
"",
|
|
54
|
-
"x_center_ratio | confidence | text | name | score",
|
|
55
|
-
]
|
|
56
|
-
for cluster in clusters:
|
|
57
|
-
match = next((m for m in matches if abs(m.x_center_ratio - cluster.x_center_ratio) < 0.02), None)
|
|
58
|
-
lines_out.append(
|
|
59
|
-
f"{cluster.x_center_ratio:.3f} | {cluster.confidence:.3f} | {cluster.text} | "
|
|
60
|
-
f"{match.augment.name if match else 'UNMATCHED'} | "
|
|
61
|
-
f"{match.match_score if match else 0:.3f}"
|
|
62
|
-
)
|
|
63
|
-
lines_out.extend(["", "raw_ocr:"])
|
|
64
|
-
lines_out.extend(f"{line.confidence:.3f}\t{line.text}" for line in lines)
|
|
65
|
-
(run_dir / "ocr_log.txt").write_text("\n".join(lines_out) + "\n", encoding="utf-8")
|
|
66
|
-
return run_dir
|
|
1
|
+
"""F9 / debug-vision export for the augment title strip."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from PIL import Image, ImageDraw
|
|
9
|
+
|
|
10
|
+
from ..core.paths import PROJECT_ROOT
|
|
11
|
+
from ..data.models import AugmentMatch, OCRLine
|
|
12
|
+
from .title_strip import CardCluster
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def save_vision_debug(
|
|
16
|
+
strip: Image.Image,
|
|
17
|
+
processed: Image.Image,
|
|
18
|
+
clusters: list[CardCluster],
|
|
19
|
+
lines: list[OCRLine],
|
|
20
|
+
matches: list[AugmentMatch],
|
|
21
|
+
*,
|
|
22
|
+
output_dir: Path | None = None,
|
|
23
|
+
fullscreen: Image.Image | None = None,
|
|
24
|
+
) -> Path:
|
|
25
|
+
target = output_dir or (PROJECT_ROOT / "debug_output")
|
|
26
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
27
|
+
stamp = time.strftime("%Y%m%d-%H%M%S")
|
|
28
|
+
run_dir = target / stamp
|
|
29
|
+
run_dir.mkdir(parents=True, exist_ok=True)
|
|
30
|
+
|
|
31
|
+
strip.save(run_dir / "title_strip_raw.png")
|
|
32
|
+
processed.save(run_dir / "title_strip_processed.png")
|
|
33
|
+
|
|
34
|
+
annotated = strip.convert("RGB").copy()
|
|
35
|
+
draw = ImageDraw.Draw(annotated)
|
|
36
|
+
draw.rectangle((0, 0, annotated.width - 1, annotated.height - 1), outline="#2F80ED", width=3)
|
|
37
|
+
for index, cluster in enumerate(clusters, 1):
|
|
38
|
+
box = (
|
|
39
|
+
max(0, int(cluster.x_min)),
|
|
40
|
+
max(0, int(cluster.y_min)),
|
|
41
|
+
min(annotated.width - 1, int(cluster.x_max)),
|
|
42
|
+
min(annotated.height - 1, int(cluster.y_max)),
|
|
43
|
+
)
|
|
44
|
+
draw.rectangle(box, outline="#3DDC97", width=2)
|
|
45
|
+
draw.text((box[0] + 3, max(0, box[1] - 12)), str(index), fill="#3DDC97")
|
|
46
|
+
annotated.save(run_dir / "annotated_strip.png")
|
|
47
|
+
if fullscreen is not None:
|
|
48
|
+
fullscreen.convert("RGB").save(run_dir / "game_fullscreen.png")
|
|
49
|
+
|
|
50
|
+
lines_out = [
|
|
51
|
+
f"cards={len(matches)} clusters={len(clusters)} ocr_lines={len(lines)}",
|
|
52
|
+
"matches=" + ", ".join(f"{m.augment.name}:{m.raw_text}:{m.match_score:.3f}" for m in matches),
|
|
53
|
+
"",
|
|
54
|
+
"x_center_ratio | confidence | text | name | score",
|
|
55
|
+
]
|
|
56
|
+
for cluster in clusters:
|
|
57
|
+
match = next((m for m in matches if abs(m.x_center_ratio - cluster.x_center_ratio) < 0.02), None)
|
|
58
|
+
lines_out.append(
|
|
59
|
+
f"{cluster.x_center_ratio:.3f} | {cluster.confidence:.3f} | {cluster.text} | "
|
|
60
|
+
f"{match.augment.name if match else 'UNMATCHED'} | "
|
|
61
|
+
f"{match.match_score if match else 0:.3f}"
|
|
62
|
+
)
|
|
63
|
+
lines_out.extend(["", "raw_ocr:"])
|
|
64
|
+
lines_out.extend(f"{line.confidence:.3f}\t{line.text}" for line in lines)
|
|
65
|
+
(run_dir / "ocr_log.txt").write_text("\n".join(lines_out) + "\n", encoding="utf-8")
|
|
66
|
+
return run_dir
|
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
"""X-axis clustering for one horizontal augment-title strip."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import re
|
|
6
|
-
from dataclasses import dataclass, field
|
|
7
|
-
|
|
8
|
-
from ..data.models import OCRLine
|
|
9
|
-
|
|
10
|
-
_PUNCT_RE = re.compile(r"[\s·•・::,,.。!!??/\\|()()\[\]【】<>《》\"'“”‘’_\-—+=*#~`^%&;;]+")
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def clean_ocr_text(text: str) -> str:
|
|
14
|
-
return _PUNCT_RE.sub("", str(text or "")).strip()
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def _bounds(line: OCRLine) -> tuple[float, float, float, float]:
|
|
18
|
-
if not line.box:
|
|
19
|
-
return (0.0, 0.0, 0.0, 0.0)
|
|
20
|
-
xs = [point[0] for point in line.box]
|
|
21
|
-
ys = [point[1] for point in line.box]
|
|
22
|
-
return (min(xs), min(ys), max(xs), max(ys))
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
@dataclass(frozen=True)
|
|
26
|
-
class CardCluster:
|
|
27
|
-
index: int
|
|
28
|
-
text: str
|
|
29
|
-
confidence: float
|
|
30
|
-
x_min: float
|
|
31
|
-
x_max: float
|
|
32
|
-
y_min: float
|
|
33
|
-
y_max: float
|
|
34
|
-
x_center_ratio: float
|
|
35
|
-
lines: tuple[OCRLine, ...] = field(default_factory=tuple)
|
|
36
|
-
|
|
37
|
-
@property
|
|
38
|
-
def center_x(self) -> float:
|
|
39
|
-
return (self.x_min + self.x_max) / 2
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def cluster_title_lines(
|
|
43
|
-
lines: list[OCRLine],
|
|
44
|
-
strip_width: float,
|
|
45
|
-
*,
|
|
46
|
-
gap_ratio: float = 0.025,
|
|
47
|
-
min_text_chars: int = 2,
|
|
48
|
-
max_cards: int = 4,
|
|
49
|
-
) -> list[CardCluster]:
|
|
50
|
-
"""Group OCR boxes into 2–4 card title clusters by X-axis proximity."""
|
|
51
|
-
|
|
52
|
-
width = max(1.0, float(strip_width))
|
|
53
|
-
gap_threshold = max(8.0, float(gap_ratio) * width)
|
|
54
|
-
rows: list[tuple[OCRLine, str, tuple[float, float, float, float]]] = []
|
|
55
|
-
for line in lines:
|
|
56
|
-
text = clean_ocr_text(line.text)
|
|
57
|
-
if len(text) < 1:
|
|
58
|
-
continue
|
|
59
|
-
box = _bounds(line)
|
|
60
|
-
if box[2] <= box[0]:
|
|
61
|
-
continue
|
|
62
|
-
rows.append((line, text, box))
|
|
63
|
-
rows.sort(key=lambda item: item[2][0])
|
|
64
|
-
|
|
65
|
-
groups: list[list[tuple[OCRLine, str, tuple[float, float, float, float]]]] = []
|
|
66
|
-
for row in rows:
|
|
67
|
-
_line, _text, box = row
|
|
68
|
-
if not groups:
|
|
69
|
-
groups.append([row])
|
|
70
|
-
continue
|
|
71
|
-
current = groups[-1]
|
|
72
|
-
cur_x_min = min(item[2][0] for item in current)
|
|
73
|
-
cur_x_max = max(item[2][2] for item in current)
|
|
74
|
-
overlap = min(cur_x_max, box[2]) - max(cur_x_min, box[0])
|
|
75
|
-
gap = box[0] - cur_x_max
|
|
76
|
-
if overlap > 0 or gap <= gap_threshold:
|
|
77
|
-
current.append(row)
|
|
78
|
-
else:
|
|
79
|
-
groups.append([row])
|
|
80
|
-
|
|
81
|
-
clusters: list[CardCluster] = []
|
|
82
|
-
for group in groups:
|
|
83
|
-
group.sort(key=lambda item: ((item[2][1] + item[2][3]) / 2, (item[2][0] + item[2][2]) / 2))
|
|
84
|
-
text = "".join(item[1] for item in group)
|
|
85
|
-
if len(text) < min_text_chars:
|
|
86
|
-
continue
|
|
87
|
-
x_min = min(item[2][0] for item in group)
|
|
88
|
-
x_max = max(item[2][2] for item in group)
|
|
89
|
-
y_min = min(item[2][1] for item in group)
|
|
90
|
-
y_max = max(item[2][3] for item in group)
|
|
91
|
-
conf = sum(item[0].confidence for item in group) / len(group)
|
|
92
|
-
clusters.append(
|
|
93
|
-
CardCluster(
|
|
94
|
-
index=len(clusters),
|
|
95
|
-
text=text,
|
|
96
|
-
confidence=conf,
|
|
97
|
-
x_min=x_min,
|
|
98
|
-
x_max=x_max,
|
|
99
|
-
y_min=y_min,
|
|
100
|
-
y_max=y_max,
|
|
101
|
-
x_center_ratio=max(0.0, min(1.0, ((x_min + x_max) / 2) / width)),
|
|
102
|
-
lines=tuple(item[0] for item in group),
|
|
103
|
-
)
|
|
104
|
-
)
|
|
105
|
-
|
|
106
|
-
if len(clusters) > max_cards:
|
|
107
|
-
clusters = sorted(clusters, key=lambda item: (item.x_max - item.x_min) * item.confidence, reverse=True)[:max_cards]
|
|
108
|
-
clusters.sort(key=lambda item: item.center_x)
|
|
109
|
-
clusters = [
|
|
110
|
-
CardCluster(
|
|
111
|
-
index=index,
|
|
112
|
-
text=item.text,
|
|
113
|
-
confidence=item.confidence,
|
|
114
|
-
x_min=item.x_min,
|
|
115
|
-
x_max=item.x_max,
|
|
116
|
-
y_min=item.y_min,
|
|
117
|
-
y_max=item.y_max,
|
|
118
|
-
x_center_ratio=item.x_center_ratio,
|
|
119
|
-
lines=item.lines,
|
|
120
|
-
)
|
|
121
|
-
for index, item in enumerate(clusters)
|
|
122
|
-
]
|
|
123
|
-
return clusters
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
def cluster_signature(clusters: list[CardCluster]) -> tuple[tuple[str, ...], int]:
|
|
127
|
-
return (tuple(item.text for item in clusters), len(clusters))
|
|
1
|
+
"""X-axis clustering for one horizontal augment-title strip."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
|
|
8
|
+
from ..data.models import OCRLine
|
|
9
|
+
|
|
10
|
+
_PUNCT_RE = re.compile(r"[\s·•・::,,.。!!??/\\|()()\[\]【】<>《》\"'“”‘’_\-—+=*#~`^%&;;]+")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def clean_ocr_text(text: str) -> str:
|
|
14
|
+
return _PUNCT_RE.sub("", str(text or "")).strip()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _bounds(line: OCRLine) -> tuple[float, float, float, float]:
|
|
18
|
+
if not line.box:
|
|
19
|
+
return (0.0, 0.0, 0.0, 0.0)
|
|
20
|
+
xs = [point[0] for point in line.box]
|
|
21
|
+
ys = [point[1] for point in line.box]
|
|
22
|
+
return (min(xs), min(ys), max(xs), max(ys))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class CardCluster:
|
|
27
|
+
index: int
|
|
28
|
+
text: str
|
|
29
|
+
confidence: float
|
|
30
|
+
x_min: float
|
|
31
|
+
x_max: float
|
|
32
|
+
y_min: float
|
|
33
|
+
y_max: float
|
|
34
|
+
x_center_ratio: float
|
|
35
|
+
lines: tuple[OCRLine, ...] = field(default_factory=tuple)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def center_x(self) -> float:
|
|
39
|
+
return (self.x_min + self.x_max) / 2
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def cluster_title_lines(
|
|
43
|
+
lines: list[OCRLine],
|
|
44
|
+
strip_width: float,
|
|
45
|
+
*,
|
|
46
|
+
gap_ratio: float = 0.025,
|
|
47
|
+
min_text_chars: int = 2,
|
|
48
|
+
max_cards: int = 4,
|
|
49
|
+
) -> list[CardCluster]:
|
|
50
|
+
"""Group OCR boxes into 2–4 card title clusters by X-axis proximity."""
|
|
51
|
+
|
|
52
|
+
width = max(1.0, float(strip_width))
|
|
53
|
+
gap_threshold = max(8.0, float(gap_ratio) * width)
|
|
54
|
+
rows: list[tuple[OCRLine, str, tuple[float, float, float, float]]] = []
|
|
55
|
+
for line in lines:
|
|
56
|
+
text = clean_ocr_text(line.text)
|
|
57
|
+
if len(text) < 1:
|
|
58
|
+
continue
|
|
59
|
+
box = _bounds(line)
|
|
60
|
+
if box[2] <= box[0]:
|
|
61
|
+
continue
|
|
62
|
+
rows.append((line, text, box))
|
|
63
|
+
rows.sort(key=lambda item: item[2][0])
|
|
64
|
+
|
|
65
|
+
groups: list[list[tuple[OCRLine, str, tuple[float, float, float, float]]]] = []
|
|
66
|
+
for row in rows:
|
|
67
|
+
_line, _text, box = row
|
|
68
|
+
if not groups:
|
|
69
|
+
groups.append([row])
|
|
70
|
+
continue
|
|
71
|
+
current = groups[-1]
|
|
72
|
+
cur_x_min = min(item[2][0] for item in current)
|
|
73
|
+
cur_x_max = max(item[2][2] for item in current)
|
|
74
|
+
overlap = min(cur_x_max, box[2]) - max(cur_x_min, box[0])
|
|
75
|
+
gap = box[0] - cur_x_max
|
|
76
|
+
if overlap > 0 or gap <= gap_threshold:
|
|
77
|
+
current.append(row)
|
|
78
|
+
else:
|
|
79
|
+
groups.append([row])
|
|
80
|
+
|
|
81
|
+
clusters: list[CardCluster] = []
|
|
82
|
+
for group in groups:
|
|
83
|
+
group.sort(key=lambda item: ((item[2][1] + item[2][3]) / 2, (item[2][0] + item[2][2]) / 2))
|
|
84
|
+
text = "".join(item[1] for item in group)
|
|
85
|
+
if len(text) < min_text_chars:
|
|
86
|
+
continue
|
|
87
|
+
x_min = min(item[2][0] for item in group)
|
|
88
|
+
x_max = max(item[2][2] for item in group)
|
|
89
|
+
y_min = min(item[2][1] for item in group)
|
|
90
|
+
y_max = max(item[2][3] for item in group)
|
|
91
|
+
conf = sum(item[0].confidence for item in group) / len(group)
|
|
92
|
+
clusters.append(
|
|
93
|
+
CardCluster(
|
|
94
|
+
index=len(clusters),
|
|
95
|
+
text=text,
|
|
96
|
+
confidence=conf,
|
|
97
|
+
x_min=x_min,
|
|
98
|
+
x_max=x_max,
|
|
99
|
+
y_min=y_min,
|
|
100
|
+
y_max=y_max,
|
|
101
|
+
x_center_ratio=max(0.0, min(1.0, ((x_min + x_max) / 2) / width)),
|
|
102
|
+
lines=tuple(item[0] for item in group),
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
if len(clusters) > max_cards:
|
|
107
|
+
clusters = sorted(clusters, key=lambda item: (item.x_max - item.x_min) * item.confidence, reverse=True)[:max_cards]
|
|
108
|
+
clusters.sort(key=lambda item: item.center_x)
|
|
109
|
+
clusters = [
|
|
110
|
+
CardCluster(
|
|
111
|
+
index=index,
|
|
112
|
+
text=item.text,
|
|
113
|
+
confidence=item.confidence,
|
|
114
|
+
x_min=item.x_min,
|
|
115
|
+
x_max=item.x_max,
|
|
116
|
+
y_min=item.y_min,
|
|
117
|
+
y_max=item.y_max,
|
|
118
|
+
x_center_ratio=item.x_center_ratio,
|
|
119
|
+
lines=item.lines,
|
|
120
|
+
)
|
|
121
|
+
for index, item in enumerate(clusters)
|
|
122
|
+
]
|
|
123
|
+
return clusters
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def cluster_signature(clusters: list[CardCluster]) -> tuple[tuple[str, ...], int]:
|
|
127
|
+
return (tuple(item.text for item in clusters), len(clusters))
|