pass1-lite 0.2.0__py3-none-any.whl
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.
- pass1_lite/__init__.py +82 -0
- pass1_lite/adapters.py +528 -0
- pass1_lite/cli.py +523 -0
- pass1_lite/client.py +290 -0
- pass1_lite/core.py +870 -0
- pass1_lite-0.2.0.dist-info/METADATA +98 -0
- pass1_lite-0.2.0.dist-info/RECORD +12 -0
- pass1_lite-0.2.0.dist-info/WHEEL +5 -0
- pass1_lite-0.2.0.dist-info/entry_points.txt +2 -0
- pass1_lite-0.2.0.dist-info/licenses/LICENSE +202 -0
- pass1_lite-0.2.0.dist-info/licenses/NOTICE +36 -0
- pass1_lite-0.2.0.dist-info/top_level.txt +1 -0
pass1_lite/__init__.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Standalone Pass1_Lite region refinement, adapters, and OCR client for two-stage document OCR."""
|
|
2
|
+
|
|
3
|
+
from .adapters import (
|
|
4
|
+
LABEL_TASK_MAPPING,
|
|
5
|
+
assemble_markdown,
|
|
6
|
+
extract_paddlex_boxes,
|
|
7
|
+
from_hf_detections,
|
|
8
|
+
from_paddlex_result,
|
|
9
|
+
load_layout_regions,
|
|
10
|
+
otsl_to_html,
|
|
11
|
+
render_page_comparison,
|
|
12
|
+
task_type_for_label,
|
|
13
|
+
)
|
|
14
|
+
from .client import (
|
|
15
|
+
GENERIC_VLM_PROMPTS,
|
|
16
|
+
XIAOMI_PROMPTS,
|
|
17
|
+
call_openai_vision,
|
|
18
|
+
detect_repetition_loop,
|
|
19
|
+
encode_crop_png_b64,
|
|
20
|
+
ocr_page_crops,
|
|
21
|
+
)
|
|
22
|
+
from .core import (
|
|
23
|
+
DEFAULT_DILATE_PX,
|
|
24
|
+
DEFAULT_LPF_SCALE,
|
|
25
|
+
DEFAULT_LPF_SIGMA,
|
|
26
|
+
DEFAULT_MAX_EDGE,
|
|
27
|
+
DEFAULT_MOIRE_THRESHOLD,
|
|
28
|
+
DEFAULT_POLY_FILL_THRESH,
|
|
29
|
+
DEFAULT_TABLE_THRESHOLD,
|
|
30
|
+
PreparedCrop,
|
|
31
|
+
PreparedPageCrops,
|
|
32
|
+
apply_lpf_upscale,
|
|
33
|
+
attach_polygons_to_regions,
|
|
34
|
+
bg_subpixel_moire_score,
|
|
35
|
+
filter_table_regions,
|
|
36
|
+
fit_image,
|
|
37
|
+
load_image_rgb,
|
|
38
|
+
mask_and_crop_region,
|
|
39
|
+
normalize_polygon_points,
|
|
40
|
+
prepare_page_crops,
|
|
41
|
+
refine_column_reading_order,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
__version__ = "0.2.0"
|
|
45
|
+
|
|
46
|
+
__all__ = [
|
|
47
|
+
"__version__",
|
|
48
|
+
"DEFAULT_DILATE_PX",
|
|
49
|
+
"DEFAULT_LPF_SCALE",
|
|
50
|
+
"DEFAULT_LPF_SIGMA",
|
|
51
|
+
"DEFAULT_MAX_EDGE",
|
|
52
|
+
"DEFAULT_MOIRE_THRESHOLD",
|
|
53
|
+
"DEFAULT_POLY_FILL_THRESH",
|
|
54
|
+
"DEFAULT_TABLE_THRESHOLD",
|
|
55
|
+
"GENERIC_VLM_PROMPTS",
|
|
56
|
+
"LABEL_TASK_MAPPING",
|
|
57
|
+
"PreparedCrop",
|
|
58
|
+
"PreparedPageCrops",
|
|
59
|
+
"XIAOMI_PROMPTS",
|
|
60
|
+
"apply_lpf_upscale",
|
|
61
|
+
"assemble_markdown",
|
|
62
|
+
"attach_polygons_to_regions",
|
|
63
|
+
"bg_subpixel_moire_score",
|
|
64
|
+
"call_openai_vision",
|
|
65
|
+
"detect_repetition_loop",
|
|
66
|
+
"encode_crop_png_b64",
|
|
67
|
+
"extract_paddlex_boxes",
|
|
68
|
+
"filter_table_regions",
|
|
69
|
+
"fit_image",
|
|
70
|
+
"from_hf_detections",
|
|
71
|
+
"from_paddlex_result",
|
|
72
|
+
"load_image_rgb",
|
|
73
|
+
"load_layout_regions",
|
|
74
|
+
"mask_and_crop_region",
|
|
75
|
+
"normalize_polygon_points",
|
|
76
|
+
"ocr_page_crops",
|
|
77
|
+
"otsl_to_html",
|
|
78
|
+
"prepare_page_crops",
|
|
79
|
+
"refine_column_reading_order",
|
|
80
|
+
"render_page_comparison",
|
|
81
|
+
"task_type_for_label",
|
|
82
|
+
]
|
pass1_lite/adapters.py
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
"""Layout detector adapters, Markdown assembly, and visual inspection helpers for Pass1_Lite.
|
|
2
|
+
|
|
3
|
+
All helpers operate purely on Python stdlib + NumPy + Pillow (zero `torch`, `paddlex`,
|
|
4
|
+
or `transformers` runtime coupling). Label-to-task mappings follow the upstream
|
|
5
|
+
Xiaomi-OCR-0 (`Apache-2.0`) two-stage layout contract.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import html
|
|
10
|
+
import json
|
|
11
|
+
import re
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any, Dict, List, Literal, Optional, Sequence, Tuple, Union
|
|
14
|
+
|
|
15
|
+
from PIL import Image, ImageDraw, ImageFont
|
|
16
|
+
|
|
17
|
+
from .core import (
|
|
18
|
+
DEFAULT_TABLE_THRESHOLD,
|
|
19
|
+
PreparedPageCrops,
|
|
20
|
+
_clip_and_attach_polygon,
|
|
21
|
+
_extract_box4,
|
|
22
|
+
_extract_poly_array,
|
|
23
|
+
filter_table_regions,
|
|
24
|
+
load_image_rgb,
|
|
25
|
+
mask_and_crop_region,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
LABEL_TASK_MAPPING: Dict[str, Tuple[str, ...]] = {
|
|
29
|
+
"text": (
|
|
30
|
+
"abstract",
|
|
31
|
+
"algorithm",
|
|
32
|
+
"content",
|
|
33
|
+
"doc_title",
|
|
34
|
+
"figure_title",
|
|
35
|
+
"paragraph_title",
|
|
36
|
+
"reference_content",
|
|
37
|
+
"text",
|
|
38
|
+
"vertical_text",
|
|
39
|
+
"vision_footnote",
|
|
40
|
+
"seal",
|
|
41
|
+
"formula_number",
|
|
42
|
+
"header",
|
|
43
|
+
"footer",
|
|
44
|
+
"number",
|
|
45
|
+
"footnote",
|
|
46
|
+
"aside_text",
|
|
47
|
+
"reference",
|
|
48
|
+
),
|
|
49
|
+
"table": ("table",),
|
|
50
|
+
"formula": ("display_formula", "inline_formula"),
|
|
51
|
+
"skip": ("chart", "image", "footer_image", "header_image"),
|
|
52
|
+
"abandon": (),
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
_LABEL_TO_TASK: Dict[str, str] = {
|
|
56
|
+
lbl: tt for tt, labels in LABEL_TASK_MAPPING.items() for lbl in labels
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_OTSL_TAG_RE = re.compile(r"(<fcel>|<ecel>|<lcel>|<ucel>|<xcel>|<nl>)")
|
|
60
|
+
_INLINE_HTML_TAG_RE = re.compile(r"(</?(?:sub|sup|br|b|i|em|strong)\s*/?>)", re.IGNORECASE)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _escape_table_cell(text: str) -> str:
|
|
64
|
+
"""HTML-escape plain cell text (`<`, `>`, `&`) while preserving safe inline markup (`<sub>`, `<sup>`, `<br>`)."""
|
|
65
|
+
parts = _INLINE_HTML_TAG_RE.split(text)
|
|
66
|
+
out: List[str] = []
|
|
67
|
+
for idx, part in enumerate(parts):
|
|
68
|
+
if idx % 2 == 1:
|
|
69
|
+
out.append(part)
|
|
70
|
+
else:
|
|
71
|
+
out.append(html.escape(part, quote=False))
|
|
72
|
+
return "".join(out)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _normalize_existing_regions(
|
|
76
|
+
raw_regs: Sequence[Dict[str, Any]],
|
|
77
|
+
*,
|
|
78
|
+
table_threshold: float = DEFAULT_TABLE_THRESHOLD,
|
|
79
|
+
) -> List[Dict[str, Any]]:
|
|
80
|
+
"""Backfill missing `task_type` from `label`, drop `abandon`, and apply `table_threshold`.
|
|
81
|
+
|
|
82
|
+
Unknown custom labels (from non-PaddleX detectors) without an explicit `task_type`
|
|
83
|
+
default to `"text"` so custom region schemas remain usable without dropping blocks.
|
|
84
|
+
"""
|
|
85
|
+
normalized: List[Dict[str, Any]] = []
|
|
86
|
+
for r in raw_regs:
|
|
87
|
+
item = dict(r)
|
|
88
|
+
tt = item.get("task_type")
|
|
89
|
+
if not tt:
|
|
90
|
+
inferred = task_type_for_label(str(item.get("label", "")))
|
|
91
|
+
if inferred == "abandon":
|
|
92
|
+
continue
|
|
93
|
+
item["task_type"] = inferred if inferred is not None else "text"
|
|
94
|
+
elif str(tt) == "abandon":
|
|
95
|
+
continue
|
|
96
|
+
normalized.append(item)
|
|
97
|
+
return filter_table_regions(normalized, table_threshold=table_threshold)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def task_type_for_label(label: str) -> Optional[str]:
|
|
101
|
+
"""Map a PP-DocLayoutV3 class label to Xiaomi-OCR-0 `task_type` (`text`/`table`/`formula`/`skip`)."""
|
|
102
|
+
return _LABEL_TO_TASK.get(str(label))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def extract_paddlex_boxes(result: Any) -> List[Dict[str, Any]]:
|
|
106
|
+
"""Extract raw detection box dicts from a PaddleX `PP-DocLayoutV3` prediction object or dict."""
|
|
107
|
+
if isinstance(result, list):
|
|
108
|
+
return [b for b in result if isinstance(b, dict)]
|
|
109
|
+
if hasattr(result, "get") and result.get("boxes") is not None:
|
|
110
|
+
return list(result["boxes"])
|
|
111
|
+
try:
|
|
112
|
+
inner = result["res"]
|
|
113
|
+
except (KeyError, TypeError, AttributeError):
|
|
114
|
+
inner = None
|
|
115
|
+
if hasattr(inner, "get") and inner.get("boxes") is not None:
|
|
116
|
+
return list(inner["boxes"])
|
|
117
|
+
json_val = getattr(result, "json", None)
|
|
118
|
+
if callable(json_val):
|
|
119
|
+
json_val = json_val()
|
|
120
|
+
if isinstance(json_val, dict):
|
|
121
|
+
if json_val.get("boxes") is not None:
|
|
122
|
+
return list(json_val["boxes"])
|
|
123
|
+
inner = json_val.get("res")
|
|
124
|
+
if isinstance(inner, dict) and inner.get("boxes") is not None:
|
|
125
|
+
return list(inner["boxes"])
|
|
126
|
+
return []
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _boxes_to_regions(
|
|
130
|
+
raw_boxes: Sequence[Dict[str, Any]],
|
|
131
|
+
img_w: int,
|
|
132
|
+
img_h: int,
|
|
133
|
+
*,
|
|
134
|
+
table_threshold: float = DEFAULT_TABLE_THRESHOLD,
|
|
135
|
+
) -> List[Dict[str, Any]]:
|
|
136
|
+
"""Convert raw pixel-coordinate detection dicts (`coordinate` + optional `polygon_points`) to regions."""
|
|
137
|
+
if img_w <= 0 or img_h <= 0:
|
|
138
|
+
raise ValueError(f"img_w and img_h must be positive, got ({img_w}, {img_h})")
|
|
139
|
+
|
|
140
|
+
regions: List[Dict[str, Any]] = []
|
|
141
|
+
for raw_idx, box in enumerate(raw_boxes):
|
|
142
|
+
label = str(box.get("label", ""))
|
|
143
|
+
task_type = box.get("task_type") or task_type_for_label(label)
|
|
144
|
+
if task_type is None or task_type == "abandon":
|
|
145
|
+
continue
|
|
146
|
+
raw_score = box.get("score")
|
|
147
|
+
if label == "table" and raw_score is not None and float(raw_score) < table_threshold:
|
|
148
|
+
continue
|
|
149
|
+
|
|
150
|
+
coord = _extract_box4(box.get("coordinate"))
|
|
151
|
+
if coord is None:
|
|
152
|
+
continue
|
|
153
|
+
x1 = max(0.0, min(coord[0], float(img_w)))
|
|
154
|
+
y1 = max(0.0, min(coord[1], float(img_h)))
|
|
155
|
+
x2 = max(0.0, min(coord[2], float(img_w)))
|
|
156
|
+
y2 = max(0.0, min(coord[3], float(img_h)))
|
|
157
|
+
if x2 <= x1 or y2 <= y1:
|
|
158
|
+
continue
|
|
159
|
+
|
|
160
|
+
bbox_2d = [
|
|
161
|
+
int(x1 / img_w * 1000),
|
|
162
|
+
int(y1 / img_h * 1000),
|
|
163
|
+
int(x2 / img_w * 1000),
|
|
164
|
+
int(y2 / img_h * 1000),
|
|
165
|
+
]
|
|
166
|
+
fallback_rect = [
|
|
167
|
+
[bbox_2d[0], bbox_2d[1]],
|
|
168
|
+
[bbox_2d[2], bbox_2d[1]],
|
|
169
|
+
[bbox_2d[2], bbox_2d[3]],
|
|
170
|
+
[bbox_2d[0], bbox_2d[3]],
|
|
171
|
+
]
|
|
172
|
+
reg: Dict[str, Any] = {
|
|
173
|
+
"index": len(regions),
|
|
174
|
+
"det_idx": raw_idx,
|
|
175
|
+
"label": label,
|
|
176
|
+
"bbox_2d": bbox_2d,
|
|
177
|
+
"polygon": fallback_rect,
|
|
178
|
+
"task_type": str(task_type),
|
|
179
|
+
}
|
|
180
|
+
if raw_score is not None:
|
|
181
|
+
reg["score"] = float(raw_score)
|
|
182
|
+
|
|
183
|
+
raw_poly = box.get("polygon_points")
|
|
184
|
+
if raw_poly is None:
|
|
185
|
+
raw_poly = box.get("polygon_px")
|
|
186
|
+
if raw_poly is not None:
|
|
187
|
+
if _clip_and_attach_polygon(
|
|
188
|
+
reg,
|
|
189
|
+
raw_poly,
|
|
190
|
+
img_w,
|
|
191
|
+
img_h,
|
|
192
|
+
poly_key_out="polygon_norm",
|
|
193
|
+
poly_px_key_out="polygon_px",
|
|
194
|
+
poly_px_size_key_out="polygon_px_size",
|
|
195
|
+
):
|
|
196
|
+
reg["polygon"] = reg["polygon_norm"]
|
|
197
|
+
regions.append(reg)
|
|
198
|
+
return regions
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def from_paddlex_result(
|
|
202
|
+
result: Any,
|
|
203
|
+
img_w: int,
|
|
204
|
+
img_h: int,
|
|
205
|
+
*,
|
|
206
|
+
table_threshold: float = DEFAULT_TABLE_THRESHOLD,
|
|
207
|
+
) -> List[Dict[str, Any]]:
|
|
208
|
+
"""Convert raw PaddleX `PP-DocLayoutV3` output directly into `prepare_page_crops`-ready regions.
|
|
209
|
+
|
|
210
|
+
Preserves exact page-clipped `polygon_px` + `polygon_px_size` + full-precision `polygon_norm`
|
|
211
|
+
(plus 4-point fallback `polygon`), while respecting `table_threshold` (default `0.25`).
|
|
212
|
+
"""
|
|
213
|
+
return _boxes_to_regions(
|
|
214
|
+
extract_paddlex_boxes(result),
|
|
215
|
+
img_w,
|
|
216
|
+
img_h,
|
|
217
|
+
table_threshold=table_threshold,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def from_hf_detections(
|
|
222
|
+
detections: Sequence[Dict[str, Any]],
|
|
223
|
+
img_w: int,
|
|
224
|
+
img_h: int,
|
|
225
|
+
*,
|
|
226
|
+
table_threshold: float = DEFAULT_TABLE_THRESHOLD,
|
|
227
|
+
) -> List[Dict[str, Any]]:
|
|
228
|
+
"""Convert Hugging Face `PP-DocLayoutV3` postprocessed detections into `prepare_page_crops` regions."""
|
|
229
|
+
return _boxes_to_regions(
|
|
230
|
+
detections,
|
|
231
|
+
img_w,
|
|
232
|
+
img_h,
|
|
233
|
+
table_threshold=table_threshold,
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def load_layout_regions(
|
|
238
|
+
layout_source: Union[str, Path, Dict[str, Any], Sequence[Dict[str, Any]]],
|
|
239
|
+
img_w: int,
|
|
240
|
+
img_h: int,
|
|
241
|
+
*,
|
|
242
|
+
fmt: Literal["auto", "regions", "paddlex", "hf"] = "auto",
|
|
243
|
+
table_threshold: float = DEFAULT_TABLE_THRESHOLD,
|
|
244
|
+
) -> List[Dict[str, Any]]:
|
|
245
|
+
"""Load layout JSON/dict from Pass1_Lite regions, Xiaomi JSONL entry, PaddleX, or HF format."""
|
|
246
|
+
if isinstance(layout_source, (str, Path)):
|
|
247
|
+
payload = json.loads(Path(layout_source).read_text(encoding="utf-8"))
|
|
248
|
+
else:
|
|
249
|
+
payload = layout_source
|
|
250
|
+
|
|
251
|
+
if fmt == "paddlex":
|
|
252
|
+
return from_paddlex_result(payload, img_w, img_h, table_threshold=table_threshold)
|
|
253
|
+
if fmt == "hf":
|
|
254
|
+
items = payload["detections"] if isinstance(payload, dict) and "detections" in payload else payload
|
|
255
|
+
return from_hf_detections(items, img_w, img_h, table_threshold=table_threshold)
|
|
256
|
+
if fmt == "regions":
|
|
257
|
+
if isinstance(payload, dict):
|
|
258
|
+
raw_regs = payload.get("layout_result", payload.get("regions", []))
|
|
259
|
+
else:
|
|
260
|
+
raw_regs = payload
|
|
261
|
+
return _normalize_existing_regions(raw_regs, table_threshold=table_threshold)
|
|
262
|
+
if fmt != "auto":
|
|
263
|
+
raise ValueError(f"Unsupported layout format: {fmt!r}")
|
|
264
|
+
|
|
265
|
+
# Auto-detect format
|
|
266
|
+
if isinstance(payload, dict):
|
|
267
|
+
if "layout_result" in payload and isinstance(payload["layout_result"], list):
|
|
268
|
+
return _normalize_existing_regions(payload["layout_result"], table_threshold=table_threshold)
|
|
269
|
+
if "regions" in payload and isinstance(payload["regions"], list):
|
|
270
|
+
return _normalize_existing_regions(payload["regions"], table_threshold=table_threshold)
|
|
271
|
+
if "boxes" in payload or "res" in payload:
|
|
272
|
+
return from_paddlex_result(payload, img_w, img_h, table_threshold=table_threshold)
|
|
273
|
+
if "detections" in payload and isinstance(payload["detections"], list):
|
|
274
|
+
return from_hf_detections(payload["detections"], img_w, img_h, table_threshold=table_threshold)
|
|
275
|
+
raise ValueError("Could not auto-detect layout dict format; expected layout_result/regions/boxes/res/detections.")
|
|
276
|
+
|
|
277
|
+
if isinstance(payload, (list, tuple)):
|
|
278
|
+
if not payload:
|
|
279
|
+
return []
|
|
280
|
+
first = payload[0]
|
|
281
|
+
if isinstance(first, dict) and "bbox_2d" in first:
|
|
282
|
+
return _normalize_existing_regions(payload, table_threshold=table_threshold)
|
|
283
|
+
if isinstance(first, dict) and "coordinate" in first:
|
|
284
|
+
return from_hf_detections(payload, img_w, img_h, table_threshold=table_threshold)
|
|
285
|
+
raise ValueError("Could not auto-detect layout sequence format; expected items with 'bbox_2d' or 'coordinate'.")
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def otsl_to_html(otsl: str) -> str:
|
|
289
|
+
"""Convert compact OTSL table markup (`<fcel>/<ecel>/<lcel>/<ucel>/<xcel>/<nl>`) into HTML `<table>`.
|
|
290
|
+
|
|
291
|
+
Returns input unchanged if it already looks like HTML or contains no OTSL tags.
|
|
292
|
+
"""
|
|
293
|
+
text = otsl.strip()
|
|
294
|
+
if not text or "<table" in text.lower():
|
|
295
|
+
return text
|
|
296
|
+
if not any(tag in text for tag in ("<fcel>", "<ecel>", "<nl>")):
|
|
297
|
+
return text
|
|
298
|
+
|
|
299
|
+
raw_rows = [row for row in text.split("<nl>") if row.strip()]
|
|
300
|
+
if not raw_rows:
|
|
301
|
+
return text
|
|
302
|
+
|
|
303
|
+
parsed_rows: List[List[Tuple[str, str]]] = []
|
|
304
|
+
for row in raw_rows:
|
|
305
|
+
tokens = _OTSL_TAG_RE.split(row)
|
|
306
|
+
cells: List[Tuple[str, str]] = []
|
|
307
|
+
i = 0
|
|
308
|
+
while i < len(tokens):
|
|
309
|
+
tok = tokens[i]
|
|
310
|
+
if tok in ("<fcel>", "<ecel>", "<lcel>", "<ucel>", "<xcel>"):
|
|
311
|
+
content = tokens[i + 1].strip() if i + 1 < len(tokens) else ""
|
|
312
|
+
cells.append((tok, content if tok == "<fcel>" else ""))
|
|
313
|
+
i += 2
|
|
314
|
+
else:
|
|
315
|
+
i += 1
|
|
316
|
+
if cells:
|
|
317
|
+
parsed_rows.append(cells)
|
|
318
|
+
if not parsed_rows:
|
|
319
|
+
return text
|
|
320
|
+
|
|
321
|
+
max_cols = max(len(r) for r in parsed_rows)
|
|
322
|
+
for r in parsed_rows:
|
|
323
|
+
while len(r) < max_cols:
|
|
324
|
+
r.append(("<ecel>", ""))
|
|
325
|
+
|
|
326
|
+
n_rows = len(parsed_rows)
|
|
327
|
+
# Resolve spans cleanly on grid: origin row extends horizontally ONLY across <lcel>;
|
|
328
|
+
# origin column extends vertically ONLY across <ucel> (<xcel> marks 2D interior slots).
|
|
329
|
+
html_rows: List[str] = []
|
|
330
|
+
for r_idx in range(n_rows):
|
|
331
|
+
row_cells: List[str] = []
|
|
332
|
+
for c_idx in range(max_cols):
|
|
333
|
+
tag, content = parsed_rows[r_idx][c_idx]
|
|
334
|
+
if tag not in ("<fcel>", "<ecel>"):
|
|
335
|
+
continue
|
|
336
|
+
colspan = 1
|
|
337
|
+
while c_idx + colspan < max_cols and parsed_rows[r_idx][c_idx + colspan][0] == "<lcel>":
|
|
338
|
+
colspan += 1
|
|
339
|
+
rowspan = 1
|
|
340
|
+
while r_idx + rowspan < n_rows and parsed_rows[r_idx + rowspan][c_idx][0] == "<ucel>":
|
|
341
|
+
rowspan += 1
|
|
342
|
+
attrs = ""
|
|
343
|
+
if rowspan > 1:
|
|
344
|
+
attrs += f' rowspan="{rowspan}"'
|
|
345
|
+
if colspan > 1:
|
|
346
|
+
attrs += f' colspan="{colspan}"'
|
|
347
|
+
row_cells.append(f"<td{attrs}>{_escape_table_cell(content)}</td>")
|
|
348
|
+
html_rows.append("<tr>" + "".join(row_cells) + "</tr>")
|
|
349
|
+
return "<table>" + "".join(html_rows) + "</table>"
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def assemble_markdown(
|
|
353
|
+
regions: Sequence[Dict[str, Any]],
|
|
354
|
+
region_texts: Optional[Dict[int, str]] = None,
|
|
355
|
+
*,
|
|
356
|
+
text_key: str = "merged",
|
|
357
|
+
skip_labels: Sequence[str] = (),
|
|
358
|
+
) -> str:
|
|
359
|
+
"""Assemble per-region OCR strings into page Markdown strictly following `index` order."""
|
|
360
|
+
skip_set = set(skip_labels)
|
|
361
|
+
ordered = sorted(regions, key=lambda r: int(r.get("index", 0)))
|
|
362
|
+
blocks: List[str] = []
|
|
363
|
+
for r in ordered:
|
|
364
|
+
if str(r.get("task_type", "")) in ("skip", "abandon"):
|
|
365
|
+
continue
|
|
366
|
+
if str(r.get("label", "")) in skip_set:
|
|
367
|
+
continue
|
|
368
|
+
idx = int(r.get("index", 0))
|
|
369
|
+
raw = region_texts.get(idx, "") if region_texts is not None else str(r.get(text_key, ""))
|
|
370
|
+
txt = raw.strip()
|
|
371
|
+
if txt:
|
|
372
|
+
blocks.append(txt)
|
|
373
|
+
return "\n\n".join(blocks)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _load_font(size: int = 16, bold: bool = False) -> ImageFont.ImageFont:
|
|
377
|
+
candidates = (
|
|
378
|
+
[
|
|
379
|
+
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
|
380
|
+
"/usr/share/fonts/opentype/urw-base35/NimbusSans-Bold.otf",
|
|
381
|
+
]
|
|
382
|
+
if bold
|
|
383
|
+
else [
|
|
384
|
+
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
|
385
|
+
"/usr/share/fonts/opentype/urw-base35/NimbusSans-Regular.otf",
|
|
386
|
+
]
|
|
387
|
+
)
|
|
388
|
+
for path in candidates:
|
|
389
|
+
if Path(path).is_file():
|
|
390
|
+
try:
|
|
391
|
+
return ImageFont.truetype(path, size=size)
|
|
392
|
+
except OSError:
|
|
393
|
+
continue
|
|
394
|
+
return ImageFont.load_default()
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def render_page_comparison(
|
|
398
|
+
page_source: Union[str, Path, Image.Image],
|
|
399
|
+
bundle: PreparedPageCrops,
|
|
400
|
+
*,
|
|
401
|
+
apply_exif_transpose: bool = False,
|
|
402
|
+
max_crops: int = 4,
|
|
403
|
+
) -> Image.Image:
|
|
404
|
+
"""Render a side-by-side visual diagnostic sheet (annotated page + Before/After region crops)."""
|
|
405
|
+
img_rgb = load_image_rgb(page_source, apply_exif_transpose=apply_exif_transpose)
|
|
406
|
+
try:
|
|
407
|
+
pw, ph = img_rgb.size
|
|
408
|
+
# Left panel: annotated page
|
|
409
|
+
panel_h = 760
|
|
410
|
+
scale = min(560.0 / max(1, pw), (panel_h - 60) / max(1, ph))
|
|
411
|
+
disp_w, disp_h = max(120, int(round(pw * scale))), max(120, int(round(ph * scale)))
|
|
412
|
+
page_vis = img_rgb.resize((disp_w, disp_h), Image.Resampling.LANCZOS)
|
|
413
|
+
pdraw = ImageDraw.Draw(page_vis)
|
|
414
|
+
font_sm = _load_font(13, bold=True)
|
|
415
|
+
font_md = _load_font(15, bold=True)
|
|
416
|
+
font_title = _load_font(18, bold=True)
|
|
417
|
+
|
|
418
|
+
crop_by_idx = {c.region_index: c for c in bundle.crops}
|
|
419
|
+
for r in bundle.regions:
|
|
420
|
+
bbox = _extract_box4(r.get("bbox_2d"))
|
|
421
|
+
if bbox is None:
|
|
422
|
+
continue
|
|
423
|
+
idx = int(r.get("index", 0))
|
|
424
|
+
cobj = crop_by_idx.get(idx)
|
|
425
|
+
if cobj and cobj.poly_masked and cobj.moire_lpf_applied:
|
|
426
|
+
color = (220, 60, 40)
|
|
427
|
+
elif cobj and cobj.poly_masked:
|
|
428
|
+
color = (30, 120, 220)
|
|
429
|
+
elif cobj and cobj.moire_lpf_applied:
|
|
430
|
+
color = (210, 130, 20)
|
|
431
|
+
else:
|
|
432
|
+
color = (40, 160, 80)
|
|
433
|
+
|
|
434
|
+
x1 = int(round(bbox[0] * disp_w / 1000.0))
|
|
435
|
+
y1 = int(round(bbox[1] * disp_h / 1000.0))
|
|
436
|
+
x2 = int(round(bbox[2] * disp_w / 1000.0))
|
|
437
|
+
y2 = int(round(bbox[3] * disp_h / 1000.0))
|
|
438
|
+
pdraw.rectangle([x1, y1, x2, y2], outline=color, width=2)
|
|
439
|
+
raw_poly = r.get("polygon_norm")
|
|
440
|
+
poly_arr = _extract_poly_array(raw_poly) if raw_poly is not None else None
|
|
441
|
+
if poly_arr is None:
|
|
442
|
+
poly_arr = _extract_poly_array(r.get("polygon"))
|
|
443
|
+
if poly_arr is not None:
|
|
444
|
+
pts = [
|
|
445
|
+
(int(round(float(pt[0]) * disp_w / 1000.0)), int(round(float(pt[1]) * disp_h / 1000.0)))
|
|
446
|
+
for pt in poly_arr
|
|
447
|
+
]
|
|
448
|
+
if len(pts) >= 3:
|
|
449
|
+
pdraw.polygon(pts, outline=(160, 40, 200))
|
|
450
|
+
badge = f"#{idx}"
|
|
451
|
+
pdraw.rectangle([x1, max(0, y1 - 16), x1 + 28, y1], fill=color)
|
|
452
|
+
pdraw.text((x1 + 3, max(0, y1 - 15)), badge, fill=(255, 255, 255), font=font_sm)
|
|
453
|
+
|
|
454
|
+
# Pick highlighted crops (prioritizing masked or LPF-processed crops)
|
|
455
|
+
ranked = sorted(
|
|
456
|
+
bundle.crops,
|
|
457
|
+
key=lambda c: (not (c.poly_masked or c.moire_lpf_applied), not c.poly_masked, c.region_index),
|
|
458
|
+
)[: max(1, max_crops)]
|
|
459
|
+
reg_by_idx = {int(r.get("index", i)): r for i, r in enumerate(bundle.regions)}
|
|
460
|
+
|
|
461
|
+
right_w = 680
|
|
462
|
+
pad = 20
|
|
463
|
+
canvas_w = disp_w + right_w + pad * 3
|
|
464
|
+
canvas_h = max(panel_h, 80 + len(ranked) * 175)
|
|
465
|
+
canvas = Image.new("RGB", (canvas_w, canvas_h), (248, 250, 252))
|
|
466
|
+
cdraw = ImageDraw.Draw(canvas)
|
|
467
|
+
|
|
468
|
+
header = (
|
|
469
|
+
f"Pass1_Lite Inspection | Moire score: {bundle.moire_score:.2f} "
|
|
470
|
+
f"({'LPF ON' if bundle.is_moire_page else 'LPF OFF'}) | Regions: {len(bundle.crops)}"
|
|
471
|
+
)
|
|
472
|
+
cdraw.rectangle([0, 0, canvas_w, 46], fill=(24, 34, 52))
|
|
473
|
+
cdraw.text((pad, 12), header, fill=(255, 255, 255), font=font_title)
|
|
474
|
+
|
|
475
|
+
canvas.paste(page_vis, (pad, 62))
|
|
476
|
+
cdraw.rectangle([pad, 62, pad + disp_w, 62 + disp_h], outline=(200, 208, 220), width=1)
|
|
477
|
+
leg_y = 62 + disp_h + 12
|
|
478
|
+
leg_items = [
|
|
479
|
+
((220, 60, 40), "Mask+LPF"),
|
|
480
|
+
((30, 120, 220), "Poly Mask"),
|
|
481
|
+
((210, 130, 20), "Moire LPF"),
|
|
482
|
+
((40, 160, 80), "Unchanged"),
|
|
483
|
+
]
|
|
484
|
+
lx = pad
|
|
485
|
+
for lcol, ltxt in leg_items:
|
|
486
|
+
cdraw.rectangle([lx, leg_y + 2, lx + 12, leg_y + 14], fill=lcol)
|
|
487
|
+
cdraw.text((lx + 16, leg_y), ltxt, fill=(60, 72, 90), font=font_sm)
|
|
488
|
+
lx += 108
|
|
489
|
+
|
|
490
|
+
rx = disp_w + pad * 2
|
|
491
|
+
col_w = (right_w - 16) // 2
|
|
492
|
+
cdraw.text((rx, 60), "BEFORE (Baseline Rect Crop)", fill=(160, 45, 45), font=font_md)
|
|
493
|
+
cdraw.text((rx + col_w + 16, 60), "AFTER (Pass1_Lite Refined)", fill=(20, 120, 65), font=font_md)
|
|
494
|
+
|
|
495
|
+
cur_y = 88
|
|
496
|
+
slot_h = 132
|
|
497
|
+
for cobj in ranked:
|
|
498
|
+
reg = reg_by_idx.get(cobj.region_index, {})
|
|
499
|
+
before_crop = mask_and_crop_region(img_rgb, reg.get("bbox_2d"), None, poly_fill_thresh=1.0)
|
|
500
|
+
after_crop = cobj.crop
|
|
501
|
+
|
|
502
|
+
tags = []
|
|
503
|
+
if cobj.poly_masked:
|
|
504
|
+
tags.append("poly-masked")
|
|
505
|
+
if cobj.moire_lpf_applied:
|
|
506
|
+
tags.append("moire-LPF-2x")
|
|
507
|
+
if not tags:
|
|
508
|
+
tags.append("unchanged")
|
|
509
|
+
cdraw.text(
|
|
510
|
+
(rx, cur_y),
|
|
511
|
+
f"Region #{cobj.region_index} [{cobj.label} / {cobj.task_type}] — {', '.join(tags)}",
|
|
512
|
+
fill=(40, 50, 65),
|
|
513
|
+
font=font_sm,
|
|
514
|
+
)
|
|
515
|
+
cur_y += 20
|
|
516
|
+
for col_i, im_c in enumerate((before_crop, after_crop)):
|
|
517
|
+
bx = rx + col_i * (col_w + 16)
|
|
518
|
+
cdraw.rectangle([bx, cur_y, bx + col_w, cur_y + slot_h], fill=(255, 255, 255), outline=(205, 212, 224))
|
|
519
|
+
thumb = im_c.copy()
|
|
520
|
+
thumb.thumbnail((col_w - 8, slot_h - 8), Image.Resampling.LANCZOS)
|
|
521
|
+
ox = bx + (col_w - thumb.width) // 2
|
|
522
|
+
oy = cur_y + (slot_h - thumb.height) // 2
|
|
523
|
+
canvas.paste(thumb, (ox, oy))
|
|
524
|
+
cur_y += slot_h + 18
|
|
525
|
+
|
|
526
|
+
return canvas
|
|
527
|
+
finally:
|
|
528
|
+
img_rgb.close()
|