Wenvia commited on
Commit
d147f90
·
verified ·
1 Parent(s): dcee05d

Model files used by Sale Deed AI V3

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +2 -0
  2. hi-en/VERSION +1 -0
  3. hi-en/arjuna_hi.yaml +95 -0
  4. hi-en/kanen_infer/__init__.py +9 -0
  5. hi-en/kanen_infer/api.py +119 -0
  6. hi-en/kanen_infer/arjuna_cli.py +111 -0
  7. hi-en/kanen_infer/assemble.py +505 -0
  8. hi-en/kanen_infer/batch_pipeline.py +322 -0
  9. hi-en/kanen_infer/charset.py +72 -0
  10. hi-en/kanen_infer/cli.py +71 -0
  11. hi-en/kanen_infer/config.py +112 -0
  12. hi-en/kanen_infer/formats.py +76 -0
  13. hi-en/kanen_infer/geometry.py +19 -0
  14. hi-en/kanen_infer/hardware.py +151 -0
  15. hi-en/kanen_infer/io.py +68 -0
  16. hi-en/kanen_infer/models.py +65 -0
  17. hi-en/kanen_infer/pipeline_base.py +283 -0
  18. hi-en/kanen_infer/providers.py +248 -0
  19. hi-en/kanen_infer/rec_onnx.py +75 -0
  20. hi-en/models/det/inference.onnx +3 -0
  21. hi-en/models/layout/LICENSE-AGPL-3.0.txt +661 -0
  22. hi-en/models/layout/NOTICE.md +15 -0
  23. hi-en/models/layout/arjuna_layout_v15.onnx +3 -0
  24. hi-en/models/layout/classes.txt +9 -0
  25. hi-en/models/layout/classes_v15.txt +15 -0
  26. hi-en/models/layout/kanen_layout_v14.onnx +3 -0
  27. hi-en/models/layout/selftest.jpg +3 -0
  28. hi-en/models/layout/selftest_ref_v14.json +72 -0
  29. hi-en/models/layout/selftest_ref_v15.json +72 -0
  30. hi-en/models/rec/arjuna_rec_hi.onnx +3 -0
  31. hi-en/models/rec/charset.json +1 -0
  32. kn-en/LICENSE +3 -0
  33. kn-en/VERSION +1 -0
  34. kn-en/kanen.yaml +58 -0
  35. kn-en/kanen_infer/__init__.py +9 -0
  36. kn-en/kanen_infer/api.py +110 -0
  37. kn-en/kanen_infer/assemble.py +261 -0
  38. kn-en/kanen_infer/batch_pipeline.py +230 -0
  39. kn-en/kanen_infer/charset.py +72 -0
  40. kn-en/kanen_infer/cli.py +67 -0
  41. kn-en/kanen_infer/config.py +77 -0
  42. kn-en/kanen_infer/formats.py +76 -0
  43. kn-en/kanen_infer/geometry.py +19 -0
  44. kn-en/kanen_infer/io.py +68 -0
  45. kn-en/kanen_infer/pipeline_base.py +201 -0
  46. kn-en/kanen_infer/providers.py +157 -0
  47. kn-en/kanen_infer/rec_onnx.py +52 -0
  48. kn-en/models/det/inference.onnx +3 -0
  49. kn-en/models/det/inference.yml +52 -0
  50. kn-en/models/layout/LICENSE-AGPL-3.0.txt +661 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ kn-en/models/layout/selftest.jpg filter=lfs diff=lfs merge=lfs -text
37
+ hi-en/models/layout/selftest.jpg filter=lfs diff=lfs merge=lfs -text
hi-en/VERSION ADDED
@@ -0,0 +1 @@
 
 
1
+ 1.1.3
hi-en/arjuna_hi.yaml ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Arjuna-OCR [Hi & En] v1.1.3 — inference configuration.
2
+ # Paths are relative to this file. Environment overrides: KANEN_CONFIG, KANEN_TRT=1, KANEN_ORT_CPU=1,
3
+ # KANEN_REC_ONNX, KANEN_BACKGROUND_TRT=0 (build TensorRT engines up front instead of in the background).
4
+ name: "Arjuna-OCR [Hi & En]"
5
+ version: 1.1.3 # package version; stamped into every output document as model.pipeline
6
+
7
+ models:
8
+ det: models/det/inference.onnx
9
+ layout: models/layout/arjuna_layout_v15.onnx # Arjuna layout v15 (15 classes incl. barcode/list_item/section_header)
10
+ layout_labels: models/layout/classes_v15.txt
11
+ layout_selftest: models/layout/selftest.jpg
12
+ # MUST come from the SAME layout model: a v12 reference rejects every v14 engine at 75 % and silently
13
+ # drops the layout stage to the CUDA provider (5x slower end to end).
14
+ layout_selftest_ref: models/layout/selftest_ref_v15.json # a v14 reference REJECTS a correct v15 engine
15
+ rec: models/rec/arjuna_rec_hi.onnx
16
+ charset: models/rec/charset.json
17
+
18
+ provider:
19
+ policy: auto # auto = trt -> cuda -> cpu
20
+ trt_fp16: true
21
+ engine_cache: trt_cache
22
+ trt_workspace_gb: 4
23
+ cuda_mem_limit_gb: 6
24
+ cudnn_conv_algo_search: HEURISTIC
25
+
26
+ detector:
27
+ # Resolution-aware rescale. A page whose MEDIAN detected line is shorter than min_line_px is
28
+ # re-detected after upscaling so its lines land near target_line_px. The recogniser is trained at
29
+ # H=32 and the detector never enlarges a small page on its own, so a low-DPI scan was being read
30
+ # at 12-25 px. 0 disables. Costs a second detect pass, and ONLY on the pages that need it.
31
+ min_line_px: 32
32
+ target_line_px: 45
33
+ max_page_scale: 3.0
34
+ canvases: [1280, 1920, 2560]
35
+ width_classes: [0.5, 0.625, 0.75, 0.875, 1.0]
36
+ min_scale: 0.9
37
+ default_canvas: 1280
38
+ thresh: 0.2
39
+ box_thresh: 0.45
40
+ unclip: 1.4
41
+ pad_top: 0.03
42
+ pad_bottom: 0.25
43
+ batch: 2
44
+
45
+ layout:
46
+ mode: always
47
+ batch: 4
48
+ score_thresh: 0.15 # chosen on real-text survival, not mAP
49
+ fill: [1.0, 0.8] # two passes: 1.0 for small blocks, 0.8 keeps whole-page tables inside the head's
50
+ # regression range. [1.0] is ~7 % faster and returned 2 fewer lines on the blind set.
51
+ selftest_min_match: 1.0
52
+
53
+ recognizer:
54
+ buckets: [128, 192, 256, 384, 512, 640, 768, 1024, 1280]
55
+ batch: 64
56
+ height: 32
57
+ max_ratio: 40
58
+
59
+ second_pass:
60
+ conf: 0.85
61
+ cap: 6
62
+
63
+ runtime:
64
+ workers: auto
65
+ page_batch: 16 # 16 measured fastest on a 4-core host; 32 is past the knee
66
+ log_level: warning
67
+ polygons: false
68
+
69
+ assemble:
70
+ low_res_px: 26 # FLAG (never alter) a line read from very few pixels
71
+ retype_visual_min_lines: 4 # a figure/seal holding this many readable lines is a mislabelled panel -> re-type it
72
+ row_cross_frac: 0.20 # two side-by-side blocks are ONE table when a spanning strip or crossing lines say so AND the gutter clears 2.5x line height
73
+ # KNOWN LIMITATION — see README "Known limitations". Text inside these block types is withheld from page
74
+ # `text` (the lines stay in `blocks` with their flags). On registration records this has been observed to
75
+ # delete a whole correctly-read panel when layout mislabels it `figure` at low confidence.
76
+ suppress_text_in: [] # nothing is ever deleted. Measured on 54 held-out pages / 14,184 words:
77
+ # [] loses 0 words, ["handwriting","barcode"] loses 26. Lines keep their
78
+ # in_seal / in_handwriting / in_figure flags so a consumer can filter.
79
+ # Guards. Without them, suppression deleted 119 lines of REAL text out of 160 withheld across 87 pages of
80
+ # registration records — certificate and GRN numbers, dates, the registrar's seal text. Cause: an e-stamp
81
+ # panel holds emblems, a barcode and a QR code, so the layout model boxes the WHOLE panel as `figure` at
82
+ # confidence 0.31-0.38 over a third of the page. With these guards: 119 -> 28, and `figure` no longer
83
+ # appears among suppressed blocks at all. Set any to its OFF value (0.0 / 1.0 / 0) to restore the old
84
+ # unconditional behaviour.
85
+ suppress_min_conf: 0.50 # a marginal layout call keeps the text
86
+ suppress_max_area: 0.10 # a block larger than 10 % of the page is not a figure
87
+ suppress_max_lines: 3
88
+ # Merge SAME-CLASS layout blocks drawn over the same region. The layout model sometimes cuts one paragraph
89
+ # into boxes that interleave vertically; every line is then recognised correctly but emitted in block order,
90
+ # which scrambles the page (observed: 14/14 lines right, all in the wrong place). Requires overlap on BOTH
91
+ # axes — x by 0.60 of the narrower box, y by 0.25 of the shorter — so two-column pages, stacked paragraphs
92
+ # and nested seals never merge. Measured over 87 pages: non-space characters unchanged; line count falls
93
+ # only where fragments of one row rejoin ("2." + "Type of Property" -> "2. Type of Property"). 0 disables.
94
+ merge_split_blocks_x: 0.60
95
+ merge_split_blocks_y: 0.25 # a "figure" holding more than 3 readable lines is a mislabelled panel
hi-en/kanen_infer/__init__.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ """Arjuna-OCR [Kn&En] inference package (Kannada + English document OCR): ONNX Runtime + optional TensorRT, no PyTorch."""
2
+ from pathlib import Path as _P
3
+ try: __version__ = (_P(__file__).resolve().parent.parent / "VERSION").read_text(encoding="utf-8").strip()
4
+ except Exception: __version__ = "1.1.3"
5
+ NAME = "Arjuna-OCR [Kn&En]" # product display name — also `name:` in kanen.yaml (the config value wins at run time)
6
+ from .config import load_config, PKG_ROOT # noqa: E402
7
+ from .api import KanEnOCR # noqa: E402
8
+ from .formats import to_json, to_markdown, to_text, to_hocr, to_alto, to_tsv, render # noqa: E402
9
+ __all__ = ["KanEnOCR", "load_config", "PKG_ROOT", "NAME", "__version__", "to_json", "to_markdown", "to_text", "to_hocr", "to_alto", "to_tsv", "render"]
hi-en/kanen_infer/api.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Arjuna-OCR [Kn&En] Python API.
2
+
3
+ from kanen_infer import KanEnOCR
4
+ ocr = KanEnOCR() # loads kanen.yaml, builds/validates engines (warm-up), see ocr.ready()
5
+ doc = ocr.page("scan.png") # one kanen-ocr/1.0 document (dict)
6
+ docs = ocr.pages(["a.png", "b.pdf"]) # batched; ocr.stream(...) yields documents as they finish
7
+ boxes = ocr.detect(img) # text-line polygons only
8
+ texts = ocr.recognize(crops) # [(text, conf, min_conf)] for line images
9
+ blocks = ocr.layout(img) # layout blocks only
10
+ ocr.render(doc, "hocr") # json | md | txt | hocr | alto | tsv
11
+
12
+ Inputs: file path, bytes, numpy BGR array, PIL image, file object; PDFs through `pages()`/`stream()` (pypdfium2 or pdftoppm,
13
+ optional). The output document never depends on the provider or the batch composition beyond fp16 noise on garbage lines;
14
+ no confidence filtering, no text rewriting, no re-ordering happens here."""
15
+ import logging, time
16
+ import numpy as np, cv2
17
+ from pathlib import Path
18
+ from .config import load_config, PKG_ROOT
19
+ from .providers import Providers
20
+ from .rec_onnx import KanEnRecognizer
21
+ from .pipeline_base import Detector, Layout, crop
22
+ from .batch_pipeline import BatchOCR
23
+ from .io import to_bgr, expand_inputs, iter_page_sources
24
+ from . import formats
25
+
26
+ LOG = logging.getLogger("kanen")
27
+
28
+ class KanEnOCR:
29
+ def __init__(self, config=None, providers=None, layout_mode=None, workers=None, warmup=True, **overrides):
30
+ """config: path to a kanen.yaml/json, a dict, or None (package default). providers: 'trt' | 'cuda' | 'cpu' | 'auto'
31
+ (overrides config provider.policy). Other keyword overrides use dotted section names, e.g. layout={'mode': 'auto'}."""
32
+ ov = dict(overrides)
33
+ if providers: ov.setdefault("provider", {})["policy"] = providers
34
+ if layout_mode: ov.setdefault("layout", {})["mode"] = layout_mode
35
+ if workers: ov.setdefault("runtime", {})["workers"] = workers
36
+ self.cfg = load_config(config, ov); self.name = self.cfg.get("name", "Arjuna-OCR [Kn&En]"); self.version = str(self.cfg.get("version", "1.0.0"))
37
+ t0 = time.time(); self.prov = Providers(self.cfg)
38
+ self.rec = KanEnRecognizer(cfg=self.cfg, providers=self.prov); self.det = Detector(self.cfg, self.prov)
39
+ self.lay = Layout(self.cfg, self.prov) if self.cfg["layout"].get("mode", "always") != "never" else None
40
+ self.batch = BatchOCR(self.rec, cfg=self.cfg, providers=self.prov, det=self.det, lay=self.lay)
41
+ self._load_s = round(time.time() - t0, 1); self._warm = None
42
+ if warmup: self.warmup()
43
+
44
+ # ---- status --------------------------------------------------------------------------------------------------
45
+ def warmup(self):
46
+ """Run the bundled self-test page through the full pipeline once: builds/validates TensorRT engines for the
47
+ default shapes (layout self-test, detector canvas, recognizer profile) so the first real page is not slow."""
48
+ t0 = time.time(); p = self.cfg.path("models", "layout_selftest")
49
+ if p.exists():
50
+ img = to_bgr(str(p)); self.batch.run([img], page_batch=1, loader=lambda x: x)
51
+ self._warm = round(time.time() - t0, 1); return self.ready()
52
+
53
+ def optimize(self, wait=False, timeout=None):
54
+ """Build the TensorRT engines for this machine in the background and swap them in once each one is validated
55
+ against the session it replaces. Safe to call at any time; returns immediately unless `wait` is set.
56
+ Long-lived processes (server, endpoint handler, batch jobs) should call this at startup: the pipeline keeps
57
+ serving on the CUDA provider meanwhile, and gets 2-3x faster when the engines are ready."""
58
+ n = self.prov.start_background_builds() if hasattr(self.prov, "start_background_builds") else 0
59
+ if wait and n: return self.prov.wait_for_engines(timeout)
60
+ return {"builds_started": n}
61
+
62
+ def ready(self):
63
+ """Provider each model ended on, fallbacks that happened, engine-cache state, model hashes."""
64
+ st = self.prov.summary()
65
+ if self.lay is not None: st.setdefault("layout", {})["engine"] = self.lay.stage
66
+ return {"name": self.name, "version": self.version, "config": self.cfg.source, "gpu": self.prov.gpu, "tensorrt": self.prov.trt_ver,
67
+ "providers": st, "events": list(self.prov.events), "engine_cache": str(self.prov.cache_root), "load_s": self._load_s, "warmup_s": self._warm,
68
+ "model": dict(self.batch.model_ids)}
69
+
70
+ # ---- full documents -------------------------------------------------------------------------------------------
71
+ def page(self, image, polygons=None, dpi=None):
72
+ """Single page (latency path): one kanen-ocr/1.0 document."""
73
+ img = to_bgr(image); docs, _ = self.batch.process([img], dpi=dpi, with_polygons=self._poly(polygons)); d = docs[0]
74
+ if isinstance(image, (str, Path)): d["page"]["file"] = Path(image).name
75
+ return d
76
+
77
+ def pages(self, inputs, page_batch=None, polygons=None, pdf_dpi=200):
78
+ """Batched: list/dir/glob of image or PDF paths, or a list of arrays / PIL images / bytes -> list of documents."""
79
+ return [d for d in self.stream(inputs, page_batch, polygons, pdf_dpi)]
80
+
81
+ def stream(self, inputs, page_batch=None, polygons=None, pdf_dpi=200):
82
+ """Generator variant of pages(): yields documents in input order as each page batch completes."""
83
+ sources = self._sources(inputs, pdf_dpi); labels = [s[0] for s in sources]; items = [s[1] for s in sources]
84
+ page_batch = page_batch or self.cfg["runtime"].get("page_batch", 8); k = 0
85
+ for doc, st in self.batch.stream(items, page_batch=page_batch, loader=to_bgr, with_polygons=self._poly(polygons)):
86
+ if labels[k]: doc["page"]["file"] = labels[k]
87
+ self.last_stats = st; k += 1; yield doc
88
+
89
+ def _sources(self, inputs, pdf_dpi):
90
+ if isinstance(inputs, (str, Path)) or (isinstance(inputs, (list, tuple)) and inputs and all(isinstance(x, (str, Path)) for x in inputs)):
91
+ return list(iter_page_sources(expand_inputs(inputs), dpi=pdf_dpi))
92
+ if isinstance(inputs, (list, tuple)): return [(getattr(x, "name", None) or "", x) for x in inputs]
93
+ return [("", inputs)]
94
+
95
+ def _poly(self, polygons): return bool(self.cfg["runtime"].get("polygons", False)) if polygons is None else bool(polygons)
96
+
97
+ # ---- single stages --------------------------------------------------------------------------------------------
98
+ def detect(self, image):
99
+ """Text-line detection only -> list of (polygon 4x2 float32 in page pixels, score)."""
100
+ return self.det(to_bgr(image))
101
+
102
+ def recognize(self, crops, batch=None):
103
+ """Recognition only on a list of line images (any size, BGR or gray) -> [(text, confidence, min_conf)]."""
104
+ res = self.rec([to_bgr(c) for c in crops], bs=batch)
105
+ return [(t, float(c), float(getattr(c, "min_char", c))) for t, c in res]
106
+
107
+ def layout(self, image):
108
+ """Layout blocks only -> [{type, score, bbox, order}] (v15 labels: text, title, section_header, list_item, table, figure, caption, footnote, formula, header, footer, seal, handwriting, id_panel, barcode)."""
109
+ if self.lay is None: self.lay = Layout(self.cfg, self.prov); self.batch.lay = self.lay
110
+ return self.lay(to_bgr(image))[0]
111
+
112
+ def crops(self, image, boxes=None):
113
+ """Perspective line crops for detect() boxes (helper for recognize())."""
114
+ img = to_bgr(image); boxes = self.detect(img) if boxes is None else boxes
115
+ return [crop(img, b) for b, _ in boxes]
116
+
117
+ # ---- serialisation --------------------------------------------------------------------------------------------
118
+ def render(self, doc, fmt="json"):
119
+ return formats.render(doc, fmt)
hi-en/kanen_infer/arjuna_cli.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """`arjuna` command line — the deployment front end.
2
+
3
+ arjuna doctor what this machine can do, what the pipeline will use, and why
4
+ arjuna ocr page.pdf -o out/ OCR files, directories, globs or PDFs (same engine as kanen-ocr)
5
+ arjuna serve --port 8080 HTTP server (FastAPI); builds TensorRT engines in the background
6
+ arjuna bench measured pages/s on the bundled sample pages, before and after optimisation
7
+ arjuna warm build and validate the TensorRT engines for this machine, then exit
8
+
9
+ `ocr` stays on the CUDA/CPU provider for small jobs (a TensorRT build costs more than it saves for a page or two) and
10
+ optimises automatically from `--optimize-after` pages upwards. `serve`, `bench` and `warm` always optimise.
11
+ """
12
+ import argparse, json, os, sys, time
13
+
14
+ def _cfg(args):
15
+ from .config import load_config
16
+ from . import hardware
17
+ cfg = load_config(args.config)
18
+ if getattr(args, "provider", None): cfg["provider"]["policy"] = args.provider
19
+ changes = [] if getattr(args, "no_autoconfig", False) else hardware.autoconfig(cfg, quality=getattr(args, "quality", None))
20
+ return cfg, changes
21
+
22
+ def cmd_doctor(a):
23
+ from . import hardware
24
+ cfg, changes = _cfg(a)
25
+ hw = hardware.probe(cfg["provider"].get("policy")); print(hardware.report(cfg, changes, hw))
26
+ if a.run:
27
+ from .api import KanEnOCR
28
+ import cv2, glob
29
+ pages = sorted(glob.glob(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "samples", "pages", "*")))[:1]
30
+ if pages:
31
+ t = time.time(); ocr = KanEnOCR(config=cfg, warmup=False); load = time.time() - t
32
+ t = time.time(); doc = ocr.page(cv2.imread(pages[0])); dt = time.time() - t
33
+ lines = sum(len(b["lines"]) for b in doc["blocks"])
34
+ print(f"\n load {load:.1f}s\n sample page {dt:.1f}s, {lines} lines, {len(doc['blocks'])} blocks ({os.path.basename(pages[0])})")
35
+ print(f" providers {json.dumps({k: v['provider'] for k, v in ocr.prov.status.items() if k in ('det', 'rec', 'layout')})}")
36
+ for e in ocr.prov.events[:6]: print(f" {e}")
37
+ return 0
38
+
39
+ def cmd_warm(a):
40
+ from .api import KanEnOCR
41
+ cfg, changes = _cfg(a)
42
+ print("building TensorRT engines for this machine (this is the one slow step; it is cached afterwards)...", flush=True)
43
+ t = time.time(); ocr = KanEnOCR(config=cfg, warmup=True)
44
+ res = ocr.optimize(wait=True, timeout=a.timeout)
45
+ print(f"done in {time.time() - t:.0f}s: {json.dumps(res)}")
46
+ print("providers:", json.dumps({k: v["provider"] for k, v in ocr.prov.status.items() if k in ("det", "rec", "layout")}))
47
+ return 0
48
+
49
+ def cmd_bench(a):
50
+ import cv2, glob
51
+ from .api import KanEnOCR
52
+ cfg, changes = _cfg(a)
53
+ root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
54
+ pages = sorted(glob.glob(os.path.join(root, "samples", "pages", "*")))
55
+ if not pages: print("no sample pages bundled"); return 1
56
+ imgs = [cv2.imread(p) for p in pages] * max(1, a.repeat)
57
+ ocr = KanEnOCR(config=cfg, warmup=True)
58
+ def run(tag):
59
+ t = time.time(); docs = ocr.pages(imgs); dt = time.time() - t
60
+ print(f" {tag:<22} {len(imgs)/dt:5.2f} pages/s ({dt:.1f}s for {len(imgs)} pages, {sum(len(b['lines']) for d in docs for b in d['blocks'])} lines)")
61
+ return len(imgs) / dt
62
+ print(f"pages: {len(imgs)} · providers before: {json.dumps({k: v['provider'] for k, v in ocr.prov.status.items() if k in ('det','rec','layout')})}")
63
+ run("as loaded")
64
+ ocr.optimize(wait=True, timeout=a.timeout)
65
+ print(f"providers after: {json.dumps({k: v['provider'] for k, v in ocr.prov.status.items() if k in ('det','rec','layout')})}")
66
+ run("after optimisation"); run("steady state")
67
+ return 0
68
+
69
+ def cmd_serve(a):
70
+ import importlib.util
71
+ root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
72
+ spec = importlib.util.spec_from_file_location("kanen_serve", os.path.join(root, "serve.py"))
73
+ mod = importlib.util.module_from_spec(spec); sys.modules["kanen_serve"] = mod; spec.loader.exec_module(mod)
74
+ return mod.serve(a)
75
+
76
+ def cmd_ocr(a):
77
+ from .cli import main as kanen_main
78
+ argv = ["--input"] + a.inputs + ["--out-dir", a.out_dir, "--format", a.format]
79
+ for flag, val in (("--page-batch", a.page_batch), ("--workers", a.workers), ("--provider", a.provider), ("--config", a.config)):
80
+ if val: argv += [flag, str(val)]
81
+ if a.json_lines: argv.append("--json-lines")
82
+ if a.quiet: argv.append("--quiet")
83
+ os.environ["KANEN_OPTIMIZE_AFTER"] = str(a.optimize_after)
84
+ return kanen_main(argv)
85
+
86
+ def build_parser():
87
+ ap = argparse.ArgumentParser(prog="arjuna", description="Arjuna-OCR [Kn&En] — Kannada + English document OCR")
88
+ ap.add_argument("--version", action="store_true")
89
+ sub = ap.add_subparsers(dest="cmd")
90
+ common = lambda p: (p.add_argument("--config", default=None), p.add_argument("--provider", choices=["auto", "trt", "cuda", "cpu"], default=None),
91
+ p.add_argument("--quality", choices=["fast", "balanced", "exact"], default=None), p.add_argument("--no-autoconfig", action="store_true"))
92
+ d = sub.add_parser("doctor", help="hardware, providers, auto-configuration"); common(d); d.add_argument("--run", action="store_true", help="also OCR one bundled page"); d.set_defaults(fn=cmd_doctor)
93
+ w = sub.add_parser("warm", help="build + validate the TensorRT engines, then exit"); common(w); w.add_argument("--timeout", type=int, default=1800); w.set_defaults(fn=cmd_warm)
94
+ b = sub.add_parser("bench", help="measured pages/s on the bundled pages"); common(b); b.add_argument("--repeat", type=int, default=4); b.add_argument("--timeout", type=int, default=1800); b.set_defaults(fn=cmd_bench)
95
+ s = sub.add_parser("serve", help="HTTP server"); common(s); s.add_argument("--host", default="0.0.0.0"); s.add_argument("--port", type=int, default=8080)
96
+ s.add_argument("--workers", type=int, default=None); s.add_argument("--layout-mode", choices=["always", "auto", "never"], default=None); s.set_defaults(fn=cmd_serve)
97
+ o = sub.add_parser("ocr", help="OCR files / directories / globs / PDFs"); common(o)
98
+ o.add_argument("inputs", nargs="+"); o.add_argument("--out-dir", "-o", default="arjuna_out"); o.add_argument("--format", "-f", default="json")
99
+ o.add_argument("--page-batch", type=int, default=None); o.add_argument("--workers", type=int, default=None); o.add_argument("--json-lines", action="store_true")
100
+ o.add_argument("--quiet", "-q", action="store_true"); o.add_argument("--optimize-after", type=int, default=8, help="build TensorRT engines when a job has at least this many pages")
101
+ o.set_defaults(fn=cmd_ocr)
102
+ return ap
103
+
104
+ def main(argv=None):
105
+ a = build_parser().parse_args(argv)
106
+ if a.version:
107
+ from . import __version__; print(__version__); return 0
108
+ if not getattr(a, "fn", None): build_parser().print_help(); return 0
109
+ return a.fn(a)
110
+
111
+ if __name__ == "__main__": sys.exit(main())
hi-en/kanen_infer/assemble.py ADDED
@@ -0,0 +1,505 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Assembler: layout blocks (Arjuna layout v12) + text-line polygons + recognised text -> kanen-ocr/1.0 JSON.
2
+ Pure geometry, no model. Also: Markdown / plain-text renderers."""
3
+ import numpy as np
4
+ from .charset import script_of
5
+ import re
6
+ WEAK_CHAR = 0.50 # min-char confidence below which a line is flagged weak_char (calibrated on 3,478 dev lines: < 0.5 -> 65 % exact)
7
+ _MIX_RE = re.compile(r"(?=\S*[A-Za-z])(?=\S*[\u0900-\u097F])\S+") # token with both a Latin letter and a Devanagari letter (Hindi package)
8
+
9
+ TYPE_MAP = {"doc_title": "title", "paragraph_title": "section_title", "text": "text", "abstract": "text", "content": "text", "reference": "text",
10
+ "reference_content": "text", "aside_text": "text", "vertical_text": "text", "footnote": "footnote", "vision_footnote": "footnote",
11
+ "header": "header", "footer": "footer", "number": "page_number", "table": "table", "image": "figure", "chart": "figure",
12
+ "figure_title": "figure_caption", "seal": "seal", "display_formula": "formula", "inline_formula": "formula", "formula_number": "formula",
13
+ "algorithm": "text", "header_image": "figure", "footer_image": "figure",
14
+ "title": "title", "figure": "figure", "id_panel": "id_panel", "handwriting": "handwriting", # v1.2 layout classes (text/table/seal/header/footer map 1:1 above)
15
+ # v15 layout adds six classes v1.2 could not express. `barcode` is the important one: without it an
16
+ # e-stamp panel can only be called `figure`, and suppressing that figure deleted 119 lines of real
17
+ # text across 87 pages. A barcode carries no readable text, so suppressing IT costs nothing.
18
+ "section_header": "section_title", "list_item": "text", "caption": "figure_caption", "barcode": "barcode"}
19
+
20
+ def _bbox(poly): p = np.asarray(poly, np.float32); return [float(p[:, 0].min()), float(p[:, 1].min()), float(p[:, 0].max()), float(p[:, 1].max())]
21
+
22
+ def _overlap_frac(line_bb, block_bb):
23
+ ix = max(0, min(line_bb[2], block_bb[2]) - max(line_bb[0], block_bb[0])); iy = max(0, min(line_bb[3], block_bb[3]) - max(line_bb[1], block_bb[1]))
24
+ return ix * iy / max((line_bb[2] - line_bb[0]) * (line_bb[3] - line_bb[1]), 1e-6)
25
+
26
+ def merge_line_fragments(lines, y_tol=0.5, x_gap=1.5):
27
+ """Merge horizontally adjacent boxes on the same baseline (detector sometimes splits a line at wide gaps).
28
+ lines: list of dict(bbox, polygon, text, conf). Returns merged list."""
29
+ lines = sorted(lines, key=lambda l: (l["bbox"][1], l["bbox"][0])); out = []
30
+ for l in lines:
31
+ merged = False
32
+ for m in out:
33
+ h = min(m["bbox"][3] - m["bbox"][1], l["bbox"][3] - l["bbox"][1])
34
+ same_row = abs((m["bbox"][1] + m["bbox"][3]) / 2 - (l["bbox"][1] + l["bbox"][3]) / 2) < y_tol * h
35
+ gap = l["bbox"][0] - m["bbox"][2]
36
+ if same_row and -0.2 * h < gap < x_gap * h:
37
+ m["bbox"] = [min(m["bbox"][0], l["bbox"][0]), min(m["bbox"][1], l["bbox"][1]), max(m["bbox"][2], l["bbox"][2]), max(m["bbox"][3], l["bbox"][3])]
38
+ m["text"] = (m["text"] + " " + l["text"]).strip(); m["confidence"] = min(m["confidence"], l["confidence"]); m["min_conf"] = min(m.get("min_conf", 1), l.get("min_conf", 1)); m["polygon"] = None; merged = True; break
39
+ if not merged: out.append(dict(l))
40
+ return out
41
+
42
+ def xy_cut_order(blocks, page_w, full_frac=0.60):
43
+ """Reading order: furniture, then columns top-to-bottom, with full-width blocks as band separators.
44
+
45
+ The previous version disabled column ordering whenever ANY block was neither wholly left nor wholly
46
+ right. On a two-column book page the centred running header ("संक्षिप्त देवीभागवत", 94 px of a 446 px
47
+ page) satisfies that, so column ordering switched off and the fallback `sort by (y, x)` interleaved the
48
+ two columns line by line -- the page came back unreadable even though every line was recognised.
49
+
50
+ A block is only a band separator if it is genuinely wide (>= `full_frac` of the page). A narrow block
51
+ that straddles the midline belongs to the column its CENTRE falls in. Headers and footers are page
52
+ furniture: they are lifted out and placed first and last rather than competing with the body.
53
+ """
54
+ if not blocks: return []
55
+ y = lambda b: b["bbox"][1]
56
+ cx = lambda b: (b["bbox"][0] + b["bbox"][2]) / 2
57
+ wd = lambda b: b["bbox"][2] - b["bbox"][0]
58
+ hdr = [b for b in blocks if b["type"] == "header"]
59
+ ftr = [b for b in blocks if b["type"] in ("footer", "page_number")]
60
+ body = [b for b in blocks if b not in hdr and b not in ftr]
61
+ full = [b for b in body if wd(b) >= full_frac * page_w]
62
+ cols = [b for b in body if b not in full]
63
+ mid = page_w / 2
64
+ left = [b for b in cols if cx(b) < mid]
65
+ right = [b for b in cols if cx(b) >= mid]
66
+ if len(left) >= 2 and len(right) >= 2:
67
+ out, prev = [], -1e18
68
+ for fb in sorted(full, key=y) + [None]: # each full-width block closes a band
69
+ lim = y(fb) if fb is not None else 1e18
70
+ out += sorted([b for b in left if prev <= y(b) < lim], key=y)
71
+ out += sorted([b for b in right if prev <= y(b) < lim], key=y)
72
+ if fb is not None: out.append(fb); prev = y(fb)
73
+ body_order = out
74
+ else:
75
+ body_order = sorted(body, key=lambda b: (round(y(b) / 20), b["bbox"][0]))
76
+ return sorted(hdr, key=y) + body_order + sorted(ftr, key=y)
77
+
78
+ def rows_of(lines, tol=0.5):
79
+ """Cluster line boxes into visual rows (y-centre within tol x the smaller height), left-to-right inside a row."""
80
+ ls = sorted(lines, key=lambda l: (l["bbox"][1] + l["bbox"][3]) / 2); rows = []
81
+ for l in ls:
82
+ yc = (l["bbox"][1] + l["bbox"][3]) / 2; h = l["bbox"][3] - l["bbox"][1]
83
+ if rows:
84
+ r = rows[-1]; ryc = np.mean([(m["bbox"][1] + m["bbox"][3]) / 2 for m in r]); rh = np.median([m["bbox"][3] - m["bbox"][1] for m in r])
85
+ if abs(yc - ryc) < tol * min(h, rh): r.append(l); continue
86
+ rows.append([l])
87
+ return [sorted(r, key=lambda l: l["bbox"][0]) for r in rows]
88
+
89
+ def split_row_by_gap(r, max_gap_h=2.5):
90
+ """Split a visual row into runs where the horizontal gap between neighbours exceeds max_gap_h x line height: two
91
+ columns (Kannada | English on ID cards, label | value pairs far apart) must stay separate lines; fragments of one
92
+ justified/spaced line (gaps of a few glyph widths) still join. Geometry only."""
93
+ r = sorted(r, key=lambda l: l["bbox"][0]); runs = [[r[0]]]
94
+ for l in r[1:]:
95
+ prev = runs[-1][-1]; h = max(8.0, min(prev["bbox"][3] - prev["bbox"][1], l["bbox"][3] - l["bbox"][1]))
96
+ if l["bbox"][0] - prev["bbox"][2] > max_gap_h * h: runs.append([l])
97
+ else: runs[-1].append(l)
98
+ return runs
99
+
100
+ def join_row(r):
101
+ """One text line per visual row: fragments of a justified/spaced line are joined with single spaces."""
102
+ if len(r) == 1: return dict(r[0])
103
+ return {"bbox": [min(l["bbox"][0] for l in r), min(l["bbox"][1] for l in r), max(l["bbox"][2] for l in r), max(l["bbox"][3] for l in r)],
104
+ "polygon": None, "text": " ".join(l["text"] for l in r).strip(), "confidence": min(l["confidence"] for l in r), "min_conf": min(l.get("min_conf", 1) for l in r)}
105
+
106
+ def _runs(mask1d, min_gap=3):
107
+ idx = np.flatnonzero(mask1d)
108
+ if idx.size == 0: return []
109
+ out = []; start = idx[0]; prev = idx[0]
110
+ for i in idx[1:]:
111
+ if i - prev > min_gap: out.append((start + prev) / 2); start = i
112
+ prev = i
113
+ out.append((start + prev) / 2); return out
114
+
115
+ def _rule_positions(mask, axis, min_len):
116
+ """Centre coordinate of every connected component of a rule mask longer than min_len along `axis` (robust to residual tilt)."""
117
+ import cv2
118
+ n, _, st, cen = cv2.connectedComponentsWithStats(mask, connectivity=8)
119
+ pos = sorted(cen[i][1 - axis] for i in range(1, n) if st[i][cv2.CC_STAT_WIDTH if axis == 0 else cv2.CC_STAT_HEIGHT] >= min_len)
120
+ out = []
121
+ for p in pos:
122
+ if out and p - out[-1] < 6: out[-1] = (out[-1] + p) / 2
123
+ else: out.append(p)
124
+ return out
125
+
126
+ def _merge_continuations(rows):
127
+ """A row whose cells all sit under occupied columns of the previous row and none in its first column is a wrapped cell."""
128
+ out = []
129
+ for r in rows:
130
+ ne = [c for c in r["cells"] if c["text"]]
131
+ if out and ne:
132
+ prev = out[-1]; pne = [c for c in prev["cells"] if c["text"]]
133
+ first = min(c["col"] for c in prev["cells"]) if prev["cells"] else 0
134
+ if all(c["col"] > first for c in ne) and all(any(p["col"] == c["col"] for p in pne) for c in ne):
135
+ for c in ne:
136
+ p = next(p for p in pne if p["col"] == c["col"]); p["text"] = (p["text"] + " " + c["text"]).strip()
137
+ p["bbox"] = [min(p["bbox"][0], c["bbox"][0]), min(p["bbox"][1], c["bbox"][1]), max(p["bbox"][2], c["bbox"][2]), max(p["bbox"][3], c["bbox"][3])]
138
+ continue
139
+ out.append(r)
140
+ return out
141
+
142
+ def ruled_grid(gray, bbox, lines):
143
+ """Table structure from printed ruling lines (morphological opening + connected components). Horizontal rules give row
144
+ bands; inside a band, vertical rules give cells - or, when a band has no vertical rules (aligned-column forms), each
145
+ visual row of the band becomes a table row whose cells are the separate line boxes. Returns
146
+ rows=[{cells:[{col,text,bbox,confidence}], ruled}] or None when the region has no horizontal rules. Pure geometry."""
147
+ import cv2
148
+ x0, y0, x1, y1 = [int(round(v)) for v in bbox]; reg = gray[max(0, y0):y1, max(0, x0):x1]
149
+ if reg.size == 0 or reg.shape[0] < 20 or reg.shape[1] < 20: return None
150
+ H, W = reg.shape; bw = cv2.adaptiveThreshold(reg, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 31, 9)
151
+ hor = cv2.morphologyEx(bw, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_RECT, (max(20, W // 30), 1)))
152
+ hor = cv2.dilate(hor, cv2.getStructuringElement(cv2.MORPH_RECT, (max(20, W // 8), 1))) # bridge faint / dashed rules (text-derived runs are rejected below)
153
+ loc = [(l["bbox"][0] - x0, l["bbox"][1] - y0, l["bbox"][2] - x0, l["bbox"][3] - y0) for l in lines]
154
+ def through_text(y): return any(b + 0.1 * (d - b) < y < d - 0.1 * (d - b) for a, b, c, d in loc) # a rule never crosses the middle of a text line
155
+ def through_text_x(x, ia, ib): return any(a + 0.1 * (c - a) < x < c - 0.1 * (c - a) and b < ib and d > ia for a, b, c, d in loc)
156
+ hr = [y for y in _rule_positions(hor, 0, 0.25 * W) if 0.01 * H + 4 < y < H - 0.01 * H - 4 and not through_text(y)]
157
+ if not hr: return None
158
+ ys = [0.0] + hr + [float(H)]; bands = []
159
+ for a, b in zip(ys[:-1], ys[1:]):
160
+ ia, ib = int(a), int(b)
161
+ if ib - ia < 8: continue
162
+ ver = cv2.morphologyEx(bw[ia:ib], cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_RECT, (1, max(8, int((ib - ia) * 0.4)))))
163
+ ver = cv2.dilate(ver, cv2.getStructuringElement(cv2.MORPH_RECT, (1, max(6, int((ib - ia) * 0.25)))))
164
+ vr = [x for x in _rule_positions(ver, 1, 0.85 * (ib - ia)) if 0.03 * W < x < 0.97 * W and not through_text_x(x, ia, ib)] # cell borders span the band, never cross text; table border excluded
165
+ bands.append((ia, ib, vr))
166
+ allx = sorted(x for _, _, vr in bands for x in vr); G = []
167
+ for x in allx:
168
+ if not G or x - G[-1][-1] > 12: G.append([x])
169
+ else: G[-1].append(x)
170
+ G = [float(np.mean(g)) for g in G]
171
+ def gcol(x): return sum(1 for g in G if g < x + 6)
172
+ rows = []
173
+ for ia, ib, vr in bands:
174
+ inband = [l for l in lines if ia <= (l["bbox"][1] + l["bbox"][3]) / 2 - y0 < ib]
175
+ if not inband: continue
176
+ if vr: # ruled cells: everything between two vertical rules is one cell (a wrapped cell keeps its lines together);
177
+ # the global rule set is used so a band whose faint rule was missed still gets the table's columns
178
+ xs = [0.0] + G + [float(W)]
179
+ percol = [[l for l in inband if ca <= (l["bbox"][0] + l["bbox"][2]) / 2 - x0 < cb] for ca, cb in zip(xs[:-1], xs[1:])]
180
+ multi = sum(1 for inc in percol if len(rows_of(inc)) > 1)
181
+ # several columns stacked -> the band holds several table rows whose separating rule was not printed/found
182
+ groups = rows_of(inband) if multi >= 2 else [inband]
183
+ for g in groups:
184
+ cells = []
185
+ for ca, cb in zip(xs[:-1], xs[1:]):
186
+ inc = [l for l in g if ca <= (l["bbox"][0] + l["bbox"][2]) / 2 - x0 < cb]
187
+ if not inc: cells.append({"col": gcol(ca), "text": "", "bbox": None, "confidence": None}); continue
188
+ cells.append({"col": gcol(ca), "text": " ".join(join_row(r)["text"] for r in rows_of(inc)), "bbox": [min(l["bbox"][0] for l in inc), min(l["bbox"][1] for l in inc), max(l["bbox"][2] for l in inc), max(l["bbox"][3] for l in inc)], "confidence": min(l["confidence"] for l in inc)})
189
+ rows.append({"cells": cells, "ruled": True})
190
+ if len(groups) > 1: rows[-len(groups):] = _merge_continuations(rows[-len(groups):])
191
+ else: # no vertical rules in this band: each visual row is a table row, each line box a cell
192
+ for r in rows_of(inband):
193
+ rows.append({"cells": [{"col": (gcol(l["bbox"][0] - x0) if G else ci), "text": l["text"], "bbox": l["bbox"], "confidence": l["confidence"]} for ci, l in enumerate(r)], "ruled": False})
194
+ return rows if rows else None
195
+
196
+ def group_table(lines):
197
+ """Unruled tables: rows by y-clustering, columns by clustering cell x-starts; a row that only continues cells of the
198
+ previous row (no cell in the first column, every cell under an occupied column) is a wrapped cell -> merged upward."""
199
+ if not lines: return []
200
+ rows = rows_of(lines)
201
+ spans = sorted(((l["bbox"][0], l["bbox"][2]) for r in rows for l in r)); cols = []
202
+ for a, b in spans: # columns = groups of horizontally overlapping cells (works for left/centre/right alignment)
203
+ if cols and a < cols[-1][1] - 4: cols[-1][1] = max(cols[-1][1], b)
204
+ else: cols.append([a, b])
205
+ def col_of(l):
206
+ xc = (l["bbox"][0] + l["bbox"][2]) / 2
207
+ return next((i for i, (a, b) in enumerate(cols) if a - 4 <= xc <= b + 4), int(np.argmin([abs(xc - (a + b) / 2) for a, b in cols])) if cols else 0)
208
+ return _merge_continuations([{"cells": [{"bbox": l["bbox"], "text": l["text"], "confidence": l["confidence"], "col": col_of(l)} for l in r], "ruled": False} for r in rows])
209
+
210
+
211
+ def merge_split_blocks(blocks, x_frac=0.6, y_frac=0.25, page_w=None, full_frac=0.60):
212
+ """Merge SAME-CLASS layout blocks that cover the same region.
213
+
214
+ The layout model sometimes cuts one continuous paragraph into several boxes that INTERLEAVE vertically.
215
+ Each box is individually plausible, so nothing downstream notices; but the assembler emits block by block,
216
+ so the lines come out grouped by box instead of in reading order. Observed on a plain Hindi prose page:
217
+ three boxes over one paragraph, all 14 lines recognised correctly and every one of them in the wrong place.
218
+
219
+ Merging is deliberately conservative, because the failure mode of being too eager is far worse -- it would
220
+ glue the columns of a two-column page together:
221
+
222
+ * same `type` only -- a seal inside a text region must stay its own block
223
+ * the boxes must overlap on the X axis by `x_frac` of the NARROWER box. Side-by-side columns barely
224
+ overlap horizontally, so they never merge.
225
+ * they must also overlap on the Y axis by `y_frac` of the SHORTER box. Stacked paragraphs in one column
226
+ do not overlap vertically, so they never merge either.
227
+
228
+ Only both conditions together describe "two boxes drawn over the same block of text". Lines are re-sorted
229
+ by geometry inside the merged box by the existing `rows_of`, so reading order is restored.
230
+
231
+ The "columns never merge" argument above holds for a PAIR of columns, but not for a chain. On a
232
+ two-column Hindi book page one spurious low-confidence box straddled both columns at the foot; it
233
+ overlapped the left column enough to absorb it, the union then reached the right column, and both columns
234
+ ended up in a single 31-line block. So merging may never turn column-width boxes into a full-width one:
235
+ if neither input already spans `full_frac` of the page, the result may not either. `page_w=None` keeps the
236
+ old behaviour for callers that do not pass it.
237
+ """
238
+ def ov(a0, a1, b0, b1):
239
+ return max(0.0, min(a1, b1) - max(a0, b0))
240
+ out, used = [], [False] * len(blocks)
241
+ for i, b in enumerate(blocks):
242
+ if used[i]: continue
243
+ cur = dict(b); cur["bbox"] = list(b["bbox"]); merged = 0
244
+ changed = True
245
+ while changed: # keep absorbing: three-way splits need more than one pass
246
+ changed = False
247
+ for j, c in enumerate(blocks):
248
+ if used[j] or j == i or c["type"] != cur["type"]: continue
249
+ ax0, ay0, ax1, ay1 = cur["bbox"]; bx0, by0, bx1, by1 = c["bbox"]
250
+ wx = ov(ax0, ax1, bx0, bx1) / max(1.0, min(ax1 - ax0, bx1 - bx0))
251
+ wy = ov(ay0, ay1, by0, by1) / max(1.0, min(ay1 - ay0, by1 - by0))
252
+ if wx < x_frac or wy < y_frac: continue
253
+ nb = [min(ax0, bx0), min(ay0, by0), max(ax1, bx1), max(ay1, by1)]
254
+ if page_w:
255
+ # A full-width block and a column-width block are never two halves of one paragraph.
256
+ # The spurious box that swallowed both columns was ITSELF already full-width (409 px of
257
+ # 446), so a rule about the merged width could not catch it -- the mismatch must be
258
+ # tested on the INPUTS.
259
+ lim = full_frac * page_w
260
+ if ((ax1 - ax0) >= lim) != ((bx1 - bx0) >= lim): continue
261
+ if True:
262
+ cur["bbox"] = nb
263
+ cur["confidence"] = max(cur.get("confidence", 0), c.get("confidence", 0))
264
+ cur["order"] = min(x for x in (cur.get("order"), c.get("order")) if x is not None) \
265
+ if (cur.get("order") is not None or c.get("order") is not None) else None
266
+ used[j] = True; merged += 1; changed = True
267
+ used[i] = True
268
+ if merged: cur["merged_from"] = merged + 1
269
+ out.append(cur)
270
+ return out
271
+
272
+ VISUAL_TYPES = ("figure", "seal", "id_panel", "barcode")
273
+
274
+ def _looks_grid(b, min_rows=2, min_cols=2):
275
+ """Do this block's lines tile into >= min_rows x min_cols cells? Clusters line x-centres into column
276
+ groups and y-centres into row groups; a paragraph gives 1 column, a table gives 2+."""
277
+ ls = b.get("lines") or []
278
+ if len(ls) < min_rows * min_cols: return False
279
+ import numpy as _np
280
+ cx = sorted(((l["bbox"][0] + l["bbox"][2]) / 2) for l in ls)
281
+ w = (b["bbox"][2] - b["bbox"][0]) or 1.0
282
+ cols = 1
283
+ for a, c in zip(cx, cx[1:]):
284
+ if c - a > 0.18 * w: cols += 1
285
+ yc = sorted(((l["bbox"][1] + l["bbox"][3]) / 2) for l in ls)
286
+ h = _np.median([l["bbox"][3] - l["bbox"][1] for l in ls]) or 1.0
287
+ rows = 1
288
+ for a, c in zip(yc, yc[1:]):
289
+ if c - a > 0.6 * h: rows += 1
290
+ return cols >= min_cols and rows >= min_rows
291
+
292
+ def retype_mislabelled_visuals(blocks, min_lines):
293
+ """A `figure`/`seal`/`id_panel` holding many READABLE lines is a mislabelled panel, not a picture.
294
+
295
+ The config already encodes exactly this judgement for suppression (`suppress_max_lines`: "a figure
296
+ holding more than N readable lines is a mislabelled panel"). It was never applied to the block's TYPE,
297
+ so a ruled table detected as `figure` kept being ordered and exported as a picture: observed on an
298
+ English-Kannada word table where the layout model returned ONE `figure` (conf 0.70, 58 % of the page)
299
+ containing 28 readable lines, and 28 of 34 lines carried the `in_figure` flag -- the model's own signal
300
+ that the type was wrong. Keyed on that signal, not on any one page.
301
+ """
302
+ if not min_lines: return blocks
303
+ for b in blocks:
304
+ if b["type"] in VISUAL_TYPES and len([l for l in (b.get("lines") or []) if (l.get("text") or "").strip()]) >= min_lines:
305
+ b["retyped_from"] = b["type"]
306
+ b["type"] = "table" if _looks_grid(b) else "text"
307
+ return blocks
308
+
309
+ def merge_row_structures(blocks, lines, row_cross_frac=0.20, y_frac=0.50, min_rows=3):
310
+ """Side-by-side blocks that are really the two halves of ONE table are read ROW-WISE, not column-wise.
311
+
312
+ A label/value table ("1. Property No." | ": A-50, Third Floor") is often returned as two blocks, one per
313
+ column. Read block by block that emits every label, then every value -- the rows are destroyed even
314
+ though every line is correct.
315
+
316
+ The discriminator is the GUTTER. Two genuine columns have a clean vertical channel that no line crosses
317
+ anywhere down the page. In a table the channel is crossed constantly: the detector merges label and
318
+ value into one box whenever the gap is small ("3. Segment/Block Name & Code : Southcity-I"), and a
319
+ numbering strip spans the whole width. So: count the lines sharing the two blocks' y-band that cross the
320
+ gutter; if more than `row_cross_frac` of them do, the gutter is not real and the blocks are one region.
321
+
322
+ ONE crossing line is not enough -- a two-column prose page had exactly one (a detector error gluing a
323
+ line across the column rule), 1.4 % of its lines, while the deed table crosses on ~30 %.
324
+
325
+ Merging is all that is needed: `rows_of()` already clusters lines into visual rows and orders them
326
+ left-to-right inside a row, so the merged block comes out label, value, label, value. No line is
327
+ dropped or moved to the end.
328
+ """
329
+ if not blocks or row_cross_frac <= 0: return blocks
330
+ out = list(blocks); changed = True
331
+ while changed:
332
+ changed = False
333
+ for i in range(len(out)):
334
+ for j in range(i + 1, len(out)):
335
+ a, b = out[i], out[j]
336
+ if a["type"] != b["type"] or a["type"] == "table": continue
337
+ if len(a.get("lines") or []) < min_rows or len(b.get("lines") or []) < min_rows: continue
338
+ ax0, ay0, ax1, ay1 = a["bbox"]; bx0, by0, bx1, by1 = b["bbox"]
339
+ if ax0 > bx0: (ax0, ay0, ax1, ay1), (bx0, by0, bx1, by1) = (bx0, by0, bx1, by1), (ax0, ay0, ax1, ay1)
340
+ gap0, gap1 = ax1, bx0
341
+ if gap1 <= gap0: continue # they overlap in x: not a gutter
342
+ oy = min(ay1, by1) - max(ay0, by0)
343
+ if oy < y_frac * min(ay1 - ay0, by1 - by0): continue # not the same horizontal band
344
+ band0, band1 = max(ay0, by0), min(ay1, by1)
345
+ inband = [l for l in lines if l["bbox"][3] > band0 and l["bbox"][1] < band1]
346
+ if len(inband) < 4: continue
347
+ cross = sum(1 for l in inband if l["bbox"][0] < gap0 and l["bbox"][2] > gap1)
348
+ # Second trigger: a whole BLOCK that spans the gutter across most of the shared band.
349
+ # On a deed the numbering strip ("2." "3." "4.") is returned as its own block covering the
350
+ # full table width, which proves the two halves belong to one structure even when the gutter
351
+ # itself is perfectly clean -- blind00_p0000 has 0 of 33 lines crossing yet is one table.
352
+ # A stray glued line cannot trigger this: on the two-column prose page the spanning orphan
353
+ # covers 22 px of a 560 px band.
354
+ # A spanning block only counts as a numbering strip if it has SEVERAL lines. A single glued
355
+ # line also spans the gutter -- the two-column prose page has exactly one, and with a short
356
+ # shared band it satisfied the 50 % rule and interleaved the columns. Requiring >= min_rows
357
+ # lines in the strip AND in both halves separates "one table" from "one bad line".
358
+ span = any(c is not a and c is not b and c["bbox"][0] < gap0 and c["bbox"][2] > gap1
359
+ and len(c.get("lines") or []) >= min_rows
360
+ and (min(c["bbox"][3], band1) - max(c["bbox"][1], band0)) >= 0.5 * (band1 - band0)
361
+ for c in out)
362
+ if not span and cross / len(inband) < row_cross_frac: continue # real gutter -> genuine columns
363
+ # The merge is only safe if the assembler's OWN row splitter will keep the two halves apart.
364
+ # `split_row_by_gap` separates a visual row when the horizontal gap exceeds 2.5x the line
365
+ # height; below that, `join_row` CONCATENATES the two sides. On a two-column prose page whose
366
+ # columns nearly touch that produced lines like
367
+ # "रुरु नामक जानवर उस नारकी जीवके जो पुरुष राजा अथवा राजकर्मचारी होकर"
368
+ # -- two unrelated sentences in one line, with every word still present, so a "no word lost"
369
+ # check cannot see it. Require the gutter to clear the same threshold.
370
+ lh = np.median([l["bbox"][3] - l["bbox"][1] for l in inband]) if inband else 0.0
371
+ if (gap1 - gap0) < 2.5 * lh: continue
372
+ a["bbox"] = [min(a["bbox"][0], b["bbox"][0]), min(a["bbox"][1], b["bbox"][1]),
373
+ max(a["bbox"][2], b["bbox"][2]), max(a["bbox"][3], b["bbox"][3])]
374
+ a["lines"] = (a.get("lines") or []) + (b.get("lines") or [])
375
+ a["row_merged"] = a.get("row_merged", 1) + 1
376
+ out.pop(j); changed = True; break
377
+ if changed: break
378
+ return out
379
+
380
+ def assemble(page_w, page_h, layout_blocks, det_boxes, texts, model_ids, timing=None, page_index=0, dpi=None,
381
+ with_polygons=True, img=None, suppress_text_in=(), suppress_min_conf=0.0, low_res_px=0.0, retype_visual_min_lines=4, row_cross_frac=0.20,
382
+ suppress_max_area=1.0, suppress_max_lines=0, merge_split_blocks_x=0.0, merge_split_blocks_y=0.25):
383
+ """suppress_text_in: block types whose recognised text is withheld from `doc["text"]` and replaced by a
384
+ `*[type]*` marker, the lines being kept in `blocks` with their flags.
385
+
386
+ Default is empty, so behaviour is unchanged unless a config asks for it. Why it exists: a print recognizer
387
+ fed a signature or a seal does not abstain — it emits confident nonsense. An end-to-end review of the Hindi
388
+ pair found fabricated phone numbers on signature blocks, with confidence high enough that the `check` flag
389
+ (which fires on 41.5 % of lines) could not be used to triage them. The layout model already identifies those
390
+ blocks; this lets a caller act on that instead of reading invented text out of `doc["text"]`.
391
+ Set via `assemble.suppress_text_in` in the config; the Hindi overlay uses
392
+ ["handwriting", "seal", "figure"].
393
+
394
+ THREE GUARDS, because unconditional suppression deletes far more signal than noise. Measured over 87
395
+ pages of registration records: 160 lines were withheld, of which **119 looked like real text** --
396
+ 81 in `figure`, 20 in `seal`, only 18 in `handwriting`. Examples lost: certificate and GRN numbers,
397
+ `दिनांक:23-04-2020`, `उप/सयुंक्त पंजीयन अधिकारी( वजीराबाद )`. Root cause: an e-stamp panel holds two
398
+ emblems, a barcode and a QR code, so the layout model boxes the WHOLE panel as `figure` (conf 0.31-0.38,
399
+ up to 35 % of the page) and every line inside it is dropped.
400
+
401
+ suppress_min_conf do not suppress below this layout confidence -- a marginal call should keep text
402
+ suppress_max_area do not suppress a block larger than this fraction of the page
403
+ suppress_max_lines do not suppress a block holding more than this many recognised lines; a `figure`
404
+ full of readable text is a mislabelled panel, not a picture
405
+
406
+ All default to OFF so existing callers are unchanged."""
407
+ lines = []
408
+ for (poly, dscore), (text, conf) in zip(det_boxes, texts):
409
+ if not text.strip(): continue
410
+ lines.append({"bbox": _bbox(poly), "polygon": [[float(x), float(y)] for x, y in poly] if with_polygons else None, "text": text, "confidence": round(float(conf), 4),
411
+ "min_conf": round(float(getattr(conf, "min_char", None) if getattr(conf, "min_char", None) is not None else conf), 4)})
412
+ blocks = []
413
+ for i, b in enumerate(layout_blocks):
414
+ blocks.append({"id": f"b{i}", "type": TYPE_MAP.get(b["type"], "other"), "raw_type": b["type"], "confidence": round(float(b["score"]), 4), "bbox": [round(v, 1) for v in b["bbox"]], "order": b.get("order"), "lines": []})
415
+ if merge_split_blocks_x > 0:
416
+ blocks = merge_split_blocks(blocks, merge_split_blocks_x, merge_split_blocks_y, page_w=page_w)
417
+ for k, b in enumerate(blocks): b["id"] = f"b{k}"
418
+ # assign lines to best-overlapping block; unassigned lines become their own text blocks
419
+ orphans = []
420
+ area = lambda bb: max(1.0, (bb[2] - bb[0]) * (bb[3] - bb[1]))
421
+ for l in lines:
422
+ best, bi = 0.0, -1
423
+ for i, b in enumerate(blocks):
424
+ f = _overlap_frac(l["bbox"], b["bbox"])
425
+ # nested blocks (v1.2 layout: a handwriting / seal / id_panel box inside a text region): on a near-tie the
426
+ # smaller, more specific block wins, so signature fragments are not glued to the surrounding text rows
427
+ if f > best + 0.05 or (f >= best - 0.05 and bi >= 0 and f >= 0.5 and area(b["bbox"]) < area(blocks[bi]["bbox"])): best, bi = f, i
428
+ if best >= 0.5: blocks[bi]["lines"].append(l)
429
+ else: orphans.append(l)
430
+ for l in orphans:
431
+ blocks.append({"id": f"b{len(blocks)}", "type": "text", "raw_type": "orphan_line", "confidence": l["confidence"], "bbox": [round(v, 1) for v in l["bbox"]], "order": None, "lines": [l]})
432
+ blocks = [b for b in blocks if b["lines"] or b["type"] in ("figure", "seal", "table", "id_panel", "handwriting", "barcode")]
433
+ blocks = retype_mislabelled_visuals(blocks, retype_visual_min_lines)
434
+ blocks = merge_row_structures(blocks, lines, row_cross_frac)
435
+ # order: native rank where available, else xy-cut; orphans placed by position among ordered blocks
436
+ ranked = [b for b in blocks if b["order"] is not None]; unranked = [b for b in blocks if b["order"] is None]
437
+ ordered = sorted(ranked, key=lambda b: b["order"])
438
+ for b in unranked:
439
+ pos = next((k for k, o in enumerate(ordered) if o["bbox"][1] > b["bbox"][1] and o["bbox"][0] <= b["bbox"][2]), len(ordered)); ordered.insert(pos, b)
440
+ if not ranked: ordered = xy_cut_order(blocks, page_w)
441
+ full = []
442
+ gray = None
443
+ if img is not None:
444
+ import cv2; gray = img if img.ndim == 2 else cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
445
+ heights = [l["bbox"][3] - l["bbox"][1] for b in ordered for l in b["lines"]]; med_h = float(np.median(heights)) if heights else 0.0
446
+ for k, b in enumerate(ordered):
447
+ b["order"] = k
448
+ if b["type"] == "table":
449
+ # table cells stay separate boxes; structure from printed rules when present, else from alignment
450
+ b["lines"] = [l for r in rows_of(b["lines"]) for l in r]
451
+ rows = ruled_grid(gray, b["bbox"], b["lines"]) if gray is not None else None
452
+ b["rows"] = rows or group_table(b["lines"]); b["ruled"] = bool(rows)
453
+ else:
454
+ b["lines"] = [join_row(run) for r in rows_of(b["lines"]) for run in split_row_by_gap(r)] # one text line per visual row run (fragments of a spaced line re-joined; wide-gap columns kept apart)
455
+ for li, l in enumerate(b["lines"]):
456
+ l["id"] = f"{b['id']}.l{li}"; l["script"] = script_of(l["text"]); l["bbox"] = [round(v, 1) for v in l["bbox"]]
457
+ # review flags (never deletion): calibration on 5,478 labelled lines -> conf >= 0.99 is 97.7 % line-exact,
458
+ # 0.95-0.98 only 83.5 %; lines inside seal/figure/header blocks are mostly stamp/handwriting fragments
459
+ fl = []
460
+ if low_res_px and (l["bbox"][3] - l["bbox"][1]) < low_res_px: fl.append("low_res")
461
+ if l["confidence"] < 0.90: fl.append("low_conf")
462
+ elif l["confidence"] < 0.98: fl.append("check")
463
+ if b["type"] in ("seal", "figure", "handwriting", "id_panel", "barcode"): fl.append("in_" + b["type"])
464
+ # review flags (script/character-repertoire checks); flags only, the text is never altered
465
+ if l.get("min_conf", 1.0) < WEAK_CHAR: fl.append("weak_char") # one very uncertain character (garbage lines usually have one)
466
+ if _MIX_RE.search(l["text"]): fl.append("script_mix") # Latin and Kannada letters inside one token (other-script garbage)
467
+ if med_h and (l["bbox"][3] - l["bbox"][1]) > 3.0 * med_h: fl.append("oversize") # stray ornament / drop-cap sized line
468
+ if fl: l["flags"] = fl
469
+ if b["type"] in suppress_text_in:
470
+ _area = ((b["bbox"][2] - b["bbox"][0]) * (b["bbox"][3] - b["bbox"][1])) / max(1.0, page_w * page_h)
471
+ _keep = ((b.get("confidence", 1.0) < suppress_min_conf) # layout was unsure
472
+ or (_area > suppress_max_area) # too big to be a figure
473
+ or (suppress_max_lines and len(b["lines"]) > suppress_max_lines)) # full of readable text
474
+ if _keep:
475
+ b["text_suppressed"] = False
476
+ b["suppression_overridden"] = round(_area, 4) # visible in the output, not silent
477
+ else:
478
+ # the lines stay in `blocks` with their flags; only the page text withholds them
479
+ b["text_suppressed"] = True
480
+ if b["lines"] or b["type"] in ("figure", "seal"): full.append(f"*[{b['type']}]*")
481
+ continue
482
+ if b["type"] == "table" and b["rows"]: block_text = "\n".join(" | ".join(c["text"] for c in r["cells"]) for r in b["rows"])
483
+ else: block_text = "\n".join(l["text"] for l in b["lines"])
484
+ if block_text.strip(): full.append(block_text)
485
+ return {"schema": "kanen-ocr/1.0", "model": model_ids, "page": {"index": page_index, "width": page_w, "height": page_h, "dpi": dpi, "rotation": 0},
486
+ "blocks": ordered, "text": "\n\n".join(full), "timing_ms": timing or {}}
487
+
488
+ def to_markdown(doc):
489
+ out = []
490
+ for b in doc["blocks"]:
491
+ t = b["type"]; txt = " ".join(l["text"] for l in b["lines"]).strip()
492
+ if t == "title": out.append(f"# {txt}")
493
+ elif t == "section_title": out.append(f"## {txt}")
494
+ elif t == "table" and b.get("rows"):
495
+ ncol = max((c["col"] for r in b["rows"] for c in r["cells"]), default=0) + 1
496
+ rows = []
497
+ for r in b["rows"]:
498
+ cells = [""] * ncol
499
+ for c in r["cells"]: cells[c["col"]] = (cells[c["col"]] + " " + c["text"]).strip()
500
+ rows.append("| " + " | ".join(x.replace("|", "\\|") for x in cells) + " |")
501
+ if rows: out.append(rows[0] + "\n|" + "---|" * ncol + "\n" + "\n".join(rows[1:]))
502
+ elif b.get("text_suppressed") or t in ("figure", "seal"): out.append(f"*[{t}]*")
503
+ elif t in ("header", "footer", "page_number"): out.append(f"<sub>{txt}</sub>")
504
+ elif txt: out.append(txt)
505
+ return "\n\n".join(out) + "\n"
hi-en/kanen_infer/batch_pipeline.py ADDED
@@ -0,0 +1,322 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Arjuna-OCR [Kn&En] batched throughput pipeline.
2
+ Organised for throughput instead of single-page latency (the API's `page()` runs the same code with one page):
3
+ * decode + canvas choice in a prefetch thread pool (next page batch is decoded while the GPU works on this one)
4
+ * detector runs ONCE per page at a canvas chosen from the page size (no 1280 -> re-detect search), batched across pages
5
+ * DB post-processing + perspective crops in worker threads
6
+ * recognizer sees the crops of the whole page batch in one width-bucketed call; orientation-180 fallback and the
7
+ confidence-gated second pass are pooled the same way
8
+ * layout model batched across pages
9
+ No output rewriting anywhere: every character comes from the recognizer. All tunables come from the package config.
10
+ """
11
+ import time, math, os
12
+ from concurrent.futures import ThreadPoolExecutor
13
+ import numpy as np, cv2
14
+ from .config import load_config, _FALLBACK
15
+ from .providers import Providers
16
+ from .pipeline_base import Detector, Layout, crop, MEAN, STD
17
+ from .assemble import assemble
18
+ from .geometry import rotate_page, deskew_angle, rotate_small
19
+
20
+ def _asm(cfg):
21
+ """assemble section of the config, tolerant of a plain dict or a Config object."""
22
+ return (cfg.get("assemble", {}) if hasattr(cfg, "get") else {}) or {}
23
+
24
+
25
+ def n_readable(res, conf=0.90, min_char=0.50, min_len=4):
26
+ """readable text mass = characters in lines that read cleanly (line conf and weakest character high, >= 4 chars so
27
+ stray '|' / '1' fragments do not vote). Majority vote for mixed-orientation pages (upright form + rotated header)."""
28
+ return sum(len(t) for t, c in res if len(t) >= min_len and float(c) >= conf and (getattr(c, "min_char", None) if getattr(c, "min_char", None) is not None else float(c)) >= min_char)
29
+
30
+ def orient_score(res):
31
+ """orientation evidence: mean line confidence + mean weakest-character confidence (upside-down text = confident garbage with one very weak frame)"""
32
+ if not res: return 0.0
33
+ return float(np.mean([float(c) for _, c in res]) + np.mean([getattr(c, "min_char", None) if getattr(c, "min_char", None) is not None else float(c) for _, c in res]))
34
+
35
+ CANVASES = tuple(_FALLBACK["detector"]["canvases"])
36
+ WIDTH_CLASSES = tuple(_FALLBACK["detector"]["width_classes"]) # canvas width as a fraction of the long side -> at most 5 ORT plans per canvas (no square padding)
37
+ INV_STD = (1.0 / STD).astype(np.float32); MEAN255 = tuple(float(m * 255) for m in MEAN) # same channel order as Detector (PaddleOCR applies the mean to the BGR image as read)
38
+ PAD_VAL = ((1.0 - MEAN) / STD).astype(np.float32) # normalised white, like Detector's white canvas
39
+
40
+ def choose_canvas(h, w, min_scale=0.9, canvases=CANVASES):
41
+ """Smallest fixed canvas that keeps the page at >= min_scale of its native size (150-DPI private-document scans -> 1920, A4@300 -> 2560)."""
42
+ m = max(h, w)
43
+ for c in canvases:
44
+ if c >= min_scale * m: return c
45
+ return canvases[-1]
46
+
47
+ def canvas_shape(h, w, min_scale=0.9, canvases=CANVASES, width_classes=WIDTH_CLASSES):
48
+ """(limit, canvas_h, canvas_w): long side = limit, short side snapped up to a width class (multiple of 32)."""
49
+ limit = choose_canvas(h, w, min_scale, canvases); r = min(1.0, limit / max(h, w)); nh, nw = h * r, w * r
50
+ if h >= w: cw = next(int(round(limit * f / 32)) * 32 for f in width_classes if limit * f >= nw); return limit, limit, cw
51
+ ch = next(int(round(limit * f / 32)) * 32 for f in width_classes if limit * f >= nh); return limit, ch, limit
52
+
53
+ def rotate_boxes(boxes, deg, w, h):
54
+ """Map detector polygons into the deskewed page frame (same rotation rotate_small applies to the image)."""
55
+ if not boxes: return boxes
56
+ M = cv2.getRotationMatrix2D((w / 2, h / 2), deg, 1.0)
57
+ return [(cv2.transform(b[None].astype(np.float32), M)[0], sc) for b, sc in boxes]
58
+
59
+ def _layout_id(cfg):
60
+ """Name of the layout model ACTUALLY loaded. This used to be the literal "arjuna-layout-v12",
61
+ so every output document was stamped v12 no matter which model the config pointed at."""
62
+ import os
63
+ stem = os.path.splitext(os.path.basename(str(cfg.path("models", "layout"))))[0]
64
+ return stem.replace("kanen_layout_", "arjuna-layout-").replace("arjuna_layout_", "arjuna-layout-")
65
+
66
+
67
+ class BatchOCR:
68
+ def __init__(self, rec=None, cfg=None, providers=None, det=None, lay=None, use_layout=None, layout_mode=None, workers=None, tta_conf=None, tta_cap=None, rec_bs=None, det_bs=None, det_size="small"):
69
+ """rec/det/lay: model objects to share (built from cfg when None). layout_mode: 'always' | 'auto' (only pages that look
70
+ like tables/forms) | 'never'. tta_cap: second-pass lines per page. Every default comes from the config."""
71
+ from .rec_onnx import KanEnRecognizer
72
+ self.cfg = cfg or getattr(rec, "cfg", None) or load_config(); self.prov = providers or getattr(rec, "prov", None) or Providers(self.cfg)
73
+ dc, lc, rc, sp, rt = self.cfg["detector"], self.cfg["layout"], self.cfg["recognizer"], self.cfg["second_pass"], self.cfg["runtime"]
74
+ self.layout_mode = layout_mode or lc.get("mode", "always")
75
+ if use_layout is False: self.layout_mode = "never"
76
+ self.rec = rec or KanEnRecognizer(cfg=self.cfg, providers=self.prov); self.det = det or Detector(self.cfg, self.prov)
77
+ self.lay = lay if lay is not None else (Layout(self.cfg, self.prov) if self.layout_mode != "never" else None)
78
+ self.tta_conf = float(sp["conf"] if tta_conf is None else tta_conf); self.tta_cap = int(sp["cap"] if tta_cap is None else tta_cap)
79
+ self.rec_bs = int(rc["batch"] if rec_bs is None else rec_bs); self.det_bs = int(dc["batch"] if det_bs is None else det_bs)
80
+ self.canvases = tuple(int(c) for c in dc["canvases"]); self.width_classes = tuple(float(f) for f in dc["width_classes"]); self.min_scale = float(dc["min_scale"])
81
+ # resolution-aware rescale: 0 disables. A page whose MEDIAN detected line is shorter than
82
+ # min_line_px is re-detected after upscaling so its lines land near target_line_px.
83
+ self.min_line_px = float(dc.get("min_line_px", 0))
84
+ self.target_line_px = float(dc.get("target_line_px", 45))
85
+ self.max_page_scale = float(dc.get("max_page_scale", 3.0))
86
+ self.workers = int(workers) if workers else self.cfg.workers(); self.pool = ThreadPoolExecutor(self.workers); self._planned = set()
87
+ self._det_buf = {} # (batch, canvas h, canvas w) -> reusable detector input buffer
88
+ self.model_ids = {"layout": _layout_id(self.cfg) if self.lay else None, "det": f"ppocrv6-{det_size}-det-kn-v121", "rec": getattr(self.rec, "name", "kanen-rec"),
89
+ "rec_md5": getattr(self.rec, "onnx_md5", None), "rec_onnx_md5": getattr(self.rec, "onnx_md5", None), "charset_md5": getattr(self.rec, "charset_md5", None), "pipeline": str(self.cfg.get("version", "1.0.0"))}
90
+
91
+ def _sess(self, limit):
92
+ """detector session for a canvas limit (one per limit, as in Detector)."""
93
+ return self.det._session(limit)
94
+
95
+ def _plan(self, limit, ch, cw, n):
96
+ key = (limit, ch, cw, n)
97
+ if key not in self._planned: # ORT CUDA EP re-plans on a new input shape (~1 s) -> do it once, outside the timed path
98
+ s = self._sess(limit)
99
+ z = np.zeros((n, ch, cw, 3), np.uint8) if getattr(self.det, "u8", False) else np.zeros((n, 3, ch, cw), np.float32)
100
+ s.run(None, {s.get_inputs()[0].name: z}); self._planned.add(key)
101
+
102
+ def _prep_det(self, img, ch, cw, limit, dst=None):
103
+ """Normalise one page into the detector's canvas. `dst` is the slice of the batch buffer to write into: filling it
104
+ in place avoids building a per-page canvas and then stacking them, which copied ~28 MB per page at canvas 1920."""
105
+ h, w = img.shape[:2]; r = min(1.0, limit / max(h, w)); nh, nw = max(32, int(round(h * r / 32)) * 32), max(32, int(round(w * r / 32)) * 32)
106
+ nh, nw = min(nh, ch), min(nw, cw)
107
+ if getattr(self.det, "u8", False): # graph normalises: hand it the raw canvas, a quarter of the bytes and no float conversion
108
+ if dst is None:
109
+ canvas = np.full((ch, cw, 3), 255, np.uint8); canvas[:nh, :nw] = cv2.resize(img, (nw, nh))
110
+ return canvas, (nh, nw, w / nw, h / nh)
111
+ if nh < ch or nw < cw: dst[:] = 255
112
+ cv2.resize(img, (nw, nh), dst=dst[:nh, :nw])
113
+ return None, (nh, nw, w / nw, h / nh)
114
+ x = cv2.dnn.blobFromImage(cv2.resize(img, (nw, nh)), 1 / 255.0, (nw, nh), MEAN255, swapRB=False)[0] # (3,nh,nw), (x/255 - mean)
115
+ x *= INV_STD[:, None, None]
116
+ if dst is None:
117
+ canvas = np.empty((3, ch, cw), np.float32); canvas[:] = PAD_VAL[:, None, None]; canvas[:, :nh, :nw] = x
118
+ return canvas, (nh, nw, w / nw, h / nh)
119
+ if nh < ch or nw < cw: dst[:] = PAD_VAL[:, None, None] # only pay for the pad where the page does not cover the canvas
120
+ dst[:, :nh, :nw] = x
121
+ return None, (nh, nw, w / nw, h / nh)
122
+
123
+ # ---- detector, batched over pages that share a canvas shape ----
124
+ def _det_batch(self, imgs, limit, ch, cw):
125
+ s = self._sess(limit); self._plan(limit, ch, cw, len(imgs))
126
+ key = (len(imgs), ch, cw)
127
+ buf = self._det_buf.get(key)
128
+ if buf is None:
129
+ buf = self._det_buf[key] = (np.empty((len(imgs), ch, cw, 3), np.uint8) if getattr(self.det, "u8", False)
130
+ else np.empty((len(imgs), 3, ch, cw), np.float32)) # reused across batches
131
+ meta = [m for _, m in self.pool.map(lambda km: self._prep_det(km[1], ch, cw, limit, buf[km[0]]), list(enumerate(imgs)))]
132
+ x = buf
133
+ prob = s.run(None, {s.get_inputs()[0].name: x})[0][:, 0]
134
+ return list(self.pool.map(lambda k: self.det._post(prob[k][:meta[k][0], :meta[k][1]], meta[k][2], meta[k][3]), range(len(imgs))))
135
+
136
+ def _detect(self, imgs):
137
+ """imgs -> boxes per page; one detector pass per page, batched per canvas shape, no square padding."""
138
+ groups = {}
139
+ for i, im in enumerate(imgs): groups.setdefault(canvas_shape(*im.shape[:2], min_scale=self.min_scale, canvases=self.canvases, width_classes=self.width_classes), []).append(i)
140
+ out = [None] * len(imgs)
141
+ for (limit, ch, cw), ids in groups.items():
142
+ for j in range(0, len(ids), self.det_bs):
143
+ sub = ids[j:j + self.det_bs]
144
+ for i, b in zip(sub, self._det_batch([imgs[i] for i in sub], limit, ch, cw)): out[i] = b
145
+ return out
146
+
147
+ # Layout batch is fixed (config layout.batch): TensorRT builds one engine per input shape (variable sizes = long rebuilds), so
148
+ # pages are always run at the configured batch, zero-padded; the engine is self-tested before its first use (Layout.ensure).
149
+ def _layout(self, imgs):
150
+ if not self.lay or not imgs: return [[] for _ in imgs]
151
+ if self.lay.trt and self.lay.bs not in self.lay.ok: self.lay.ensure(self.lay.bs) # validates (or swaps) the engine batch before the first use
152
+ return self.lay.batch(imgs)
153
+
154
+ def _crops(self, img, boxes, pad=None):
155
+ if pad is None: return [crop(img, b) for b, _ in boxes]
156
+ out = []
157
+ for b, _ in boxes:
158
+ b = b.copy(); hgt = 0.5 * ((b[3, 1] - b[0, 1]) + (b[2, 1] - b[1, 1])); b[0, 1] -= pad[0] * hgt; b[1, 1] -= pad[0] * hgt; b[2, 1] += pad[1] * hgt; b[3, 1] += pad[1] * hgt
159
+ out.append(crop(img, b))
160
+ return out
161
+
162
+ # ---------------- stage A (detector side) and stage B (recogniser side) so consecutive batches can overlap ----------------
163
+ def _stage_a(self, imgs):
164
+ """detect -> orientation 90/270 -> deskew (boxes rotated, no second pass) -> crops. CPU+GPU(det)."""
165
+ t0 = time.perf_counter(); n = len(imgs); rot = [0] * n; ang = [0.0] * n
166
+ boxes = self._detect(imgs)
167
+ boxes = self._rescale_small_pages(imgs, boxes)
168
+ vert = [i for i in range(n) if boxes[i] and np.median([np.linalg.norm(b[1] - b[0]) / max(np.linalg.norm(b[3] - b[0]), 1) for b, _ in boxes[i]]) < 1.0]
169
+ if vert: # candidate 90/270: keep the orientation with more readable lines (mixed-orientation forms stay upright)
170
+ imgs90 = [rotate_page(imgs[i], 1) for i in vert]; boxes90 = self._detect(imgs90)
171
+ for i, im90, b90 in zip(vert, imgs90, boxes90):
172
+ good0 = n_readable(self.rec(self._crops(imgs[i], boxes[i]), bs=self.rec_bs)) if boxes[i] else 0
173
+ good90 = n_readable(self.rec(self._crops(im90, b90), bs=self.rec_bs)) if b90 else 0
174
+ if good90 > good0 or (good90 == 0 and good0 == 0): imgs[i], boxes[i], rot[i] = im90, b90, 90 # nothing readable either way -> trust the aspect rule; the 180° check finishes 270° pages
175
+ for i in range(n):
176
+ ang[i] = deskew_angle(boxes[i])
177
+ if abs(ang[i]) > 0.4:
178
+ hh, ww = imgs[i].shape[:2]; imgs[i] = rotate_small(imgs[i], ang[i]); boxes[i] = rotate_boxes(boxes[i], ang[i], ww, hh)
179
+ # layout only where the page looks structured (many short boxes) or when forced; runs in its own thread
180
+ need_lay = [i for i in range(n) if self.lay and (self.layout_mode == "always" or (self.layout_mode == "auto" and self._structured(boxes[i])))]
181
+ lay_future = self.pool.submit(self._layout, [imgs[i] for i in need_lay]) if need_lay else None
182
+ # crops: one warp task per crop (the 4-core box has little headroom, but warps release the GIL)
183
+ jobs = [(i, b) for i in range(n) for b, _ in boxes[i]]
184
+ flat = list(self.pool.map(lambda ib: crop(imgs[ib[0]], ib[1]), jobs))
185
+ return {"imgs": imgs, "boxes": boxes, "rot": rot, "ang": ang, "flat": flat, "jobs": jobs, "lay_future": lay_future, "need_lay": need_lay, "t_a": time.perf_counter() - t0}
186
+
187
+
188
+ @staticmethod
189
+ def _median_line_px(page_boxes):
190
+ """Median detected line height in page pixels, or 0 when nothing was detected."""
191
+ if not page_boxes: return 0.0
192
+ hs = [float(np.linalg.norm(b[3] - b[0])) for b, _ in page_boxes]
193
+ return float(np.median(hs)) if hs else 0.0
194
+
195
+ def _rescale_small_pages(self, imgs, boxes):
196
+ """Re-detect pages whose text is too small, on an UPSCALED COPY, then map the boxes back.
197
+
198
+ The recogniser is trained at H=32 and `Detector.__call__` caps its scale at
199
+ `min(1.0, limit / max(h, w))`, so it never enlarges a small page: a low-DPI scan is detected at
200
+ 12-25 px lines. Detection, not recognition, is what fails there -- at ~13 px a page comes back as
201
+ correctly spelled words in the WRONG ORDER.
202
+
203
+ The page image itself is NOT replaced. An earlier version swapped in the upscaled copy, which left
204
+ every bbox in the output in upscaled coordinates: a 446x687 input returned boxes out to x=976,
205
+ y=1560, so anything drawing them on the original page was wrong. Detection runs on a temporary
206
+ enlarged copy and the quads are divided back by the scale, so the whole document stays in INPUT
207
+ coordinates and the crop path is untouched.
208
+ """
209
+ if not self.min_line_px: return boxes
210
+ todo = []
211
+ for i, page in enumerate(boxes):
212
+ med = self._median_line_px(page)
213
+ if 0 < med < self.min_line_px:
214
+ # never enlarge past the biggest detector canvas: beyond it the detector just shrinks the
215
+ # page back and the extra pixels are discarded after churning the geometry
216
+ h, w = imgs[i].shape[:2]
217
+ cap = max(1.0, float(max(self.canvases)) / max(h, w))
218
+ s = min(self.max_page_scale, cap, self.target_line_px / med)
219
+ if s > 1.05: todo.append((i, s))
220
+ if not todo: return boxes
221
+ big = []
222
+ for i, s in todo:
223
+ h, w = imgs[i].shape[:2]
224
+ big.append(cv2.resize(imgs[i], (int(round(w * s)), int(round(h * s))), interpolation=cv2.INTER_CUBIC))
225
+ redone = self._detect(big)
226
+ for (i, s), page in zip(todo, redone):
227
+ boxes[i] = [(np.asarray(q, np.float32) / s, sc) for q, sc in page] # back to INPUT coordinates
228
+ return boxes
229
+
230
+ def _structured(self, boxes, min_boxes=25, short_frac=0.45):
231
+ """Heuristic for 'this page has tables/forms': many boxes and a large share of short ones."""
232
+ if len(boxes) < min_boxes: return False
233
+ ws = [np.linalg.norm(b[1] - b[0]) for b, _ in boxes]; med = np.median(ws)
234
+ return np.mean([w < 0.5 * med for w in ws]) > short_frac or len(boxes) > 80
235
+
236
+ def _stage_b(self, A, page_indices=None, dpi=None, with_polygons=False):
237
+ """recognise (pooled) -> 180° fallback -> capped second pass -> layout join -> assemble. GPU(rec)+CPU."""
238
+ t0 = time.perf_counter(); imgs, boxes, rot, ang, jobs = A["imgs"], A["boxes"], A["rot"], A["ang"], A["jobs"]; n = len(imgs)
239
+ res = self.rec(A["flat"], bs=self.rec_bs) if A["flat"] else []
240
+ texts = [[] for _ in range(n)]
241
+ for (i, _), r in zip(jobs, res): texts[i].append(r)
242
+ # 180° candidates: low mean confidence OR weak characters (upside-down pages read as confident garbage, min-char ~0.3)
243
+ top5 = lambda r: sorted(r, key=lambda tc: -len(tc[0]))[:5]
244
+ flip = [i for i in range(n) if texts[i] and (np.mean([c for _, c in texts[i]]) < 0.55 or orient_score(top5(texts[i])) < 1.35)]
245
+ if flip:
246
+ fimgs = [rotate_page(imgs[i], 2) for i in flip]; fboxes = self._detect(fimgs)
247
+ fcrops = [self._crops(im, b) for im, b in zip(fimgs, fboxes)]; ff = [c for cs in fcrops for c in cs]; fres = self.rec(ff, bs=self.rec_bs) if ff else []; p = 0
248
+ for i, im, b, cs in zip(flip, fimgs, fboxes, fcrops):
249
+ r = fres[p:p + len(cs)]; p += len(cs)
250
+ if r and orient_score(top5(r)) > orient_score(top5(texts[i])) + 0.10: imgs[i], boxes[i], texts[i], rot[i] = im, b, r, (rot[i] + 180) % 360
251
+ # confidence-gated second pass: only the lowest-confidence lines (cap per page), pooled across the batch
252
+ low = []
253
+ for i in range(n):
254
+ cand = sorted([(c, j) for j, (_, c) in enumerate(texts[i]) if c < self.tta_conf])[:self.tta_cap]; low += [(i, j) for _, j in cand]
255
+ if low:
256
+ alt = list(self.pool.map(lambda ij: self._crops(imgs[ij[0]], [boxes[ij[0]][ij[1]]], pad=(0.06, 0.08))[0], low))
257
+ for (i, j), (tx, c) in zip(low, self.rec(alt, bs=self.rec_bs)):
258
+ if c > texts[i][j][1] + 0.02: texts[i][j] = (tx, c)
259
+ t_rec = time.perf_counter() - t0; t1 = time.perf_counter()
260
+ blocks = [[] for _ in range(n)]
261
+ if A["lay_future"]:
262
+ for i, bl in zip(A["need_lay"], A["lay_future"].result()): blocks[i] = bl
263
+ for i in range(n): # layout ran on the un-deskewed page: rotate its blocks into the deskewed frame
264
+ if abs(ang[i]) > 0.4 and blocks[i]:
265
+ hh, ww = imgs[i].shape[:2]; M = cv2.getRotationMatrix2D((ww / 2, hh / 2), ang[i], 1.0)
266
+ for b in blocks[i]:
267
+ x0, y0, x1, y1 = b["bbox"]; pts = cv2.transform(np.array([[[x0, y0], [x1, y0], [x1, y1], [x0, y1]]], np.float32), M)[0]
268
+ b["bbox"] = [float(pts[:, 0].min()), float(pts[:, 1].min()), float(pts[:, 0].max()), float(pts[:, 1].max())]
269
+ t_lay = time.perf_counter() - t1; t2 = time.perf_counter()
270
+ def build(i):
271
+ d = assemble(imgs[i].shape[1], imgs[i].shape[0], blocks[i], boxes[i], texts[i], self.model_ids, None, (page_indices or list(range(n)))[i], dpi, with_polygons, img=imgs[i],
272
+ suppress_text_in=tuple(_asm(self.cfg).get("suppress_text_in", ())),
273
+ low_res_px=float(_asm(self.cfg).get("low_res_px", 0)),
274
+ retype_visual_min_lines=int(_asm(self.cfg).get("retype_visual_min_lines", 4)),
275
+ row_cross_frac=float(_asm(self.cfg).get("row_cross_frac", 0.20)),
276
+ suppress_min_conf=float(_asm(self.cfg).get("suppress_min_conf", 0.0)),
277
+ suppress_max_area=float(_asm(self.cfg).get("suppress_max_area", 1.0)),
278
+ suppress_max_lines=int(_asm(self.cfg).get("suppress_max_lines", 0)),
279
+ merge_split_blocks_x=float(_asm(self.cfg).get("merge_split_blocks_x", 0.0)),
280
+ merge_split_blocks_y=float(_asm(self.cfg).get("merge_split_blocks_y", 0.25)))
281
+ d["page"]["rotation"] = rot[i]; d["page"]["deskew_deg"] = round(ang[i], 2); d["page"]["second_pass_lines"] = sum(1 for a, _ in low if a == i); d["page"]["layout_run"] = i in A["need_lay"]; return d
282
+ docs = list(self.pool.map(build, range(n)))
283
+ T = {"det+crop": A["t_a"], "rec": t_rec, "layout_wait": t_lay, "assemble": time.perf_counter() - t2}
284
+ for d in docs: d["timing_ms"] = {k: round(v * 1000 / n, 1) for k, v in T.items()}
285
+ return docs, T
286
+
287
+ def process(self, imgs, page_indices=None, dpi=None, with_polygons=False):
288
+ """One page batch, stages A then B (no overlap). Use run() for the pipelined version."""
289
+ t0 = time.perf_counter(); A = self._stage_a(imgs); docs, T = self._stage_b(A, page_indices, dpi, with_polygons); T["total"] = time.perf_counter() - t0
290
+ return docs, T
291
+
292
+ def run(self, paths, page_batch=None, loader=None, with_polygons=False):
293
+ """Pipelined batched run over all pages; returns (docs, stats). See `stream` for the generator variant."""
294
+ docs = []; stats = {}
295
+ for d, st in self.stream(paths, page_batch, loader, with_polygons): docs.append(d); stats = st
296
+ return docs, stats
297
+
298
+ def stream(self, paths, page_batch=None, loader=None, with_polygons=False):
299
+ """Pipelined: decode(k+2) | stage A(k+1) | stage B(k) run concurrently. stage A is detector-GPU + CPU crops,
300
+ stage B is recogniser-GPU + CPU assembly, so the two GPU models interleave and the CPU work hides behind them.
301
+ paths: file paths (decoded with cv2.imread) or anything `loader` turns into a BGR array.
302
+ Yields (doc, running_stats) per page as soon as its page batch is finished; the last stats dict is the total."""
303
+ page_batch = int(page_batch or self.cfg["runtime"].get("page_batch", 8)); loader = loader or cv2.imread
304
+ batches = [paths[i:i + page_batch] for i in range(0, len(paths), page_batch)]
305
+ if not batches: return
306
+ dec = lambda ps: list(self.pool.map(loader, ps))
307
+ stageA = ThreadPoolExecutor(1) # one worker: stage A of batch k+1 overlaps stage B of batch k
308
+ agg = {}; npages = 0; t0 = time.perf_counter()
309
+ try:
310
+ imgs0 = dec(batches[0]); a_fut = stageA.submit(self._stage_a, imgs0); nxt = self.pool.submit(dec, batches[1]) if len(batches) > 1 else None
311
+ for k in range(len(batches)):
312
+ A = a_fut.result()
313
+ if k + 1 < len(batches):
314
+ imgs1 = nxt.result(); nxt = self.pool.submit(dec, batches[k + 2]) if k + 2 < len(batches) else None
315
+ a_fut = stageA.submit(self._stage_a, imgs1)
316
+ d, T = self._stage_b(A, page_indices=list(range(npages, npages + len(A["imgs"]))), with_polygons=with_polygons); npages += len(d)
317
+ for kk, v in T.items(): agg[kk] = agg.get(kk, 0.0) + v
318
+ wall = time.perf_counter() - t0
319
+ st = {"pages": npages, "wall_s": wall, "pages_per_s": npages / max(wall, 1e-9), "ms_per_page": {kk: round(v * 1000 / max(npages, 1), 1) for kk, v in agg.items()}}
320
+ for doc in d: yield doc, st
321
+ finally:
322
+ stageA.shutdown(wait=False)
hi-en/kanen_infer/charset.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """KanEn dictionary + label normaliser.
2
+ Classes: CTC blank (0) + ASCII printable (incl. space) + Kannada block (used code points) + document symbols.
3
+ Everything NFC. ZWJ/ZWNJ are stripped (decision: fonts render the same joined form; keep labels visual)."""
4
+ import unicodedata, re
5
+
6
+ ASCII = [chr(i) for i in range(32, 127)] # 95 incl. space
7
+ KANNADA = [chr(c) for c in range(0x0C80, 0x0CF4)] # candidate block; filtered by 'assigned'
8
+ KANNADA = [c for c in KANNADA if unicodedata.category(c) not in ("Cn",)]
9
+ SYMBOLS = list("₹–—‘’“”•·°×÷½¼¾§¶।॥…‑‐−™©®")
10
+ ZW = {"\u200c", "\u200d", "\u200b", "\ufeff", "\u2060", "\xad", "\u200e", "\u200f"}
11
+
12
+ def build_dict():
13
+ seen, out = set(), []
14
+ for c in ASCII + KANNADA + SYMBOLS:
15
+ if c not in seen: seen.add(c); out.append(c)
16
+ return out # index 0 in the model = blank; chars start at 1
17
+
18
+ CHARS = build_dict()
19
+ CHAR2ID = {c: i + 1 for i, c in enumerate(CHARS)}
20
+ NUM_CLASSES = len(CHARS) + 1
21
+
22
+ _ws = re.compile(r"[ \t ]+")
23
+ QUOTE_MAP = {"‘": "'", "’": "'", "“": '"', "”": '"'} # optional folding for eval only
24
+
25
+ def normalize(s: str, fold_quotes: bool = False) -> str:
26
+ s = unicodedata.normalize("NFC", s)
27
+ s = "".join(ch for ch in s if ch not in ZW)
28
+ s = s.replace("಼", "") # Kannada nukta: not used in printed text
29
+ if fold_quotes:
30
+ s = "".join(QUOTE_MAP.get(ch, ch) for ch in s)
31
+ s = _ws.sub(" ", s).strip()
32
+ return s
33
+
34
+ def encode(s: str):
35
+ ids, oov = [], []
36
+ for ch in s:
37
+ i = CHAR2ID.get(ch)
38
+ if i is None: oov.append(ch)
39
+ else: ids.append(i)
40
+ return ids, oov
41
+
42
+ def decode(ids):
43
+ return "".join(CHARS[i - 1] for i in ids if 0 < i <= len(CHARS))
44
+
45
+ def script_of(s: str) -> str:
46
+ kn = sum(0x0C80 <= ord(c) <= 0x0CFF for c in s); en = sum(c.isascii() and c.isalpha() for c in s)
47
+ dig = sum(c.isdigit() for c in s)
48
+ if kn and en: return "mixed"
49
+ if kn: return "kn"
50
+ if en: return "en"
51
+ return "num" if dig else "other"
52
+
53
+ if __name__ == "__main__":
54
+ print("classes incl. blank:", NUM_CLASSES, "| kannada:", len(KANNADA), "| ascii:", len(ASCII), "| symbols:", len(SYMBOLS))
55
+
56
+
57
+ # --- Kannada orthographic sanity (label QA only; never used to alter model output) ---
58
+ _VSIGN = set(chr(c) for c in range(0x0CBE, 0x0CCD)) | {"\u0CD5", "\u0CD6"} # dependent vowel signs + length marks
59
+ _VIRAMA = "\u0CCD"; _CONS = set(chr(c) for c in range(0x0C95, 0x0CBA)) | {"\u0CDE"} # incl. archaic ೞ
60
+ def kannada_orthography_issues(s: str):
61
+ """Returns a list of reasons the string cannot be valid printed Kannada (empty list = looks valid)."""
62
+ issues = []
63
+ for i, ch in enumerate(s):
64
+ prev = s[i - 1] if i else ""
65
+ if ch in _VSIGN and (prev in _VSIGN or prev == _VIRAMA or prev == "" or prev == " "): issues.append(f"vowel sign after '{prev}' at {i}")
66
+ if ch == _VIRAMA and (prev == _VIRAMA or prev in _VSIGN or prev not in _CONS): issues.append(f"virama after '{prev}' at {i}")
67
+ # Latin/ASCII letters glued inside a Kannada word (no space between scripts)
68
+ import re
69
+ if re.search(r"[\u0C80-\u0CFF][A-Za-z]|[A-Za-z][\u0C80-\u0CFF]", s): issues.append("latin letter glued to kannada")
70
+ if re.search(r"[0-9][\u0CBE-\u0CCD]", s): issues.append("digit followed by kannada mark")
71
+ if re.search(r"[\u0CDE\u0CF1\u0CF2]", s): issues.append("rare/archaic kannada letter")
72
+ return issues
hi-en/kanen_infer/cli.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """`kanen-ocr` command line.
2
+
3
+ kanen-ocr --input scans/ --out-dir out/ --format all
4
+ kanen-ocr --input "pages/*.png" --out-dir out/ --format json --page-batch 8 --workers 8 --provider trt
5
+ kanen-ocr --input report.pdf --out-dir out/ --format txt,tsv --json-lines
6
+
7
+ Exit codes: 0 ok · 1 no input pages · 2 models/engines failed to load · 3 some pages failed (others written).
8
+ The summary line reports pages, pages/s, the provider each model ended on and every fallback that happened."""
9
+ import argparse, json, os, sys, time, traceback
10
+ from pathlib import Path
11
+
12
+ FORMAT_CHOICES = ["json", "md", "txt", "hocr", "alto", "tsv", "all"]
13
+
14
+ def build_parser():
15
+ ap = argparse.ArgumentParser(prog="kanen-ocr", description="Arjuna-OCR [Kn&En]: layout-aware Kannada + English document OCR (kanen-ocr/1.0 JSON and other serialisations)")
16
+ ap.add_argument("--input", "-i", nargs="+", default=None, help="image file(s), directory, glob, or PDF")
17
+ ap.add_argument("--out-dir", "-o", default="kanen_out", help="output directory (one file per page and format)")
18
+ ap.add_argument("--format", "-f", default="json", help="comma list of json,md,txt,hocr,alto,tsv or 'all'")
19
+ ap.add_argument("--json-lines", action="store_true", help="also write results.jsonl (one document per line) and results.json (list)")
20
+ ap.add_argument("--page-batch", type=int, default=None); ap.add_argument("--layout-mode", choices=["always", "auto", "never"], default=None)
21
+ ap.add_argument("--workers", type=int, default=None); ap.add_argument("--provider", choices=["auto", "trt", "cuda", "cpu"], default=None)
22
+ ap.add_argument("--polygons", action="store_true", help="include 4-point polygons in line objects"); ap.add_argument("--pdf-dpi", type=int, default=200)
23
+ ap.add_argument("--config", default=None, help="alternative kanen.yaml / .json"); ap.add_argument("--quiet", "-q", action="store_true"); ap.add_argument("--limit", type=int, default=0)
24
+ ap.add_argument("--version", action="store_true", help="print version and exit")
25
+ return ap
26
+
27
+ def main(argv=None):
28
+ a = build_parser().parse_args(argv)
29
+ from . import __version__
30
+ if a.version: print(__version__); return 0
31
+ if not a.input: build_parser().error("the following arguments are required: --input/-i")
32
+ from .api import KanEnOCR
33
+ from .io import expand_inputs
34
+ from .formats import FORMATS, render
35
+ fmts = FORMAT_CHOICES[:-1] if a.format == "all" else [f.strip() for f in a.format.split(",") if f.strip()]
36
+ bad = [f for f in fmts if f not in FORMATS]
37
+ if bad: print(f"unknown format(s) {bad}; choose from {FORMAT_CHOICES}", file=sys.stderr); return 1
38
+ paths = expand_inputs(a.input); paths = paths[:a.limit] if a.limit else paths
39
+ if not paths: print(f"no input pages match {a.input}", file=sys.stderr); return 1
40
+ say = (lambda *x: None) if a.quiet else (lambda *x: print(*x, file=sys.stderr, flush=True))
41
+ t0 = time.time()
42
+ try:
43
+ ocr = KanEnOCR(config=a.config, providers=a.provider, layout_mode=a.layout_mode, workers=a.workers)
44
+ try: # long jobs earn the TensorRT build; a page or two does not (the build costs more than it saves)
45
+ _after = int(os.environ.get("KANEN_OPTIMIZE_AFTER", "8"))
46
+ if len(paths) >= _after and hasattr(ocr, "optimize"): ocr.optimize()
47
+ except Exception: pass
48
+ except Exception as e:
49
+ print(f"failed to load models: {type(e).__name__}: {e}", file=sys.stderr); traceback.print_exc(); return 2
50
+ r = ocr.ready(); prov = ", ".join(f"{k}={v.get('provider')}{'' if v.get('provider') != 'trt' else ('-fp16' if v.get('fp16') else '-fp32')}" for k, v in r["providers"].items())
51
+ say(f"{r['name']} {r['version']} | providers: {prov} | load {r['load_s']}s warm-up {r['warmup_s']}s | {len(paths)} input file(s)")
52
+ out = Path(a.out_dir); out.mkdir(parents=True, exist_ok=True); docs = []; failed = 0; jl = open(out / "results.jsonl", "w", encoding="utf-8") if a.json_lines else None
53
+ try:
54
+ for doc in ocr.stream(paths, page_batch=a.page_batch, polygons=a.polygons, pdf_dpi=a.pdf_dpi):
55
+ label = doc["page"].get("file", f"page_{doc['page']['index']}"); base, _, pg = label.partition("#") # PDFs: name.pdf#3 -> name_p3
56
+ stem = os.path.splitext(Path(base).name)[0] + (f"_p{pg}" if pg else "")
57
+ for f in fmts:
58
+ (out / (stem + FORMATS[f])).write_text(render(doc, f), encoding="utf-8")
59
+ if jl: jl.write(json.dumps(doc, ensure_ascii=False) + "\n")
60
+ docs.append(doc)
61
+ except Exception as e:
62
+ failed += 1; print(f"page failed: {type(e).__name__}: {e}", file=sys.stderr); traceback.print_exc()
63
+ finally:
64
+ if jl: jl.close()
65
+ if a.json_lines: (out / "results.json").write_text(json.dumps(docs, ensure_ascii=False, indent=1), encoding="utf-8")
66
+ st = getattr(ocr, "last_stats", {}) or {}; wall = time.time() - t0; n = len(docs)
67
+ fb = [e for v in ocr.ready()["providers"].values() for e in v.get("fallbacks", [])]
68
+ say(f"done: {n} pages in {st.get('wall_s', wall):.1f} s = {st.get('pages_per_s', n / max(wall, 1e-9)):.2f} pages/s | ms/page {st.get('ms_per_page', {})} | providers: {prov} | fallbacks: {len(fb)}{(' -> ' + '; '.join(fb)) if fb else ''} | wrote {out}")
69
+ return 3 if failed else 0
70
+
71
+ if __name__ == "__main__": sys.exit(main())
hi-en/kanen_infer/config.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Configuration for the Arjuna-OCR [Kn&En] inference package: `kanen.yaml` next to the package + environment overrides.
2
+ Paths are resolved relative to the package root with pathlib (Linux and Windows)."""
3
+ import copy, json, logging, os
4
+ from pathlib import Path
5
+
6
+ PKG_ROOT = Path(__file__).resolve().parent.parent # directory that holds kanen.yaml, models/, trt_cache/
7
+ def _root() -> Path:
8
+ """the directory that actually holds kanen.yaml + models/ — the package directory in a clone, the downloaded
9
+ Hugging Face snapshot for a pip install (see kanen_infer/models.py)"""
10
+ try:
11
+ from .models import resolve
12
+ return resolve(PKG_ROOT)
13
+ except Exception:
14
+ return PKG_ROOT
15
+ def _cfg_in(root):
16
+ """This package ships arjuna_hi.yaml; the Kannada one ships kanen.yaml. Resolve whichever is present
17
+ instead of hardcoding kanen.yaml, which made the Hindi package fall back to KANNADA defaults."""
18
+ for n in ("arjuna_hi.yaml", "kanen.yaml"):
19
+ if (Path(root) / n).exists(): return Path(root) / n
20
+ return Path(root) / "arjuna_hi.yaml"
21
+ DEFAULT_CONFIG = _cfg_in(PKG_ROOT)
22
+ LOG = logging.getLogger("kanen")
23
+
24
+ _FALLBACK = { # used when the config is missing or a key is absent; must mirror the shipped arjuna_hi.yaml.
25
+ # It previously mirrored the KANNADA config, so a missing config silently selected the
26
+ # Kannada recogniser and the v12 layout inside the Hindi package.
27
+ "name": "Arjuna-OCR [Hi & En]", "version": "1.1.3",
28
+ "models": {"det": "models/det/inference.onnx", "layout": "models/layout/arjuna_layout_v15.onnx", "layout_labels": "models/layout/classes_v15.txt",
29
+ "layout_selftest": "models/layout/selftest.jpg", "layout_selftest_ref": "models/layout/selftest_ref_v15.json", "rec": "models/rec/arjuna_rec_hi.onnx", "charset": "models/rec/charset.json"},
30
+ "provider": {"policy": "auto", "trt_fp16": True, "engine_cache": "trt_cache", "trt_workspace_gb": 4, "cuda_mem_limit_gb": 6, "cudnn_conv_algo_search": "HEURISTIC"},
31
+ "detector": {"canvases": [1280, 1920, 2560], "width_classes": [0.5, 0.625, 0.75, 0.875, 1.0], "min_scale": 0.9, "default_canvas": 1280,
32
+ "thresh": 0.2, "box_thresh": 0.45, "unclip": 1.4, "pad_top": 0.03, "pad_bottom": 0.25, "batch": 2},
33
+ "layout": {"mode": "always", "batch": 4, "score_thresh": 0.3, "fill": [1.0, 0.8], "selftest_min_match": 1.0},
34
+ "recognizer": {"buckets": [128, 192, 256, 384, 512, 640, 768, 1024, 1280], "batch": 64, "height": 32, "max_ratio": 40},
35
+ "second_pass": {"conf": 0.85, "cap": 6},
36
+ "runtime": {"workers": "auto", "page_batch": 8, "log_level": "warning", "polygons": False},
37
+ }
38
+
39
+ def _merge(base, over):
40
+ out = copy.deepcopy(base)
41
+ for k, v in (over or {}).items():
42
+ out[k] = _merge(out[k], v) if isinstance(v, dict) and isinstance(out.get(k), dict) else copy.deepcopy(v)
43
+ return out
44
+
45
+ def _writable(p: Path) -> bool:
46
+ try:
47
+ target = p if p.exists() else p.parent
48
+ target.mkdir(parents=True, exist_ok=True)
49
+ probe = target / ".arjuna_write_test"; probe.write_text("ok"); probe.unlink(); return True
50
+ except Exception: return False
51
+
52
+ def _read(path):
53
+ p = Path(path)
54
+ text = p.read_text(encoding="utf-8")
55
+ if p.suffix.lower() == ".json": return json.loads(text)
56
+ import yaml
57
+ return yaml.safe_load(text) or {}
58
+
59
+ class Config(dict):
60
+ """dict with attribute access and resolved paths; `cfg.path('models','rec')` -> absolute Path."""
61
+ root: Path
62
+ def __getattr__(self, k):
63
+ try: return self[k]
64
+ except KeyError as e: raise AttributeError(k) from e
65
+ def path(self, *keys) -> Path:
66
+ v = self
67
+ for k in keys: v = v[k]
68
+ p = Path(str(v)).expanduser()
69
+ return p if p.is_absolute() else (self.root / p)
70
+ def workers(self) -> int:
71
+ w = self["runtime"].get("workers", "auto")
72
+ return int(w) if str(w).isdigit() else max(4, (os.cpu_count() or 4) // 2)
73
+
74
+ def load_config(config=None, overrides=None) -> Config:
75
+ """config: None (kanen.yaml next to the package, or $KANEN_CONFIG), a path, or a dict. overrides: nested dict applied last.
76
+ Environment: KANEN_TRT=1 -> provider.policy 'trt'; KANEN_ORT_CPU=1 -> 'cpu'; KANEN_REC_ONNX -> models.rec; KANEN_LOG -> runtime.log_level."""
77
+ root = _root()
78
+ globals()["DEFAULT_CONFIG"] = _cfg_in(root)
79
+ src = None
80
+ if isinstance(config, dict): data = _merge(_FALLBACK, config)
81
+ else:
82
+ src = Path(config) if config else Path(os.environ.get("KANEN_CONFIG") or _cfg_in(root))
83
+ data = _merge(_FALLBACK, _read(src)) if src.exists() else copy.deepcopy(_FALLBACK)
84
+ root = Path(os.path.abspath(src)).parent if src.exists() else root
85
+ if overrides: data = _merge(data, overrides)
86
+ if os.environ.get("KANEN_ORT_CPU"): data["provider"]["policy"] = "cpu"
87
+ elif os.environ.get("KANEN_TRT") == "1": data["provider"]["policy"] = "trt"
88
+ if os.environ.get("KANEN_REC_ONNX"): data["models"]["rec"] = os.environ["KANEN_REC_ONNX"]
89
+ if os.environ.get("KANEN_LOG"): data["runtime"]["log_level"] = os.environ["KANEN_LOG"]
90
+ cfg = Config(data); cfg.root = root; cfg.source = str(src) if src else "<dict>"
91
+ # engine cache: keep it next to the models in a clone, but a pip install resolves the models inside the read-only
92
+ # Hugging Face cache, so fall back to the user cache directory (TensorRT engines are machine-specific anyway)
93
+ env_cache = os.environ.get("ARJUNA_ENGINE_CACHE") or os.environ.get("KANEN_ENGINE_CACHE")
94
+ if env_cache: cfg["provider"]["engine_cache"] = env_cache
95
+ else:
96
+ cache = cfg.path("provider", "engine_cache")
97
+ if not _writable(cache):
98
+ home = Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache")) / "arjuna" / "engines"
99
+ cfg["provider"]["engine_cache"] = str(home)
100
+ LOG.debug(f"engine cache {cache} is not writable, using {home}")
101
+ setup_logging(cfg["runtime"].get("log_level", "warning"))
102
+ return cfg
103
+
104
+ def setup_logging(level="warning"):
105
+ lvl = getattr(logging, str(level).upper(), logging.WARNING)
106
+ if not LOG.handlers:
107
+ h = logging.StreamHandler(); h.setFormatter(logging.Formatter("[%(name)s] %(message)s")); LOG.addHandler(h)
108
+ LOG.setLevel(lvl); LOG.propagate = False
109
+ try:
110
+ import onnxruntime as ort
111
+ ort.set_default_logger_severity(3 if lvl >= logging.WARNING else 1) # silence TensorRT/ORT chatter unless debug/info
112
+ except Exception: pass
hi-en/kanen_infer/formats.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Serialisations of one `kanen-ocr/1.0` document. All of them are views of the same JSON: nothing is filtered,
2
+ re-ordered or rewritten — the text, order, confidences and flags are exactly those of the document.
3
+
4
+ to_json(doc) the document itself (schema kanen-ocr/1.0)
5
+ to_markdown(doc) Markdown: titles, paragraphs, tables as pipe tables, figure/seal markers
6
+ to_text(doc) reading-order plain text: one block per paragraph (blank line between), table rows one per line with ' | '
7
+ to_hocr(doc) hOCR 1.2 (ocr_page > ocr_carea > ocr_line > ocrx_word), bbox + x_wconf per line
8
+ to_alto(doc) ALTO 4 XML (TextBlock > TextLine > String with WC), one String per line
9
+ to_tsv(doc) page, block, line, bbox, text, confidence, min_conf, flags (tab separated, header row)
10
+ """
11
+ import json
12
+ from xml.sax.saxutils import escape, quoteattr
13
+ from .assemble import to_markdown # noqa: F401 (re-exported)
14
+
15
+ FORMATS = {"json": ".json", "md": ".md", "txt": ".txt", "hocr": ".hocr", "alto": ".xml", "tsv": ".tsv"}
16
+
17
+ def to_json(doc, indent=1):
18
+ return json.dumps(doc, ensure_ascii=False, indent=indent)
19
+
20
+ def block_text(b):
21
+ if b.get("type") == "table" and b.get("rows"): return "\n".join(" | ".join(c["text"] for c in r["cells"]) for r in b["rows"])
22
+ return "\n".join(l["text"] for l in b.get("lines", []))
23
+
24
+ def to_text(doc):
25
+ parts = [t for t in (block_text(b) for b in doc["blocks"]) if t.strip()]
26
+ return "\n\n".join(parts) + "\n"
27
+
28
+ def _ibox(bb): return " ".join(str(int(round(v))) for v in bb)
29
+
30
+ def to_hocr(doc, title=None, name="Arjuna-OCR [Kn&En]"):
31
+ pg = doc["page"]; ver = doc.get("model", {}).get("pipeline", ""); pidx = pg.get("index", 0)
32
+ fn = pg.get("file") or f"page_{pidx}"
33
+ out = ['<?xml version="1.0" encoding="UTF-8"?>', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
34
+ '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="kn">', "<head>", f"<title>{escape(title or fn)}</title>", '<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>',
35
+ f'<meta name="ocr-system" content="{escape(name)} {escape(str(ver))}"/>', '<meta name="ocr-capabilities" content="ocr_page ocr_carea ocr_line ocrx_word"/>', "</head>", "<body>",
36
+ f'<div class="ocr_page" id="page_{pidx + 1}" title={quoteattr(f"image {fn}; bbox 0 0 {int(pg["width"])} {int(pg["height"])}; ppageno {pidx}")}>']
37
+ for b in doc["blocks"]:
38
+ bid = b.get("id", "b"); out.append(f'<div class="ocr_carea" id="{bid}" title={quoteattr("bbox " + _ibox(b["bbox"]))}>')
39
+ out.append(f'<p class="ocr_par" id="{bid}_par" title={quoteattr("bbox " + _ibox(b["bbox"]))}>')
40
+ for l in b.get("lines", []):
41
+ conf = int(round(100 * float(l["confidence"]))); lid = l.get("id", bid + ".l"); t = "bbox " + _ibox(l["bbox"])
42
+ out.append(f'<span class="ocr_line" id="{lid}" title={quoteattr(t)}>' f'<span class="ocrx_word" id="{lid}_w0" title={quoteattr(f"{t}; x_wconf {conf}")}>{escape(l["text"])}</span></span>')
43
+ out.append("</p></div>")
44
+ out += ["</div>", "</body>", "</html>", ""]
45
+ return "\n".join(out)
46
+
47
+ def to_alto(doc, name="Arjuna-OCR [Kn&En]"):
48
+ pg = doc["page"]; ver = doc.get("model", {}).get("pipeline", ""); pidx = pg.get("index", 0); fn = pg.get("file") or f"page_{pidx}"
49
+ out = ['<?xml version="1.0" encoding="UTF-8"?>', '<alto xmlns="http://www.loc.gov/standards/alto/ns-v4#">', "<Description>", "<MeasurementUnit>pixel</MeasurementUnit>",
50
+ f"<sourceImageInformation><fileName>{escape(fn)}</fileName></sourceImageInformation>",
51
+ f"<OCRProcessing ID=\"ocr\"><ocrProcessingStep><processingSoftware><softwareName>{escape(name)}</softwareName><softwareVersion>{escape(str(ver))}</softwareVersion></processingSoftware></ocrProcessingStep></OCRProcessing>",
52
+ "</Description>", "<Layout>", f'<Page ID="page_{pidx + 1}" PHYSICAL_IMG_NR="{pidx + 1}" WIDTH="{int(pg["width"])}" HEIGHT="{int(pg["height"])}">', "<PrintSpace>"]
53
+ def geo(bb): x0, y0, x1, y1 = bb; return f'HPOS="{int(round(x0))}" VPOS="{int(round(y0))}" WIDTH="{int(round(x1 - x0))}" HEIGHT="{int(round(y1 - y0))}"'
54
+ for b in doc["blocks"]:
55
+ bid = b.get("id", "b"); out.append(f'<TextBlock ID="{bid}" {geo(b["bbox"])}>')
56
+ for l in b.get("lines", []):
57
+ lid = l.get("id", bid + ".l")
58
+ out.append(f'<TextLine ID="{lid}" {geo(l["bbox"])}><String ID="{lid}_s" {geo(l["bbox"])} CONTENT={quoteattr(l["text"])} WC="{float(l["confidence"]):.4f}"/></TextLine>')
59
+ out.append("</TextBlock>")
60
+ out += ["</PrintSpace>", "</Page>", "</Layout>", "</alto>", ""]
61
+ return "\n".join(out)
62
+
63
+ TSV_HEADER = "page\tblock\tline\tbbox\ttext\tconfidence\tmin_conf\tflags"
64
+
65
+ def tsv_rows(doc):
66
+ pg = doc["page"]; page = pg.get("file") or str(pg.get("index", 0))
67
+ for b in doc["blocks"]:
68
+ for l in b.get("lines", []):
69
+ yield [page, b.get("id", ""), l.get("id", ""), ",".join(str(int(round(v))) for v in l["bbox"]), l["text"].replace("\t", " "), f'{float(l["confidence"]):.4f}', f'{float(l.get("min_conf", l["confidence"])):.4f}', " ".join(l.get("flags", []))]
70
+
71
+ def to_tsv(doc, header=True):
72
+ rows = ["\t".join(r) for r in tsv_rows(doc)]
73
+ return ((TSV_HEADER + "\n") if header else "") + "\n".join(rows) + ("\n" if rows else "")
74
+
75
+ def render(doc, fmt):
76
+ return {"json": to_json, "md": to_markdown, "txt": to_text, "hocr": to_hocr, "alto": to_alto, "tsv": to_tsv}[fmt](doc)
hi-en/kanen_infer/geometry.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Page geometry helpers (orientation / deskew)."""
2
+ import math, numpy as np, cv2
3
+
4
+ def rotate_page(img, k): # k in {0,1,2,3} multiples of 90° counter-clockwise
5
+ return np.ascontiguousarray(np.rot90(img, k)) if k else img
6
+
7
+ def deskew_angle(boxes):
8
+ """Median angle (deg) of wide line boxes; positive = counter-clockwise tilt."""
9
+ angs = []
10
+ for b, _ in boxes:
11
+ w = np.linalg.norm(b[1] - b[0]); h = np.linalg.norm(b[3] - b[0])
12
+ if w > 4 * h and w > 60:
13
+ angs.append(math.degrees(math.atan2(b[1][1] - b[0][1], b[1][0] - b[0][0])))
14
+ return float(np.median(angs)) if len(angs) >= 5 else 0.0
15
+
16
+ def rotate_small(img, deg):
17
+ h, w = img.shape[:2]; M = cv2.getRotationMatrix2D((w / 2, h / 2), deg, 1.0)
18
+ return cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_LINEAR, borderValue=(255, 255, 255))
19
+
hi-en/kanen_infer/hardware.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Hardware detection and auto-configuration: what this machine can do, and the settings that make it fast here.
2
+
3
+ `probe()` collects GPU / driver / CUDA / TensorRT / onnxruntime / CPU / RAM facts and the provider ONNX Runtime will
4
+ actually be able to use (checking that the provider's libraries load, not just that the name is compiled in).
5
+ `autoconfig(cfg)` scales the batch sizes and thread counts in the config to the machine. It never touches anything that
6
+ changes results — canvases, thresholds, padding and the models stay exactly as shipped; only batching and threads move.
7
+ `report()` renders both as text for `arjuna doctor`.
8
+ """
9
+ import json, os, platform, shutil, subprocess, sys
10
+ import onnxruntime as ort
11
+
12
+ def _run(cmd, timeout=10):
13
+ try:
14
+ r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
15
+ return r.stdout.strip() if r.returncode == 0 else ""
16
+ except Exception: return ""
17
+
18
+ def nvidia_smi():
19
+ """name, driver, total VRAM (MiB), compute capability — per GPU"""
20
+ out = _run(["nvidia-smi", "--query-gpu=name,driver_version,memory.total,compute_cap", "--format=csv,noheader,nounits"])
21
+ gpus = []
22
+ for line in out.splitlines():
23
+ parts = [p.strip() for p in line.split(",")]
24
+ if len(parts) >= 3:
25
+ gpus.append({"name": parts[0], "driver": parts[1], "vram_mb": int(float(parts[2])) if parts[2].replace(".", "").isdigit() else None,
26
+ "compute_capability": parts[3] if len(parts) > 3 else None})
27
+ return gpus
28
+
29
+ def cuda_runtime_version():
30
+ try:
31
+ import ctypes
32
+ lib = ctypes.CDLL("libcudart.so.12" if sys.platform != "win32" else "cudart64_12.dll")
33
+ v = ctypes.c_int(); lib.cudaRuntimeGetVersion(ctypes.byref(v)); return f"{v.value // 1000}.{(v.value % 1000) // 10}"
34
+ except Exception: return None
35
+
36
+ def trt_version():
37
+ try:
38
+ import tensorrt; return tensorrt.__version__
39
+ except Exception: return None
40
+
41
+ def usable_providers():
42
+ """providers whose libraries really load here (ORT lists names its build was compiled with, which is not the same thing)"""
43
+ import numpy as np, tempfile
44
+ if not hasattr(ort, "get_available_providers"): # both builds were installed and one was removed: they share files
45
+ return {"listed": [], "usable": [], "note": "onnxruntime is installed but broken — run: pip install --force-reinstall onnxruntime-gpu (or onnxruntime)"}
46
+ names = ort.get_available_providers(); ok = []
47
+ try:
48
+ from .providers import preload_cuda_libs; preload_cuda_libs()
49
+ except Exception: pass
50
+ try: import tensorrt # noqa: F401 (loads libnvinfer so the TensorRT provider library can resolve it)
51
+ except Exception: pass
52
+ # a 1-node model is enough to see whether a provider can create a session
53
+ try:
54
+ import onnx
55
+ from onnx import helper, TensorProto
56
+ g = helper.make_graph([helper.make_node("Relu", ["x"], ["y"])], "t", [helper.make_tensor_value_info("x", TensorProto.FLOAT, [1, 1])], [helper.make_tensor_value_info("y", TensorProto.FLOAT, [1, 1])])
57
+ m = helper.make_model(g, opset_imports=[helper.make_opsetid("", 13)])
58
+ m.ir_version = 10 # a newer onnx writes an IR version this onnxruntime may refuse; the probe only needs to load
59
+ path = os.path.join(tempfile.gettempdir(), "kanen_probe.onnx"); open(path, "wb").write(m.SerializeToString())
60
+ except Exception:
61
+ return {"listed": names, "usable": names, "note": "onnx not installed: provider names not verified"}
62
+ so = ort.SessionOptions(); so.log_severity_level = 4
63
+ import contextlib, io
64
+ for p in names:
65
+ if p in ("AzureExecutionProvider",): continue
66
+ try: # ORT prints its own provider errors to stderr; the probe reports them itself
67
+ with contextlib.redirect_stderr(io.StringIO()), contextlib.redirect_stdout(io.StringIO()):
68
+ s = ort.InferenceSession(path, so, providers=[p])
69
+ if s.get_providers()[0] == p or p == "CPUExecutionProvider": ok.append(p)
70
+ except Exception: pass
71
+ if not ok: return {"listed": names, "usable": names, "note": "provider probe could not run; reporting the names onnxruntime lists"}
72
+ return {"listed": names, "usable": ok}
73
+ def probe(policy=None):
74
+ gpus = nvidia_smi(); prov = usable_providers()
75
+ if prov.get("note", "").startswith("onnxruntime is installed but broken"):
76
+ return {"os": f"{platform.system()} {platform.release()}", "machine": platform.machine(), "python": platform.python_version(),
77
+ "cpu_cores": os.cpu_count() or 1, "ram_gb": None, "onnxruntime": getattr(ort, "__version__", "broken"), "gpus": gpus,
78
+ "cuda_runtime": None, "tensorrt": trt_version(), "providers_listed": [], "providers_usable": [], "nvidia_smi": bool(shutil.which("nvidia-smi")),
79
+ "plan": "cpu", "plan_notes": [prov["note"]]}
80
+ hw = {"os": f"{platform.system()} {platform.release()}", "machine": platform.machine(), "python": platform.python_version(),
81
+ "cpu_cores": os.cpu_count() or 1, "ram_gb": round(os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES") / 1e9, 1) if hasattr(os, "sysconf") and "SC_PHYS_PAGES" in os.sysconf_names else None,
82
+ "onnxruntime": ort.__version__, "gpus": gpus, "cuda_runtime": cuda_runtime_version(), "tensorrt": trt_version(),
83
+ "providers_listed": prov["listed"], "providers_usable": prov["usable"], "nvidia_smi": bool(shutil.which("nvidia-smi"))}
84
+ trt_ok = "TensorrtExecutionProvider" in prov["usable"] and hw["tensorrt"] and gpus
85
+ cuda_ok = "CUDAExecutionProvider" in prov["usable"] and gpus
86
+ ov_ok = "OpenVINOExecutionProvider" in prov["usable"]
87
+ hw["plan"] = "trt" if trt_ok else ("cuda" if cuda_ok else ("openvino" if ov_ok else "cpu"))
88
+ why = []
89
+ forced = (policy or "").lower()
90
+ if forced in ("cpu", "cuda", "trt") and forced != "auto":
91
+ if forced != hw["plan"]:
92
+ order = {"cpu": 0, "cuda": 1, "trt": 2}
93
+ if order.get(forced, 0) <= order.get(hw["plan"], 0):
94
+ why.append(f"provider forced to {forced.upper()} by configuration/environment (this machine could run {hw['plan'].upper()})")
95
+ hw["plan"] = forced
96
+ else: why.append(f"{forced.upper()} requested but not usable here: running {hw['plan'].upper()}")
97
+ if not gpus: why.append("no NVIDIA GPU visible (nvidia-smi found nothing)" if hw["nvidia_smi"] else "nvidia-smi not found: no NVIDIA driver on this machine")
98
+ elif not cuda_ok: why.append("CUDAExecutionProvider cannot load (install onnxruntime-gpu matching your CUDA, or the CUDA/cuDNN runtime libraries)")
99
+ if gpus and cuda_ok and not trt_ok: why.append("TensorRT not usable (no tensorrt wheel or provider libraries): running on the CUDA provider, about 2-3x slower")
100
+ if hw["plan"] == "cpu" and not ov_ok: why.append("on Intel CPUs the OpenVINO build is 1.4-1.9x faster: pip uninstall -y onnxruntime && pip install onnxruntime-openvino")
101
+ if hw["plan"] == "openvino": why.append("OpenVINO execution provider (CPU); set provider.openvino_device to GPU or NPU for Intel accelerators")
102
+ try: # both onnxruntime and onnxruntime-gpu installed: they share the same import name and overwrite each other
103
+ from importlib.metadata import distributions
104
+ installed = {d.metadata["Name"].lower() for d in distributions() if d.metadata.get("Name")}
105
+ if {"onnxruntime", "onnxruntime-gpu"} <= installed:
106
+ why.append("both onnxruntime and onnxruntime-gpu are installed; keep one: pip uninstall -y onnxruntime")
107
+ except Exception: pass
108
+ hw["plan_notes"] = why
109
+ return hw
110
+
111
+ # VRAM (GB) -> (page_batch, recognizer batch, layout batch, detector batch). Conservative: these only affect speed.
112
+ _GPU_TIERS = [(20, (12, 96, 8, 4)), (12, (8, 64, 4, 2)), (7, (6, 48, 4, 2)), (4, (4, 32, 2, 2)), (0, (2, 16, 1, 1))]
113
+ def autoconfig(cfg, hw=None, quality=None):
114
+ """scale batching/threads to the machine; returns the list of changes (for `doctor` and the logs)"""
115
+ hw = hw or probe(cfg["provider"].get("policy")); changes = []
116
+ cores = hw["cpu_cores"]
117
+ if hw["plan"] in ("trt", "cuda") and hw["gpus"]:
118
+ vram = (hw["gpus"][0].get("vram_mb") or 8192) / 1024.0
119
+ for floor, (pb, rb, lb, db) in _GPU_TIERS:
120
+ if vram >= floor: break
121
+ mem_limit = max(2.0, min(vram * 0.6, 12.0))
122
+ for section, key, val in (("runtime", "page_batch", pb), ("recognizer", "batch", rb), ("layout", "batch", lb), ("detector", "batch", db), ("provider", "cuda_mem_limit_gb", round(mem_limit, 1))):
123
+ if cfg[section].get(key) != val: changes.append(f"{section}.{key}: {cfg[section].get(key)} -> {val}"); cfg[section][key] = val
124
+ ws = round(max(1.0, min(vram * 0.25, 4.0)), 1)
125
+ if cfg["provider"].get("trt_workspace_gb") != ws: changes.append(f"provider.trt_workspace_gb: {cfg['provider'].get('trt_workspace_gb')} -> {ws}"); cfg["provider"]["trt_workspace_gb"] = ws
126
+ else: # CPU only: recognition is the bottleneck, keep batches small and use the cores for the worker pool
127
+ for section, key, val in (("runtime", "page_batch", max(1, min(4, cores // 2))), ("recognizer", "batch", 16), ("layout", "batch", 1), ("detector", "batch", 1)):
128
+ if cfg[section].get(key) != val: changes.append(f"{section}.{key}: {cfg[section].get(key)} -> {val}"); cfg[section][key] = val
129
+ w = max(2, cores - 1 if cores <= 8 else int(cores * 0.75))
130
+ if cfg["runtime"].get("workers") != w: changes.append(f"runtime.workers: {cfg['runtime'].get('workers')} -> {w}"); cfg["runtime"]["workers"] = w
131
+ if quality:
132
+ q = {"fast": {"canvases": [1280], "second_pass_cap": 0}, "balanced": {}, "exact": {"canvases": [1280, 1920, 2560], "second_pass_cap": 12}}.get(quality, {})
133
+ if "canvases" in q and cfg["detector"].get("canvases") != q["canvases"]: changes.append(f"detector.canvases -> {q['canvases']} (quality={quality})"); cfg["detector"]["canvases"] = q["canvases"]
134
+ if "second_pass_cap" in q: changes.append(f"second_pass.cap -> {q['second_pass_cap']} (quality={quality})"); cfg["second_pass"]["cap"] = q["second_pass_cap"]
135
+ return changes
136
+ def report(cfg=None, changes=None, hw=None):
137
+ hw = hw or probe(cfg["provider"].get("policy") if cfg else None); L = [f"Arjuna-OCR — machine report", "",
138
+ f" OS {hw['os']} ({hw['machine']}), Python {hw['python']}",
139
+ f" CPU / RAM {hw['cpu_cores']} cores" + (f", {hw['ram_gb']} GB RAM" if hw["ram_gb"] else ""),
140
+ f" onnxruntime {hw['onnxruntime']}"]
141
+ if hw["gpus"]:
142
+ for i, g in enumerate(hw["gpus"]):
143
+ L.append(f" GPU {i} {g['name']} · driver {g['driver']}" + (f" · {g['vram_mb']} MiB" if g["vram_mb"] else "") + (f" · sm{g['compute_capability']}" if g["compute_capability"] else ""))
144
+ else: L.append(" GPU none detected")
145
+ L += [f" CUDA runtime {hw['cuda_runtime'] or 'not found'}", f" TensorRT {hw['tensorrt'] or 'not installed'}",
146
+ f" providers usable: {', '.join(p.replace('ExecutionProvider','') for p in hw['providers_usable']) or 'none'}", "",
147
+ f" plan {hw['plan'].upper()}" + (" (fp16 engines, built on first run in the background)" if hw["plan"] == "trt" else "")]
148
+ for n in hw["plan_notes"]: L.append(f" {n}")
149
+ if changes: L += ["", " auto-configuration (speed only, results unchanged):"] + [f" {c}" for c in changes]
150
+ return "\n".join(L)
151
+ if __name__ == "__main__": print(report())
hi-en/kanen_infer/io.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Input handling: file path, bytes, numpy BGR array, PIL image, directory / glob expansion, and PDF pages
2
+ (rendered with pypdfium2 if installed, else `pdftoppm` from poppler if on PATH; both optional)."""
3
+ import glob, io as _io, os, shutil, subprocess, tempfile
4
+ from pathlib import Path
5
+ import numpy as np, cv2
6
+
7
+ IMAGE_EXT = {".png", ".jpg", ".jpeg", ".tif", ".tiff", ".bmp", ".webp", ".jp2"}
8
+ PDF_EXT = {".pdf"}
9
+
10
+ def to_bgr(x):
11
+ """Any supported input -> numpy uint8 BGR (H, W, 3). Raises ValueError if it cannot be decoded."""
12
+ if isinstance(x, np.ndarray):
13
+ if x.ndim == 2: return cv2.cvtColor(x, cv2.COLOR_GRAY2BGR)
14
+ if x.ndim == 3 and x.shape[2] == 4: return cv2.cvtColor(x, cv2.COLOR_BGRA2BGR)
15
+ if x.ndim == 3 and x.shape[2] == 3: return np.ascontiguousarray(x)
16
+ raise ValueError(f"unsupported array shape {x.shape}")
17
+ if isinstance(x, (bytes, bytearray, memoryview)):
18
+ im = cv2.imdecode(np.frombuffer(bytes(x), np.uint8), cv2.IMREAD_COLOR)
19
+ if im is None: raise ValueError("could not decode image bytes")
20
+ return im
21
+ if isinstance(x, (str, os.PathLike)):
22
+ im = cv2.imdecode(np.fromfile(str(x), np.uint8), cv2.IMREAD_COLOR) # fromfile: unicode paths on Windows
23
+ if im is None: raise ValueError(f"could not read image {x}")
24
+ return im
25
+ if hasattr(x, "convert") and hasattr(x, "size"): # PIL.Image without importing PIL
26
+ return cv2.cvtColor(np.asarray(x.convert("RGB")), cv2.COLOR_RGB2BGR)
27
+ if hasattr(x, "read"): return to_bgr(x.read())
28
+ raise ValueError(f"unsupported input type {type(x).__name__}")
29
+
30
+ def expand_inputs(inputs):
31
+ """file / directory / glob / list -> sorted list of image and PDF paths (strings)."""
32
+ if isinstance(inputs, (str, os.PathLike)): inputs = [inputs]
33
+ out = []
34
+ for it in inputs:
35
+ p = Path(it)
36
+ if p.is_dir(): out += sorted(str(f) for f in p.iterdir() if f.suffix.lower() in IMAGE_EXT | PDF_EXT)
37
+ elif p.exists(): out.append(str(p))
38
+ else:
39
+ g = sorted(glob.glob(str(it), recursive=True))
40
+ out += [f for f in g if Path(f).suffix.lower() in IMAGE_EXT | PDF_EXT]
41
+ return out
42
+
43
+ def pdf_pages(path, dpi=200, pages=None):
44
+ """Render a PDF to BGR page images. Uses pypdfium2 when importable, else poppler's pdftoppm; ImportError otherwise.
45
+ pages: optional 1-based list of page numbers."""
46
+ try:
47
+ import pypdfium2 as pdfium
48
+ doc = pdfium.PdfDocument(str(path)); idx = [p - 1 for p in pages] if pages else range(len(doc)); out = []
49
+ for i in idx:
50
+ bm = doc[i].render(scale=dpi / 72.0); pil = bm.to_pil(); out.append(cv2.cvtColor(np.asarray(pil.convert("RGB")), cv2.COLOR_RGB2BGR))
51
+ return out
52
+ except ImportError: pass
53
+ exe = shutil.which("pdftoppm")
54
+ if not exe: raise ImportError("PDF input needs `pip install pypdfium2` or poppler's pdftoppm on PATH")
55
+ with tempfile.TemporaryDirectory() as td:
56
+ cmd = [exe, "-r", str(dpi), "-png"]
57
+ if pages: cmd += ["-f", str(min(pages)), "-l", str(max(pages))]
58
+ subprocess.run(cmd + [str(path), os.path.join(td, "p")], check=True, capture_output=True)
59
+ files = sorted(Path(td).glob("p*.png"), key=lambda f: int("".join(ch for ch in f.stem if ch.isdigit()) or 0))
60
+ if pages: files = [f for f in files if int("".join(ch for ch in f.stem if ch.isdigit())) in set(pages)]
61
+ return [to_bgr(str(f)) for f in files]
62
+
63
+ def iter_page_sources(paths, dpi=200):
64
+ """Yield (label, loader) per page: images load lazily by path; PDFs are rendered page by page."""
65
+ for p in paths:
66
+ if Path(p).suffix.lower() in PDF_EXT:
67
+ for k, im in enumerate(pdf_pages(p, dpi=dpi)): yield f"{Path(p).name}#{k + 1}", im
68
+ else: yield Path(p).name, p
hi-en/kanen_infer/models.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Where the model files come from.
2
+
3
+ Installed from PyPI the wheel carries code only (~200 KB), so the three ONNX models are fetched once from the Hugging Face
4
+ repository at a **pinned revision** — a package version always maps to exactly the models it was tested against — and cached
5
+ in the usual Hugging Face cache. Inside a cloned repository (models/ next to kanen.yaml) nothing is downloaded.
6
+
7
+ Resolution order:
8
+ 1. $ARJUNA_MODELS — a directory holding models/ and kanen.yaml (air-gapped installs, custom builds)
9
+ 2. models/ next to the package root (running from a clone of the repo)
10
+ 3. the Hugging Face repo $ARJUNA_MODEL_REPO (default anandkaman/arjuna-ocr-kn-en-inference) at $ARJUNA_MODEL_REVISION (default the
11
+ package version), downloaded on first use
12
+ """
13
+ import logging, os
14
+ from pathlib import Path
15
+
16
+ LOG = logging.getLogger("kanen")
17
+ REPO = os.environ.get("ARJUNA_MODEL_REPO", "anandkaman/arjuna-ocr-hi-en")
18
+ # The models this code was tested against. Deliberately NOT the Python package version: a packaging fix can ship without
19
+ # moving the models, and the pipeline version stamped into every document stays the model's (VERSION / kanen.yaml).
20
+ MODEL_REVISION = "v1.1.3" # must track the release: a stale value silently fetches the WRONG models
21
+ # This package ships arjuna_hi.yaml, NOT kanen.yaml. Keying the completeness check on "kanen.yaml" made
22
+ # _has_models() return False even with the correct Hindi models/ present, so the package downloaded the
23
+ # KANNADA repo and ran kanen_rec_e5.onnx (221 Kn/En classes) on Hindi pages — plausible-looking garbage.
24
+ CONFIG_NAMES = ("arjuna_hi.yaml", "kanen.yaml")
25
+ PATTERNS = ["models/*", "models/**", "arjuna_hi.yaml", "kanen.yaml", "VERSION", "samples/pages/*"]
26
+
27
+ def config_path(root: Path):
28
+ """the config file this package actually ships, whatever it is called"""
29
+ for n in CONFIG_NAMES:
30
+ if (root / n).exists(): return root / n
31
+ return None
32
+ _resolved = None
33
+
34
+ def _has_models(root: Path) -> bool:
35
+ return config_path(root) is not None and (root / "models").is_dir() and any((root / "models").glob("*/*.onnx"))
36
+
37
+ def revision() -> str:
38
+ return os.environ.get("ARJUNA_MODEL_REVISION") or MODEL_REVISION
39
+
40
+ def resolve(pkg_root: Path, download: bool = True) -> Path:
41
+ """return the directory that holds kanen.yaml + models/, downloading it if this is a code-only install"""
42
+ global _resolved
43
+ if _resolved is not None: return _resolved
44
+ env = os.environ.get("ARJUNA_MODELS")
45
+ if env:
46
+ p = Path(env).expanduser()
47
+ if not _has_models(p): raise FileNotFoundError(f"ARJUNA_MODELS={p} does not contain kanen.yaml and models/")
48
+ _resolved = p; return p
49
+ if _has_models(pkg_root): _resolved = pkg_root; return pkg_root
50
+ if not download: return pkg_root
51
+ try:
52
+ from huggingface_hub import snapshot_download
53
+ except ImportError as e:
54
+ raise FileNotFoundError(
55
+ "model files not found next to the package and huggingface_hub is not installed.\n"
56
+ " pip install huggingface_hub (then the models download automatically), or\n"
57
+ " set ARJUNA_MODELS=/path/to/arjuna-ocr pointing at a clone of the model repository") from e
58
+ rev = revision()
59
+ LOG.warning(f"downloading the Arjuna-OCR models from {REPO}@{rev} (about 170 MB, once)")
60
+ try:
61
+ path = snapshot_download(REPO, revision=rev, allow_patterns=PATTERNS)
62
+ except Exception as e: # a package published before its tag exists, or an offline machine
63
+ LOG.warning(f"{REPO}@{rev} not available ({type(e).__name__}); falling back to main")
64
+ path = snapshot_download(REPO, revision="main", allow_patterns=PATTERNS)
65
+ _resolved = Path(path); return _resolved
hi-en/kanen_infer/pipeline_base.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Detector (PP-OCRv6 det, DB post-processing) and Layout (Arjuna layout v12, DocLayout-YOLO based) models on ONNX Runtime, plus the crop helpers.
2
+ Parameters come from the package config (`kanen.yaml`); sessions come from `providers.Providers` (trt -> cuda -> cpu).
3
+ The post-processing is identical to the v1.0 reference: no thresholds, padding or ordering were changed."""
4
+ import logging, os, shutil, time
5
+ import numpy as np, cv2, pyclipper, yaml
6
+ from shapely.geometry import Polygon
7
+ from .config import load_config, PKG_ROOT
8
+ from .providers import Providers
9
+
10
+ LOG = logging.getLogger("kanen")
11
+ ROOT = str(PKG_ROOT) # kept for callers that import ROOT
12
+ MEAN = np.array([0.485, 0.456, 0.406], np.float32); STD = np.array([0.229, 0.224, 0.225], np.float32)
13
+
14
+ def graph_input(path):
15
+ """(input name, is_uint8_NHWC) of an ONNX graph, read without creating a session"""
16
+ try:
17
+ import onnx
18
+ g = onnx.load(path, load_external_data=False).graph
19
+ i = g.input[0]; dims = [d.dim_value for d in i.type.tensor_type.shape.dim]
20
+ return i.name, (i.type.tensor_type.elem_type == 2 and len(dims) == 4 and dims[-1] == 3) # 2 = UINT8
21
+ except Exception:
22
+ return "x", False
23
+
24
+ class Detector:
25
+ def __init__(self, cfg=None, providers=None, limit=None):
26
+ # pad_bottom=0.25: Kannada below-baseline conjuncts were clipped in 88 % of lines with DB's default boxes;
27
+ # 25 % bottom extension -> 1.2 % clipped, 0 intrusion into the next line (measured on MILE, 2026-09-13).
28
+ self.cfg = cfg or load_config(); self.prov = providers or Providers(self.cfg); d = self.cfg["detector"]
29
+ self.path = str(self.cfg.path("models", "det"))
30
+ self.limit = int(limit or d.get("default_canvas", 1280)); self.thresh = float(d["thresh"]); self.box_thresh = float(d["box_thresh"]); self.unclip = float(d["unclip"])
31
+ self.pad_top, self.pad_bottom = float(d["pad_top"]), float(d["pad_bottom"])
32
+ # one TensorRT engine covering every canvas shape the batched path can produce (long side in `canvases`, short side
33
+ # >= min(width_classes) * smallest canvas, batch <= detector.batch): built once by prepare_engines, never rebuilt mid-run
34
+ cv = [int(c) for c in d["canvases"]]; lo = max(32, int(round(cv[0] * min(float(f) for f in d["width_classes"]) / 32)) * 32); hi = max(cv); nb = int(d.get("batch", 2))
35
+ # the shipped graph may take normalised float NCHW ("x") or the raw uint8 canvas NHWC ("x_u8", normalisation folded
36
+ # into the graph: no float conversion on the CPU and a quarter of the bytes to upload)
37
+ self.inp, self.u8 = graph_input(self.path)
38
+ if self.u8: # NHWC uint8 canvas
39
+ self.trt_opts = {"trt_profile_min_shapes": f"{self.inp}:1x{lo}x{lo}x3", "trt_profile_opt_shapes": f"{self.inp}:{nb}x1920x1216x3",
40
+ "trt_profile_max_shapes": f"{self.inp}:{nb}x{hi}x{hi}x3"}
41
+ else:
42
+ self.trt_opts = {"trt_profile_min_shapes": f"{self.inp}:1x3x{lo}x{lo}", "trt_profile_opt_shapes": f"{self.inp}:{nb}x3x1920x1216",
43
+ "trt_profile_max_shapes": f"{self.inp}:{nb}x3x{hi}x{hi}"}
44
+ # one ORT session per canvas size: the CUDA EP re-plans (~1 s) every time the input shape changes, so the
45
+ # scale-normalised re-detects must not share a session with the default canvas.
46
+ self._sessions = {}; self.s = self._session(self.limit)
47
+ def _session(self, limit):
48
+ if limit not in self._sessions:
49
+ def _same(new, cur, _limit=limit): # probability maps must agree before an engine may replace a working session
50
+ m = min(_limit, 960); rs = np.random.RandomState(0)
51
+ x = rs.randint(0, 255, (1, m, m, 3), dtype=np.uint8) if self.u8 else rs.rand(1, 3, m, m).astype(np.float32)
52
+ try:
53
+ a = new.run(None, {new.get_inputs()[0].name: x})[0]; b = cur.run(None, {cur.get_inputs()[0].name: x})[0]
54
+ except Exception: return False
55
+ return a.shape == b.shape and float(np.abs(a - b).max()) < 0.05
56
+ s = (self.prov.session_async(self.path, "det", validate=_same, trt_opts=self.trt_opts)
57
+ if getattr(self.prov, "background_trt", True) else self.prov.session(self.path, "det", trt_opts=self.trt_opts))
58
+ x = (np.zeros((1, limit, limit, 3), np.uint8) if self.u8 else np.zeros((1, 3, limit, limit), np.float32))
59
+ s.run(None, {s.get_inputs()[0].name: x}) # warm-up plan
60
+ self._sessions[limit] = s
61
+ return self._sessions[limit]
62
+ @property
63
+ def provider(self): return self.prov.status.get("det", {}).get("provider")
64
+
65
+ def __call__(self, img_bgr):
66
+ h, w = img_bgr.shape[:2]; self.s = self._session(self.limit)
67
+ r = min(1.0, self.limit / max(h, w))
68
+ nh, nw = max(32, int(round(h * r / 32)) * 32), max(32, int(round(w * r / 32)) * 32)
69
+ canvas = np.full((self.limit, self.limit, 3), 255, np.uint8) # fixed shape -> no ORT re-planning
70
+ canvas[:nh, :nw] = cv2.resize(img_bgr, (nw, nh))
71
+ x = canvas[None] if self.u8 else ((canvas.astype(np.float32) / 255.0 - MEAN) / STD).transpose(2, 0, 1)[None]
72
+ prob = self.s.run(None, {self.s.get_inputs()[0].name: x})[0][0, 0][:nh, :nw]
73
+ return self._post(prob, w / nw, h / nh)
74
+ def _post(self, prob, sx, sy):
75
+ """DB post-processing: probability map -> line quads.
76
+
77
+ Hot path on a 4-core box (20.6 ms/page before this rewrite), so it avoids Python-level geometry:
78
+ * score with cv2.mean over the contour mask instead of boolean fancy indexing (which copies)
79
+ * contour area/perimeter from cv2 (shoelace) instead of building a shapely Polygon per box
80
+ The pyclipper offset stays: expanding the fitted rectangle analytically instead was 1.8 ms faster again but moved
81
+ the boxes (mean IoU 0.96 against the previous output, 304/304 boxes changed), so it was rejected. With the two
82
+ changes above the boxes are bit-identical to the previous implementation (verified 304/304 on frozen pages) at
83
+ 3.7 ms/page instead of 5.2.
84
+ """
85
+ bit = (prob > self.thresh).astype(np.uint8)
86
+ cnts, _ = cv2.findContours(bit, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
87
+ out = []
88
+ H, W = prob.shape
89
+ for c in cnts[:3000]:
90
+ if len(c) < 4: continue
91
+ pts = c.reshape(-1, 2)
92
+ x0, y0 = pts.min(0); x1, y1 = pts.max(0) + 1
93
+ mask = np.zeros((y1 - y0, x1 - x0), np.uint8); cv2.fillPoly(mask, [pts - (x0, y0)], 1)
94
+ score = cv2.mean(prob[y0:y1, x0:x1], mask)[0]
95
+ if score < self.box_thresh: continue
96
+ area = cv2.contourArea(c); per = cv2.arcLength(c, True)
97
+ if area < 4: continue
98
+ d = area * self.unclip / max(per, 1e-6)
99
+ pc = pyclipper.PyclipperOffset(); pc.AddPath(pts.tolist(), pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
100
+ ex = pc.Execute(d)
101
+ if not ex: continue
102
+ rect = cv2.minAreaRect(np.array(ex[0], np.float32))
103
+ if min(rect[1]) < 3: continue
104
+ box = order_pts(cv2.boxPoints(rect))
105
+ box[:, 0] *= sx; box[:, 1] *= sy
106
+ hgt = 0.5 * ((box[3, 1] - box[0, 1]) + (box[2, 1] - box[1, 1]))
107
+ box[0, 1] -= self.pad_top * hgt; box[1, 1] -= self.pad_top * hgt; box[2, 1] += self.pad_bottom * hgt; box[3, 1] += self.pad_bottom * hgt
108
+ box[:, 1] = np.clip(box[:, 1], 0, H * sy)
109
+ out.append((box, float(score)))
110
+ return out
111
+
112
+ def order_pts(b):
113
+ b = b[np.argsort(b[:, 0])]
114
+ l, r = b[:2][np.argsort(b[:2, 1])], b[2:][np.argsort(b[2:, 1])]
115
+ return np.array([l[0], r[0], r[1], l[1]], np.float32)
116
+
117
+ def crop(img, box):
118
+ w = int(max(np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[2] - box[3])))
119
+ h = int(max(np.linalg.norm(box[0] - box[3]), np.linalg.norm(box[1] - box[2])))
120
+ M = cv2.getPerspectiveTransform(box, np.array([[0, 0], [w, 0], [w, h], [0, h]], np.float32))
121
+ return cv2.warpPerspective(img, M, (w, h), flags=cv2.INTER_CUBIC)
122
+
123
+ class Layout:
124
+ """v15 layout model: DocLayout-YOLO (YOLOv10-based) trained on DocLayNet + our relabelled Indian pages — 15 classes,
125
+ 1024 letterbox input, top-300 selection inside the graph
126
+ (NMS-free), output (B, 300, 6) = x0 y0 x1 y1 conf cls in letterbox pixels. No reading-order head: the assembler's
127
+ xy-cut supplies the order.
128
+ TensorRT's timing-based tactic selection occasionally builds an engine that computes garbage for a correct graph
129
+ (seen for this model and for PP-DocLayoutV3 in v1.1: a fresh fp16 engine returned 2 of 7 blocks while PyTorch fp16,
130
+ ORT fp16 and rebuilt engines were exact). So every engine is self-tested against the bundled page + reference boxes
131
+ before use: fp16 -> fp16 rebuild -> fp32 TensorRT -> CUDA EP. The model itself is fp16-safe."""
132
+ # Fallback only: the real list comes from `models.layout_labels` in the config, in model index order.
133
+ # `barcode` is the class v1.2 lacked: without it an e-stamp panel can only be called `figure`, and
134
+ # suppressing that figure deleted 119 lines of real text across 87 pages.
135
+ NAMES = ["text", "title", "section_header", "list_item", "table", "figure", "caption", "footnote",
136
+ "formula", "header", "footer", "seal", "handwriting", "id_panel", "barcode"]
137
+ SIZE = 1024
138
+ def __init__(self, cfg=None, providers=None):
139
+ self.cfg = cfg or load_config(); self.prov = providers or Providers(self.cfg); lc = self.cfg["layout"]
140
+ self.path = str(self.cfg.path("models", "layout"))
141
+ use32 = (self.prov.cache_root / "layout_fp32" / "USE_FP32").exists() # a previous run found the fp16 engine wrong on this GPU
142
+ if getattr(self.prov, "background_trt", True) and not use32:
143
+ # start on CUDA, build fp16 then fp32 TensorRT in the background; an engine is only swapped in when it
144
+ # reproduces the reference blocks on the bundled page (the self-test that caught a wrong fp16 engine before)
145
+ self.s = self.prov.session_async(self.path, "layout", validate=lambda new, cur: self._validate_session(new),
146
+ plan=[(True, "layout"), (False, "layout_fp32")])
147
+ else:
148
+ self.s = self.prov.session(self.path, "layout", fp16=False, cache_sub="layout_fp32") if use32 else self.prov.session(self.path, "layout")
149
+ self.inp = self.s.get_inputs()[0].name
150
+ lp = self.cfg.path("models", "layout_labels") if self.cfg["models"].get("layout_labels") else None
151
+ self.labels = [l.strip() for l in open(lp, encoding="utf-8") if l.strip()] if lp and lp.exists() else list(self.NAMES)
152
+ self.trt = "Tensorrt" in self.s.get_providers()[0]; self.ok = set(); self.bs = int(lc.get("batch", 4)) # batch the batched pipeline should use
153
+ self.thr = float(lc.get("score_thresh", 0.3)); self.selftest_min_match = float(lc.get("selftest_min_match", 0.8))
154
+ # fill: the page's long side occupies this fraction of the canvas. The YOLOv10 head regresses at most 15 x 32 = 480 px per
155
+ # side from an anchor, so a block wider/taller than ~900 px of a 1024 canvas (whole-page tables on landscape gazettes /
156
+ # assembly records) comes out truncated and under-confident at fill 1.0; at 0.8 every page box fits (table conf 0.18 -> 0.79).
157
+ f = lc.get("fill", [1.0, 0.8]); self.fills = [float(v) for v in (f if isinstance(f, (list, tuple)) else [f])]; self.fill = self.fills[0]
158
+ st = self.cfg.path("models", "layout_selftest"); self.test_img = cv2.imread(str(st)) if st.exists() else None
159
+ rp = self.cfg.path("models", "layout_selftest_ref") if self.cfg["models"].get("layout_selftest_ref") else None
160
+ self.test_ref = __import__("json").load(open(rp, encoding="utf-8")) if rp and rp.exists() else None
161
+ self.stage = ("trt-fp32" if use32 else "trt-fp16") if self.trt else self.prov.status["layout"]["provider"]
162
+ @property
163
+ def provider(self): return self.prov.status.get("layout", {}).get("provider")
164
+ def _validate_session(self, sess):
165
+ """a candidate engine must reproduce the reference blocks on the bundled page before it may serve requests"""
166
+ if self.test_img is None or not self.test_ref: return True
167
+ try:
168
+ x1 = np.zeros((1, 3, self.SIZE, self.SIZE), np.float32); x1[0], _ = self.prep(self.test_img)
169
+ sess.run(None, {self.inp: x1}) # extends the optimisation profile to 1..bs before the check
170
+ x = np.zeros((self.bs, 3, self.SIZE, self.SIZE), np.float32); x[0] = x1[0]
171
+ got = self.parse(sess.run(None, {self.inp: x})[0][0], 1.0, thr=0.3)
172
+ except Exception: return False
173
+ def iou(a, b):
174
+ ix = max(0, min(a[2], b[2]) - max(a[0], b[0])); iy = max(0, min(a[3], b[3]) - max(a[1], b[1])); i = ix * iy
175
+ return i / max(1e-6, (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - i)
176
+ hit = sum(1 for ref in self.test_ref if any(g["type"] == ref["type"] and iou(g["bbox"], ref["bbox"]) >= 0.7 for g in got))
177
+ return hit / max(1, len(self.test_ref)) >= self.selftest_min_match
178
+ def prep(self, img_bgr, fill=None):
179
+ """letterbox (top-left, pad 114) to SIZE x SIZE, RGB, 0-1 -> ((3,S,S) float32, scale)"""
180
+ S = self.SIZE; h, w = img_bgr.shape[:2]; r = S * (self.fill if fill is None else fill) / max(h, w); nh, nw = max(1, int(round(h * r))), max(1, int(round(w * r)))
181
+ can = np.full((S, S, 3), 114, np.uint8); can[:nh, :nw] = cv2.resize(img_bgr, (nw, nh), interpolation=cv2.INTER_AREA)
182
+ return can[:, :, ::-1].transpose(2, 0, 1).astype(np.float32) / 255.0, r
183
+ def prep_from(self, base, img_bgr, fill):
184
+ """(canvas, scale) for `fill`, derived from the canvas already built at the largest fill"""
185
+ (arr, r0) = base; S = self.SIZE; h, w = img_bgr.shape[:2]
186
+ r = S * fill / max(h, w); nh0, nw0 = max(1, int(round(h * r0))), max(1, int(round(w * r0)))
187
+ nh, nw = max(1, int(round(h * r))), max(1, int(round(w * r)))
188
+ src = arr[:, :nh0, :nw0].transpose(1, 2, 0) # (nh0, nw0, 3) float32, already /255
189
+ small = cv2.resize(src, (nw, nh), interpolation=cv2.INTER_AREA)
190
+ can = np.full((S, S, 3), 114 / 255.0, np.float32); can[:nh, :nw] = small
191
+ return can.transpose(2, 0, 1), r
192
+
193
+ def run(self, x):
194
+ """raw graph call with a validated engine for this batch size; x: (bs,3,S,S) float32 -> (bs,300,6)"""
195
+ bs = x.shape[0]
196
+ if self.trt and bs not in self.ok: self.ensure(bs)
197
+ return self.s.run(None, {self.inp: x})[0]
198
+ def parse(self, out, r, thr=None):
199
+ thr = self.thr if thr is None else thr; blocks = []
200
+ for x0, y0, x1, y1, sc, cls in out:
201
+ if not (sc >= thr) or not np.isfinite([x0, y0, x1, y1]).all() or not 0 <= int(cls) < len(self.labels): continue
202
+ blocks.append({"type": self.labels[int(cls)], "score": float(sc), "bbox": [float(x0 / r), float(y0 / r), float(x1 / r), float(y1 / r)], "order": None})
203
+ return blocks
204
+ @staticmethod
205
+ def merge(primary, extra, page_hw, min_frac=0.7, iou_thr=0.5, covered=0.5):
206
+ """primary = boxes from the first fill (kept as they are); extra = boxes from the reduced fill(s), which exist only to
207
+ recover blocks too large for the head at full fill: an extra box is added when its long side is >= min_frac of the page's
208
+ long side, no same-class primary box overlaps it at IoU >= iou_thr, and same-class primary boxes cover < `covered` of its
209
+ area (a form page already split into sub-tables keeps the sub-tables). Geometry only."""
210
+ L = float(max(page_hw)); out = list(primary)
211
+ def area(bb): return max(0.0, bb[2] - bb[0]) * max(0.0, bb[3] - bb[1])
212
+ def inter(a, b): return max(0.0, min(a[2], b[2]) - max(a[0], b[0])) * max(0.0, min(a[3], b[3]) - max(a[1], b[1]))
213
+ for e in sorted(extra, key=lambda x: -x["score"]):
214
+ bb = e["bbox"]
215
+ if max(bb[2] - bb[0], bb[3] - bb[1]) < min_frac * L: continue
216
+ same = [k for k in out if k["type"] == e["type"]]
217
+ if any(inter(bb, k["bbox"]) / max(1e-6, area(bb) + area(k["bbox"]) - inter(bb, k["bbox"])) >= iou_thr for k in same): continue
218
+ if sum(inter(bb, k["bbox"]) for k in same) >= covered * area(bb): continue
219
+ out.append(e)
220
+ return out
221
+ def batch(self, imgs):
222
+ """blocks for a list of pages: every page is run at each configured fill (one canvas per fill), run at the validated
223
+ engine batch (zero-padded), and the per-fill boxes are merged by class-aware NMS."""
224
+ if not imgs: return []
225
+ items = [(i, f) for i in range(len(imgs)) for f in self.fills]; per = [{f: [] for f in self.fills} for _ in imgs]; BS = self.bs
226
+ prepped = {}
227
+ for j in range(0, len(items), BS):
228
+ chunk = items[j:j + BS]; k = len(chunk); x = np.zeros((BS, 3, self.SIZE, self.SIZE), np.float32); rs = []
229
+ for n, (i, f) in enumerate(chunk):
230
+ got = prepped.get((i, f))
231
+ if got is None:
232
+ # the reduced fills are letterboxes of the same page: resize the largest canvas down instead of
233
+ # resampling the full-resolution page again (a 2200 px page costs ~4 ms per extra pass)
234
+ base = prepped.get((i, max(self.fills)))
235
+ got = prepped[(i, f)] = self.prep_from(base, imgs[i], f) if (base is not None and f < max(self.fills)) else self.prep(imgs[i], f)
236
+ x[n], r = got; rs.append(r)
237
+ o = self.run(x)
238
+ for n, (i, f) in enumerate(chunk): per[i][f] += self.parse(o[n], rs[n])
239
+ prepped.clear()
240
+ f0 = self.fills[0]
241
+ return [self.merge(d[f0], [b for f in self.fills[1:] for b in d[f]], imgs[i].shape[:2]) if len(self.fills) > 1 else d[f0] for i, d in enumerate(per)]
242
+ def _selftest(self, bs):
243
+ """fraction of the reference boxes (bundled page) that the engine reproduces (same class, IoU >= 0.7 at conf >= 0.3);
244
+ without a reference file: 1.0 if the engine returns >= 3 finite blocks, else 0."""
245
+ if self.test_img is None: return 1.0
246
+ x = np.zeros((bs, 3, self.SIZE, self.SIZE), np.float32); x[0], r = self.prep(self.test_img)
247
+ o = self.s.run(None, {self.inp: x})[0][0]; got = self.parse(o, 1.0, thr=0.3)
248
+ if not self.test_ref: return 1.0 if len(got) >= 3 else 0.0
249
+ def iou(a, b):
250
+ ix = max(0, min(a[2], b[2]) - max(a[0], b[0])); iy = max(0, min(a[3], b[3]) - max(a[1], b[1])); i = ix * iy
251
+ return i / max(1e-6, (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - i)
252
+ hit = sum(1 for ref in self.test_ref if any(g["type"] == ref["type"] and iou(g["bbox"], ref["bbox"]) >= 0.7 for g in got))
253
+ return hit / max(1, len(self.test_ref))
254
+ def ensure(self, bs):
255
+ """validate the TensorRT engine for batch `bs`. Plan: shipped/cached fp16 engine -> fp16 rebuilt with a 1..bs optimisation
256
+ profile (RTX 5060 Ti, TensorRT 10.16: the fp16 engine built for the fixed shape [bs=4] drops every wide block, the same
257
+ graph built for [1..4] is exact) -> fp32 TensorRT (own cache dir) -> CUDA EP. A fallback below fp16 is remembered in
258
+ `<cache>/layout_fp32/USE_FP32` so later runs go straight to the working engine instead of rebuilding fp16 each start."""
259
+ st = self.prov.status["layout"]; marker = self.prov.cache_root / "layout_fp32" / "USE_FP32"
260
+ plan = [("fp16", True, "layout", False), ("fp16", True, "layout", True), ("fp32", False, "layout_fp32", False)]
261
+ start = 2 if (marker.exists() and self.stage == "trt-fp32") else 0
262
+ for attempt in range(start, len(plan)):
263
+ tag, fp16, sub, warm = plan[attempt]
264
+ t = time.time()
265
+ if warm: self.s.run(None, {self.inp: np.zeros((1, 3, self.SIZE, self.SIZE), np.float32)}) # extend the profile to 1..bs before the test
266
+ m = self._selftest(bs)
267
+ if m >= self.selftest_min_match:
268
+ self.ok.add(bs); self.bs = bs; self.stage = f"trt-{tag}"; st["fp16"] = fp16
269
+ if fp16 and marker.exists(): marker.unlink()
270
+ return
271
+ msg = f"[layout] TensorRT {tag} engine for batch {bs} failed its self-test ({m:.0%} of the reference blocks, attempt {attempt + 1}, {time.time() - t:.0f}s) -> rebuilding"
272
+ LOG.warning(msg); st["fallbacks"].append(msg); self.prov.events.append(msg)
273
+ self.s = None; shutil.rmtree(str(self.prov.cache_root / sub), ignore_errors=True)
274
+ if attempt + 1 < len(plan):
275
+ _, nfp16, nsub, _ = plan[attempt + 1]; self.s = self.prov.session(self.path, "layout", fp16=nfp16, cache_sub=nsub)
276
+ if not nfp16: marker.parent.mkdir(parents=True, exist_ok=True); marker.write_text("fp16 layout engine failed its self-test on this GPU; using the fp32 TensorRT engine\n")
277
+ msg = "[layout] no working TensorRT engine -> CUDA execution provider for the layout model"
278
+ LOG.warning(msg); st["fallbacks"].append(msg); self.prov.events.append(msg)
279
+ self.s = self.prov.session(self.path, "layout", force="cuda"); self.trt = False; self.bs = bs; self.ok.add(bs); self.stage = st["provider"]
280
+ def __call__(self, img_bgr, thr=None):
281
+ """single page -> (blocks, None); blocks = [{type, score, bbox, order=None}] in page pixels"""
282
+ bl = self.batch([img_bgr])[0]
283
+ return ([b for b in bl if b["score"] >= thr] if thr is not None else bl), None
hi-en/kanen_infer/providers.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ONNX Runtime session factory with the provider policy trt -> cuda -> cpu and the engine-cache bookkeeping.
2
+
3
+ * `Providers(cfg)` reads the policy (`auto` | `trt` | `cuda` | `cpu`), checks `trt_cache/ENGINES.json` against the local GPU /
4
+ TensorRT version (a foreign or stale cache is moved aside so engines rebuild instead of crashing) and records, per model,
5
+ which execution provider it finally ended on plus every fallback that happened (`Providers.status`).
6
+ * `session(path, key, fp16=True, force=None, trt_opts=None)` creates one `InferenceSession`; a provider that cannot be
7
+ loaded (no tensorrt wheel, no CUDA device, corrupt engine) falls through to the next one in the chain.
8
+ Nothing here touches model outputs: the same graph runs on whichever provider loads."""
9
+ import json, logging, os, shutil, subprocess, threading, time
10
+ from pathlib import Path
11
+ import onnxruntime as ort
12
+
13
+ LOG = logging.getLogger("kanen")
14
+ TRT = "TensorrtExecutionProvider"; CUDA = "CUDAExecutionProvider"; CPU = "CPUExecutionProvider"
15
+ OV = "OpenVINOExecutionProvider" # only present in the onnxruntime-openvino build; 1.4-1.9x over the CPU provider
16
+ SHORT = {TRT: "trt", CUDA: "cuda", OV: "openvino", CPU: "cpu"}
17
+ MANIFEST = "ENGINES.json"
18
+ _PRELOADED = False
19
+ _BUILD_LOCK = threading.Lock() # one TensorRT build at a time: concurrent builds starve the session that is serving
20
+
21
+ def _cuda_libs_loadable():
22
+ import ctypes, sys
23
+ names = ["cublasLt64_12.dll", "cudart64_12.dll"] if sys.platform == "win32" else ["libcublasLt.so.12", "libcudart.so.12"]
24
+ try:
25
+ for n in names: ctypes.CDLL(n)
26
+ return True
27
+ except OSError: return False
28
+
29
+ def preload_cuda_libs():
30
+ """Make the CUDA 12 / cuDNN 9 runtime libraries visible to onnxruntime-gpu. Order: already on the loader path ->
31
+ `onnxruntime.preload_dlls()` (nvidia-cublas-cu12 / nvidia-cudnn-cu12 ... pip wheels) -> importing torch (whose wheel
32
+ bundles the same libraries). Without any of these onnxruntime silently falls back to the CPU provider."""
33
+ global _PRELOADED
34
+ if _PRELOADED: return
35
+ _PRELOADED = True
36
+ if _cuda_libs_loadable(): return
37
+ try:
38
+ if hasattr(ort, "preload_dlls"):
39
+ import contextlib, io
40
+ with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): ort.preload_dlls()
41
+ except Exception as e: LOG.debug(f"preload_dlls: {e}")
42
+ if _cuda_libs_loadable(): return
43
+ try:
44
+ import torch # noqa: F401
45
+ LOG.info("CUDA runtime libraries loaded through torch")
46
+ except Exception:
47
+ LOG.warning("CUDA 12 runtime libraries not found (install nvidia-cublas-cu12/nvidia-cudnn-cu12 or torch, or put CUDA on PATH): GPU providers will fall back to CPU")
48
+
49
+ def gpu_name():
50
+ try:
51
+ out = subprocess.run(["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], capture_output=True, text=True, timeout=10)
52
+ names = [l.strip() for l in out.stdout.splitlines() if l.strip()]
53
+ return names[0] if names else None
54
+ except Exception: return None
55
+
56
+ def trt_version():
57
+ try:
58
+ import tensorrt; return tensorrt.__version__
59
+ except Exception: return None
60
+
61
+ def md5_file(path, n=1 << 20):
62
+ import hashlib; h = hashlib.md5()
63
+ with open(path, "rb") as f:
64
+ for chunk in iter(lambda: f.read(n), b""): h.update(chunk)
65
+ return h.hexdigest()[:12]
66
+
67
+ class SwapSession:
68
+ """An InferenceSession that can be replaced at runtime. Used for the background TensorRT build: the pipeline starts on
69
+ the CUDA provider immediately (no 1-4 minute wait on an unseen GPU) and this object swaps in the TensorRT session once
70
+ it has been built AND validated against the session it replaces. Calls in flight keep using the old session."""
71
+ def __init__(self, session, key, on_swap=None):
72
+ self._s = session; self.key = key; self._lock = threading.Lock(); self.swapped = False; self.on_swap = on_swap
73
+ @property
74
+ def session(self):
75
+ with self._lock: return self._s
76
+ def swap(self, new):
77
+ with self._lock: self._s, self.swapped = new, True
78
+ if self.on_swap: self.on_swap(new)
79
+ def run(self, *a, **k): return self.session.run(*a, **k)
80
+ def get_inputs(self): return self.session.get_inputs()
81
+ def get_outputs(self): return self.session.get_outputs()
82
+ def get_providers(self): return self.session.get_providers()
83
+ def __getattr__(self, name): return getattr(self.__dict__["_s"], name)
84
+
85
+ class Providers:
86
+ def __init__(self, cfg):
87
+ self.cfg = cfg; p = cfg["provider"]
88
+ self.policy = str(p.get("policy", "auto")).lower(); self.fp16 = bool(p.get("trt_fp16", True))
89
+ # background_trt: serve on CUDA at once and swap in TensorRT when it is built AND validated (set 0 to build up front)
90
+ self.background_trt = os.environ.get("KANEN_BACKGROUND_TRT", str(int(bool(p.get("background_trt", True))))) not in ("0", "false", "False")
91
+ self.cache_root = cfg.path("provider", "engine_cache")
92
+ if self.policy != "cpu": preload_cuda_libs()
93
+ self.available = ort.get_available_providers()
94
+ self.status = {} # model key -> dict(requested, provider, fp16, fallbacks[], cache, load_s)
95
+ self.events = [] # human-readable fallback log
96
+ self.gpu = gpu_name() if CUDA in self.available else None; self.trt_ver = trt_version()
97
+ if self.policy in ("auto", "trt"): self._check_manifest()
98
+
99
+ # ---- engine cache manifest -------------------------------------------------------------------------------------
100
+ def _check_manifest(self):
101
+ """Engines are only valid for the GPU architecture + TensorRT version they were built with. A cache built elsewhere
102
+ is moved to trt_cache/stale_<timestamp>/ so the first run rebuilds instead of failing on a foreign engine."""
103
+ mf = self.cache_root / MANIFEST
104
+ if not mf.exists() or self.trt_ver is None: return
105
+ try: m = json.loads(mf.read_text(encoding="utf-8"))
106
+ except Exception: return
107
+ same_gpu = (not self.gpu) or (not m.get("gpu")) or (m.get("gpu") == self.gpu)
108
+ same_trt = (not m.get("tensorrt")) or (m.get("tensorrt") == self.trt_ver)
109
+ same_ort = (not m.get("onnxruntime")) or (m.get("onnxruntime") == ort.__version__) # ORT upgrades change the graph hash -> engines miss
110
+ if same_gpu and same_trt and same_ort: return
111
+ dst = self.cache_root / f"stale_{time.strftime('%Y%m%d_%H%M%S')}"; dst.mkdir(parents=True, exist_ok=True)
112
+ for sub in self.cache_root.iterdir():
113
+ if sub.is_dir() and not sub.name.startswith("stale_"): shutil.move(str(sub), str(dst / sub.name))
114
+ shutil.move(str(mf), str(dst / MANIFEST))
115
+ msg = f"engine cache was built for {m.get('gpu')} / TensorRT {m.get('tensorrt')}, this machine has {self.gpu} / {self.trt_ver}: moved to {dst.name}, engines rebuild on first use (~4 min)"
116
+ LOG.warning(msg); self.events.append(msg)
117
+
118
+ def write_manifest(self, extra=None):
119
+ """Called by prepare_engines after a validated build: per-model status + md5 of every engine file."""
120
+ engines = {}
121
+ for sub in sorted(self.cache_root.iterdir()) if self.cache_root.exists() else []:
122
+ if sub.is_dir() and not sub.name.startswith("stale_"):
123
+ engines[sub.name] = {f.name: {"md5": md5_file(f), "bytes": f.stat().st_size} for f in sorted(sub.iterdir()) if f.suffix in (".engine", ".profile", ".timing")}
124
+ models = {k: dict(v, cache=os.path.relpath(v["cache"], self.cache_root) if v.get("cache") else None) for k, v in self.status.items()} # relative: the manifest ships with the package
125
+ m = {"package": self.cfg.get("name"), "version": self.cfg.get("version"), "gpu": self.gpu, "tensorrt": self.trt_ver, "onnxruntime": ort.__version__,
126
+ "built": time.strftime("%Y-%m-%d %H:%M:%S"), "models": models, "engines": engines}
127
+ if extra: m.update(extra)
128
+ self.cache_root.mkdir(parents=True, exist_ok=True); (self.cache_root / MANIFEST).write_text(json.dumps(m, indent=1), encoding="utf-8")
129
+ return m
130
+
131
+ # ---- sessions ----------------------------------------------------------------------------------------------------
132
+ def chain(self, force=None):
133
+ pol = (force or self.policy)
134
+ ov = ["openvino"] if OV in self.available else []
135
+ order = {"auto": ["trt", "cuda"] + ov + ["cpu"], "trt": ["trt", "cuda"] + ov + ["cpu"], "cuda": ["cuda"] + ov + ["cpu"],
136
+ "openvino": ov + ["cpu"], "cpu": ["cpu"]}.get(pol)
137
+ if order is None: raise ValueError(f"unknown provider policy {pol!r} (auto|trt|cuda|openvino|cpu)")
138
+ return order
139
+
140
+ def _opts(self, kind, cache, fp16, trt_opts):
141
+ p = self.cfg["provider"]
142
+ cuda = (CUDA, {"cudnn_conv_algo_search": p.get("cudnn_conv_algo_search", "HEURISTIC"), "arena_extend_strategy": "kSameAsRequested",
143
+ "gpu_mem_limit": int(float(p.get("cuda_mem_limit_gb", 6)) * 1024 ** 3)})
144
+ if kind == "cpu": return [CPU]
145
+ if kind == "openvino":
146
+ o = {"device_type": str(p.get("openvino_device", "CPU")).upper()}
147
+ n = self.cfg["runtime"].get("workers")
148
+ if isinstance(n, int) and n > 0: o["num_of_threads"] = int(n)
149
+ return [(OV, o), CPU]
150
+ if kind == "cuda": return [cuda]
151
+ import tensorrt # noqa: F401 (loads libnvinfer / nvinfer_10.dll from the pip wheel so the ORT provider can find it)
152
+ cache.mkdir(parents=True, exist_ok=True)
153
+ o = {"trt_fp16_enable": bool(fp16), "trt_engine_cache_enable": True, "trt_engine_cache_path": str(cache), "trt_timing_cache_enable": True,
154
+ "trt_timing_cache_path": str(cache), "trt_max_workspace_size": int(float(p.get("trt_workspace_gb", 4)) * 1024 ** 3)}
155
+ if trt_opts: o.update(trt_opts)
156
+ return [(TRT, o), cuda]
157
+
158
+ def session(self, path, key, fp16=None, force=None, trt_opts=None, cache_sub=None):
159
+ """key: model name for the status table ('det', 'layout', 'rec'); cache_sub: engine sub-directory (default = key)."""
160
+ fp16 = self.fp16 if fp16 is None else fp16; cache = self.cache_root / (cache_sub or key)
161
+ so = ort.SessionOptions(); so.log_severity_level = 3
162
+ st = self.status.setdefault(key, {"requested": (force or self.policy), "provider": None, "fp16": fp16, "fallbacks": [], "cache": str(cache), "load_s": 0.0})
163
+ t0 = time.time(); last = None
164
+ for kind in self.chain(force):
165
+ if kind == "cuda" and CUDA not in self.available: st["fallbacks"].append("cuda: CUDAExecutionProvider not available in this onnxruntime build"); continue
166
+ if kind == "trt" and TRT not in self.available: st["fallbacks"].append("trt: TensorrtExecutionProvider not available in this onnxruntime build"); continue
167
+ if kind == "openvino" and OV not in self.available: continue
168
+ for attempt in (1, 2):
169
+ try:
170
+ s = ort.InferenceSession(str(path), so, providers=self._opts(kind, cache, fp16, trt_opts))
171
+ got = s.get_providers()[0]; want = {"trt": TRT, "cuda": CUDA, "openvino": OV, "cpu": CPU}[kind]
172
+ if got != want: # ORT loads silently on the next provider when a library is missing: treat as a failed attempt
173
+ msg = f"{key}: {kind} requested but onnxruntime created the session on {SHORT.get(got, got)} (provider library not loadable?)"
174
+ LOG.warning(msg); st["fallbacks"].append(msg); self.events.append(msg)
175
+ if kind != self.chain(force)[-1]: break
176
+ st["provider"] = SHORT.get(got, got); st["fp16"] = fp16 if got == TRT else False; st["load_s"] = round(time.time() - t0, 1)
177
+ return s
178
+ except Exception as e: # corrupt / foreign engine: clear this model's cache once and retry, then fall through
179
+ last = e; text = str(e); msg = f"{key}: {kind} provider failed ({type(e).__name__}: {text.splitlines()[0][:160]})"
180
+ LOG.warning(msg); st["fallbacks"].append(msg); self.events.append(msg)
181
+ if kind == "trt" and attempt == 1 and cache.exists() and any(w in text.lower() for w in ("engine", "deserializ", "cache")):
182
+ stale = cache.parent / f"stale_{time.strftime('%Y%m%d_%H%M%S')}_{cache.name}"; shutil.move(str(cache), str(stale))
183
+ self.events.append(f"{key}: engine cache moved to {stale.name}, rebuilding"); continue
184
+ break
185
+ raise RuntimeError(f"no execution provider could load {path}: {last}")
186
+
187
+ def session_async(self, path, key, validate=None, fp16=None, trt_opts=None, cache_sub=None, plan=None):
188
+ """Return a session that works NOW and becomes the TensorRT one later.
189
+ If TensorRT is not in the chain (policy cuda/cpu, no wheel, no GPU) this is just `session(...)`.
190
+ Otherwise: create the CUDA (or CPU) session, hand it back wrapped in SwapSession, and build TensorRT on a worker
191
+ thread. `validate(trt_session, current_session) -> bool` decides whether the new engine may be swapped in; a
192
+ failing engine is dropped and the model keeps running on what it had."""
193
+ if "trt" not in self.chain() or TRT not in self.available:
194
+ return self.session(path, key, fp16=fp16, trt_opts=trt_opts, cache_sub=cache_sub)
195
+ cache = self.cache_root / (cache_sub or key)
196
+ if cache.exists() and any(cache.glob("*.engine")):
197
+ # engines for this model are already built for this GPU/TensorRT (the manifest check has already moved a
198
+ # foreign cache aside): loading them takes a moment, so there is nothing to hide behind a background build
199
+ return self.session(path, key, fp16=fp16, trt_opts=trt_opts, cache_sub=cache_sub)
200
+ if getattr(self, "_optimize_requested", False):
201
+ # optimize() was already called: a session created later (e.g. the detector's second canvas size) should build
202
+ # its engine now rather than queue a build nobody will start
203
+ return self.session(path, key, fp16=fp16, trt_opts=trt_opts, cache_sub=cache_sub)
204
+ base = self.session(path, key, force="cuda", fp16=False, cache_sub=cache_sub)
205
+ st = self.status[key]; st["background_trt"] = "building"
206
+ holder = SwapSession(base, key)
207
+ attempts = plan or [(self.fp16 if fp16 is None else fp16, cache_sub or key)]
208
+ def build():
209
+ t0 = time.time()
210
+ with _BUILD_LOCK:
211
+ for i, (use_fp16, sub) in enumerate(attempts):
212
+ tag = f"fp16" if use_fp16 else "fp32"
213
+ try:
214
+ trt = self.session(path, f"{key}_trt{i}", fp16=use_fp16, trt_opts=trt_opts, cache_sub=sub)
215
+ if self.status[f"{key}_trt{i}"]["provider"] != "trt":
216
+ raise RuntimeError("build did not end on the TensorRT provider")
217
+ if validate is not None and not validate(trt, holder.session):
218
+ msg = f"[{key}] TensorRT {tag} engine failed validation after {time.time()-t0:.0f}s"
219
+ LOG.warning(msg); self.events.append(msg); st["background_trt"] = f"rejected ({tag})"; continue
220
+ holder.swap(trt); st["provider"] = "trt"; st["fp16"] = use_fp16
221
+ st["background_trt"] = f"swapped to {tag} after {time.time()-t0:.0f}s"
222
+ LOG.info(f"[{key}] TensorRT {tag} engine ready and validated after {time.time()-t0:.0f}s"); return
223
+ except Exception as e:
224
+ msg = f"[{key}] TensorRT {tag} build failed ({type(e).__name__}: {str(e).splitlines()[0][:140]})"
225
+ LOG.warning(msg); self.events.append(msg); st["background_trt"] = f"failed ({tag})"
226
+ self.events.append(f"[{key}] no TensorRT engine passed: staying on {st['provider']}")
227
+ self._pending = getattr(self, "_pending", []); self._pending.append((key, build))
228
+ st["background_trt"] = "queued"
229
+ return holder
230
+
231
+ def start_background_builds(self):
232
+ self._optimize_requested = True
233
+ """Kick off the queued TensorRT builds. Called by the long-lived entry points (server, endpoint handler, batch
234
+ runs) — not by one-shot CLI calls, where a 1-2 minute build would cost more than it saves. Idempotent."""
235
+ self.builds = getattr(self, "builds", [])
236
+ for key, fn in getattr(self, "_pending", []):
237
+ self.status[key]["background_trt"] = "building"
238
+ th = threading.Thread(target=fn, name=f"trt-build-{key}", daemon=True); th.start(); self.builds.append(th)
239
+ self._pending = []
240
+ return len(self.builds)
241
+
242
+ def wait_for_engines(self, timeout=None):
243
+ """block until the background TensorRT builds finish (used by `arjuna bench` and the Docker image build)"""
244
+ for th in getattr(self, "builds", []): th.join(timeout)
245
+ return {k: v.get("background_trt") for k, v in self.status.items() if "background_trt" in v}
246
+
247
+ def summary(self):
248
+ return {k: {"provider": v["provider"], "fp16": v["fp16"], "fallbacks": list(v["fallbacks"])} for k, v in self.status.items()}
hi-en/kanen_infer/rec_onnx.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ """KanEn recognizer, ONNX Runtime only (TensorRT fp16 / CUDA / CPU by the provider policy). Greedy CTC decode with the frozen charset.
3
+ Same batching rule as the training-side recognizer: every crop is padded only to its own width bucket (padding a short crop
4
+ to a much wider batch width makes the model double the first glyph)."""
5
+ import hashlib, json, math
6
+ import numpy as np, cv2
7
+ from .config import load_config
8
+ from .providers import Providers, md5_file
9
+ import logging
10
+ LOG = logging.getLogger("kanen")
11
+
12
+ class Conf(float):
13
+ """line confidence (mean max-prob over emitted characters) carrying `min_char` (weakest emitted character)"""
14
+ min_char = None
15
+
16
+ class KanEnRecognizer:
17
+ def __init__(self, onnx_path=None, buckets=None, cfg=None, providers=None):
18
+ self.cfg = cfg or load_config(); self.prov = providers or Providers(self.cfg); rc = self.cfg["recognizer"]
19
+ onnx_path = str(onnx_path or self.cfg.path("models", "rec")); self.path = onnx_path
20
+ self.buckets = tuple(int(b) for b in (buckets or rc["buckets"])); self.H = int(rc.get("height", 32)); self.MAX_RATIO = float(rc.get("max_ratio", 40)); self.bs = int(rc.get("batch", 64))
21
+ self.chars = json.load(open(self.cfg.path("models", "charset"), encoding="utf-8"))
22
+ self.onnx_md5 = md5_file(onnx_path); self.charset_md5 = hashlib.md5("".join(self.chars).encode("utf-8")).hexdigest()[:12]
23
+ trt_opts = {"trt_profile_min_shapes": f"image:1x3x{self.H}x{self.buckets[0]}", "trt_profile_max_shapes": f"image:64x3x{self.H}x{self.buckets[-1]}", "trt_profile_opt_shapes": f"image:48x3x{self.H}x256"}
24
+ def _same(new, cur): # the swapped engine must decode a probe crop to the same characters as the session it replaces
25
+ x = np.full((2, 3, self.H, 256), 0.5, np.float32)
26
+ try:
27
+ a = new.run(None, {new.get_inputs()[0].name: x})[0]; b = cur.run(None, {cur.get_inputs()[0].name: x})[0]
28
+ except Exception: return False
29
+ return a.shape == b.shape and bool((a.argmax(-1) == b.argmax(-1)).all())
30
+ self.s = (self.prov.session_async(onnx_path, "rec", validate=_same, trt_opts=trt_opts)
31
+ if getattr(self.prov, "background_trt", True) else self.prov.session(onnx_path, "rec", trt_opts=trt_opts))
32
+ self.name = "kanen-rec-" + ("trt" if "Tensorrt" in self.s.get_providers()[0] else "ort")
33
+ @property
34
+ def provider(self): return self.prov.status.get("rec", {}).get("provider")
35
+
36
+ def _provider(self):
37
+ try: return self.s.get_providers()[0]
38
+ except Exception: return ""
39
+ def __call__(self, crops, bs=None):
40
+ if not crops: return []
41
+ bs = bs or self.bs; H = self.H
42
+ # the CUDA provider runs this graph in fp32 and needs far more memory per item than the fp16 TensorRT engine;
43
+ # while we are on CUDA (e.g. during the background engine build) cap the batch so a large configured batch
44
+ # cannot exhaust the arena. The cap lifts by itself once the TensorRT engine is swapped in.
45
+ if "Tensorrt" not in self._provider(): bs = min(bs, int(os.environ.get("KANEN_CUDA_REC_BATCH", "16")))
46
+ gray = [c if c.ndim == 2 else cv2.cvtColor(c, cv2.COLOR_BGR2GRAY) for c in crops]
47
+ ratios = [min(g.shape[1] / max(g.shape[0], 1), self.MAX_RATIO) for g in gray]
48
+ bucket_of = lambda i: next((b for b in self.buckets if b >= int(math.ceil(ratios[i] * H / 4) * 4)), self.buckets[-1])
49
+ groups = {}
50
+ for i in range(len(gray)): groups.setdefault(bucket_of(i), []).append(i)
51
+ out = [None] * len(gray)
52
+ for W, members in sorted(groups.items()):
53
+ n = max(4, min(bs, int(bs * 320 // max(W, 64))))
54
+ for i in range(0, len(members), n):
55
+ idx = members[i:i + n]; x = np.ones((len(idx), 3, H, W), np.float32)
56
+ for k, j in enumerate(idx):
57
+ g = gray[j]; nw = min(W, max(8, int(round(g.shape[1] * H / max(g.shape[0], 1)))))
58
+ r = cv2.resize(g, (nw, H), interpolation=cv2.INTER_AREA if g.shape[0] > H else cv2.INTER_CUBIC).astype(np.float32) / 255.0
59
+ x[k, :, :, :nw] = (r - 0.5) / 0.5
60
+ try:
61
+ probs = self.s.run(None, {"image": x})[0] # (B, T, C) softmax
62
+ except Exception as e: # out of memory on this device: halve the batch and keep going (never fail a page)
63
+ if "memory" not in str(e).lower() and "alloc" not in str(e).lower(): raise
64
+ half = max(1, len(idx) // 2); LOG.warning(f"recognizer: {type(e).__name__} at batch {len(idx)} (width {W}), retrying at {half}")
65
+ probs = np.concatenate([self.s.run(None, {"image": x[k:k + half]})[0] for k in range(0, len(idx), half)], axis=0)
66
+ self.bs = max(4, min(self.bs, half * 2)) # remember the smaller batch for the rest of this run
67
+ ids = probs.argmax(-1); pmax = probs.max(-1)
68
+ for k, j in enumerate(idx):
69
+ row = ids[k]; keep = (row != 0) & (row != np.roll(row, 1)); s, prev = [], 0
70
+ for t in row:
71
+ if t != prev and t != 0: s.append(self.chars[t - 1])
72
+ prev = t
73
+ c = Conf(float(pmax[k][keep].mean()) if keep.any() else 0.0); c.min_char = float(pmax[k][keep].min()) if keep.any() else 0.0
74
+ out[j] = ("".join(s), c)
75
+ return out
hi-en/models/det/inference.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e4ef16c346d19d8aa16f3a9f7e42f20124063335c427591ea4518b076ad212a6
3
+ size 9870857
hi-en/models/layout/LICENSE-AGPL-3.0.txt ADDED
@@ -0,0 +1,661 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GNU AFFERO GENERAL PUBLIC LICENSE
2
+ Version 3, 19 November 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+ Preamble
9
+
10
+ The GNU Affero General Public License is a free, copyleft license for
11
+ software and other kinds of works, specifically designed to ensure
12
+ cooperation with the community in the case of network server software.
13
+
14
+ The licenses for most software and other practical works are designed
15
+ to take away your freedom to share and change the works. By contrast,
16
+ our General Public Licenses are intended to guarantee your freedom to
17
+ share and change all versions of a program--to make sure it remains free
18
+ software for all its users.
19
+
20
+ When we speak of free software, we are referring to freedom, not
21
+ price. Our General Public Licenses are designed to make sure that you
22
+ have the freedom to distribute copies of free software (and charge for
23
+ them if you wish), that you receive source code or can get it if you
24
+ want it, that you can change the software or use pieces of it in new
25
+ free programs, and that you know you can do these things.
26
+
27
+ Developers that use our General Public Licenses protect your rights
28
+ with two steps: (1) assert copyright on the software, and (2) offer
29
+ you this License which gives you legal permission to copy, distribute
30
+ and/or modify the software.
31
+
32
+ A secondary benefit of defending all users' freedom is that
33
+ improvements made in alternate versions of the program, if they
34
+ receive widespread use, become available for other developers to
35
+ incorporate. Many developers of free software are heartened and
36
+ encouraged by the resulting cooperation. However, in the case of
37
+ software used on network servers, this result may fail to come about.
38
+ The GNU General Public License permits making a modified version and
39
+ letting the public access it on a server without ever releasing its
40
+ source code to the public.
41
+
42
+ The GNU Affero General Public License is designed specifically to
43
+ ensure that, in such cases, the modified source code becomes available
44
+ to the community. It requires the operator of a network server to
45
+ provide the source code of the modified version running there to the
46
+ users of that server. Therefore, public use of a modified version, on
47
+ a publicly accessible server, gives the public access to the source
48
+ code of the modified version.
49
+
50
+ An older license, called the Affero General Public License and
51
+ published by Affero, was designed to accomplish similar goals. This is
52
+ a different license, not a version of the Affero GPL, but Affero has
53
+ released a new version of the Affero GPL which permits relicensing under
54
+ this license.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ TERMS AND CONDITIONS
60
+
61
+ 0. Definitions.
62
+
63
+ "This License" refers to version 3 of the GNU Affero General Public License.
64
+
65
+ "Copyright" also means copyright-like laws that apply to other kinds of
66
+ works, such as semiconductor masks.
67
+
68
+ "The Program" refers to any copyrightable work licensed under this
69
+ License. Each licensee is addressed as "you". "Licensees" and
70
+ "recipients" may be individuals or organizations.
71
+
72
+ To "modify" a work means to copy from or adapt all or part of the work
73
+ in a fashion requiring copyright permission, other than the making of an
74
+ exact copy. The resulting work is called a "modified version" of the
75
+ earlier work or a work "based on" the earlier work.
76
+
77
+ A "covered work" means either the unmodified Program or a work based
78
+ on the Program.
79
+
80
+ To "propagate" a work means to do anything with it that, without
81
+ permission, would make you directly or secondarily liable for
82
+ infringement under applicable copyright law, except executing it on a
83
+ computer or modifying a private copy. Propagation includes copying,
84
+ distribution (with or without modification), making available to the
85
+ public, and in some countries other activities as well.
86
+
87
+ To "convey" a work means any kind of propagation that enables other
88
+ parties to make or receive copies. Mere interaction with a user through
89
+ a computer network, with no transfer of a copy, is not conveying.
90
+
91
+ An interactive user interface displays "Appropriate Legal Notices"
92
+ to the extent that it includes a convenient and prominently visible
93
+ feature that (1) displays an appropriate copyright notice, and (2)
94
+ tells the user that there is no warranty for the work (except to the
95
+ extent that warranties are provided), that licensees may convey the
96
+ work under this License, and how to view a copy of this License. If
97
+ the interface presents a list of user commands or options, such as a
98
+ menu, a prominent item in the list meets this criterion.
99
+
100
+ 1. Source Code.
101
+
102
+ The "source code" for a work means the preferred form of the work
103
+ for making modifications to it. "Object code" means any non-source
104
+ form of a work.
105
+
106
+ A "Standard Interface" means an interface that either is an official
107
+ standard defined by a recognized standards body, or, in the case of
108
+ interfaces specified for a particular programming language, one that
109
+ is widely used among developers working in that language.
110
+
111
+ The "System Libraries" of an executable work include anything, other
112
+ than the work as a whole, that (a) is included in the normal form of
113
+ packaging a Major Component, but which is not part of that Major
114
+ Component, and (b) serves only to enable use of the work with that
115
+ Major Component, or to implement a Standard Interface for which an
116
+ implementation is available to the public in source code form. A
117
+ "Major Component", in this context, means a major essential component
118
+ (kernel, window system, and so on) of the specific operating system
119
+ (if any) on which the executable work runs, or a compiler used to
120
+ produce the work, or an object code interpreter used to run it.
121
+
122
+ The "Corresponding Source" for a work in object code form means all
123
+ the source code needed to generate, install, and (for an executable
124
+ work) run the object code and to modify the work, including scripts to
125
+ control those activities. However, it does not include the work's
126
+ System Libraries, or general-purpose tools or generally available free
127
+ programs which are used unmodified in performing those activities but
128
+ which are not part of the work. For example, Corresponding Source
129
+ includes interface definition files associated with source files for
130
+ the work, and the source code for shared libraries and dynamically
131
+ linked subprograms that the work is specifically designed to require,
132
+ such as by intimate data communication or control flow between those
133
+ subprograms and other parts of the work.
134
+
135
+ The Corresponding Source need not include anything that users
136
+ can regenerate automatically from other parts of the Corresponding
137
+ Source.
138
+
139
+ The Corresponding Source for a work in source code form is that
140
+ same work.
141
+
142
+ 2. Basic Permissions.
143
+
144
+ All rights granted under this License are granted for the term of
145
+ copyright on the Program, and are irrevocable provided the stated
146
+ conditions are met. This License explicitly affirms your unlimited
147
+ permission to run the unmodified Program. The output from running a
148
+ covered work is covered by this License only if the output, given its
149
+ content, constitutes a covered work. This License acknowledges your
150
+ rights of fair use or other equivalent, as provided by copyright law.
151
+
152
+ You may make, run and propagate covered works that you do not
153
+ convey, without conditions so long as your license otherwise remains
154
+ in force. You may convey covered works to others for the sole purpose
155
+ of having them make modifications exclusively for you, or provide you
156
+ with facilities for running those works, provided that you comply with
157
+ the terms of this License in conveying all material for which you do
158
+ not control copyright. Those thus making or running the covered works
159
+ for you must do so exclusively on your behalf, under your direction
160
+ and control, on terms that prohibit them from making any copies of
161
+ your copyrighted material outside their relationship with you.
162
+
163
+ Conveying under any other circumstances is permitted solely under
164
+ the conditions stated below. Sublicensing is not allowed; section 10
165
+ makes it unnecessary.
166
+
167
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168
+
169
+ No covered work shall be deemed part of an effective technological
170
+ measure under any applicable law fulfilling obligations under article
171
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172
+ similar laws prohibiting or restricting circumvention of such
173
+ measures.
174
+
175
+ When you convey a covered work, you waive any legal power to forbid
176
+ circumvention of technological measures to the extent such circumvention
177
+ is effected by exercising rights under this License with respect to
178
+ the covered work, and you disclaim any intention to limit operation or
179
+ modification of the work as a means of enforcing, against the work's
180
+ users, your or third parties' legal rights to forbid circumvention of
181
+ technological measures.
182
+
183
+ 4. Conveying Verbatim Copies.
184
+
185
+ You may convey verbatim copies of the Program's source code as you
186
+ receive it, in any medium, provided that you conspicuously and
187
+ appropriately publish on each copy an appropriate copyright notice;
188
+ keep intact all notices stating that this License and any
189
+ non-permissive terms added in accord with section 7 apply to the code;
190
+ keep intact all notices of the absence of any warranty; and give all
191
+ recipients a copy of this License along with the Program.
192
+
193
+ You may charge any price or no price for each copy that you convey,
194
+ and you may offer support or warranty protection for a fee.
195
+
196
+ 5. Conveying Modified Source Versions.
197
+
198
+ You may convey a work based on the Program, or the modifications to
199
+ produce it from the Program, in the form of source code under the
200
+ terms of section 4, provided that you also meet all of these conditions:
201
+
202
+ a) The work must carry prominent notices stating that you modified
203
+ it, and giving a relevant date.
204
+
205
+ b) The work must carry prominent notices stating that it is
206
+ released under this License and any conditions added under section
207
+ 7. This requirement modifies the requirement in section 4 to
208
+ "keep intact all notices".
209
+
210
+ c) You must license the entire work, as a whole, under this
211
+ License to anyone who comes into possession of a copy. This
212
+ License will therefore apply, along with any applicable section 7
213
+ additional terms, to the whole of the work, and all its parts,
214
+ regardless of how they are packaged. This License gives no
215
+ permission to license the work in any other way, but it does not
216
+ invalidate such permission if you have separately received it.
217
+
218
+ d) If the work has interactive user interfaces, each must display
219
+ Appropriate Legal Notices; however, if the Program has interactive
220
+ interfaces that do not display Appropriate Legal Notices, your
221
+ work need not make them do so.
222
+
223
+ A compilation of a covered work with other separate and independent
224
+ works, which are not by their nature extensions of the covered work,
225
+ and which are not combined with it such as to form a larger program,
226
+ in or on a volume of a storage or distribution medium, is called an
227
+ "aggregate" if the compilation and its resulting copyright are not
228
+ used to limit the access or legal rights of the compilation's users
229
+ beyond what the individual works permit. Inclusion of a covered work
230
+ in an aggregate does not cause this License to apply to the other
231
+ parts of the aggregate.
232
+
233
+ 6. Conveying Non-Source Forms.
234
+
235
+ You may convey a covered work in object code form under the terms
236
+ of sections 4 and 5, provided that you also convey the
237
+ machine-readable Corresponding Source under the terms of this License,
238
+ in one of these ways:
239
+
240
+ a) Convey the object code in, or embodied in, a physical product
241
+ (including a physical distribution medium), accompanied by the
242
+ Corresponding Source fixed on a durable physical medium
243
+ customarily used for software interchange.
244
+
245
+ b) Convey the object code in, or embodied in, a physical product
246
+ (including a physical distribution medium), accompanied by a
247
+ written offer, valid for at least three years and valid for as
248
+ long as you offer spare parts or customer support for that product
249
+ model, to give anyone who possesses the object code either (1) a
250
+ copy of the Corresponding Source for all the software in the
251
+ product that is covered by this License, on a durable physical
252
+ medium customarily used for software interchange, for a price no
253
+ more than your reasonable cost of physically performing this
254
+ conveying of source, or (2) access to copy the
255
+ Corresponding Source from a network server at no charge.
256
+
257
+ c) Convey individual copies of the object code with a copy of the
258
+ written offer to provide the Corresponding Source. This
259
+ alternative is allowed only occasionally and noncommercially, and
260
+ only if you received the object code with such an offer, in accord
261
+ with subsection 6b.
262
+
263
+ d) Convey the object code by offering access from a designated
264
+ place (gratis or for a charge), and offer equivalent access to the
265
+ Corresponding Source in the same way through the same place at no
266
+ further charge. You need not require recipients to copy the
267
+ Corresponding Source along with the object code. If the place to
268
+ copy the object code is a network server, the Corresponding Source
269
+ may be on a different server (operated by you or a third party)
270
+ that supports equivalent copying facilities, provided you maintain
271
+ clear directions next to the object code saying where to find the
272
+ Corresponding Source. Regardless of what server hosts the
273
+ Corresponding Source, you remain obligated to ensure that it is
274
+ available for as long as needed to satisfy these requirements.
275
+
276
+ e) Convey the object code using peer-to-peer transmission, provided
277
+ you inform other peers where the object code and Corresponding
278
+ Source of the work are being offered to the general public at no
279
+ charge under subsection 6d.
280
+
281
+ A separable portion of the object code, whose source code is excluded
282
+ from the Corresponding Source as a System Library, need not be
283
+ included in conveying the object code work.
284
+
285
+ A "User Product" is either (1) a "consumer product", which means any
286
+ tangible personal property which is normally used for personal, family,
287
+ or household purposes, or (2) anything designed or sold for incorporation
288
+ into a dwelling. In determining whether a product is a consumer product,
289
+ doubtful cases shall be resolved in favor of coverage. For a particular
290
+ product received by a particular user, "normally used" refers to a
291
+ typical or common use of that class of product, regardless of the status
292
+ of the particular user or of the way in which the particular user
293
+ actually uses, or expects or is expected to use, the product. A product
294
+ is a consumer product regardless of whether the product has substantial
295
+ commercial, industrial or non-consumer uses, unless such uses represent
296
+ the only significant mode of use of the product.
297
+
298
+ "Installation Information" for a User Product means any methods,
299
+ procedures, authorization keys, or other information required to install
300
+ and execute modified versions of a covered work in that User Product from
301
+ a modified version of its Corresponding Source. The information must
302
+ suffice to ensure that the continued functioning of the modified object
303
+ code is in no case prevented or interfered with solely because
304
+ modification has been made.
305
+
306
+ If you convey an object code work under this section in, or with, or
307
+ specifically for use in, a User Product, and the conveying occurs as
308
+ part of a transaction in which the right of possession and use of the
309
+ User Product is transferred to the recipient in perpetuity or for a
310
+ fixed term (regardless of how the transaction is characterized), the
311
+ Corresponding Source conveyed under this section must be accompanied
312
+ by the Installation Information. But this requirement does not apply
313
+ if neither you nor any third party retains the ability to install
314
+ modified object code on the User Product (for example, the work has
315
+ been installed in ROM).
316
+
317
+ The requirement to provide Installation Information does not include a
318
+ requirement to continue to provide support service, warranty, or updates
319
+ for a work that has been modified or installed by the recipient, or for
320
+ the User Product in which it has been modified or installed. Access to a
321
+ network may be denied when the modification itself materially and
322
+ adversely affects the operation of the network or violates the rules and
323
+ protocols for communication across the network.
324
+
325
+ Corresponding Source conveyed, and Installation Information provided,
326
+ in accord with this section must be in a format that is publicly
327
+ documented (and with an implementation available to the public in
328
+ source code form), and must require no special password or key for
329
+ unpacking, reading or copying.
330
+
331
+ 7. Additional Terms.
332
+
333
+ "Additional permissions" are terms that supplement the terms of this
334
+ License by making exceptions from one or more of its conditions.
335
+ Additional permissions that are applicable to the entire Program shall
336
+ be treated as though they were included in this License, to the extent
337
+ that they are valid under applicable law. If additional permissions
338
+ apply only to part of the Program, that part may be used separately
339
+ under those permissions, but the entire Program remains governed by
340
+ this License without regard to the additional permissions.
341
+
342
+ When you convey a copy of a covered work, you may at your option
343
+ remove any additional permissions from that copy, or from any part of
344
+ it. (Additional permissions may be written to require their own
345
+ removal in certain cases when you modify the work.) You may place
346
+ additional permissions on material, added by you to a covered work,
347
+ for which you have or can give appropriate copyright permission.
348
+
349
+ Notwithstanding any other provision of this License, for material you
350
+ add to a covered work, you may (if authorized by the copyright holders of
351
+ that material) supplement the terms of this License with terms:
352
+
353
+ a) Disclaiming warranty or limiting liability differently from the
354
+ terms of sections 15 and 16 of this License; or
355
+
356
+ b) Requiring preservation of specified reasonable legal notices or
357
+ author attributions in that material or in the Appropriate Legal
358
+ Notices displayed by works containing it; or
359
+
360
+ c) Prohibiting misrepresentation of the origin of that material, or
361
+ requiring that modified versions of such material be marked in
362
+ reasonable ways as different from the original version; or
363
+
364
+ d) Limiting the use for publicity purposes of names of licensors or
365
+ authors of the material; or
366
+
367
+ e) Declining to grant rights under trademark law for use of some
368
+ trade names, trademarks, or service marks; or
369
+
370
+ f) Requiring indemnification of licensors and authors of that
371
+ material by anyone who conveys the material (or modified versions of
372
+ it) with contractual assumptions of liability to the recipient, for
373
+ any liability that these contractual assumptions directly impose on
374
+ those licensors and authors.
375
+
376
+ All other non-permissive additional terms are considered "further
377
+ restrictions" within the meaning of section 10. If the Program as you
378
+ received it, or any part of it, contains a notice stating that it is
379
+ governed by this License along with a term that is a further
380
+ restriction, you may remove that term. If a license document contains
381
+ a further restriction but permits relicensing or conveying under this
382
+ License, you may add to a covered work material governed by the terms
383
+ of that license document, provided that the further restriction does
384
+ not survive such relicensing or conveying.
385
+
386
+ If you add terms to a covered work in accord with this section, you
387
+ must place, in the relevant source files, a statement of the
388
+ additional terms that apply to those files, or a notice indicating
389
+ where to find the applicable terms.
390
+
391
+ Additional terms, permissive or non-permissive, may be stated in the
392
+ form of a separately written license, or stated as exceptions;
393
+ the above requirements apply either way.
394
+
395
+ 8. Termination.
396
+
397
+ You may not propagate or modify a covered work except as expressly
398
+ provided under this License. Any attempt otherwise to propagate or
399
+ modify it is void, and will automatically terminate your rights under
400
+ this License (including any patent licenses granted under the third
401
+ paragraph of section 11).
402
+
403
+ However, if you cease all violation of this License, then your
404
+ license from a particular copyright holder is reinstated (a)
405
+ provisionally, unless and until the copyright holder explicitly and
406
+ finally terminates your license, and (b) permanently, if the copyright
407
+ holder fails to notify you of the violation by some reasonable means
408
+ prior to 60 days after the cessation.
409
+
410
+ Moreover, your license from a particular copyright holder is
411
+ reinstated permanently if the copyright holder notifies you of the
412
+ violation by some reasonable means, this is the first time you have
413
+ received notice of violation of this License (for any work) from that
414
+ copyright holder, and you cure the violation prior to 30 days after
415
+ your receipt of the notice.
416
+
417
+ Termination of your rights under this section does not terminate the
418
+ licenses of parties who have received copies or rights from you under
419
+ this License. If your rights have been terminated and not permanently
420
+ reinstated, you do not qualify to receive new licenses for the same
421
+ material under section 10.
422
+
423
+ 9. Acceptance Not Required for Having Copies.
424
+
425
+ You are not required to accept this License in order to receive or
426
+ run a copy of the Program. Ancillary propagation of a covered work
427
+ occurring solely as a consequence of using peer-to-peer transmission
428
+ to receive a copy likewise does not require acceptance. However,
429
+ nothing other than this License grants you permission to propagate or
430
+ modify any covered work. These actions infringe copyright if you do
431
+ not accept this License. Therefore, by modifying or propagating a
432
+ covered work, you indicate your acceptance of this License to do so.
433
+
434
+ 10. Automatic Licensing of Downstream Recipients.
435
+
436
+ Each time you convey a covered work, the recipient automatically
437
+ receives a license from the original licensors, to run, modify and
438
+ propagate that work, subject to this License. You are not responsible
439
+ for enforcing compliance by third parties with this License.
440
+
441
+ An "entity transaction" is a transaction transferring control of an
442
+ organization, or substantially all assets of one, or subdividing an
443
+ organization, or merging organizations. If propagation of a covered
444
+ work results from an entity transaction, each party to that
445
+ transaction who receives a copy of the work also receives whatever
446
+ licenses to the work the party's predecessor in interest had or could
447
+ give under the previous paragraph, plus a right to possession of the
448
+ Corresponding Source of the work from the predecessor in interest, if
449
+ the predecessor has it or can get it with reasonable efforts.
450
+
451
+ You may not impose any further restrictions on the exercise of the
452
+ rights granted or affirmed under this License. For example, you may
453
+ not impose a license fee, royalty, or other charge for exercise of
454
+ rights granted under this License, and you may not initiate litigation
455
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
456
+ any patent claim is infringed by making, using, selling, offering for
457
+ sale, or importing the Program or any portion of it.
458
+
459
+ 11. Patents.
460
+
461
+ A "contributor" is a copyright holder who authorizes use under this
462
+ License of the Program or a work on which the Program is based. The
463
+ work thus licensed is called the contributor's "contributor version".
464
+
465
+ A contributor's "essential patent claims" are all patent claims
466
+ owned or controlled by the contributor, whether already acquired or
467
+ hereafter acquired, that would be infringed by some manner, permitted
468
+ by this License, of making, using, or selling its contributor version,
469
+ but do not include claims that would be infringed only as a
470
+ consequence of further modification of the contributor version. For
471
+ purposes of this definition, "control" includes the right to grant
472
+ patent sublicenses in a manner consistent with the requirements of
473
+ this License.
474
+
475
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
476
+ patent license under the contributor's essential patent claims, to
477
+ make, use, sell, offer for sale, import and otherwise run, modify and
478
+ propagate the contents of its contributor version.
479
+
480
+ In the following three paragraphs, a "patent license" is any express
481
+ agreement or commitment, however denominated, not to enforce a patent
482
+ (such as an express permission to practice a patent or covenant not to
483
+ sue for patent infringement). To "grant" such a patent license to a
484
+ party means to make such an agreement or commitment not to enforce a
485
+ patent against the party.
486
+
487
+ If you convey a covered work, knowingly relying on a patent license,
488
+ and the Corresponding Source of the work is not available for anyone
489
+ to copy, free of charge and under the terms of this License, through a
490
+ publicly available network server or other readily accessible means,
491
+ then you must either (1) cause the Corresponding Source to be so
492
+ available, or (2) arrange to deprive yourself of the benefit of the
493
+ patent license for this particular work, or (3) arrange, in a manner
494
+ consistent with the requirements of this License, to extend the patent
495
+ license to downstream recipients. "Knowingly relying" means you have
496
+ actual knowledge that, but for the patent license, your conveying the
497
+ covered work in a country, or your recipient's use of the covered work
498
+ in a country, would infringe one or more identifiable patents in that
499
+ country that you have reason to believe are valid.
500
+
501
+ If, pursuant to or in connection with a single transaction or
502
+ arrangement, you convey, or propagate by procuring conveyance of, a
503
+ covered work, and grant a patent license to some of the parties
504
+ receiving the covered work authorizing them to use, propagate, modify
505
+ or convey a specific copy of the covered work, then the patent license
506
+ you grant is automatically extended to all recipients of the covered
507
+ work and works based on it.
508
+
509
+ A patent license is "discriminatory" if it does not include within
510
+ the scope of its coverage, prohibits the exercise of, or is
511
+ conditioned on the non-exercise of one or more of the rights that are
512
+ specifically granted under this License. You may not convey a covered
513
+ work if you are a party to an arrangement with a third party that is
514
+ in the business of distributing software, under which you make payment
515
+ to the third party based on the extent of your activity of conveying
516
+ the work, and under which the third party grants, to any of the
517
+ parties who would receive the covered work from you, a discriminatory
518
+ patent license (a) in connection with copies of the covered work
519
+ conveyed by you (or copies made from those copies), or (b) primarily
520
+ for and in connection with specific products or compilations that
521
+ contain the covered work, unless you entered into that arrangement,
522
+ or that patent license was granted, prior to 28 March 2007.
523
+
524
+ Nothing in this License shall be construed as excluding or limiting
525
+ any implied license or other defenses to infringement that may
526
+ otherwise be available to you under applicable patent law.
527
+
528
+ 12. No Surrender of Others' Freedom.
529
+
530
+ If conditions are imposed on you (whether by court order, agreement or
531
+ otherwise) that contradict the conditions of this License, they do not
532
+ excuse you from the conditions of this License. If you cannot convey a
533
+ covered work so as to satisfy simultaneously your obligations under this
534
+ License and any other pertinent obligations, then as a consequence you may
535
+ not convey it at all. For example, if you agree to terms that obligate you
536
+ to collect a royalty for further conveying from those to whom you convey
537
+ the Program, the only way you could satisfy both those terms and this
538
+ License would be to refrain entirely from conveying the Program.
539
+
540
+ 13. Remote Network Interaction; Use with the GNU General Public License.
541
+
542
+ Notwithstanding any other provision of this License, if you modify the
543
+ Program, your modified version must prominently offer all users
544
+ interacting with it remotely through a computer network (if your version
545
+ supports such interaction) an opportunity to receive the Corresponding
546
+ Source of your version by providing access to the Corresponding Source
547
+ from a network server at no charge, through some standard or customary
548
+ means of facilitating copying of software. This Corresponding Source
549
+ shall include the Corresponding Source for any work covered by version 3
550
+ of the GNU General Public License that is incorporated pursuant to the
551
+ following paragraph.
552
+
553
+ Notwithstanding any other provision of this License, you have
554
+ permission to link or combine any covered work with a work licensed
555
+ under version 3 of the GNU General Public License into a single
556
+ combined work, and to convey the resulting work. The terms of this
557
+ License will continue to apply to the part which is the covered work,
558
+ but the work with which it is combined will remain governed by version
559
+ 3 of the GNU General Public License.
560
+
561
+ 14. Revised Versions of this License.
562
+
563
+ The Free Software Foundation may publish revised and/or new versions of
564
+ the GNU Affero General Public License from time to time. Such new versions
565
+ will be similar in spirit to the present version, but may differ in detail to
566
+ address new problems or concerns.
567
+
568
+ Each version is given a distinguishing version number. If the
569
+ Program specifies that a certain numbered version of the GNU Affero General
570
+ Public License "or any later version" applies to it, you have the
571
+ option of following the terms and conditions either of that numbered
572
+ version or of any later version published by the Free Software
573
+ Foundation. If the Program does not specify a version number of the
574
+ GNU Affero General Public License, you may choose any version ever published
575
+ by the Free Software Foundation.
576
+
577
+ If the Program specifies that a proxy can decide which future
578
+ versions of the GNU Affero General Public License can be used, that proxy's
579
+ public statement of acceptance of a version permanently authorizes you
580
+ to choose that version for the Program.
581
+
582
+ Later license versions may give you additional or different
583
+ permissions. However, no additional obligations are imposed on any
584
+ author or copyright holder as a result of your choosing to follow a
585
+ later version.
586
+
587
+ 15. Disclaimer of Warranty.
588
+
589
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597
+
598
+ 16. Limitation of Liability.
599
+
600
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608
+ SUCH DAMAGES.
609
+
610
+ 17. Interpretation of Sections 15 and 16.
611
+
612
+ If the disclaimer of warranty and limitation of liability provided
613
+ above cannot be given local legal effect according to their terms,
614
+ reviewing courts shall apply local law that most closely approximates
615
+ an absolute waiver of all civil liability in connection with the
616
+ Program, unless a warranty or assumption of liability accompanies a
617
+ copy of the Program in return for a fee.
618
+
619
+ END OF TERMS AND CONDITIONS
620
+
621
+ How to Apply These Terms to Your New Programs
622
+
623
+ If you develop a new program, and you want it to be of the greatest
624
+ possible use to the public, the best way to achieve this is to make it
625
+ free software which everyone can redistribute and change under these terms.
626
+
627
+ To do so, attach the following notices to the program. It is safest
628
+ to attach them to the start of each source file to most effectively
629
+ state the exclusion of warranty; and each file should have at least
630
+ the "copyright" line and a pointer to where the full notice is found.
631
+
632
+ <one line to give the program's name and a brief idea of what it does.>
633
+ Copyright (C) <year> <name of author>
634
+
635
+ This program is free software: you can redistribute it and/or modify
636
+ it under the terms of the GNU Affero General Public License as published by
637
+ the Free Software Foundation, either version 3 of the License, or
638
+ (at your option) any later version.
639
+
640
+ This program is distributed in the hope that it will be useful,
641
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
642
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643
+ GNU Affero General Public License for more details.
644
+
645
+ You should have received a copy of the GNU Affero General Public License
646
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
647
+
648
+ Also add information on how to contact you by electronic and paper mail.
649
+
650
+ If your software can interact with users remotely through a computer
651
+ network, you should also make sure that it provides a way for users to
652
+ get its source. For example, if your program is a web application, its
653
+ interface could display a "Source" link that leads users to an archive
654
+ of the code. There are many ways you could offer source, and different
655
+ solutions will be better for different programs; see section 13 for the
656
+ specific requirements.
657
+
658
+ You should also get your employer (if you work as a programmer) or school,
659
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
660
+ For more information on this, and how to apply and follow the GNU AGPL, see
661
+ <https://www.gnu.org/licenses/>.
hi-en/models/layout/NOTICE.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Layout model licence — every `*_layout_*.onnx` in this directory
2
+
3
+ This package ships `arjuna_layout_v15.onnx` and `kanen_layout_v14.onnx`. **All of them** are DocLayout-YOLO / YOLOv10-based detectors fine-tuned from the
4
+ `juliozhao/DocLayout-YOLO-DocStructBench` weights (Apache-2.0) with the `doclayout_yolo` training toolkit, which is
5
+ licensed under the **GNU Affero General Public License v3.0** (Ultralytics YOLO lineage). To respect that lineage the
6
+ layout model files in this directory are distributed under **AGPL-3.0** (full text: `LICENSE-AGPL-3.0.txt`).
7
+
8
+ Everything else in this repository — the inference package `kanen_infer`, the recognizer (SVTRv2 + language pack),
9
+ the text detector (PP-OCRv6) and the documentation — is Apache-2.0 as stated in the top-level `LICENSE`.
10
+
11
+ Practical consequences:
12
+ - Using the package with any of these layout models in a networked service falls under AGPL-3.0 for the layout model. If that
13
+ does not suit you, run with `layout.mode: never` in `kanen.yaml` (text lines and tables from ruled grids still work
14
+ without the layout model) or plug in another layout model of your choice (`models.layout`, `models.layout_labels`).
15
+ - No AGPL toolkit code is included here; inference runs on ONNX Runtime.
hi-en/models/layout/arjuna_layout_v15.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:188ac04201f37cf2a78c47f2793eb2d9ed443379c9b2eb20f26a37ea5a8ecff5
3
+ size 74846629
hi-en/models/layout/classes.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ title
2
+ text
3
+ table
4
+ figure
5
+ seal
6
+ id_panel
7
+ header
8
+ footer
9
+ handwriting
hi-en/models/layout/classes_v15.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ text
2
+ title
3
+ section_header
4
+ list_item
5
+ table
6
+ figure
7
+ caption
8
+ footnote
9
+ formula
10
+ header
11
+ footer
12
+ seal
13
+ handwriting
14
+ id_panel
15
+ barcode
hi-en/models/layout/kanen_layout_v14.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9b1f23ea7ca7f9cc3d237afe0e65d6dd0302e9e9c213a6a1d9fc32ac0691a67a
3
+ size 74832733
hi-en/models/layout/selftest.jpg ADDED

Git LFS Details

  • SHA256: b225479e38d2da866bf7d1fd1519892a132118d65538b39e01ff316d1be4653e
  • Pointer size: 131 Bytes
  • Size of remote file: 115 kB
hi-en/models/layout/selftest_ref_v14.json ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "type": "title",
4
+ "score": 0.968,
5
+ "bbox": [
6
+ 486.0,
7
+ 213.5,
8
+ 537.5,
9
+ 229.7
10
+ ]
11
+ },
12
+ {
13
+ "type": "text",
14
+ "score": 0.95,
15
+ "bbox": [
16
+ 80.0,
17
+ 178.0,
18
+ 974.2,
19
+ 210.7
20
+ ]
21
+ },
22
+ {
23
+ "type": "header",
24
+ "score": 0.908,
25
+ "bbox": [
26
+ 899.8,
27
+ 104.4,
28
+ 939.9,
29
+ 118.1
30
+ ]
31
+ },
32
+ {
33
+ "type": "header",
34
+ "score": 0.901,
35
+ "bbox": [
36
+ 83.1,
37
+ 103.6,
38
+ 123.0,
39
+ 116.7
40
+ ]
41
+ },
42
+ {
43
+ "type": "table",
44
+ "score": 0.893,
45
+ "bbox": [
46
+ 104.6,
47
+ 228.5,
48
+ 925.4,
49
+ 680.4
50
+ ]
51
+ },
52
+ {
53
+ "type": "text",
54
+ "score": 0.874,
55
+ "bbox": [
56
+ 89.2,
57
+ 132.9,
58
+ 962.2,
59
+ 162.4
60
+ ]
61
+ },
62
+ {
63
+ "type": "header",
64
+ "score": 0.857,
65
+ "bbox": [
66
+ 378.8,
67
+ 103.7,
68
+ 620.2,
69
+ 121.3
70
+ ]
71
+ }
72
+ ]
hi-en/models/layout/selftest_ref_v15.json ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "type": "text",
4
+ "score": 0.966,
5
+ "bbox": [
6
+ 84.7,
7
+ 126.9,
8
+ 935.3,
9
+ 164.9
10
+ ]
11
+ },
12
+ {
13
+ "type": "header",
14
+ "score": 0.965,
15
+ "bbox": [
16
+ 382.2,
17
+ 103.7,
18
+ 620.8,
19
+ 120.9
20
+ ]
21
+ },
22
+ {
23
+ "type": "section_header",
24
+ "score": 0.964,
25
+ "bbox": [
26
+ 486.0,
27
+ 213.8,
28
+ 537.0,
29
+ 230.0
30
+ ]
31
+ },
32
+ {
33
+ "type": "text",
34
+ "score": 0.964,
35
+ "bbox": [
36
+ 83.0,
37
+ 169.8,
38
+ 937.2,
39
+ 206.1
40
+ ]
41
+ },
42
+ {
43
+ "type": "header",
44
+ "score": 0.961,
45
+ "bbox": [
46
+ 83.5,
47
+ 104.2,
48
+ 122.9,
49
+ 117.0
50
+ ]
51
+ },
52
+ {
53
+ "type": "header",
54
+ "score": 0.96,
55
+ "bbox": [
56
+ 899.2,
57
+ 104.6,
58
+ 940.5,
59
+ 118.3
60
+ ]
61
+ },
62
+ {
63
+ "type": "table",
64
+ "score": 0.958,
65
+ "bbox": [
66
+ 75.3,
67
+ 229.1,
68
+ 946.5,
69
+ 683.3
70
+ ]
71
+ }
72
+ ]
hi-en/models/rec/arjuna_rec_hi.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dea1c19158f4931c3f014602e331c92b53138e2a58fd465699e92dcacda6d842
3
+ size 81293562
hi-en/models/rec/charset.json ADDED
@@ -0,0 +1 @@
 
 
1
+ [" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "ऀ", "ँ", "ं", "ः", "ऄ", "अ", "आ", "इ", "ई", "उ", "ऊ", "ऋ", "ऌ", "ऍ", "ऎ", "ए", "ऐ", "ऑ", "ऒ", "ओ", "औ", "क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ", "ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न", "ऩ", "प", "फ", "ब", "भ", "म", "य", "र", "ऱ", "ल", "ळ", "ऴ", "व", "श", "ष", "स", "ह", "ऺ", "ऻ", "़", "ऽ", "ा", "ि", "ी", "ु", "ू", "ृ", "ॄ", "ॅ", "ॆ", "े", "ै", "ॉ", "ॊ", "ो", "ौ", "्", "ॎ", "ॏ", "ॐ", "॑", "॒", "॓", "॔", "ॕ", "ॖ", "ॗ", "क़", "ख़", "ग़", "ज़", "ड़", "ढ़", "फ़", "य़", "ॠ", "ॡ", "ॢ", "ॣ", "।", "॥", "०", "१", "२", "३", "४", "५", "६", "७", "८", "९", "॰", "ॱ", "ॲ", "ॳ", "ॴ", "ॵ", "ॶ", "ॷ", "ॸ", "ॹ", "ॺ", "ॻ", "ॼ", "ॽ", "ॾ", "ॿ", "–", "—", "‘", "’", "“", "”", "„", "«", "»", "‹", "›", "•", "·", "…", "‑", "‐", "−", "₹", "€", "£", "¥", "¢", "₨", "©", "®", "™", "§", "¶", "†", "‡", "№", "℅", "‰", "µ", "¤", "°", "′", "″", "½", "¼", "¾", "⅓", "⅔", "⅛", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "⁰", "⁻", "∑", "∫", "√", "≤", "≥", "≠", "±", "×", "÷", "∞", "∂", "∆", "∇", "≈", "≡", "∈", "∉", "⊂", "∪", "∩", "π", "α", "β", "γ", "θ", "λ", "μ", "σ", "Ω", "☐", "☑", "☒", "✓", "✗", "✘", "★", "☆", "●", "○", "■", "□", "◆", "▪", "▶", "→", "←", "↑", "↓", "⇒"]
kn-en/LICENSE ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Apache License 2.0 — see https://www.apache.org/licenses/LICENSE-2.0
2
+
3
+ Copyright 2026 Chethan (anandkaman). Base models: PaddleOCR (Apache-2.0), OpenOCR SVTRv2 (Apache-2.0).
kn-en/VERSION ADDED
@@ -0,0 +1 @@
 
 
1
+ 1.2.0
kn-en/kanen.yaml ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Arjuna-OCR [Kn&En] v1.2 — inference configuration.
2
+ # All paths are relative to the package root (the directory holding this file) unless absolute.
3
+ # Environment overrides (kept for compatibility): KANEN_TRT=1 -> provider.policy=trt, KANEN_ORT_CPU=1 -> cpu,
4
+ # KANEN_REC_ONNX=<path> -> models.rec, KANEN_CONFIG=<path> -> use another config file, KANEN_LOG=<level>.
5
+ name: "Arjuna-OCR [Kn&En]" # product name used in banners / hOCR / ALTO metadata (rename here only)
6
+ version: 1.2.0 # package version; stamped into every output document as model.pipeline
7
+
8
+ models:
9
+ det: models/det/inference.onnx # PP-OCRv6 text detector (small), DB head
10
+ layout: models/layout/kanen_layout_v12.onnx # Arjuna layout v12 (DocLayout-YOLO based, 9 classes), 1024 letterbox, (B,300,6) output
11
+ layout_labels: models/layout/classes.txt # one class name per line, in model index order
12
+ layout_selftest: models/layout/selftest.jpg # public page used to self-test every layout TensorRT engine ...
13
+ layout_selftest_ref: models/layout/selftest_ref.json # ... against these reference blocks (CPU ONNX Runtime output, conf >= 0.45)
14
+ rec: models/rec/kanen_rec_e5.onnx # SVTRv2-B + Kannada/English pack (E5 = v1.2), CTC, 221 classes
15
+ charset: models/rec/charset.json
16
+
17
+ provider:
18
+ policy: auto # auto = trt -> cuda -> cpu (first one that loads) | trt | cuda | cpu
19
+ trt_fp16: true # fp16 TensorRT engines (layout: fp16 -> rebuilt fp16 -> fp32 TensorRT -> CUDA if its self-test fails)
20
+ engine_cache: trt_cache # TensorRT engine + timing caches, one sub-directory per model
21
+ trt_workspace_gb: 4
22
+ cuda_mem_limit_gb: 6
23
+ cudnn_conv_algo_search: HEURISTIC
24
+
25
+ detector:
26
+ canvases: [1280, 1920, 2560] # fixed canvas long sides; the batched path picks the smallest that keeps >= min_scale of the page
27
+ width_classes: [0.5, 0.625, 0.75, 0.875, 1.0] # short side snapped up to these fractions of the canvas (bounded set of engine shapes)
28
+ min_scale: 0.9
29
+ default_canvas: 1280 # canvas of the single-image detect() path
30
+ thresh: 0.2 # DB probability threshold
31
+ box_thresh: 0.45 # mean box score threshold
32
+ unclip: 1.4
33
+ pad_top: 0.03 # box extension (fraction of line height); bottom 0.25 keeps Kannada conjuncts inside the crop
34
+ pad_bottom: 0.25
35
+ batch: 2 # pages per detector call
36
+
37
+ layout:
38
+ mode: always # always | auto (only pages that look like tables/forms) | never
39
+ batch: 4 # engine batch; pages are zero-padded to it (one TensorRT engine shape)
40
+ score_thresh: 0.3 # block confidence threshold (v12 is calibrated lower than PP-DocLayoutV3 was)
41
+ fill: [1.0, 0.8] # the page is run once per value (long side = fill x canvas) and the boxes merged: 1.0 for small blocks, 0.8 keeps whole-page blocks inside the head's regression range
42
+ selftest_min_match: 1.0 # every reference block (CPU, conf >= 0.45) must be reproduced at conf >= 0.3 with class + IoU >= 0.7 (a wrong TensorRT engine drops the wide blocks; fp16 noise is ~1e-3)
43
+
44
+ recognizer:
45
+ buckets: [128, 192, 256, 384, 512, 640, 768, 1024, 1280] # crop width buckets (each crop padded only to its own bucket)
46
+ batch: 64
47
+ height: 32
48
+ max_ratio: 40
49
+
50
+ second_pass: # confidence-gated re-read of the weakest lines with a slightly larger crop
51
+ conf: 0.85
52
+ cap: 6 # lines per page
53
+
54
+ runtime:
55
+ workers: auto # CPU worker threads for warps / DB post-processing / assembly (auto = max(4, cores/2))
56
+ page_batch: 8
57
+ log_level: warning # debug | info | warning | error
58
+ polygons: false # include 4-point polygons in line objects
kn-en/kanen_infer/__init__.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ """Arjuna-OCR [Kn&En] inference package (Kannada + English document OCR): ONNX Runtime + optional TensorRT, no PyTorch."""
2
+ from pathlib import Path as _P
3
+ try: __version__ = (_P(__file__).resolve().parent.parent / "VERSION").read_text(encoding="utf-8").strip()
4
+ except Exception: __version__ = "1.2.0"
5
+ NAME = "Arjuna-OCR [Kn&En]" # product display name — also `name:` in kanen.yaml (the config value wins at run time)
6
+ from .config import load_config, PKG_ROOT # noqa: E402
7
+ from .api import KanEnOCR # noqa: E402
8
+ from .formats import to_json, to_markdown, to_text, to_hocr, to_alto, to_tsv, render # noqa: E402
9
+ __all__ = ["KanEnOCR", "load_config", "PKG_ROOT", "NAME", "__version__", "to_json", "to_markdown", "to_text", "to_hocr", "to_alto", "to_tsv", "render"]
kn-en/kanen_infer/api.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Arjuna-OCR [Kn&En] Python API.
2
+
3
+ from kanen_infer import KanEnOCR
4
+ ocr = KanEnOCR() # loads kanen.yaml, builds/validates engines (warm-up), see ocr.ready()
5
+ doc = ocr.page("scan.png") # one kanen-ocr/1.0 document (dict)
6
+ docs = ocr.pages(["a.png", "b.pdf"]) # batched; ocr.stream(...) yields documents as they finish
7
+ boxes = ocr.detect(img) # text-line polygons only
8
+ texts = ocr.recognize(crops) # [(text, conf, min_conf)] for line images
9
+ blocks = ocr.layout(img) # layout blocks only
10
+ ocr.render(doc, "hocr") # json | md | txt | hocr | alto | tsv
11
+
12
+ Inputs: file path, bytes, numpy BGR array, PIL image, file object; PDFs through `pages()`/`stream()` (pypdfium2 or pdftoppm,
13
+ optional). The output document never depends on the provider or the batch composition beyond fp16 noise on garbage lines;
14
+ no confidence filtering, no text rewriting, no re-ordering happens here."""
15
+ import logging, time
16
+ import numpy as np, cv2
17
+ from pathlib import Path
18
+ from .config import load_config, PKG_ROOT
19
+ from .providers import Providers
20
+ from .rec_onnx import KanEnRecognizer
21
+ from .pipeline_base import Detector, Layout, crop
22
+ from .batch_pipeline import BatchOCR
23
+ from .io import to_bgr, expand_inputs, iter_page_sources
24
+ from . import formats
25
+
26
+ LOG = logging.getLogger("kanen")
27
+
28
+ class KanEnOCR:
29
+ def __init__(self, config=None, providers=None, layout_mode=None, workers=None, warmup=True, **overrides):
30
+ """config: path to a kanen.yaml/json, a dict, or None (package default). providers: 'trt' | 'cuda' | 'cpu' | 'auto'
31
+ (overrides config provider.policy). Other keyword overrides use dotted section names, e.g. layout={'mode': 'auto'}."""
32
+ ov = dict(overrides)
33
+ if providers: ov.setdefault("provider", {})["policy"] = providers
34
+ if layout_mode: ov.setdefault("layout", {})["mode"] = layout_mode
35
+ if workers: ov.setdefault("runtime", {})["workers"] = workers
36
+ self.cfg = load_config(config, ov); self.name = self.cfg.get("name", "Arjuna-OCR [Kn&En]"); self.version = str(self.cfg.get("version", "1.0.0"))
37
+ t0 = time.time(); self.prov = Providers(self.cfg)
38
+ self.rec = KanEnRecognizer(cfg=self.cfg, providers=self.prov); self.det = Detector(self.cfg, self.prov)
39
+ self.lay = Layout(self.cfg, self.prov) if self.cfg["layout"].get("mode", "always") != "never" else None
40
+ self.batch = BatchOCR(self.rec, cfg=self.cfg, providers=self.prov, det=self.det, lay=self.lay)
41
+ self._load_s = round(time.time() - t0, 1); self._warm = None
42
+ if warmup: self.warmup()
43
+
44
+ # ---- status --------------------------------------------------------------------------------------------------
45
+ def warmup(self):
46
+ """Run the bundled self-test page through the full pipeline once: builds/validates TensorRT engines for the
47
+ default shapes (layout self-test, detector canvas, recognizer profile) so the first real page is not slow."""
48
+ t0 = time.time(); p = self.cfg.path("models", "layout_selftest")
49
+ if p.exists():
50
+ img = to_bgr(str(p)); self.batch.run([img], page_batch=1, loader=lambda x: x)
51
+ self._warm = round(time.time() - t0, 1); return self.ready()
52
+
53
+ def ready(self):
54
+ """Provider each model ended on, fallbacks that happened, engine-cache state, model hashes."""
55
+ st = self.prov.summary()
56
+ if self.lay is not None: st.setdefault("layout", {})["engine"] = self.lay.stage
57
+ return {"name": self.name, "version": self.version, "config": self.cfg.source, "gpu": self.prov.gpu, "tensorrt": self.prov.trt_ver,
58
+ "providers": st, "events": list(self.prov.events), "engine_cache": str(self.prov.cache_root), "load_s": self._load_s, "warmup_s": self._warm,
59
+ "model": dict(self.batch.model_ids)}
60
+
61
+ # ---- full documents -------------------------------------------------------------------------------------------
62
+ def page(self, image, polygons=None, dpi=None):
63
+ """Single page (latency path): one kanen-ocr/1.0 document."""
64
+ img = to_bgr(image); docs, _ = self.batch.process([img], dpi=dpi, with_polygons=self._poly(polygons)); d = docs[0]
65
+ if isinstance(image, (str, Path)): d["page"]["file"] = Path(image).name
66
+ return d
67
+
68
+ def pages(self, inputs, page_batch=None, polygons=None, pdf_dpi=200):
69
+ """Batched: list/dir/glob of image or PDF paths, or a list of arrays / PIL images / bytes -> list of documents."""
70
+ return [d for d in self.stream(inputs, page_batch, polygons, pdf_dpi)]
71
+
72
+ def stream(self, inputs, page_batch=None, polygons=None, pdf_dpi=200):
73
+ """Generator variant of pages(): yields documents in input order as each page batch completes."""
74
+ sources = self._sources(inputs, pdf_dpi); labels = [s[0] for s in sources]; items = [s[1] for s in sources]
75
+ page_batch = page_batch or self.cfg["runtime"].get("page_batch", 8); k = 0
76
+ for doc, st in self.batch.stream(items, page_batch=page_batch, loader=to_bgr, with_polygons=self._poly(polygons)):
77
+ if labels[k]: doc["page"]["file"] = labels[k]
78
+ self.last_stats = st; k += 1; yield doc
79
+
80
+ def _sources(self, inputs, pdf_dpi):
81
+ if isinstance(inputs, (str, Path)) or (isinstance(inputs, (list, tuple)) and inputs and all(isinstance(x, (str, Path)) for x in inputs)):
82
+ return list(iter_page_sources(expand_inputs(inputs), dpi=pdf_dpi))
83
+ if isinstance(inputs, (list, tuple)): return [(getattr(x, "name", None) or "", x) for x in inputs]
84
+ return [("", inputs)]
85
+
86
+ def _poly(self, polygons): return bool(self.cfg["runtime"].get("polygons", False)) if polygons is None else bool(polygons)
87
+
88
+ # ---- single stages --------------------------------------------------------------------------------------------
89
+ def detect(self, image):
90
+ """Text-line detection only -> list of (polygon 4x2 float32 in page pixels, score)."""
91
+ return self.det(to_bgr(image))
92
+
93
+ def recognize(self, crops, batch=None):
94
+ """Recognition only on a list of line images (any size, BGR or gray) -> [(text, confidence, min_conf)]."""
95
+ res = self.rec([to_bgr(c) for c in crops], bs=batch)
96
+ return [(t, float(c), float(getattr(c, "min_char", c))) for t, c in res]
97
+
98
+ def layout(self, image):
99
+ """Layout blocks only -> [{type, score, bbox, order}] (labels: title, text, table, figure, seal, id_panel, header, footer, handwriting)."""
100
+ if self.lay is None: self.lay = Layout(self.cfg, self.prov); self.batch.lay = self.lay
101
+ return self.lay(to_bgr(image))[0]
102
+
103
+ def crops(self, image, boxes=None):
104
+ """Perspective line crops for detect() boxes (helper for recognize())."""
105
+ img = to_bgr(image); boxes = self.detect(img) if boxes is None else boxes
106
+ return [crop(img, b) for b, _ in boxes]
107
+
108
+ # ---- serialisation --------------------------------------------------------------------------------------------
109
+ def render(self, doc, fmt="json"):
110
+ return formats.render(doc, fmt)
kn-en/kanen_infer/assemble.py ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Assembler: layout blocks (Arjuna layout v12) + text-line polygons + recognised text -> kanen-ocr/1.0 JSON.
2
+ Pure geometry, no model. Also: Markdown / plain-text renderers."""
3
+ import numpy as np
4
+ from .charset import script_of
5
+ import re
6
+ WEAK_CHAR = 0.50 # min-char confidence below which a line is flagged weak_char (calibrated on 3,478 dev lines: < 0.5 -> 65 % exact)
7
+ _MIX_RE = re.compile(r"(?=\S*[A-Za-z])(?=\S*[\u0C85-\u0CB9])\S+") # token with both a Latin letter and a Kannada letter
8
+
9
+ TYPE_MAP = {"doc_title": "title", "paragraph_title": "section_title", "text": "text", "abstract": "text", "content": "text", "reference": "text",
10
+ "reference_content": "text", "aside_text": "text", "vertical_text": "text", "footnote": "footnote", "vision_footnote": "footnote",
11
+ "header": "header", "footer": "footer", "number": "page_number", "table": "table", "image": "figure", "chart": "figure",
12
+ "figure_title": "figure_caption", "seal": "seal", "display_formula": "formula", "inline_formula": "formula", "formula_number": "formula",
13
+ "algorithm": "text", "header_image": "figure", "footer_image": "figure",
14
+ "title": "title", "figure": "figure", "id_panel": "id_panel", "handwriting": "handwriting"} # v1.2 layout classes (text/table/seal/header/footer map 1:1 above)
15
+
16
+ def _bbox(poly): p = np.asarray(poly, np.float32); return [float(p[:, 0].min()), float(p[:, 1].min()), float(p[:, 0].max()), float(p[:, 1].max())]
17
+
18
+ def _overlap_frac(line_bb, block_bb):
19
+ ix = max(0, min(line_bb[2], block_bb[2]) - max(line_bb[0], block_bb[0])); iy = max(0, min(line_bb[3], block_bb[3]) - max(line_bb[1], block_bb[1]))
20
+ return ix * iy / max((line_bb[2] - line_bb[0]) * (line_bb[3] - line_bb[1]), 1e-6)
21
+
22
+ def merge_line_fragments(lines, y_tol=0.5, x_gap=1.5):
23
+ """Merge horizontally adjacent boxes on the same baseline (detector sometimes splits a line at wide gaps).
24
+ lines: list of dict(bbox, polygon, text, conf). Returns merged list."""
25
+ lines = sorted(lines, key=lambda l: (l["bbox"][1], l["bbox"][0])); out = []
26
+ for l in lines:
27
+ merged = False
28
+ for m in out:
29
+ h = min(m["bbox"][3] - m["bbox"][1], l["bbox"][3] - l["bbox"][1])
30
+ same_row = abs((m["bbox"][1] + m["bbox"][3]) / 2 - (l["bbox"][1] + l["bbox"][3]) / 2) < y_tol * h
31
+ gap = l["bbox"][0] - m["bbox"][2]
32
+ if same_row and -0.2 * h < gap < x_gap * h:
33
+ m["bbox"] = [min(m["bbox"][0], l["bbox"][0]), min(m["bbox"][1], l["bbox"][1]), max(m["bbox"][2], l["bbox"][2]), max(m["bbox"][3], l["bbox"][3])]
34
+ m["text"] = (m["text"] + " " + l["text"]).strip(); m["confidence"] = min(m["confidence"], l["confidence"]); m["min_conf"] = min(m.get("min_conf", 1), l.get("min_conf", 1)); m["polygon"] = None; merged = True; break
35
+ if not merged: out.append(dict(l))
36
+ return out
37
+
38
+ def xy_cut_order(blocks, page_w):
39
+ """Fallback reading order: column split then top-to-bottom."""
40
+ if not blocks: return []
41
+ mid = page_w / 2
42
+ left = [b for b in blocks if b["bbox"][2] < mid * 1.05]; right = [b for b in blocks if b["bbox"][0] > mid * 0.95]
43
+ spanning = [b for b in blocks if b not in left and b not in right]
44
+ if len(left) >= 2 and len(right) >= 2 and not spanning:
45
+ return sorted(left, key=lambda b: b["bbox"][1]) + sorted(right, key=lambda b: b["bbox"][1])
46
+ return sorted(blocks, key=lambda b: (round(b["bbox"][1] / 20), b["bbox"][0]))
47
+
48
+ def rows_of(lines, tol=0.5):
49
+ """Cluster line boxes into visual rows (y-centre within tol x the smaller height), left-to-right inside a row."""
50
+ ls = sorted(lines, key=lambda l: (l["bbox"][1] + l["bbox"][3]) / 2); rows = []
51
+ for l in ls:
52
+ yc = (l["bbox"][1] + l["bbox"][3]) / 2; h = l["bbox"][3] - l["bbox"][1]
53
+ if rows:
54
+ r = rows[-1]; ryc = np.mean([(m["bbox"][1] + m["bbox"][3]) / 2 for m in r]); rh = np.median([m["bbox"][3] - m["bbox"][1] for m in r])
55
+ if abs(yc - ryc) < tol * min(h, rh): r.append(l); continue
56
+ rows.append([l])
57
+ return [sorted(r, key=lambda l: l["bbox"][0]) for r in rows]
58
+
59
+ def split_row_by_gap(r, max_gap_h=2.5):
60
+ """Split a visual row into runs where the horizontal gap between neighbours exceeds max_gap_h x line height: two
61
+ columns (Kannada | English on ID cards, label | value pairs far apart) must stay separate lines; fragments of one
62
+ justified/spaced line (gaps of a few glyph widths) still join. Geometry only."""
63
+ r = sorted(r, key=lambda l: l["bbox"][0]); runs = [[r[0]]]
64
+ for l in r[1:]:
65
+ prev = runs[-1][-1]; h = max(8.0, min(prev["bbox"][3] - prev["bbox"][1], l["bbox"][3] - l["bbox"][1]))
66
+ if l["bbox"][0] - prev["bbox"][2] > max_gap_h * h: runs.append([l])
67
+ else: runs[-1].append(l)
68
+ return runs
69
+
70
+ def join_row(r):
71
+ """One text line per visual row: fragments of a justified/spaced line are joined with single spaces."""
72
+ if len(r) == 1: return dict(r[0])
73
+ return {"bbox": [min(l["bbox"][0] for l in r), min(l["bbox"][1] for l in r), max(l["bbox"][2] for l in r), max(l["bbox"][3] for l in r)],
74
+ "polygon": None, "text": " ".join(l["text"] for l in r).strip(), "confidence": min(l["confidence"] for l in r), "min_conf": min(l.get("min_conf", 1) for l in r)}
75
+
76
+ def _runs(mask1d, min_gap=3):
77
+ idx = np.flatnonzero(mask1d)
78
+ if idx.size == 0: return []
79
+ out = []; start = idx[0]; prev = idx[0]
80
+ for i in idx[1:]:
81
+ if i - prev > min_gap: out.append((start + prev) / 2); start = i
82
+ prev = i
83
+ out.append((start + prev) / 2); return out
84
+
85
+ def _rule_positions(mask, axis, min_len):
86
+ """Centre coordinate of every connected component of a rule mask longer than min_len along `axis` (robust to residual tilt)."""
87
+ import cv2
88
+ n, _, st, cen = cv2.connectedComponentsWithStats(mask, connectivity=8)
89
+ pos = sorted(cen[i][1 - axis] for i in range(1, n) if st[i][cv2.CC_STAT_WIDTH if axis == 0 else cv2.CC_STAT_HEIGHT] >= min_len)
90
+ out = []
91
+ for p in pos:
92
+ if out and p - out[-1] < 6: out[-1] = (out[-1] + p) / 2
93
+ else: out.append(p)
94
+ return out
95
+
96
+ def _merge_continuations(rows):
97
+ """A row whose cells all sit under occupied columns of the previous row and none in its first column is a wrapped cell."""
98
+ out = []
99
+ for r in rows:
100
+ ne = [c for c in r["cells"] if c["text"]]
101
+ if out and ne:
102
+ prev = out[-1]; pne = [c for c in prev["cells"] if c["text"]]
103
+ first = min(c["col"] for c in prev["cells"]) if prev["cells"] else 0
104
+ if all(c["col"] > first for c in ne) and all(any(p["col"] == c["col"] for p in pne) for c in ne):
105
+ for c in ne:
106
+ p = next(p for p in pne if p["col"] == c["col"]); p["text"] = (p["text"] + " " + c["text"]).strip()
107
+ p["bbox"] = [min(p["bbox"][0], c["bbox"][0]), min(p["bbox"][1], c["bbox"][1]), max(p["bbox"][2], c["bbox"][2]), max(p["bbox"][3], c["bbox"][3])]
108
+ continue
109
+ out.append(r)
110
+ return out
111
+
112
+ def ruled_grid(gray, bbox, lines):
113
+ """Table structure from printed ruling lines (morphological opening + connected components). Horizontal rules give row
114
+ bands; inside a band, vertical rules give cells - or, when a band has no vertical rules (aligned-column forms), each
115
+ visual row of the band becomes a table row whose cells are the separate line boxes. Returns
116
+ rows=[{cells:[{col,text,bbox,confidence}], ruled}] or None when the region has no horizontal rules. Pure geometry."""
117
+ import cv2
118
+ x0, y0, x1, y1 = [int(round(v)) for v in bbox]; reg = gray[max(0, y0):y1, max(0, x0):x1]
119
+ if reg.size == 0 or reg.shape[0] < 20 or reg.shape[1] < 20: return None
120
+ H, W = reg.shape; bw = cv2.adaptiveThreshold(reg, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 31, 9)
121
+ hor = cv2.morphologyEx(bw, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_RECT, (max(20, W // 30), 1)))
122
+ hor = cv2.dilate(hor, cv2.getStructuringElement(cv2.MORPH_RECT, (max(20, W // 8), 1))) # bridge faint / dashed rules (text-derived runs are rejected below)
123
+ loc = [(l["bbox"][0] - x0, l["bbox"][1] - y0, l["bbox"][2] - x0, l["bbox"][3] - y0) for l in lines]
124
+ def through_text(y): return any(b + 0.1 * (d - b) < y < d - 0.1 * (d - b) for a, b, c, d in loc) # a rule never crosses the middle of a text line
125
+ def through_text_x(x, ia, ib): return any(a + 0.1 * (c - a) < x < c - 0.1 * (c - a) and b < ib and d > ia for a, b, c, d in loc)
126
+ hr = [y for y in _rule_positions(hor, 0, 0.25 * W) if 0.01 * H + 4 < y < H - 0.01 * H - 4 and not through_text(y)]
127
+ if not hr: return None
128
+ ys = [0.0] + hr + [float(H)]; bands = []
129
+ for a, b in zip(ys[:-1], ys[1:]):
130
+ ia, ib = int(a), int(b)
131
+ if ib - ia < 8: continue
132
+ ver = cv2.morphologyEx(bw[ia:ib], cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_RECT, (1, max(8, int((ib - ia) * 0.4)))))
133
+ ver = cv2.dilate(ver, cv2.getStructuringElement(cv2.MORPH_RECT, (1, max(6, int((ib - ia) * 0.25)))))
134
+ vr = [x for x in _rule_positions(ver, 1, 0.85 * (ib - ia)) if 0.03 * W < x < 0.97 * W and not through_text_x(x, ia, ib)] # cell borders span the band, never cross text; table border excluded
135
+ bands.append((ia, ib, vr))
136
+ allx = sorted(x for _, _, vr in bands for x in vr); G = []
137
+ for x in allx:
138
+ if not G or x - G[-1][-1] > 12: G.append([x])
139
+ else: G[-1].append(x)
140
+ G = [float(np.mean(g)) for g in G]
141
+ def gcol(x): return sum(1 for g in G if g < x + 6)
142
+ rows = []
143
+ for ia, ib, vr in bands:
144
+ inband = [l for l in lines if ia <= (l["bbox"][1] + l["bbox"][3]) / 2 - y0 < ib]
145
+ if not inband: continue
146
+ if vr: # ruled cells: everything between two vertical rules is one cell (a wrapped cell keeps its lines together);
147
+ # the global rule set is used so a band whose faint rule was missed still gets the table's columns
148
+ xs = [0.0] + G + [float(W)]
149
+ percol = [[l for l in inband if ca <= (l["bbox"][0] + l["bbox"][2]) / 2 - x0 < cb] for ca, cb in zip(xs[:-1], xs[1:])]
150
+ multi = sum(1 for inc in percol if len(rows_of(inc)) > 1)
151
+ # several columns stacked -> the band holds several table rows whose separating rule was not printed/found
152
+ groups = rows_of(inband) if multi >= 2 else [inband]
153
+ for g in groups:
154
+ cells = []
155
+ for ca, cb in zip(xs[:-1], xs[1:]):
156
+ inc = [l for l in g if ca <= (l["bbox"][0] + l["bbox"][2]) / 2 - x0 < cb]
157
+ if not inc: cells.append({"col": gcol(ca), "text": "", "bbox": None, "confidence": None}); continue
158
+ cells.append({"col": gcol(ca), "text": " ".join(join_row(r)["text"] for r in rows_of(inc)), "bbox": [min(l["bbox"][0] for l in inc), min(l["bbox"][1] for l in inc), max(l["bbox"][2] for l in inc), max(l["bbox"][3] for l in inc)], "confidence": min(l["confidence"] for l in inc)})
159
+ rows.append({"cells": cells, "ruled": True})
160
+ if len(groups) > 1: rows[-len(groups):] = _merge_continuations(rows[-len(groups):])
161
+ else: # no vertical rules in this band: each visual row is a table row, each line box a cell
162
+ for r in rows_of(inband):
163
+ rows.append({"cells": [{"col": (gcol(l["bbox"][0] - x0) if G else ci), "text": l["text"], "bbox": l["bbox"], "confidence": l["confidence"]} for ci, l in enumerate(r)], "ruled": False})
164
+ return rows if rows else None
165
+
166
+ def group_table(lines):
167
+ """Unruled tables: rows by y-clustering, columns by clustering cell x-starts; a row that only continues cells of the
168
+ previous row (no cell in the first column, every cell under an occupied column) is a wrapped cell -> merged upward."""
169
+ if not lines: return []
170
+ rows = rows_of(lines)
171
+ spans = sorted(((l["bbox"][0], l["bbox"][2]) for r in rows for l in r)); cols = []
172
+ for a, b in spans: # columns = groups of horizontally overlapping cells (works for left/centre/right alignment)
173
+ if cols and a < cols[-1][1] - 4: cols[-1][1] = max(cols[-1][1], b)
174
+ else: cols.append([a, b])
175
+ def col_of(l):
176
+ xc = (l["bbox"][0] + l["bbox"][2]) / 2
177
+ return next((i for i, (a, b) in enumerate(cols) if a - 4 <= xc <= b + 4), int(np.argmin([abs(xc - (a + b) / 2) for a, b in cols])) if cols else 0)
178
+ return _merge_continuations([{"cells": [{"bbox": l["bbox"], "text": l["text"], "confidence": l["confidence"], "col": col_of(l)} for l in r], "ruled": False} for r in rows])
179
+
180
+ def assemble(page_w, page_h, layout_blocks, det_boxes, texts, model_ids, timing=None, page_index=0, dpi=None, with_polygons=True, img=None):
181
+ """layout_blocks: [{type, score, bbox, order}], det_boxes: [(poly4x2, score)], texts: [(text, conf)] aligned with det_boxes."""
182
+ lines = []
183
+ for (poly, dscore), (text, conf) in zip(det_boxes, texts):
184
+ if not text.strip(): continue
185
+ lines.append({"bbox": _bbox(poly), "polygon": [[float(x), float(y)] for x, y in poly] if with_polygons else None, "text": text, "confidence": round(float(conf), 4),
186
+ "min_conf": round(float(getattr(conf, "min_char", None) if getattr(conf, "min_char", None) is not None else conf), 4)})
187
+ blocks = []
188
+ for i, b in enumerate(layout_blocks):
189
+ blocks.append({"id": f"b{i}", "type": TYPE_MAP.get(b["type"], "other"), "raw_type": b["type"], "confidence": round(float(b["score"]), 4), "bbox": [round(v, 1) for v in b["bbox"]], "order": b.get("order", i), "lines": []})
190
+ # assign lines to best-overlapping block; unassigned lines become their own text blocks
191
+ orphans = []
192
+ area = lambda bb: max(1.0, (bb[2] - bb[0]) * (bb[3] - bb[1]))
193
+ for l in lines:
194
+ best, bi = 0.0, -1
195
+ for i, b in enumerate(blocks):
196
+ f = _overlap_frac(l["bbox"], b["bbox"])
197
+ # nested blocks (v1.2 layout: a handwriting / seal / id_panel box inside a text region): on a near-tie the
198
+ # smaller, more specific block wins, so signature fragments are not glued to the surrounding text rows
199
+ if f > best + 0.05 or (f >= best - 0.05 and bi >= 0 and f >= 0.5 and area(b["bbox"]) < area(blocks[bi]["bbox"])): best, bi = f, i
200
+ if best >= 0.5: blocks[bi]["lines"].append(l)
201
+ else: orphans.append(l)
202
+ for l in orphans:
203
+ blocks.append({"id": f"b{len(blocks)}", "type": "text", "raw_type": "orphan_line", "confidence": l["confidence"], "bbox": [round(v, 1) for v in l["bbox"]], "order": None, "lines": [l]})
204
+ blocks = [b for b in blocks if b["lines"] or b["type"] in ("figure", "seal", "table", "id_panel", "handwriting")]
205
+ # order: native rank where available, else xy-cut; orphans placed by position among ordered blocks
206
+ ranked = [b for b in blocks if b["order"] is not None]; unranked = [b for b in blocks if b["order"] is None]
207
+ ordered = sorted(ranked, key=lambda b: b["order"])
208
+ for b in unranked:
209
+ pos = next((k for k, o in enumerate(ordered) if o["bbox"][1] > b["bbox"][1] and o["bbox"][0] <= b["bbox"][2]), len(ordered)); ordered.insert(pos, b)
210
+ if not ranked: ordered = xy_cut_order(blocks, page_w)
211
+ full = []
212
+ gray = None
213
+ if img is not None:
214
+ import cv2; gray = img if img.ndim == 2 else cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
215
+ heights = [l["bbox"][3] - l["bbox"][1] for b in ordered for l in b["lines"]]; med_h = float(np.median(heights)) if heights else 0.0
216
+ for k, b in enumerate(ordered):
217
+ b["order"] = k
218
+ if b["type"] == "table":
219
+ # table cells stay separate boxes; structure from printed rules when present, else from alignment
220
+ b["lines"] = [l for r in rows_of(b["lines"]) for l in r]
221
+ rows = ruled_grid(gray, b["bbox"], b["lines"]) if gray is not None else None
222
+ b["rows"] = rows or group_table(b["lines"]); b["ruled"] = bool(rows)
223
+ else:
224
+ b["lines"] = [join_row(run) for r in rows_of(b["lines"]) for run in split_row_by_gap(r)] # one text line per visual row run (fragments of a spaced line re-joined; wide-gap columns kept apart)
225
+ for li, l in enumerate(b["lines"]):
226
+ l["id"] = f"{b['id']}.l{li}"; l["script"] = script_of(l["text"]); l["bbox"] = [round(v, 1) for v in l["bbox"]]
227
+ # review flags (never deletion): calibration on 5,478 labelled lines -> conf >= 0.99 is 97.7 % line-exact,
228
+ # 0.95-0.98 only 83.5 %; lines inside seal/figure/header blocks are mostly stamp/handwriting fragments
229
+ fl = []
230
+ if l["confidence"] < 0.90: fl.append("low_conf")
231
+ elif l["confidence"] < 0.98: fl.append("check")
232
+ if b["type"] in ("seal", "figure", "handwriting", "id_panel"): fl.append("in_" + b["type"])
233
+ # review flags (script/character-repertoire checks); flags only, the text is never altered
234
+ if l.get("min_conf", 1.0) < WEAK_CHAR: fl.append("weak_char") # one very uncertain character (garbage lines usually have one)
235
+ if _MIX_RE.search(l["text"]): fl.append("script_mix") # Latin and Kannada letters inside one token (other-script garbage)
236
+ if med_h and (l["bbox"][3] - l["bbox"][1]) > 3.0 * med_h: fl.append("oversize") # stray ornament / drop-cap sized line
237
+ if fl: l["flags"] = fl
238
+ if b["type"] == "table" and b["rows"]: block_text = "\n".join(" | ".join(c["text"] for c in r["cells"]) for r in b["rows"])
239
+ else: block_text = "\n".join(l["text"] for l in b["lines"])
240
+ if block_text.strip(): full.append(block_text)
241
+ return {"schema": "kanen-ocr/1.0", "model": model_ids, "page": {"index": page_index, "width": page_w, "height": page_h, "dpi": dpi, "rotation": 0},
242
+ "blocks": ordered, "text": "\n\n".join(full), "timing_ms": timing or {}}
243
+
244
+ def to_markdown(doc):
245
+ out = []
246
+ for b in doc["blocks"]:
247
+ t = b["type"]; txt = " ".join(l["text"] for l in b["lines"]).strip()
248
+ if t == "title": out.append(f"# {txt}")
249
+ elif t == "section_title": out.append(f"## {txt}")
250
+ elif t == "table" and b.get("rows"):
251
+ ncol = max((c["col"] for r in b["rows"] for c in r["cells"]), default=0) + 1
252
+ rows = []
253
+ for r in b["rows"]:
254
+ cells = [""] * ncol
255
+ for c in r["cells"]: cells[c["col"]] = (cells[c["col"]] + " " + c["text"]).strip()
256
+ rows.append("| " + " | ".join(x.replace("|", "\\|") for x in cells) + " |")
257
+ if rows: out.append(rows[0] + "\n|" + "---|" * ncol + "\n" + "\n".join(rows[1:]))
258
+ elif t in ("figure", "seal"): out.append(f"*[{t}]*")
259
+ elif t in ("header", "footer", "page_number"): out.append(f"<sub>{txt}</sub>")
260
+ elif txt: out.append(txt)
261
+ return "\n\n".join(out) + "\n"
kn-en/kanen_infer/batch_pipeline.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Arjuna-OCR [Kn&En] batched throughput pipeline.
2
+ Organised for throughput instead of single-page latency (the API's `page()` runs the same code with one page):
3
+ * decode + canvas choice in a prefetch thread pool (next page batch is decoded while the GPU works on this one)
4
+ * detector runs ONCE per page at a canvas chosen from the page size (no 1280 -> re-detect search), batched across pages
5
+ * DB post-processing + perspective crops in worker threads
6
+ * recognizer sees the crops of the whole page batch in one width-bucketed call; orientation-180 fallback and the
7
+ confidence-gated second pass are pooled the same way
8
+ * layout model batched across pages
9
+ No output rewriting anywhere: every character comes from the recognizer. All tunables come from the package config.
10
+ """
11
+ import time, math, os
12
+ from concurrent.futures import ThreadPoolExecutor
13
+ import numpy as np, cv2
14
+ from .config import load_config, _FALLBACK
15
+ from .providers import Providers
16
+ from .pipeline_base import Detector, Layout, crop, MEAN, STD
17
+ from .assemble import assemble
18
+ from .geometry import rotate_page, deskew_angle, rotate_small
19
+
20
+ def n_readable(res, conf=0.90, min_char=0.50, min_len=4):
21
+ """readable text mass = characters in lines that read cleanly (line conf and weakest character high, >= 4 chars so
22
+ stray '|' / '1' fragments do not vote). Majority vote for mixed-orientation pages (upright form + rotated header)."""
23
+ return sum(len(t) for t, c in res if len(t) >= min_len and float(c) >= conf and (getattr(c, "min_char", None) if getattr(c, "min_char", None) is not None else float(c)) >= min_char)
24
+
25
+ def orient_score(res):
26
+ """orientation evidence: mean line confidence + mean weakest-character confidence (upside-down text = confident garbage with one very weak frame)"""
27
+ if not res: return 0.0
28
+ return float(np.mean([float(c) for _, c in res]) + np.mean([getattr(c, "min_char", None) if getattr(c, "min_char", None) is not None else float(c) for _, c in res]))
29
+
30
+ CANVASES = tuple(_FALLBACK["detector"]["canvases"])
31
+ WIDTH_CLASSES = tuple(_FALLBACK["detector"]["width_classes"]) # canvas width as a fraction of the long side -> at most 5 ORT plans per canvas (no square padding)
32
+ INV_STD = (1.0 / STD).astype(np.float32); MEAN255 = tuple(float(m * 255) for m in MEAN) # same channel order as Detector (PaddleOCR applies the mean to the BGR image as read)
33
+ PAD_VAL = ((1.0 - MEAN) / STD).astype(np.float32) # normalised white, like Detector's white canvas
34
+
35
+ def choose_canvas(h, w, min_scale=0.9, canvases=CANVASES):
36
+ """Smallest fixed canvas that keeps the page at >= min_scale of its native size (150-DPI private-document scans -> 1920, A4@300 -> 2560)."""
37
+ m = max(h, w)
38
+ for c in canvases:
39
+ if c >= min_scale * m: return c
40
+ return canvases[-1]
41
+
42
+ def canvas_shape(h, w, min_scale=0.9, canvases=CANVASES, width_classes=WIDTH_CLASSES):
43
+ """(limit, canvas_h, canvas_w): long side = limit, short side snapped up to a width class (multiple of 32)."""
44
+ limit = choose_canvas(h, w, min_scale, canvases); r = min(1.0, limit / max(h, w)); nh, nw = h * r, w * r
45
+ if h >= w: cw = next(int(round(limit * f / 32)) * 32 for f in width_classes if limit * f >= nw); return limit, limit, cw
46
+ ch = next(int(round(limit * f / 32)) * 32 for f in width_classes if limit * f >= nh); return limit, ch, limit
47
+
48
+ def rotate_boxes(boxes, deg, w, h):
49
+ """Map detector polygons into the deskewed page frame (same rotation rotate_small applies to the image)."""
50
+ if not boxes: return boxes
51
+ M = cv2.getRotationMatrix2D((w / 2, h / 2), deg, 1.0)
52
+ return [(cv2.transform(b[None].astype(np.float32), M)[0], sc) for b, sc in boxes]
53
+
54
+ class BatchOCR:
55
+ def __init__(self, rec=None, cfg=None, providers=None, det=None, lay=None, use_layout=None, layout_mode=None, workers=None, tta_conf=None, tta_cap=None, rec_bs=None, det_bs=None, det_size="small"):
56
+ """rec/det/lay: model objects to share (built from cfg when None). layout_mode: 'always' | 'auto' (only pages that look
57
+ like tables/forms) | 'never'. tta_cap: second-pass lines per page. Every default comes from the config."""
58
+ from .rec_onnx import KanEnRecognizer
59
+ self.cfg = cfg or getattr(rec, "cfg", None) or load_config(); self.prov = providers or getattr(rec, "prov", None) or Providers(self.cfg)
60
+ dc, lc, rc, sp, rt = self.cfg["detector"], self.cfg["layout"], self.cfg["recognizer"], self.cfg["second_pass"], self.cfg["runtime"]
61
+ self.layout_mode = layout_mode or lc.get("mode", "always")
62
+ if use_layout is False: self.layout_mode = "never"
63
+ self.rec = rec or KanEnRecognizer(cfg=self.cfg, providers=self.prov); self.det = det or Detector(self.cfg, self.prov)
64
+ self.lay = lay if lay is not None else (Layout(self.cfg, self.prov) if self.layout_mode != "never" else None)
65
+ self.tta_conf = float(sp["conf"] if tta_conf is None else tta_conf); self.tta_cap = int(sp["cap"] if tta_cap is None else tta_cap)
66
+ self.rec_bs = int(rc["batch"] if rec_bs is None else rec_bs); self.det_bs = int(dc["batch"] if det_bs is None else det_bs)
67
+ self.canvases = tuple(int(c) for c in dc["canvases"]); self.width_classes = tuple(float(f) for f in dc["width_classes"]); self.min_scale = float(dc["min_scale"])
68
+ self.workers = int(workers) if workers else self.cfg.workers(); self.pool = ThreadPoolExecutor(self.workers); self._planned = set()
69
+ self.model_ids = {"layout": "arjuna-layout-v12" if self.lay else None, "det": f"ppocrv6-{det_size}-det", "rec": getattr(self.rec, "name", "kanen-rec"),
70
+ "rec_md5": getattr(self.rec, "onnx_md5", None), "rec_onnx_md5": getattr(self.rec, "onnx_md5", None), "charset_md5": getattr(self.rec, "charset_md5", None), "pipeline": str(self.cfg.get("version", "1.0.0"))}
71
+
72
+ def _sess(self, limit):
73
+ """detector session for a canvas limit (one per limit, as in Detector)."""
74
+ return self.det._session(limit)
75
+
76
+ def _plan(self, limit, ch, cw, n):
77
+ key = (limit, ch, cw, n)
78
+ if key not in self._planned: # ORT CUDA EP re-plans on a new input shape (~1 s) -> do it once, outside the timed path
79
+ s = self._sess(limit); s.run(None, {s.get_inputs()[0].name: np.zeros((n, 3, ch, cw), np.float32)}); self._planned.add(key)
80
+
81
+ def _prep_det(self, img, ch, cw, limit):
82
+ h, w = img.shape[:2]; r = min(1.0, limit / max(h, w)); nh, nw = max(32, int(round(h * r / 32)) * 32), max(32, int(round(w * r / 32)) * 32)
83
+ nh, nw = min(nh, ch), min(nw, cw)
84
+ x = cv2.dnn.blobFromImage(cv2.resize(img, (nw, nh)), 1 / 255.0, (nw, nh), MEAN255, swapRB=False)[0] # (3,nh,nw), (x/255 - mean)
85
+ x *= INV_STD[:, None, None]
86
+ canvas = np.empty((3, ch, cw), np.float32); canvas[:] = PAD_VAL[:, None, None]; canvas[:, :nh, :nw] = x
87
+ return canvas, (nh, nw, w / nw, h / nh)
88
+
89
+ # ---- detector, batched over pages that share a canvas shape ----
90
+ def _det_batch(self, imgs, limit, ch, cw):
91
+ s = self._sess(limit); self._plan(limit, ch, cw, len(imgs))
92
+ prepped = list(self.pool.map(lambda im: self._prep_det(im, ch, cw, limit), imgs))
93
+ x = np.stack([p[0] for p in prepped]); meta = [p[1] for p in prepped]
94
+ prob = s.run(None, {s.get_inputs()[0].name: x})[0][:, 0]
95
+ return list(self.pool.map(lambda k: self.det._post(prob[k][:meta[k][0], :meta[k][1]], meta[k][2], meta[k][3]), range(len(imgs))))
96
+
97
+ def _detect(self, imgs):
98
+ """imgs -> boxes per page; one detector pass per page, batched per canvas shape, no square padding."""
99
+ groups = {}
100
+ for i, im in enumerate(imgs): groups.setdefault(canvas_shape(*im.shape[:2], min_scale=self.min_scale, canvases=self.canvases, width_classes=self.width_classes), []).append(i)
101
+ out = [None] * len(imgs)
102
+ for (limit, ch, cw), ids in groups.items():
103
+ for j in range(0, len(ids), self.det_bs):
104
+ sub = ids[j:j + self.det_bs]
105
+ for i, b in zip(sub, self._det_batch([imgs[i] for i in sub], limit, ch, cw)): out[i] = b
106
+ return out
107
+
108
+ # Layout batch is fixed (config layout.batch): TensorRT builds one engine per input shape (variable sizes = long rebuilds), so
109
+ # pages are always run at the configured batch, zero-padded; the engine is self-tested before its first use (Layout.ensure).
110
+ def _layout(self, imgs):
111
+ if not self.lay or not imgs: return [[] for _ in imgs]
112
+ if self.lay.trt and self.lay.bs not in self.lay.ok: self.lay.ensure(self.lay.bs) # validates (or swaps) the engine batch before the first use
113
+ return self.lay.batch(imgs)
114
+
115
+ def _crops(self, img, boxes, pad=None):
116
+ if pad is None: return [crop(img, b) for b, _ in boxes]
117
+ out = []
118
+ for b, _ in boxes:
119
+ b = b.copy(); hgt = 0.5 * ((b[3, 1] - b[0, 1]) + (b[2, 1] - b[1, 1])); b[0, 1] -= pad[0] * hgt; b[1, 1] -= pad[0] * hgt; b[2, 1] += pad[1] * hgt; b[3, 1] += pad[1] * hgt
120
+ out.append(crop(img, b))
121
+ return out
122
+
123
+ # ---------------- stage A (detector side) and stage B (recogniser side) so consecutive batches can overlap ----------------
124
+ def _stage_a(self, imgs):
125
+ """detect -> orientation 90/270 -> deskew (boxes rotated, no second pass) -> crops. CPU+GPU(det)."""
126
+ t0 = time.perf_counter(); n = len(imgs); rot = [0] * n; ang = [0.0] * n
127
+ boxes = self._detect(imgs)
128
+ vert = [i for i in range(n) if boxes[i] and np.median([np.linalg.norm(b[1] - b[0]) / max(np.linalg.norm(b[3] - b[0]), 1) for b, _ in boxes[i]]) < 1.0]
129
+ if vert: # candidate 90/270: keep the orientation with more readable lines (mixed-orientation forms stay upright)
130
+ imgs90 = [rotate_page(imgs[i], 1) for i in vert]; boxes90 = self._detect(imgs90)
131
+ for i, im90, b90 in zip(vert, imgs90, boxes90):
132
+ good0 = n_readable(self.rec(self._crops(imgs[i], boxes[i]), bs=self.rec_bs)) if boxes[i] else 0
133
+ good90 = n_readable(self.rec(self._crops(im90, b90), bs=self.rec_bs)) if b90 else 0
134
+ if good90 > good0 or (good90 == 0 and good0 == 0): imgs[i], boxes[i], rot[i] = im90, b90, 90 # nothing readable either way -> trust the aspect rule; the 180° check finishes 270° pages
135
+ for i in range(n):
136
+ ang[i] = deskew_angle(boxes[i])
137
+ if abs(ang[i]) > 0.4:
138
+ hh, ww = imgs[i].shape[:2]; imgs[i] = rotate_small(imgs[i], ang[i]); boxes[i] = rotate_boxes(boxes[i], ang[i], ww, hh)
139
+ # layout only where the page looks structured (many short boxes) or when forced; runs in its own thread
140
+ need_lay = [i for i in range(n) if self.lay and (self.layout_mode == "always" or (self.layout_mode == "auto" and self._structured(boxes[i])))]
141
+ lay_future = self.pool.submit(self._layout, [imgs[i] for i in need_lay]) if need_lay else None
142
+ # crops: one warp task per crop (the 4-core box has little headroom, but warps release the GIL)
143
+ jobs = [(i, b) for i in range(n) for b, _ in boxes[i]]
144
+ flat = list(self.pool.map(lambda ib: crop(imgs[ib[0]], ib[1]), jobs))
145
+ return {"imgs": imgs, "boxes": boxes, "rot": rot, "ang": ang, "flat": flat, "jobs": jobs, "lay_future": lay_future, "need_lay": need_lay, "t_a": time.perf_counter() - t0}
146
+
147
+ def _structured(self, boxes, min_boxes=25, short_frac=0.45):
148
+ """Heuristic for 'this page has tables/forms': many boxes and a large share of short ones."""
149
+ if len(boxes) < min_boxes: return False
150
+ ws = [np.linalg.norm(b[1] - b[0]) for b, _ in boxes]; med = np.median(ws)
151
+ return np.mean([w < 0.5 * med for w in ws]) > short_frac or len(boxes) > 80
152
+
153
+ def _stage_b(self, A, page_indices=None, dpi=None, with_polygons=False):
154
+ """recognise (pooled) -> 180° fallback -> capped second pass -> layout join -> assemble. GPU(rec)+CPU."""
155
+ t0 = time.perf_counter(); imgs, boxes, rot, ang, jobs = A["imgs"], A["boxes"], A["rot"], A["ang"], A["jobs"]; n = len(imgs)
156
+ res = self.rec(A["flat"], bs=self.rec_bs) if A["flat"] else []
157
+ texts = [[] for _ in range(n)]
158
+ for (i, _), r in zip(jobs, res): texts[i].append(r)
159
+ # 180° candidates: low mean confidence OR weak characters (upside-down pages read as confident garbage, min-char ~0.3)
160
+ top5 = lambda r: sorted(r, key=lambda tc: -len(tc[0]))[:5]
161
+ flip = [i for i in range(n) if texts[i] and (np.mean([c for _, c in texts[i]]) < 0.55 or orient_score(top5(texts[i])) < 1.35)]
162
+ if flip:
163
+ fimgs = [rotate_page(imgs[i], 2) for i in flip]; fboxes = self._detect(fimgs)
164
+ fcrops = [self._crops(im, b) for im, b in zip(fimgs, fboxes)]; ff = [c for cs in fcrops for c in cs]; fres = self.rec(ff, bs=self.rec_bs) if ff else []; p = 0
165
+ for i, im, b, cs in zip(flip, fimgs, fboxes, fcrops):
166
+ r = fres[p:p + len(cs)]; p += len(cs)
167
+ if r and orient_score(top5(r)) > orient_score(top5(texts[i])) + 0.10: imgs[i], boxes[i], texts[i], rot[i] = im, b, r, (rot[i] + 180) % 360
168
+ # confidence-gated second pass: only the lowest-confidence lines (cap per page), pooled across the batch
169
+ low = []
170
+ for i in range(n):
171
+ cand = sorted([(c, j) for j, (_, c) in enumerate(texts[i]) if c < self.tta_conf])[:self.tta_cap]; low += [(i, j) for _, j in cand]
172
+ if low:
173
+ alt = list(self.pool.map(lambda ij: self._crops(imgs[ij[0]], [boxes[ij[0]][ij[1]]], pad=(0.06, 0.08))[0], low))
174
+ for (i, j), (tx, c) in zip(low, self.rec(alt, bs=self.rec_bs)):
175
+ if c > texts[i][j][1] + 0.02: texts[i][j] = (tx, c)
176
+ t_rec = time.perf_counter() - t0; t1 = time.perf_counter()
177
+ blocks = [[] for _ in range(n)]
178
+ if A["lay_future"]:
179
+ for i, bl in zip(A["need_lay"], A["lay_future"].result()): blocks[i] = bl
180
+ for i in range(n): # layout ran on the un-deskewed page: rotate its blocks into the deskewed frame
181
+ if abs(ang[i]) > 0.4 and blocks[i]:
182
+ hh, ww = imgs[i].shape[:2]; M = cv2.getRotationMatrix2D((ww / 2, hh / 2), ang[i], 1.0)
183
+ for b in blocks[i]:
184
+ x0, y0, x1, y1 = b["bbox"]; pts = cv2.transform(np.array([[[x0, y0], [x1, y0], [x1, y1], [x0, y1]]], np.float32), M)[0]
185
+ b["bbox"] = [float(pts[:, 0].min()), float(pts[:, 1].min()), float(pts[:, 0].max()), float(pts[:, 1].max())]
186
+ t_lay = time.perf_counter() - t1; t2 = time.perf_counter()
187
+ def build(i):
188
+ d = assemble(imgs[i].shape[1], imgs[i].shape[0], blocks[i], boxes[i], texts[i], self.model_ids, None, (page_indices or list(range(n)))[i], dpi, with_polygons, img=imgs[i])
189
+ d["page"]["rotation"] = rot[i]; d["page"]["deskew_deg"] = round(ang[i], 2); d["page"]["second_pass_lines"] = sum(1 for a, _ in low if a == i); d["page"]["layout_run"] = i in A["need_lay"]; return d
190
+ docs = list(self.pool.map(build, range(n)))
191
+ T = {"det+crop": A["t_a"], "rec": t_rec, "layout_wait": t_lay, "assemble": time.perf_counter() - t2}
192
+ for d in docs: d["timing_ms"] = {k: round(v * 1000 / n, 1) for k, v in T.items()}
193
+ return docs, T
194
+
195
+ def process(self, imgs, page_indices=None, dpi=None, with_polygons=False):
196
+ """One page batch, stages A then B (no overlap). Use run() for the pipelined version."""
197
+ t0 = time.perf_counter(); A = self._stage_a(imgs); docs, T = self._stage_b(A, page_indices, dpi, with_polygons); T["total"] = time.perf_counter() - t0
198
+ return docs, T
199
+
200
+ def run(self, paths, page_batch=None, loader=None, with_polygons=False):
201
+ """Pipelined batched run over all pages; returns (docs, stats). See `stream` for the generator variant."""
202
+ docs = []; stats = {}
203
+ for d, st in self.stream(paths, page_batch, loader, with_polygons): docs.append(d); stats = st
204
+ return docs, stats
205
+
206
+ def stream(self, paths, page_batch=None, loader=None, with_polygons=False):
207
+ """Pipelined: decode(k+2) | stage A(k+1) | stage B(k) run concurrently. stage A is detector-GPU + CPU crops,
208
+ stage B is recogniser-GPU + CPU assembly, so the two GPU models interleave and the CPU work hides behind them.
209
+ paths: file paths (decoded with cv2.imread) or anything `loader` turns into a BGR array.
210
+ Yields (doc, running_stats) per page as soon as its page batch is finished; the last stats dict is the total."""
211
+ page_batch = int(page_batch or self.cfg["runtime"].get("page_batch", 8)); loader = loader or cv2.imread
212
+ batches = [paths[i:i + page_batch] for i in range(0, len(paths), page_batch)]
213
+ if not batches: return
214
+ dec = lambda ps: list(self.pool.map(loader, ps))
215
+ stageA = ThreadPoolExecutor(1) # one worker: stage A of batch k+1 overlaps stage B of batch k
216
+ agg = {}; npages = 0; t0 = time.perf_counter()
217
+ try:
218
+ imgs0 = dec(batches[0]); a_fut = stageA.submit(self._stage_a, imgs0); nxt = self.pool.submit(dec, batches[1]) if len(batches) > 1 else None
219
+ for k in range(len(batches)):
220
+ A = a_fut.result()
221
+ if k + 1 < len(batches):
222
+ imgs1 = nxt.result(); nxt = self.pool.submit(dec, batches[k + 2]) if k + 2 < len(batches) else None
223
+ a_fut = stageA.submit(self._stage_a, imgs1)
224
+ d, T = self._stage_b(A, page_indices=list(range(npages, npages + len(A["imgs"]))), with_polygons=with_polygons); npages += len(d)
225
+ for kk, v in T.items(): agg[kk] = agg.get(kk, 0.0) + v
226
+ wall = time.perf_counter() - t0
227
+ st = {"pages": npages, "wall_s": wall, "pages_per_s": npages / max(wall, 1e-9), "ms_per_page": {kk: round(v * 1000 / max(npages, 1), 1) for kk, v in agg.items()}}
228
+ for doc in d: yield doc, st
229
+ finally:
230
+ stageA.shutdown(wait=False)
kn-en/kanen_infer/charset.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """KanEn dictionary + label normaliser.
2
+ Classes: CTC blank (0) + ASCII printable (incl. space) + Kannada block (used code points) + document symbols.
3
+ Everything NFC. ZWJ/ZWNJ are stripped (decision: fonts render the same joined form; keep labels visual)."""
4
+ import unicodedata, re
5
+
6
+ ASCII = [chr(i) for i in range(32, 127)] # 95 incl. space
7
+ KANNADA = [chr(c) for c in range(0x0C80, 0x0CF4)] # candidate block; filtered by 'assigned'
8
+ KANNADA = [c for c in KANNADA if unicodedata.category(c) not in ("Cn",)]
9
+ SYMBOLS = list("₹–—‘’“”•·°×÷½¼¾§¶।॥…‑‐−™©®")
10
+ ZW = {"\u200c", "\u200d", "\u200b", "\ufeff", "\u2060", "\xad", "\u200e", "\u200f"}
11
+
12
+ def build_dict():
13
+ seen, out = set(), []
14
+ for c in ASCII + KANNADA + SYMBOLS:
15
+ if c not in seen: seen.add(c); out.append(c)
16
+ return out # index 0 in the model = blank; chars start at 1
17
+
18
+ CHARS = build_dict()
19
+ CHAR2ID = {c: i + 1 for i, c in enumerate(CHARS)}
20
+ NUM_CLASSES = len(CHARS) + 1
21
+
22
+ _ws = re.compile(r"[ \t ]+")
23
+ QUOTE_MAP = {"‘": "'", "’": "'", "“": '"', "”": '"'} # optional folding for eval only
24
+
25
+ def normalize(s: str, fold_quotes: bool = False) -> str:
26
+ s = unicodedata.normalize("NFC", s)
27
+ s = "".join(ch for ch in s if ch not in ZW)
28
+ s = s.replace("಼", "") # Kannada nukta: not used in printed text
29
+ if fold_quotes:
30
+ s = "".join(QUOTE_MAP.get(ch, ch) for ch in s)
31
+ s = _ws.sub(" ", s).strip()
32
+ return s
33
+
34
+ def encode(s: str):
35
+ ids, oov = [], []
36
+ for ch in s:
37
+ i = CHAR2ID.get(ch)
38
+ if i is None: oov.append(ch)
39
+ else: ids.append(i)
40
+ return ids, oov
41
+
42
+ def decode(ids):
43
+ return "".join(CHARS[i - 1] for i in ids if 0 < i <= len(CHARS))
44
+
45
+ def script_of(s: str) -> str:
46
+ kn = sum(0x0C80 <= ord(c) <= 0x0CFF for c in s); en = sum(c.isascii() and c.isalpha() for c in s)
47
+ dig = sum(c.isdigit() for c in s)
48
+ if kn and en: return "mixed"
49
+ if kn: return "kn"
50
+ if en: return "en"
51
+ return "num" if dig else "other"
52
+
53
+ if __name__ == "__main__":
54
+ print("classes incl. blank:", NUM_CLASSES, "| kannada:", len(KANNADA), "| ascii:", len(ASCII), "| symbols:", len(SYMBOLS))
55
+
56
+
57
+ # --- Kannada orthographic sanity (label QA only; never used to alter model output) ---
58
+ _VSIGN = set(chr(c) for c in range(0x0CBE, 0x0CCD)) | {"\u0CD5", "\u0CD6"} # dependent vowel signs + length marks
59
+ _VIRAMA = "\u0CCD"; _CONS = set(chr(c) for c in range(0x0C95, 0x0CBA)) | {"\u0CDE"} # incl. archaic ೞ
60
+ def kannada_orthography_issues(s: str):
61
+ """Returns a list of reasons the string cannot be valid printed Kannada (empty list = looks valid)."""
62
+ issues = []
63
+ for i, ch in enumerate(s):
64
+ prev = s[i - 1] if i else ""
65
+ if ch in _VSIGN and (prev in _VSIGN or prev == _VIRAMA or prev == "" or prev == " "): issues.append(f"vowel sign after '{prev}' at {i}")
66
+ if ch == _VIRAMA and (prev == _VIRAMA or prev in _VSIGN or prev not in _CONS): issues.append(f"virama after '{prev}' at {i}")
67
+ # Latin/ASCII letters glued inside a Kannada word (no space between scripts)
68
+ import re
69
+ if re.search(r"[\u0C80-\u0CFF][A-Za-z]|[A-Za-z][\u0C80-\u0CFF]", s): issues.append("latin letter glued to kannada")
70
+ if re.search(r"[0-9][\u0CBE-\u0CCD]", s): issues.append("digit followed by kannada mark")
71
+ if re.search(r"[\u0CDE\u0CF1\u0CF2]", s): issues.append("rare/archaic kannada letter")
72
+ return issues
kn-en/kanen_infer/cli.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """`kanen-ocr` command line.
2
+
3
+ kanen-ocr --input scans/ --out-dir out/ --format all
4
+ kanen-ocr --input "pages/*.png" --out-dir out/ --format json --page-batch 8 --workers 8 --provider trt
5
+ kanen-ocr --input report.pdf --out-dir out/ --format txt,tsv --json-lines
6
+
7
+ Exit codes: 0 ok · 1 no input pages · 2 models/engines failed to load · 3 some pages failed (others written).
8
+ The summary line reports pages, pages/s, the provider each model ended on and every fallback that happened."""
9
+ import argparse, json, os, sys, time, traceback
10
+ from pathlib import Path
11
+
12
+ FORMAT_CHOICES = ["json", "md", "txt", "hocr", "alto", "tsv", "all"]
13
+
14
+ def build_parser():
15
+ ap = argparse.ArgumentParser(prog="kanen-ocr", description="Arjuna-OCR [Kn&En]: layout-aware Kannada + English document OCR (kanen-ocr/1.0 JSON and other serialisations)")
16
+ ap.add_argument("--input", "-i", nargs="+", default=None, help="image file(s), directory, glob, or PDF")
17
+ ap.add_argument("--out-dir", "-o", default="kanen_out", help="output directory (one file per page and format)")
18
+ ap.add_argument("--format", "-f", default="json", help="comma list of json,md,txt,hocr,alto,tsv or 'all'")
19
+ ap.add_argument("--json-lines", action="store_true", help="also write results.jsonl (one document per line) and results.json (list)")
20
+ ap.add_argument("--page-batch", type=int, default=None); ap.add_argument("--layout-mode", choices=["always", "auto", "never"], default=None)
21
+ ap.add_argument("--workers", type=int, default=None); ap.add_argument("--provider", choices=["auto", "trt", "cuda", "cpu"], default=None)
22
+ ap.add_argument("--polygons", action="store_true", help="include 4-point polygons in line objects"); ap.add_argument("--pdf-dpi", type=int, default=200)
23
+ ap.add_argument("--config", default=None, help="alternative kanen.yaml / .json"); ap.add_argument("--quiet", "-q", action="store_true"); ap.add_argument("--limit", type=int, default=0)
24
+ ap.add_argument("--version", action="store_true", help="print version and exit")
25
+ return ap
26
+
27
+ def main(argv=None):
28
+ a = build_parser().parse_args(argv)
29
+ from . import __version__
30
+ if a.version: print(__version__); return 0
31
+ if not a.input: build_parser().error("the following arguments are required: --input/-i")
32
+ from .api import KanEnOCR
33
+ from .io import expand_inputs
34
+ from .formats import FORMATS, render
35
+ fmts = FORMAT_CHOICES[:-1] if a.format == "all" else [f.strip() for f in a.format.split(",") if f.strip()]
36
+ bad = [f for f in fmts if f not in FORMATS]
37
+ if bad: print(f"unknown format(s) {bad}; choose from {FORMAT_CHOICES}", file=sys.stderr); return 1
38
+ paths = expand_inputs(a.input); paths = paths[:a.limit] if a.limit else paths
39
+ if not paths: print(f"no input pages match {a.input}", file=sys.stderr); return 1
40
+ say = (lambda *x: None) if a.quiet else (lambda *x: print(*x, file=sys.stderr, flush=True))
41
+ t0 = time.time()
42
+ try:
43
+ ocr = KanEnOCR(config=a.config, providers=a.provider, layout_mode=a.layout_mode, workers=a.workers)
44
+ except Exception as e:
45
+ print(f"failed to load models: {type(e).__name__}: {e}", file=sys.stderr); traceback.print_exc(); return 2
46
+ r = ocr.ready(); prov = ", ".join(f"{k}={v.get('provider')}{'' if v.get('provider') != 'trt' else ('-fp16' if v.get('fp16') else '-fp32')}" for k, v in r["providers"].items())
47
+ say(f"{r['name']} {r['version']} | providers: {prov} | load {r['load_s']}s warm-up {r['warmup_s']}s | {len(paths)} input file(s)")
48
+ out = Path(a.out_dir); out.mkdir(parents=True, exist_ok=True); docs = []; failed = 0; jl = open(out / "results.jsonl", "w", encoding="utf-8") if a.json_lines else None
49
+ try:
50
+ for doc in ocr.stream(paths, page_batch=a.page_batch, polygons=a.polygons, pdf_dpi=a.pdf_dpi):
51
+ label = doc["page"].get("file", f"page_{doc['page']['index']}"); base, _, pg = label.partition("#") # PDFs: name.pdf#3 -> name_p3
52
+ stem = os.path.splitext(Path(base).name)[0] + (f"_p{pg}" if pg else "")
53
+ for f in fmts:
54
+ (out / (stem + FORMATS[f])).write_text(render(doc, f), encoding="utf-8")
55
+ if jl: jl.write(json.dumps(doc, ensure_ascii=False) + "\n")
56
+ docs.append(doc)
57
+ except Exception as e:
58
+ failed += 1; print(f"page failed: {type(e).__name__}: {e}", file=sys.stderr); traceback.print_exc()
59
+ finally:
60
+ if jl: jl.close()
61
+ if a.json_lines: (out / "results.json").write_text(json.dumps(docs, ensure_ascii=False, indent=1), encoding="utf-8")
62
+ st = getattr(ocr, "last_stats", {}) or {}; wall = time.time() - t0; n = len(docs)
63
+ fb = [e for v in ocr.ready()["providers"].values() for e in v.get("fallbacks", [])]
64
+ say(f"done: {n} pages in {st.get('wall_s', wall):.1f} s = {st.get('pages_per_s', n / max(wall, 1e-9)):.2f} pages/s | ms/page {st.get('ms_per_page', {})} | providers: {prov} | fallbacks: {len(fb)}{(' -> ' + '; '.join(fb)) if fb else ''} | wrote {out}")
65
+ return 3 if failed else 0
66
+
67
+ if __name__ == "__main__": sys.exit(main())
kn-en/kanen_infer/config.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Configuration for the Arjuna-OCR [Kn&En] inference package: `kanen.yaml` next to the package + environment overrides.
2
+ Paths are resolved relative to the package root with pathlib (Linux and Windows)."""
3
+ import copy, json, logging, os
4
+ from pathlib import Path
5
+
6
+ PKG_ROOT = Path(__file__).resolve().parent.parent # directory that holds kanen.yaml, models/, trt_cache/
7
+ DEFAULT_CONFIG = PKG_ROOT / "kanen.yaml"
8
+ LOG = logging.getLogger("kanen")
9
+
10
+ _FALLBACK = { # used when kanen.yaml is missing or a key is absent; identical to the shipped kanen.yaml
11
+ "name": "Arjuna-OCR [Kn&En]", "version": "1.2.0",
12
+ "models": {"det": "models/det/inference.onnx", "layout": "models/layout/kanen_layout_v12.onnx", "layout_labels": "models/layout/classes.txt",
13
+ "layout_selftest": "models/layout/selftest.jpg", "layout_selftest_ref": "models/layout/selftest_ref.json", "rec": "models/rec/kanen_rec_e5.onnx", "charset": "models/rec/charset.json"},
14
+ "provider": {"policy": "auto", "trt_fp16": True, "engine_cache": "trt_cache", "trt_workspace_gb": 4, "cuda_mem_limit_gb": 6, "cudnn_conv_algo_search": "HEURISTIC"},
15
+ "detector": {"canvases": [1280, 1920, 2560], "width_classes": [0.5, 0.625, 0.75, 0.875, 1.0], "min_scale": 0.9, "default_canvas": 1280,
16
+ "thresh": 0.2, "box_thresh": 0.45, "unclip": 1.4, "pad_top": 0.03, "pad_bottom": 0.25, "batch": 2},
17
+ "layout": {"mode": "always", "batch": 4, "score_thresh": 0.3, "fill": [1.0, 0.8], "selftest_min_match": 1.0},
18
+ "recognizer": {"buckets": [128, 192, 256, 384, 512, 640, 768, 1024, 1280], "batch": 64, "height": 32, "max_ratio": 40},
19
+ "second_pass": {"conf": 0.85, "cap": 6},
20
+ "runtime": {"workers": "auto", "page_batch": 8, "log_level": "warning", "polygons": False},
21
+ }
22
+
23
+ def _merge(base, over):
24
+ out = copy.deepcopy(base)
25
+ for k, v in (over or {}).items():
26
+ out[k] = _merge(out[k], v) if isinstance(v, dict) and isinstance(out.get(k), dict) else copy.deepcopy(v)
27
+ return out
28
+
29
+ def _read(path):
30
+ p = Path(path)
31
+ text = p.read_text(encoding="utf-8")
32
+ if p.suffix.lower() == ".json": return json.loads(text)
33
+ import yaml
34
+ return yaml.safe_load(text) or {}
35
+
36
+ class Config(dict):
37
+ """dict with attribute access and resolved paths; `cfg.path('models','rec')` -> absolute Path."""
38
+ root: Path
39
+ def __getattr__(self, k):
40
+ try: return self[k]
41
+ except KeyError as e: raise AttributeError(k) from e
42
+ def path(self, *keys) -> Path:
43
+ v = self
44
+ for k in keys: v = v[k]
45
+ p = Path(str(v)).expanduser()
46
+ return p if p.is_absolute() else (self.root / p)
47
+ def workers(self) -> int:
48
+ w = self["runtime"].get("workers", "auto")
49
+ return int(w) if str(w).isdigit() else max(4, (os.cpu_count() or 4) // 2)
50
+
51
+ def load_config(config=None, overrides=None) -> Config:
52
+ """config: None (kanen.yaml next to the package, or $KANEN_CONFIG), a path, or a dict. overrides: nested dict applied last.
53
+ Environment: KANEN_TRT=1 -> provider.policy 'trt'; KANEN_ORT_CPU=1 -> 'cpu'; KANEN_REC_ONNX -> models.rec; KANEN_LOG -> runtime.log_level."""
54
+ src = None
55
+ if isinstance(config, dict): data = _merge(_FALLBACK, config); root = PKG_ROOT
56
+ else:
57
+ src = Path(config) if config else Path(os.environ.get("KANEN_CONFIG") or DEFAULT_CONFIG)
58
+ data = _merge(_FALLBACK, _read(src)) if src.exists() else copy.deepcopy(_FALLBACK)
59
+ root = src.resolve().parent if src.exists() else PKG_ROOT
60
+ if overrides: data = _merge(data, overrides)
61
+ if os.environ.get("KANEN_ORT_CPU"): data["provider"]["policy"] = "cpu"
62
+ elif os.environ.get("KANEN_TRT") == "1": data["provider"]["policy"] = "trt"
63
+ if os.environ.get("KANEN_REC_ONNX"): data["models"]["rec"] = os.environ["KANEN_REC_ONNX"]
64
+ if os.environ.get("KANEN_LOG"): data["runtime"]["log_level"] = os.environ["KANEN_LOG"]
65
+ cfg = Config(data); cfg.root = root; cfg.source = str(src) if src else "<dict>"
66
+ setup_logging(cfg["runtime"].get("log_level", "warning"))
67
+ return cfg
68
+
69
+ def setup_logging(level="warning"):
70
+ lvl = getattr(logging, str(level).upper(), logging.WARNING)
71
+ if not LOG.handlers:
72
+ h = logging.StreamHandler(); h.setFormatter(logging.Formatter("[%(name)s] %(message)s")); LOG.addHandler(h)
73
+ LOG.setLevel(lvl); LOG.propagate = False
74
+ try:
75
+ import onnxruntime as ort
76
+ ort.set_default_logger_severity(3 if lvl >= logging.WARNING else 1) # silence TensorRT/ORT chatter unless debug/info
77
+ except Exception: pass
kn-en/kanen_infer/formats.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Serialisations of one `kanen-ocr/1.0` document. All of them are views of the same JSON: nothing is filtered,
2
+ re-ordered or rewritten — the text, order, confidences and flags are exactly those of the document.
3
+
4
+ to_json(doc) the document itself (schema kanen-ocr/1.0)
5
+ to_markdown(doc) Markdown: titles, paragraphs, tables as pipe tables, figure/seal markers
6
+ to_text(doc) reading-order plain text: one block per paragraph (blank line between), table rows one per line with ' | '
7
+ to_hocr(doc) hOCR 1.2 (ocr_page > ocr_carea > ocr_line > ocrx_word), bbox + x_wconf per line
8
+ to_alto(doc) ALTO 4 XML (TextBlock > TextLine > String with WC), one String per line
9
+ to_tsv(doc) page, block, line, bbox, text, confidence, min_conf, flags (tab separated, header row)
10
+ """
11
+ import json
12
+ from xml.sax.saxutils import escape, quoteattr
13
+ from .assemble import to_markdown # noqa: F401 (re-exported)
14
+
15
+ FORMATS = {"json": ".json", "md": ".md", "txt": ".txt", "hocr": ".hocr", "alto": ".xml", "tsv": ".tsv"}
16
+
17
+ def to_json(doc, indent=1):
18
+ return json.dumps(doc, ensure_ascii=False, indent=indent)
19
+
20
+ def block_text(b):
21
+ if b.get("type") == "table" and b.get("rows"): return "\n".join(" | ".join(c["text"] for c in r["cells"]) for r in b["rows"])
22
+ return "\n".join(l["text"] for l in b.get("lines", []))
23
+
24
+ def to_text(doc):
25
+ parts = [t for t in (block_text(b) for b in doc["blocks"]) if t.strip()]
26
+ return "\n\n".join(parts) + "\n"
27
+
28
+ def _ibox(bb): return " ".join(str(int(round(v))) for v in bb)
29
+
30
+ def to_hocr(doc, title=None, name="Arjuna-OCR [Kn&En]"):
31
+ pg = doc["page"]; ver = doc.get("model", {}).get("pipeline", ""); pidx = pg.get("index", 0)
32
+ fn = pg.get("file") or f"page_{pidx}"
33
+ out = ['<?xml version="1.0" encoding="UTF-8"?>', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
34
+ '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="kn">', "<head>", f"<title>{escape(title or fn)}</title>", '<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>',
35
+ f'<meta name="ocr-system" content="{escape(name)} {escape(str(ver))}"/>', '<meta name="ocr-capabilities" content="ocr_page ocr_carea ocr_line ocrx_word"/>', "</head>", "<body>",
36
+ f'<div class="ocr_page" id="page_{pidx + 1}" title={quoteattr(f"image {fn}; bbox 0 0 {int(pg["width"])} {int(pg["height"])}; ppageno {pidx}")}>']
37
+ for b in doc["blocks"]:
38
+ bid = b.get("id", "b"); out.append(f'<div class="ocr_carea" id="{bid}" title={quoteattr("bbox " + _ibox(b["bbox"]))}>')
39
+ out.append(f'<p class="ocr_par" id="{bid}_par" title={quoteattr("bbox " + _ibox(b["bbox"]))}>')
40
+ for l in b.get("lines", []):
41
+ conf = int(round(100 * float(l["confidence"]))); lid = l.get("id", bid + ".l"); t = "bbox " + _ibox(l["bbox"])
42
+ out.append(f'<span class="ocr_line" id="{lid}" title={quoteattr(t)}>' f'<span class="ocrx_word" id="{lid}_w0" title={quoteattr(f"{t}; x_wconf {conf}")}>{escape(l["text"])}</span></span>')
43
+ out.append("</p></div>")
44
+ out += ["</div>", "</body>", "</html>", ""]
45
+ return "\n".join(out)
46
+
47
+ def to_alto(doc, name="Arjuna-OCR [Kn&En]"):
48
+ pg = doc["page"]; ver = doc.get("model", {}).get("pipeline", ""); pidx = pg.get("index", 0); fn = pg.get("file") or f"page_{pidx}"
49
+ out = ['<?xml version="1.0" encoding="UTF-8"?>', '<alto xmlns="http://www.loc.gov/standards/alto/ns-v4#">', "<Description>", "<MeasurementUnit>pixel</MeasurementUnit>",
50
+ f"<sourceImageInformation><fileName>{escape(fn)}</fileName></sourceImageInformation>",
51
+ f"<OCRProcessing ID=\"ocr\"><ocrProcessingStep><processingSoftware><softwareName>{escape(name)}</softwareName><softwareVersion>{escape(str(ver))}</softwareVersion></processingSoftware></ocrProcessingStep></OCRProcessing>",
52
+ "</Description>", "<Layout>", f'<Page ID="page_{pidx + 1}" PHYSICAL_IMG_NR="{pidx + 1}" WIDTH="{int(pg["width"])}" HEIGHT="{int(pg["height"])}">', "<PrintSpace>"]
53
+ def geo(bb): x0, y0, x1, y1 = bb; return f'HPOS="{int(round(x0))}" VPOS="{int(round(y0))}" WIDTH="{int(round(x1 - x0))}" HEIGHT="{int(round(y1 - y0))}"'
54
+ for b in doc["blocks"]:
55
+ bid = b.get("id", "b"); out.append(f'<TextBlock ID="{bid}" {geo(b["bbox"])}>')
56
+ for l in b.get("lines", []):
57
+ lid = l.get("id", bid + ".l")
58
+ out.append(f'<TextLine ID="{lid}" {geo(l["bbox"])}><String ID="{lid}_s" {geo(l["bbox"])} CONTENT={quoteattr(l["text"])} WC="{float(l["confidence"]):.4f}"/></TextLine>')
59
+ out.append("</TextBlock>")
60
+ out += ["</PrintSpace>", "</Page>", "</Layout>", "</alto>", ""]
61
+ return "\n".join(out)
62
+
63
+ TSV_HEADER = "page\tblock\tline\tbbox\ttext\tconfidence\tmin_conf\tflags"
64
+
65
+ def tsv_rows(doc):
66
+ pg = doc["page"]; page = pg.get("file") or str(pg.get("index", 0))
67
+ for b in doc["blocks"]:
68
+ for l in b.get("lines", []):
69
+ yield [page, b.get("id", ""), l.get("id", ""), ",".join(str(int(round(v))) for v in l["bbox"]), l["text"].replace("\t", " "), f'{float(l["confidence"]):.4f}', f'{float(l.get("min_conf", l["confidence"])):.4f}', " ".join(l.get("flags", []))]
70
+
71
+ def to_tsv(doc, header=True):
72
+ rows = ["\t".join(r) for r in tsv_rows(doc)]
73
+ return ((TSV_HEADER + "\n") if header else "") + "\n".join(rows) + ("\n" if rows else "")
74
+
75
+ def render(doc, fmt):
76
+ return {"json": to_json, "md": to_markdown, "txt": to_text, "hocr": to_hocr, "alto": to_alto, "tsv": to_tsv}[fmt](doc)
kn-en/kanen_infer/geometry.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Page geometry helpers (orientation / deskew)."""
2
+ import math, numpy as np, cv2
3
+
4
+ def rotate_page(img, k): # k in {0,1,2,3} multiples of 90° counter-clockwise
5
+ return np.ascontiguousarray(np.rot90(img, k)) if k else img
6
+
7
+ def deskew_angle(boxes):
8
+ """Median angle (deg) of wide line boxes; positive = counter-clockwise tilt."""
9
+ angs = []
10
+ for b, _ in boxes:
11
+ w = np.linalg.norm(b[1] - b[0]); h = np.linalg.norm(b[3] - b[0])
12
+ if w > 4 * h and w > 60:
13
+ angs.append(math.degrees(math.atan2(b[1][1] - b[0][1], b[1][0] - b[0][0])))
14
+ return float(np.median(angs)) if len(angs) >= 5 else 0.0
15
+
16
+ def rotate_small(img, deg):
17
+ h, w = img.shape[:2]; M = cv2.getRotationMatrix2D((w / 2, h / 2), deg, 1.0)
18
+ return cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_LINEAR, borderValue=(255, 255, 255))
19
+
kn-en/kanen_infer/io.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Input handling: file path, bytes, numpy BGR array, PIL image, directory / glob expansion, and PDF pages
2
+ (rendered with pypdfium2 if installed, else `pdftoppm` from poppler if on PATH; both optional)."""
3
+ import glob, io as _io, os, shutil, subprocess, tempfile
4
+ from pathlib import Path
5
+ import numpy as np, cv2
6
+
7
+ IMAGE_EXT = {".png", ".jpg", ".jpeg", ".tif", ".tiff", ".bmp", ".webp", ".jp2"}
8
+ PDF_EXT = {".pdf"}
9
+
10
+ def to_bgr(x):
11
+ """Any supported input -> numpy uint8 BGR (H, W, 3). Raises ValueError if it cannot be decoded."""
12
+ if isinstance(x, np.ndarray):
13
+ if x.ndim == 2: return cv2.cvtColor(x, cv2.COLOR_GRAY2BGR)
14
+ if x.ndim == 3 and x.shape[2] == 4: return cv2.cvtColor(x, cv2.COLOR_BGRA2BGR)
15
+ if x.ndim == 3 and x.shape[2] == 3: return np.ascontiguousarray(x)
16
+ raise ValueError(f"unsupported array shape {x.shape}")
17
+ if isinstance(x, (bytes, bytearray, memoryview)):
18
+ im = cv2.imdecode(np.frombuffer(bytes(x), np.uint8), cv2.IMREAD_COLOR)
19
+ if im is None: raise ValueError("could not decode image bytes")
20
+ return im
21
+ if isinstance(x, (str, os.PathLike)):
22
+ im = cv2.imdecode(np.fromfile(str(x), np.uint8), cv2.IMREAD_COLOR) # fromfile: unicode paths on Windows
23
+ if im is None: raise ValueError(f"could not read image {x}")
24
+ return im
25
+ if hasattr(x, "convert") and hasattr(x, "size"): # PIL.Image without importing PIL
26
+ return cv2.cvtColor(np.asarray(x.convert("RGB")), cv2.COLOR_RGB2BGR)
27
+ if hasattr(x, "read"): return to_bgr(x.read())
28
+ raise ValueError(f"unsupported input type {type(x).__name__}")
29
+
30
+ def expand_inputs(inputs):
31
+ """file / directory / glob / list -> sorted list of image and PDF paths (strings)."""
32
+ if isinstance(inputs, (str, os.PathLike)): inputs = [inputs]
33
+ out = []
34
+ for it in inputs:
35
+ p = Path(it)
36
+ if p.is_dir(): out += sorted(str(f) for f in p.iterdir() if f.suffix.lower() in IMAGE_EXT | PDF_EXT)
37
+ elif p.exists(): out.append(str(p))
38
+ else:
39
+ g = sorted(glob.glob(str(it), recursive=True))
40
+ out += [f for f in g if Path(f).suffix.lower() in IMAGE_EXT | PDF_EXT]
41
+ return out
42
+
43
+ def pdf_pages(path, dpi=200, pages=None):
44
+ """Render a PDF to BGR page images. Uses pypdfium2 when importable, else poppler's pdftoppm; ImportError otherwise.
45
+ pages: optional 1-based list of page numbers."""
46
+ try:
47
+ import pypdfium2 as pdfium
48
+ doc = pdfium.PdfDocument(str(path)); idx = [p - 1 for p in pages] if pages else range(len(doc)); out = []
49
+ for i in idx:
50
+ bm = doc[i].render(scale=dpi / 72.0); pil = bm.to_pil(); out.append(cv2.cvtColor(np.asarray(pil.convert("RGB")), cv2.COLOR_RGB2BGR))
51
+ return out
52
+ except ImportError: pass
53
+ exe = shutil.which("pdftoppm")
54
+ if not exe: raise ImportError("PDF input needs `pip install pypdfium2` or poppler's pdftoppm on PATH")
55
+ with tempfile.TemporaryDirectory() as td:
56
+ cmd = [exe, "-r", str(dpi), "-png"]
57
+ if pages: cmd += ["-f", str(min(pages)), "-l", str(max(pages))]
58
+ subprocess.run(cmd + [str(path), os.path.join(td, "p")], check=True, capture_output=True)
59
+ files = sorted(Path(td).glob("p*.png"), key=lambda f: int("".join(ch for ch in f.stem if ch.isdigit()) or 0))
60
+ if pages: files = [f for f in files if int("".join(ch for ch in f.stem if ch.isdigit())) in set(pages)]
61
+ return [to_bgr(str(f)) for f in files]
62
+
63
+ def iter_page_sources(paths, dpi=200):
64
+ """Yield (label, loader) per page: images load lazily by path; PDFs are rendered page by page."""
65
+ for p in paths:
66
+ if Path(p).suffix.lower() in PDF_EXT:
67
+ for k, im in enumerate(pdf_pages(p, dpi=dpi)): yield f"{Path(p).name}#{k + 1}", im
68
+ else: yield Path(p).name, p
kn-en/kanen_infer/pipeline_base.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Detector (PP-OCRv6 det, DB post-processing) and Layout (Arjuna layout v12, DocLayout-YOLO based) models on ONNX Runtime, plus the crop helpers.
2
+ Parameters come from the package config (`kanen.yaml`); sessions come from `providers.Providers` (trt -> cuda -> cpu).
3
+ The post-processing is identical to the v1.0 reference: no thresholds, padding or ordering were changed."""
4
+ import logging, os, shutil, time
5
+ import numpy as np, cv2, pyclipper, yaml
6
+ from shapely.geometry import Polygon
7
+ from .config import load_config, PKG_ROOT
8
+ from .providers import Providers
9
+
10
+ LOG = logging.getLogger("kanen")
11
+ ROOT = str(PKG_ROOT) # kept for callers that import ROOT
12
+ MEAN = np.array([0.485, 0.456, 0.406], np.float32); STD = np.array([0.229, 0.224, 0.225], np.float32)
13
+
14
+ class Detector:
15
+ def __init__(self, cfg=None, providers=None, limit=None):
16
+ # pad_bottom=0.25: Kannada below-baseline conjuncts were clipped in 88 % of lines with DB's default boxes;
17
+ # 25 % bottom extension -> 1.2 % clipped, 0 intrusion into the next line (measured on MILE, 2026-09-13).
18
+ self.cfg = cfg or load_config(); self.prov = providers or Providers(self.cfg); d = self.cfg["detector"]
19
+ self.path = str(self.cfg.path("models", "det"))
20
+ self.limit = int(limit or d.get("default_canvas", 1280)); self.thresh = float(d["thresh"]); self.box_thresh = float(d["box_thresh"]); self.unclip = float(d["unclip"])
21
+ self.pad_top, self.pad_bottom = float(d["pad_top"]), float(d["pad_bottom"])
22
+ # one TensorRT engine covering every canvas shape the batched path can produce (long side in `canvases`, short side
23
+ # >= min(width_classes) * smallest canvas, batch <= detector.batch): built once by prepare_engines, never rebuilt mid-run
24
+ cv = [int(c) for c in d["canvases"]]; lo = max(32, int(round(cv[0] * min(float(f) for f in d["width_classes"]) / 32)) * 32); hi = max(cv); nb = int(d.get("batch", 2))
25
+ self.trt_opts = {"trt_profile_min_shapes": f"x:1x3x{lo}x{lo}", "trt_profile_opt_shapes": f"x:{nb}x3x1920x1216", "trt_profile_max_shapes": f"x:{nb}x3x{hi}x{hi}"}
26
+ # one ORT session per canvas size: the CUDA EP re-plans (~1 s) every time the input shape changes, so the
27
+ # scale-normalised re-detects must not share a session with the default canvas.
28
+ self._sessions = {}; self.s = self._session(self.limit)
29
+ def _session(self, limit):
30
+ if limit not in self._sessions:
31
+ s = self.prov.session(self.path, "det", trt_opts=self.trt_opts); x = np.zeros((1, 3, limit, limit), np.float32); s.run(None, {s.get_inputs()[0].name: x}) # warm-up plan
32
+ self._sessions[limit] = s
33
+ return self._sessions[limit]
34
+ @property
35
+ def provider(self): return self.prov.status.get("det", {}).get("provider")
36
+
37
+ def __call__(self, img_bgr):
38
+ h, w = img_bgr.shape[:2]; self.s = self._session(self.limit)
39
+ r = min(1.0, self.limit / max(h, w))
40
+ nh, nw = max(32, int(round(h * r / 32)) * 32), max(32, int(round(w * r / 32)) * 32)
41
+ canvas = np.full((self.limit, self.limit, 3), 255, np.uint8) # fixed shape -> no ORT re-planning
42
+ canvas[:nh, :nw] = cv2.resize(img_bgr, (nw, nh))
43
+ x = ((canvas.astype(np.float32) / 255.0 - MEAN) / STD).transpose(2, 0, 1)[None]
44
+ prob = self.s.run(None, {self.s.get_inputs()[0].name: x})[0][0, 0][:nh, :nw]
45
+ return self._post(prob, w / nw, h / nh)
46
+ def _post(self, prob, sx, sy):
47
+ bit = (prob > self.thresh).astype(np.uint8)
48
+ cnts, _ = cv2.findContours(bit, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
49
+ out = []
50
+ for c in cnts[:3000]:
51
+ if len(c) < 4: continue
52
+ pts = c.reshape(-1, 2)
53
+ x0, y0 = pts.min(0); x1, y1 = pts.max(0) + 1 # score on the bounding rect only (full-map masks cost ~4 ms/box)
54
+ mask = np.zeros((y1 - y0, x1 - x0), np.uint8); cv2.fillPoly(mask, [pts - (x0, y0)], 1)
55
+ score = prob[y0:y1, x0:x1][mask == 1].mean() if mask.any() else 0
56
+ if score < self.box_thresh: continue
57
+ poly = Polygon(pts)
58
+ if poly.area < 4: continue
59
+ d = poly.area * self.unclip / max(poly.length, 1e-6)
60
+ pc = pyclipper.PyclipperOffset(); pc.AddPath(pts.tolist(), pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
61
+ ex = pc.Execute(d)
62
+ if not ex: continue
63
+ rect = cv2.minAreaRect(np.array(ex[0], np.float32))
64
+ if min(rect[1]) < 3: continue
65
+ box = cv2.boxPoints(rect); box = order_pts(box)
66
+ box[:, 0] *= sx; box[:, 1] *= sy
67
+ hgt = 0.5 * ((box[3, 1] - box[0, 1]) + (box[2, 1] - box[1, 1]))
68
+ box[0, 1] -= self.pad_top * hgt; box[1, 1] -= self.pad_top * hgt; box[2, 1] += self.pad_bottom * hgt; box[3, 1] += self.pad_bottom * hgt
69
+ box[:, 1] = np.clip(box[:, 1], 0, prob.shape[0] * sy)
70
+ out.append((box, float(score)))
71
+ return out
72
+
73
+ def order_pts(b):
74
+ b = b[np.argsort(b[:, 0])]
75
+ l, r = b[:2][np.argsort(b[:2, 1])], b[2:][np.argsort(b[2:, 1])]
76
+ return np.array([l[0], r[0], r[1], l[1]], np.float32)
77
+
78
+ def crop(img, box):
79
+ w = int(max(np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[2] - box[3])))
80
+ h = int(max(np.linalg.norm(box[0] - box[3]), np.linalg.norm(box[1] - box[2])))
81
+ M = cv2.getPerspectiveTransform(box, np.array([[0, 0], [w, 0], [w, h], [0, h]], np.float32))
82
+ return cv2.warpPerspective(img, M, (w, h), flags=cv2.INTER_CUBIC)
83
+
84
+ class Layout:
85
+ """v1.2 layout model: DocLayout-YOLO (YOLOv10-based) fine-tuned for Kannada/English documents — 9 classes (title, text,
86
+ table, figure, seal, id_panel, header, footer, handwriting), 1024 letterbox input, top-300 selection inside the graph
87
+ (NMS-free), output (B, 300, 6) = x0 y0 x1 y1 conf cls in letterbox pixels. No reading-order head: the assembler's
88
+ xy-cut supplies the order.
89
+ TensorRT's timing-based tactic selection occasionally builds an engine that computes garbage for a correct graph
90
+ (seen for this model and for PP-DocLayoutV3 in v1.1: a fresh fp16 engine returned 2 of 7 blocks while PyTorch fp16,
91
+ ORT fp16 and rebuilt engines were exact). So every engine is self-tested against the bundled page + reference boxes
92
+ before use: fp16 -> fp16 rebuild -> fp32 TensorRT -> CUDA EP. The model itself is fp16-safe."""
93
+ NAMES = ["title", "text", "table", "figure", "seal", "id_panel", "header", "footer", "handwriting"]
94
+ SIZE = 1024
95
+ def __init__(self, cfg=None, providers=None):
96
+ self.cfg = cfg or load_config(); self.prov = providers or Providers(self.cfg); lc = self.cfg["layout"]
97
+ self.path = str(self.cfg.path("models", "layout"))
98
+ use32 = (self.prov.cache_root / "layout_fp32" / "USE_FP32").exists() # a previous run found the fp16 engine wrong on this GPU
99
+ self.s = self.prov.session(self.path, "layout", fp16=False, cache_sub="layout_fp32") if use32 else self.prov.session(self.path, "layout")
100
+ self.inp = self.s.get_inputs()[0].name
101
+ lp = self.cfg.path("models", "layout_labels") if self.cfg["models"].get("layout_labels") else None
102
+ self.labels = [l.strip() for l in open(lp, encoding="utf-8") if l.strip()] if lp and lp.exists() else list(self.NAMES)
103
+ self.trt = "Tensorrt" in self.s.get_providers()[0]; self.ok = set(); self.bs = int(lc.get("batch", 4)) # batch the batched pipeline should use
104
+ self.thr = float(lc.get("score_thresh", 0.3)); self.selftest_min_match = float(lc.get("selftest_min_match", 0.8))
105
+ # fill: the page's long side occupies this fraction of the canvas. The YOLOv10 head regresses at most 15 x 32 = 480 px per
106
+ # side from an anchor, so a block wider/taller than ~900 px of a 1024 canvas (whole-page tables on landscape gazettes /
107
+ # assembly records) comes out truncated and under-confident at fill 1.0; at 0.8 every page box fits (table conf 0.18 -> 0.79).
108
+ f = lc.get("fill", [1.0, 0.8]); self.fills = [float(v) for v in (f if isinstance(f, (list, tuple)) else [f])]; self.fill = self.fills[0]
109
+ st = self.cfg.path("models", "layout_selftest"); self.test_img = cv2.imread(str(st)) if st.exists() else None
110
+ rp = self.cfg.path("models", "layout_selftest_ref") if self.cfg["models"].get("layout_selftest_ref") else None
111
+ self.test_ref = __import__("json").load(open(rp, encoding="utf-8")) if rp and rp.exists() else None
112
+ self.stage = ("trt-fp32" if use32 else "trt-fp16") if self.trt else self.prov.status["layout"]["provider"]
113
+ @property
114
+ def provider(self): return self.prov.status.get("layout", {}).get("provider")
115
+ def prep(self, img_bgr, fill=None):
116
+ """letterbox (top-left, pad 114) to SIZE x SIZE, RGB, 0-1 -> ((3,S,S) float32, scale)"""
117
+ S = self.SIZE; h, w = img_bgr.shape[:2]; r = S * (self.fill if fill is None else fill) / max(h, w); nh, nw = max(1, int(round(h * r))), max(1, int(round(w * r)))
118
+ can = np.full((S, S, 3), 114, np.uint8); can[:nh, :nw] = cv2.resize(img_bgr, (nw, nh), interpolation=cv2.INTER_AREA)
119
+ return can[:, :, ::-1].transpose(2, 0, 1).astype(np.float32) / 255.0, r
120
+ def run(self, x):
121
+ """raw graph call with a validated engine for this batch size; x: (bs,3,S,S) float32 -> (bs,300,6)"""
122
+ bs = x.shape[0]
123
+ if self.trt and bs not in self.ok: self.ensure(bs)
124
+ return self.s.run(None, {self.inp: x})[0]
125
+ def parse(self, out, r, thr=None):
126
+ thr = self.thr if thr is None else thr; blocks = []
127
+ for x0, y0, x1, y1, sc, cls in out:
128
+ if not (sc >= thr) or not np.isfinite([x0, y0, x1, y1]).all() or not 0 <= int(cls) < len(self.labels): continue
129
+ blocks.append({"type": self.labels[int(cls)], "score": float(sc), "bbox": [float(x0 / r), float(y0 / r), float(x1 / r), float(y1 / r)], "order": None})
130
+ return blocks
131
+ @staticmethod
132
+ def merge(primary, extra, page_hw, min_frac=0.7, iou_thr=0.5, covered=0.5):
133
+ """primary = boxes from the first fill (kept as they are); extra = boxes from the reduced fill(s), which exist only to
134
+ recover blocks too large for the head at full fill: an extra box is added when its long side is >= min_frac of the page's
135
+ long side, no same-class primary box overlaps it at IoU >= iou_thr, and same-class primary boxes cover < `covered` of its
136
+ area (a form page already split into sub-tables keeps the sub-tables). Geometry only."""
137
+ L = float(max(page_hw)); out = list(primary)
138
+ def area(bb): return max(0.0, bb[2] - bb[0]) * max(0.0, bb[3] - bb[1])
139
+ def inter(a, b): return max(0.0, min(a[2], b[2]) - max(a[0], b[0])) * max(0.0, min(a[3], b[3]) - max(a[1], b[1]))
140
+ for e in sorted(extra, key=lambda x: -x["score"]):
141
+ bb = e["bbox"]
142
+ if max(bb[2] - bb[0], bb[3] - bb[1]) < min_frac * L: continue
143
+ same = [k for k in out if k["type"] == e["type"]]
144
+ if any(inter(bb, k["bbox"]) / max(1e-6, area(bb) + area(k["bbox"]) - inter(bb, k["bbox"])) >= iou_thr for k in same): continue
145
+ if sum(inter(bb, k["bbox"]) for k in same) >= covered * area(bb): continue
146
+ out.append(e)
147
+ return out
148
+ def batch(self, imgs):
149
+ """blocks for a list of pages: every page is run at each configured fill (one canvas per fill), run at the validated
150
+ engine batch (zero-padded), and the per-fill boxes are merged by class-aware NMS."""
151
+ if not imgs: return []
152
+ items = [(i, f) for i in range(len(imgs)) for f in self.fills]; per = [{f: [] for f in self.fills} for _ in imgs]; BS = self.bs
153
+ for j in range(0, len(items), BS):
154
+ chunk = items[j:j + BS]; k = len(chunk); x = np.zeros((BS, 3, self.SIZE, self.SIZE), np.float32); rs = []
155
+ for n, (i, f) in enumerate(chunk): x[n], r = self.prep(imgs[i], f); rs.append(r)
156
+ o = self.run(x)
157
+ for n, (i, f) in enumerate(chunk): per[i][f] += self.parse(o[n], rs[n])
158
+ f0 = self.fills[0]
159
+ return [self.merge(d[f0], [b for f in self.fills[1:] for b in d[f]], imgs[i].shape[:2]) if len(self.fills) > 1 else d[f0] for i, d in enumerate(per)]
160
+ def _selftest(self, bs):
161
+ """fraction of the reference boxes (bundled page) that the engine reproduces (same class, IoU >= 0.7 at conf >= 0.3);
162
+ without a reference file: 1.0 if the engine returns >= 3 finite blocks, else 0."""
163
+ if self.test_img is None: return 1.0
164
+ x = np.zeros((bs, 3, self.SIZE, self.SIZE), np.float32); x[0], r = self.prep(self.test_img)
165
+ o = self.s.run(None, {self.inp: x})[0][0]; got = self.parse(o, 1.0, thr=0.3)
166
+ if not self.test_ref: return 1.0 if len(got) >= 3 else 0.0
167
+ def iou(a, b):
168
+ ix = max(0, min(a[2], b[2]) - max(a[0], b[0])); iy = max(0, min(a[3], b[3]) - max(a[1], b[1])); i = ix * iy
169
+ return i / max(1e-6, (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - i)
170
+ hit = sum(1 for ref in self.test_ref if any(g["type"] == ref["type"] and iou(g["bbox"], ref["bbox"]) >= 0.7 for g in got))
171
+ return hit / max(1, len(self.test_ref))
172
+ def ensure(self, bs):
173
+ """validate the TensorRT engine for batch `bs`. Plan: shipped/cached fp16 engine -> fp16 rebuilt with a 1..bs optimisation
174
+ profile (RTX 5060 Ti, TensorRT 10.16: the fp16 engine built for the fixed shape [bs=4] drops every wide block, the same
175
+ graph built for [1..4] is exact) -> fp32 TensorRT (own cache dir) -> CUDA EP. A fallback below fp16 is remembered in
176
+ `<cache>/layout_fp32/USE_FP32` so later runs go straight to the working engine instead of rebuilding fp16 each start."""
177
+ st = self.prov.status["layout"]; marker = self.prov.cache_root / "layout_fp32" / "USE_FP32"
178
+ plan = [("fp16", True, "layout", False), ("fp16", True, "layout", True), ("fp32", False, "layout_fp32", False)]
179
+ start = 2 if (marker.exists() and self.stage == "trt-fp32") else 0
180
+ for attempt in range(start, len(plan)):
181
+ tag, fp16, sub, warm = plan[attempt]
182
+ t = time.time()
183
+ if warm: self.s.run(None, {self.inp: np.zeros((1, 3, self.SIZE, self.SIZE), np.float32)}) # extend the profile to 1..bs before the test
184
+ m = self._selftest(bs)
185
+ if m >= self.selftest_min_match:
186
+ self.ok.add(bs); self.bs = bs; self.stage = f"trt-{tag}"; st["fp16"] = fp16
187
+ if fp16 and marker.exists(): marker.unlink()
188
+ return
189
+ msg = f"[layout] TensorRT {tag} engine for batch {bs} failed its self-test ({m:.0%} of the reference blocks, attempt {attempt + 1}, {time.time() - t:.0f}s) -> rebuilding"
190
+ LOG.warning(msg); st["fallbacks"].append(msg); self.prov.events.append(msg)
191
+ self.s = None; shutil.rmtree(str(self.prov.cache_root / sub), ignore_errors=True)
192
+ if attempt + 1 < len(plan):
193
+ _, nfp16, nsub, _ = plan[attempt + 1]; self.s = self.prov.session(self.path, "layout", fp16=nfp16, cache_sub=nsub)
194
+ if not nfp16: marker.parent.mkdir(parents=True, exist_ok=True); marker.write_text("fp16 layout engine failed its self-test on this GPU; using the fp32 TensorRT engine\n")
195
+ msg = "[layout] no working TensorRT engine -> CUDA execution provider for the layout model"
196
+ LOG.warning(msg); st["fallbacks"].append(msg); self.prov.events.append(msg)
197
+ self.s = self.prov.session(self.path, "layout", force="cuda"); self.trt = False; self.bs = bs; self.ok.add(bs); self.stage = st["provider"]
198
+ def __call__(self, img_bgr, thr=None):
199
+ """single page -> (blocks, None); blocks = [{type, score, bbox, order=None}] in page pixels"""
200
+ bl = self.batch([img_bgr])[0]
201
+ return ([b for b in bl if b["score"] >= thr] if thr is not None else bl), None
kn-en/kanen_infer/providers.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ONNX Runtime session factory with the provider policy trt -> cuda -> cpu and the engine-cache bookkeeping.
2
+
3
+ * `Providers(cfg)` reads the policy (`auto` | `trt` | `cuda` | `cpu`), checks `trt_cache/ENGINES.json` against the local GPU /
4
+ TensorRT version (a foreign or stale cache is moved aside so engines rebuild instead of crashing) and records, per model,
5
+ which execution provider it finally ended on plus every fallback that happened (`Providers.status`).
6
+ * `session(path, key, fp16=True, force=None, trt_opts=None)` creates one `InferenceSession`; a provider that cannot be
7
+ loaded (no tensorrt wheel, no CUDA device, corrupt engine) falls through to the next one in the chain.
8
+ Nothing here touches model outputs: the same graph runs on whichever provider loads."""
9
+ import json, logging, os, shutil, subprocess, time
10
+ from pathlib import Path
11
+ import onnxruntime as ort
12
+
13
+ LOG = logging.getLogger("kanen")
14
+ TRT = "TensorrtExecutionProvider"; CUDA = "CUDAExecutionProvider"; CPU = "CPUExecutionProvider"
15
+ SHORT = {TRT: "trt", CUDA: "cuda", CPU: "cpu"}
16
+ MANIFEST = "ENGINES.json"
17
+ _PRELOADED = False
18
+
19
+ def _cuda_libs_loadable():
20
+ import ctypes, sys
21
+ names = ["cublasLt64_12.dll", "cudart64_12.dll"] if sys.platform == "win32" else ["libcublasLt.so.12", "libcudart.so.12"]
22
+ try:
23
+ for n in names: ctypes.CDLL(n)
24
+ return True
25
+ except OSError: return False
26
+
27
+ def preload_cuda_libs():
28
+ """Make the CUDA 12 / cuDNN 9 runtime libraries visible to onnxruntime-gpu. Order: already on the loader path ->
29
+ `onnxruntime.preload_dlls()` (nvidia-cublas-cu12 / nvidia-cudnn-cu12 ... pip wheels) -> importing torch (whose wheel
30
+ bundles the same libraries). Without any of these onnxruntime silently falls back to the CPU provider."""
31
+ global _PRELOADED
32
+ if _PRELOADED: return
33
+ _PRELOADED = True
34
+ if _cuda_libs_loadable(): return
35
+ try:
36
+ if hasattr(ort, "preload_dlls"):
37
+ import contextlib, io
38
+ with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): ort.preload_dlls()
39
+ except Exception as e: LOG.debug(f"preload_dlls: {e}")
40
+ if _cuda_libs_loadable(): return
41
+ try:
42
+ import torch # noqa: F401
43
+ LOG.info("CUDA runtime libraries loaded through torch")
44
+ except Exception:
45
+ LOG.warning("CUDA 12 runtime libraries not found (install nvidia-cublas-cu12/nvidia-cudnn-cu12 or torch, or put CUDA on PATH): GPU providers will fall back to CPU")
46
+
47
+ def gpu_name():
48
+ try:
49
+ out = subprocess.run(["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], capture_output=True, text=True, timeout=10)
50
+ names = [l.strip() for l in out.stdout.splitlines() if l.strip()]
51
+ return names[0] if names else None
52
+ except Exception: return None
53
+
54
+ def trt_version():
55
+ try:
56
+ import tensorrt; return tensorrt.__version__
57
+ except Exception: return None
58
+
59
+ def md5_file(path, n=1 << 20):
60
+ import hashlib; h = hashlib.md5()
61
+ with open(path, "rb") as f:
62
+ for chunk in iter(lambda: f.read(n), b""): h.update(chunk)
63
+ return h.hexdigest()[:12]
64
+
65
+ class Providers:
66
+ def __init__(self, cfg):
67
+ self.cfg = cfg; p = cfg["provider"]
68
+ self.policy = str(p.get("policy", "auto")).lower(); self.fp16 = bool(p.get("trt_fp16", True))
69
+ self.cache_root = cfg.path("provider", "engine_cache")
70
+ if self.policy != "cpu": preload_cuda_libs()
71
+ self.available = ort.get_available_providers()
72
+ self.status = {} # model key -> dict(requested, provider, fp16, fallbacks[], cache, load_s)
73
+ self.events = [] # human-readable fallback log
74
+ self.gpu = gpu_name() if CUDA in self.available else None; self.trt_ver = trt_version()
75
+ if self.policy in ("auto", "trt"): self._check_manifest()
76
+
77
+ # ---- engine cache manifest -------------------------------------------------------------------------------------
78
+ def _check_manifest(self):
79
+ """Engines are only valid for the GPU architecture + TensorRT version they were built with. A cache built elsewhere
80
+ is moved to trt_cache/stale_<timestamp>/ so the first run rebuilds instead of failing on a foreign engine."""
81
+ mf = self.cache_root / MANIFEST
82
+ if not mf.exists() or self.trt_ver is None: return
83
+ try: m = json.loads(mf.read_text(encoding="utf-8"))
84
+ except Exception: return
85
+ same_gpu = (not self.gpu) or (not m.get("gpu")) or (m.get("gpu") == self.gpu)
86
+ same_trt = (not m.get("tensorrt")) or (m.get("tensorrt") == self.trt_ver)
87
+ if same_gpu and same_trt: return
88
+ dst = self.cache_root / f"stale_{time.strftime('%Y%m%d_%H%M%S')}"; dst.mkdir(parents=True, exist_ok=True)
89
+ for sub in self.cache_root.iterdir():
90
+ if sub.is_dir() and not sub.name.startswith("stale_"): shutil.move(str(sub), str(dst / sub.name))
91
+ shutil.move(str(mf), str(dst / MANIFEST))
92
+ msg = f"engine cache was built for {m.get('gpu')} / TensorRT {m.get('tensorrt')}, this machine has {self.gpu} / {self.trt_ver}: moved to {dst.name}, engines rebuild on first use (~4 min)"
93
+ LOG.warning(msg); self.events.append(msg)
94
+
95
+ def write_manifest(self, extra=None):
96
+ """Called by prepare_engines after a validated build: per-model status + md5 of every engine file."""
97
+ engines = {}
98
+ for sub in sorted(self.cache_root.iterdir()) if self.cache_root.exists() else []:
99
+ if sub.is_dir() and not sub.name.startswith("stale_"):
100
+ engines[sub.name] = {f.name: {"md5": md5_file(f), "bytes": f.stat().st_size} for f in sorted(sub.iterdir()) if f.suffix in (".engine", ".profile", ".timing")}
101
+ models = {k: dict(v, cache=os.path.relpath(v["cache"], self.cache_root) if v.get("cache") else None) for k, v in self.status.items()} # relative: the manifest ships with the package
102
+ m = {"package": self.cfg.get("name"), "version": self.cfg.get("version"), "gpu": self.gpu, "tensorrt": self.trt_ver, "onnxruntime": ort.__version__,
103
+ "built": time.strftime("%Y-%m-%d %H:%M:%S"), "models": models, "engines": engines}
104
+ if extra: m.update(extra)
105
+ self.cache_root.mkdir(parents=True, exist_ok=True); (self.cache_root / MANIFEST).write_text(json.dumps(m, indent=1), encoding="utf-8")
106
+ return m
107
+
108
+ # ---- sessions ----------------------------------------------------------------------------------------------------
109
+ def chain(self, force=None):
110
+ pol = (force or self.policy)
111
+ order = {"auto": ["trt", "cuda", "cpu"], "trt": ["trt", "cuda", "cpu"], "cuda": ["cuda", "cpu"], "cpu": ["cpu"]}.get(pol)
112
+ if order is None: raise ValueError(f"unknown provider policy {pol!r} (auto|trt|cuda|cpu)")
113
+ return order
114
+
115
+ def _opts(self, kind, cache, fp16, trt_opts):
116
+ p = self.cfg["provider"]
117
+ cuda = (CUDA, {"cudnn_conv_algo_search": p.get("cudnn_conv_algo_search", "HEURISTIC"), "arena_extend_strategy": "kSameAsRequested",
118
+ "gpu_mem_limit": int(float(p.get("cuda_mem_limit_gb", 6)) * 1024 ** 3)})
119
+ if kind == "cpu": return [CPU]
120
+ if kind == "cuda": return [cuda]
121
+ import tensorrt # noqa: F401 (loads libnvinfer / nvinfer_10.dll from the pip wheel so the ORT provider can find it)
122
+ cache.mkdir(parents=True, exist_ok=True)
123
+ o = {"trt_fp16_enable": bool(fp16), "trt_engine_cache_enable": True, "trt_engine_cache_path": str(cache), "trt_timing_cache_enable": True,
124
+ "trt_timing_cache_path": str(cache), "trt_max_workspace_size": int(float(p.get("trt_workspace_gb", 4)) * 1024 ** 3)}
125
+ if trt_opts: o.update(trt_opts)
126
+ return [(TRT, o), cuda]
127
+
128
+ def session(self, path, key, fp16=None, force=None, trt_opts=None, cache_sub=None):
129
+ """key: model name for the status table ('det', 'layout', 'rec'); cache_sub: engine sub-directory (default = key)."""
130
+ fp16 = self.fp16 if fp16 is None else fp16; cache = self.cache_root / (cache_sub or key)
131
+ so = ort.SessionOptions(); so.log_severity_level = 3
132
+ st = self.status.setdefault(key, {"requested": (force or self.policy), "provider": None, "fp16": fp16, "fallbacks": [], "cache": str(cache), "load_s": 0.0})
133
+ t0 = time.time(); last = None
134
+ for kind in self.chain(force):
135
+ if kind == "cuda" and CUDA not in self.available: st["fallbacks"].append("cuda: CUDAExecutionProvider not available in this onnxruntime build"); continue
136
+ if kind == "trt" and TRT not in self.available: st["fallbacks"].append("trt: TensorrtExecutionProvider not available in this onnxruntime build"); continue
137
+ for attempt in (1, 2):
138
+ try:
139
+ s = ort.InferenceSession(str(path), so, providers=self._opts(kind, cache, fp16, trt_opts))
140
+ got = s.get_providers()[0]; want = {"trt": TRT, "cuda": CUDA, "cpu": CPU}[kind]
141
+ if got != want: # ORT loads silently on the next provider when a library is missing: treat as a failed attempt
142
+ msg = f"{key}: {kind} requested but onnxruntime created the session on {SHORT.get(got, got)} (provider library not loadable?)"
143
+ LOG.warning(msg); st["fallbacks"].append(msg); self.events.append(msg)
144
+ if kind != self.chain(force)[-1]: break
145
+ st["provider"] = SHORT.get(got, got); st["fp16"] = fp16 if got == TRT else False; st["load_s"] = round(time.time() - t0, 1)
146
+ return s
147
+ except Exception as e: # corrupt / foreign engine: clear this model's cache once and retry, then fall through
148
+ last = e; text = str(e); msg = f"{key}: {kind} provider failed ({type(e).__name__}: {text.splitlines()[0][:160]})"
149
+ LOG.warning(msg); st["fallbacks"].append(msg); self.events.append(msg)
150
+ if kind == "trt" and attempt == 1 and cache.exists() and any(w in text.lower() for w in ("engine", "deserializ", "cache")):
151
+ stale = cache.parent / f"stale_{time.strftime('%Y%m%d_%H%M%S')}_{cache.name}"; shutil.move(str(cache), str(stale))
152
+ self.events.append(f"{key}: engine cache moved to {stale.name}, rebuilding"); continue
153
+ break
154
+ raise RuntimeError(f"no execution provider could load {path}: {last}")
155
+
156
+ def summary(self):
157
+ return {k: {"provider": v["provider"], "fp16": v["fp16"], "fallbacks": list(v["fallbacks"])} for k, v in self.status.items()}
kn-en/kanen_infer/rec_onnx.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """KanEn recognizer, ONNX Runtime only (TensorRT fp16 / CUDA / CPU by the provider policy). Greedy CTC decode with the frozen charset.
2
+ Same batching rule as the training-side recognizer: every crop is padded only to its own width bucket (padding a short crop
3
+ to a much wider batch width makes the model double the first glyph)."""
4
+ import hashlib, json, math
5
+ import numpy as np, cv2
6
+ from .config import load_config
7
+ from .providers import Providers, md5_file
8
+
9
+ class Conf(float):
10
+ """line confidence (mean max-prob over emitted characters) carrying `min_char` (weakest emitted character)"""
11
+ min_char = None
12
+
13
+ class KanEnRecognizer:
14
+ def __init__(self, onnx_path=None, buckets=None, cfg=None, providers=None):
15
+ self.cfg = cfg or load_config(); self.prov = providers or Providers(self.cfg); rc = self.cfg["recognizer"]
16
+ onnx_path = str(onnx_path or self.cfg.path("models", "rec")); self.path = onnx_path
17
+ self.buckets = tuple(int(b) for b in (buckets or rc["buckets"])); self.H = int(rc.get("height", 32)); self.MAX_RATIO = float(rc.get("max_ratio", 40)); self.bs = int(rc.get("batch", 64))
18
+ self.chars = json.load(open(self.cfg.path("models", "charset"), encoding="utf-8"))
19
+ self.onnx_md5 = md5_file(onnx_path); self.charset_md5 = hashlib.md5("".join(self.chars).encode("utf-8")).hexdigest()[:12]
20
+ trt_opts = {"trt_profile_min_shapes": f"image:1x3x{self.H}x{self.buckets[0]}", "trt_profile_max_shapes": f"image:64x3x{self.H}x{self.buckets[-1]}", "trt_profile_opt_shapes": f"image:48x3x{self.H}x256"}
21
+ self.s = self.prov.session(onnx_path, "rec", trt_opts=trt_opts)
22
+ self.name = "kanen-rec-" + ("trt" if "Tensorrt" in self.s.get_providers()[0] else "ort")
23
+ @property
24
+ def provider(self): return self.prov.status.get("rec", {}).get("provider")
25
+
26
+ def __call__(self, crops, bs=None):
27
+ if not crops: return []
28
+ bs = bs or self.bs; H = self.H
29
+ gray = [c if c.ndim == 2 else cv2.cvtColor(c, cv2.COLOR_BGR2GRAY) for c in crops]
30
+ ratios = [min(g.shape[1] / max(g.shape[0], 1), self.MAX_RATIO) for g in gray]
31
+ bucket_of = lambda i: next((b for b in self.buckets if b >= int(math.ceil(ratios[i] * H / 4) * 4)), self.buckets[-1])
32
+ groups = {}
33
+ for i in range(len(gray)): groups.setdefault(bucket_of(i), []).append(i)
34
+ out = [None] * len(gray)
35
+ for W, members in sorted(groups.items()):
36
+ n = max(4, min(bs, int(bs * 320 // max(W, 64))))
37
+ for i in range(0, len(members), n):
38
+ idx = members[i:i + n]; x = np.ones((len(idx), 3, H, W), np.float32)
39
+ for k, j in enumerate(idx):
40
+ g = gray[j]; nw = min(W, max(8, int(round(g.shape[1] * H / max(g.shape[0], 1)))))
41
+ r = cv2.resize(g, (nw, H), interpolation=cv2.INTER_AREA if g.shape[0] > H else cv2.INTER_LINEAR).astype(np.float32) / 255.0
42
+ x[k, :, :, :nw] = (r - 0.5) / 0.5
43
+ probs = self.s.run(None, {"image": x})[0] # (B, T, C) softmax
44
+ ids = probs.argmax(-1); pmax = probs.max(-1)
45
+ for k, j in enumerate(idx):
46
+ row = ids[k]; keep = (row != 0) & (row != np.roll(row, 1)); s, prev = [], 0
47
+ for t in row:
48
+ if t != prev and t != 0: s.append(self.chars[t - 1])
49
+ prev = t
50
+ c = Conf(float(pmax[k][keep].mean()) if keep.any() else 0.0); c.min_char = float(pmax[k][keep].min()) if keep.any() else 0.0
51
+ out[j] = ("".join(s), c)
52
+ return out
kn-en/models/det/inference.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d73e0058b7a8086bbd57f3d10b8bcd4ff95363f67e06e2762b5e814fe9c9410e
3
+ size 9880512
kn-en/models/det/inference.yml ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Global:
2
+ model_name: PP-OCRv6_small_det
3
+ Hpi:
4
+ backend_configs:
5
+ paddle_infer:
6
+ trt_dynamic_shapes: &id001
7
+ x:
8
+ - - 1
9
+ - 3
10
+ - 32
11
+ - 32
12
+ - - 1
13
+ - 3
14
+ - 736
15
+ - 736
16
+ - - 1
17
+ - 3
18
+ - 4000
19
+ - 4000
20
+ tensorrt:
21
+ dynamic_shapes: *id001
22
+ PostProcess:
23
+ box_thresh: 0.45
24
+ max_candidates: 3000
25
+ name: DBPostProcess
26
+ thresh: 0.2
27
+ unclip_ratio: 1.4
28
+ PreProcess:
29
+ transform_ops:
30
+ - DecodeImage:
31
+ channel_first: false
32
+ img_mode: BGR
33
+ - DetLabelEncode: null
34
+ - DetResizeForTest: null
35
+ - NormalizeImage:
36
+ mean:
37
+ - 0.485
38
+ - 0.456
39
+ - 0.406
40
+ order: hwc
41
+ scale: 1./255.
42
+ std:
43
+ - 0.229
44
+ - 0.224
45
+ - 0.225
46
+ - ToCHWImage: null
47
+ - KeepKeys:
48
+ keep_keys:
49
+ - image
50
+ - shape
51
+ - polys
52
+ - ignore_tags
kn-en/models/layout/LICENSE-AGPL-3.0.txt ADDED
@@ -0,0 +1,661 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GNU AFFERO GENERAL PUBLIC LICENSE
2
+ Version 3, 19 November 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+ Preamble
9
+
10
+ The GNU Affero General Public License is a free, copyleft license for
11
+ software and other kinds of works, specifically designed to ensure
12
+ cooperation with the community in the case of network server software.
13
+
14
+ The licenses for most software and other practical works are designed
15
+ to take away your freedom to share and change the works. By contrast,
16
+ our General Public Licenses are intended to guarantee your freedom to
17
+ share and change all versions of a program--to make sure it remains free
18
+ software for all its users.
19
+
20
+ When we speak of free software, we are referring to freedom, not
21
+ price. Our General Public Licenses are designed to make sure that you
22
+ have the freedom to distribute copies of free software (and charge for
23
+ them if you wish), that you receive source code or can get it if you
24
+ want it, that you can change the software or use pieces of it in new
25
+ free programs, and that you know you can do these things.
26
+
27
+ Developers that use our General Public Licenses protect your rights
28
+ with two steps: (1) assert copyright on the software, and (2) offer
29
+ you this License which gives you legal permission to copy, distribute
30
+ and/or modify the software.
31
+
32
+ A secondary benefit of defending all users' freedom is that
33
+ improvements made in alternate versions of the program, if they
34
+ receive widespread use, become available for other developers to
35
+ incorporate. Many developers of free software are heartened and
36
+ encouraged by the resulting cooperation. However, in the case of
37
+ software used on network servers, this result may fail to come about.
38
+ The GNU General Public License permits making a modified version and
39
+ letting the public access it on a server without ever releasing its
40
+ source code to the public.
41
+
42
+ The GNU Affero General Public License is designed specifically to
43
+ ensure that, in such cases, the modified source code becomes available
44
+ to the community. It requires the operator of a network server to
45
+ provide the source code of the modified version running there to the
46
+ users of that server. Therefore, public use of a modified version, on
47
+ a publicly accessible server, gives the public access to the source
48
+ code of the modified version.
49
+
50
+ An older license, called the Affero General Public License and
51
+ published by Affero, was designed to accomplish similar goals. This is
52
+ a different license, not a version of the Affero GPL, but Affero has
53
+ released a new version of the Affero GPL which permits relicensing under
54
+ this license.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ TERMS AND CONDITIONS
60
+
61
+ 0. Definitions.
62
+
63
+ "This License" refers to version 3 of the GNU Affero General Public License.
64
+
65
+ "Copyright" also means copyright-like laws that apply to other kinds of
66
+ works, such as semiconductor masks.
67
+
68
+ "The Program" refers to any copyrightable work licensed under this
69
+ License. Each licensee is addressed as "you". "Licensees" and
70
+ "recipients" may be individuals or organizations.
71
+
72
+ To "modify" a work means to copy from or adapt all or part of the work
73
+ in a fashion requiring copyright permission, other than the making of an
74
+ exact copy. The resulting work is called a "modified version" of the
75
+ earlier work or a work "based on" the earlier work.
76
+
77
+ A "covered work" means either the unmodified Program or a work based
78
+ on the Program.
79
+
80
+ To "propagate" a work means to do anything with it that, without
81
+ permission, would make you directly or secondarily liable for
82
+ infringement under applicable copyright law, except executing it on a
83
+ computer or modifying a private copy. Propagation includes copying,
84
+ distribution (with or without modification), making available to the
85
+ public, and in some countries other activities as well.
86
+
87
+ To "convey" a work means any kind of propagation that enables other
88
+ parties to make or receive copies. Mere interaction with a user through
89
+ a computer network, with no transfer of a copy, is not conveying.
90
+
91
+ An interactive user interface displays "Appropriate Legal Notices"
92
+ to the extent that it includes a convenient and prominently visible
93
+ feature that (1) displays an appropriate copyright notice, and (2)
94
+ tells the user that there is no warranty for the work (except to the
95
+ extent that warranties are provided), that licensees may convey the
96
+ work under this License, and how to view a copy of this License. If
97
+ the interface presents a list of user commands or options, such as a
98
+ menu, a prominent item in the list meets this criterion.
99
+
100
+ 1. Source Code.
101
+
102
+ The "source code" for a work means the preferred form of the work
103
+ for making modifications to it. "Object code" means any non-source
104
+ form of a work.
105
+
106
+ A "Standard Interface" means an interface that either is an official
107
+ standard defined by a recognized standards body, or, in the case of
108
+ interfaces specified for a particular programming language, one that
109
+ is widely used among developers working in that language.
110
+
111
+ The "System Libraries" of an executable work include anything, other
112
+ than the work as a whole, that (a) is included in the normal form of
113
+ packaging a Major Component, but which is not part of that Major
114
+ Component, and (b) serves only to enable use of the work with that
115
+ Major Component, or to implement a Standard Interface for which an
116
+ implementation is available to the public in source code form. A
117
+ "Major Component", in this context, means a major essential component
118
+ (kernel, window system, and so on) of the specific operating system
119
+ (if any) on which the executable work runs, or a compiler used to
120
+ produce the work, or an object code interpreter used to run it.
121
+
122
+ The "Corresponding Source" for a work in object code form means all
123
+ the source code needed to generate, install, and (for an executable
124
+ work) run the object code and to modify the work, including scripts to
125
+ control those activities. However, it does not include the work's
126
+ System Libraries, or general-purpose tools or generally available free
127
+ programs which are used unmodified in performing those activities but
128
+ which are not part of the work. For example, Corresponding Source
129
+ includes interface definition files associated with source files for
130
+ the work, and the source code for shared libraries and dynamically
131
+ linked subprograms that the work is specifically designed to require,
132
+ such as by intimate data communication or control flow between those
133
+ subprograms and other parts of the work.
134
+
135
+ The Corresponding Source need not include anything that users
136
+ can regenerate automatically from other parts of the Corresponding
137
+ Source.
138
+
139
+ The Corresponding Source for a work in source code form is that
140
+ same work.
141
+
142
+ 2. Basic Permissions.
143
+
144
+ All rights granted under this License are granted for the term of
145
+ copyright on the Program, and are irrevocable provided the stated
146
+ conditions are met. This License explicitly affirms your unlimited
147
+ permission to run the unmodified Program. The output from running a
148
+ covered work is covered by this License only if the output, given its
149
+ content, constitutes a covered work. This License acknowledges your
150
+ rights of fair use or other equivalent, as provided by copyright law.
151
+
152
+ You may make, run and propagate covered works that you do not
153
+ convey, without conditions so long as your license otherwise remains
154
+ in force. You may convey covered works to others for the sole purpose
155
+ of having them make modifications exclusively for you, or provide you
156
+ with facilities for running those works, provided that you comply with
157
+ the terms of this License in conveying all material for which you do
158
+ not control copyright. Those thus making or running the covered works
159
+ for you must do so exclusively on your behalf, under your direction
160
+ and control, on terms that prohibit them from making any copies of
161
+ your copyrighted material outside their relationship with you.
162
+
163
+ Conveying under any other circumstances is permitted solely under
164
+ the conditions stated below. Sublicensing is not allowed; section 10
165
+ makes it unnecessary.
166
+
167
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168
+
169
+ No covered work shall be deemed part of an effective technological
170
+ measure under any applicable law fulfilling obligations under article
171
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172
+ similar laws prohibiting or restricting circumvention of such
173
+ measures.
174
+
175
+ When you convey a covered work, you waive any legal power to forbid
176
+ circumvention of technological measures to the extent such circumvention
177
+ is effected by exercising rights under this License with respect to
178
+ the covered work, and you disclaim any intention to limit operation or
179
+ modification of the work as a means of enforcing, against the work's
180
+ users, your or third parties' legal rights to forbid circumvention of
181
+ technological measures.
182
+
183
+ 4. Conveying Verbatim Copies.
184
+
185
+ You may convey verbatim copies of the Program's source code as you
186
+ receive it, in any medium, provided that you conspicuously and
187
+ appropriately publish on each copy an appropriate copyright notice;
188
+ keep intact all notices stating that this License and any
189
+ non-permissive terms added in accord with section 7 apply to the code;
190
+ keep intact all notices of the absence of any warranty; and give all
191
+ recipients a copy of this License along with the Program.
192
+
193
+ You may charge any price or no price for each copy that you convey,
194
+ and you may offer support or warranty protection for a fee.
195
+
196
+ 5. Conveying Modified Source Versions.
197
+
198
+ You may convey a work based on the Program, or the modifications to
199
+ produce it from the Program, in the form of source code under the
200
+ terms of section 4, provided that you also meet all of these conditions:
201
+
202
+ a) The work must carry prominent notices stating that you modified
203
+ it, and giving a relevant date.
204
+
205
+ b) The work must carry prominent notices stating that it is
206
+ released under this License and any conditions added under section
207
+ 7. This requirement modifies the requirement in section 4 to
208
+ "keep intact all notices".
209
+
210
+ c) You must license the entire work, as a whole, under this
211
+ License to anyone who comes into possession of a copy. This
212
+ License will therefore apply, along with any applicable section 7
213
+ additional terms, to the whole of the work, and all its parts,
214
+ regardless of how they are packaged. This License gives no
215
+ permission to license the work in any other way, but it does not
216
+ invalidate such permission if you have separately received it.
217
+
218
+ d) If the work has interactive user interfaces, each must display
219
+ Appropriate Legal Notices; however, if the Program has interactive
220
+ interfaces that do not display Appropriate Legal Notices, your
221
+ work need not make them do so.
222
+
223
+ A compilation of a covered work with other separate and independent
224
+ works, which are not by their nature extensions of the covered work,
225
+ and which are not combined with it such as to form a larger program,
226
+ in or on a volume of a storage or distribution medium, is called an
227
+ "aggregate" if the compilation and its resulting copyright are not
228
+ used to limit the access or legal rights of the compilation's users
229
+ beyond what the individual works permit. Inclusion of a covered work
230
+ in an aggregate does not cause this License to apply to the other
231
+ parts of the aggregate.
232
+
233
+ 6. Conveying Non-Source Forms.
234
+
235
+ You may convey a covered work in object code form under the terms
236
+ of sections 4 and 5, provided that you also convey the
237
+ machine-readable Corresponding Source under the terms of this License,
238
+ in one of these ways:
239
+
240
+ a) Convey the object code in, or embodied in, a physical product
241
+ (including a physical distribution medium), accompanied by the
242
+ Corresponding Source fixed on a durable physical medium
243
+ customarily used for software interchange.
244
+
245
+ b) Convey the object code in, or embodied in, a physical product
246
+ (including a physical distribution medium), accompanied by a
247
+ written offer, valid for at least three years and valid for as
248
+ long as you offer spare parts or customer support for that product
249
+ model, to give anyone who possesses the object code either (1) a
250
+ copy of the Corresponding Source for all the software in the
251
+ product that is covered by this License, on a durable physical
252
+ medium customarily used for software interchange, for a price no
253
+ more than your reasonable cost of physically performing this
254
+ conveying of source, or (2) access to copy the
255
+ Corresponding Source from a network server at no charge.
256
+
257
+ c) Convey individual copies of the object code with a copy of the
258
+ written offer to provide the Corresponding Source. This
259
+ alternative is allowed only occasionally and noncommercially, and
260
+ only if you received the object code with such an offer, in accord
261
+ with subsection 6b.
262
+
263
+ d) Convey the object code by offering access from a designated
264
+ place (gratis or for a charge), and offer equivalent access to the
265
+ Corresponding Source in the same way through the same place at no
266
+ further charge. You need not require recipients to copy the
267
+ Corresponding Source along with the object code. If the place to
268
+ copy the object code is a network server, the Corresponding Source
269
+ may be on a different server (operated by you or a third party)
270
+ that supports equivalent copying facilities, provided you maintain
271
+ clear directions next to the object code saying where to find the
272
+ Corresponding Source. Regardless of what server hosts the
273
+ Corresponding Source, you remain obligated to ensure that it is
274
+ available for as long as needed to satisfy these requirements.
275
+
276
+ e) Convey the object code using peer-to-peer transmission, provided
277
+ you inform other peers where the object code and Corresponding
278
+ Source of the work are being offered to the general public at no
279
+ charge under subsection 6d.
280
+
281
+ A separable portion of the object code, whose source code is excluded
282
+ from the Corresponding Source as a System Library, need not be
283
+ included in conveying the object code work.
284
+
285
+ A "User Product" is either (1) a "consumer product", which means any
286
+ tangible personal property which is normally used for personal, family,
287
+ or household purposes, or (2) anything designed or sold for incorporation
288
+ into a dwelling. In determining whether a product is a consumer product,
289
+ doubtful cases shall be resolved in favor of coverage. For a particular
290
+ product received by a particular user, "normally used" refers to a
291
+ typical or common use of that class of product, regardless of the status
292
+ of the particular user or of the way in which the particular user
293
+ actually uses, or expects or is expected to use, the product. A product
294
+ is a consumer product regardless of whether the product has substantial
295
+ commercial, industrial or non-consumer uses, unless such uses represent
296
+ the only significant mode of use of the product.
297
+
298
+ "Installation Information" for a User Product means any methods,
299
+ procedures, authorization keys, or other information required to install
300
+ and execute modified versions of a covered work in that User Product from
301
+ a modified version of its Corresponding Source. The information must
302
+ suffice to ensure that the continued functioning of the modified object
303
+ code is in no case prevented or interfered with solely because
304
+ modification has been made.
305
+
306
+ If you convey an object code work under this section in, or with, or
307
+ specifically for use in, a User Product, and the conveying occurs as
308
+ part of a transaction in which the right of possession and use of the
309
+ User Product is transferred to the recipient in perpetuity or for a
310
+ fixed term (regardless of how the transaction is characterized), the
311
+ Corresponding Source conveyed under this section must be accompanied
312
+ by the Installation Information. But this requirement does not apply
313
+ if neither you nor any third party retains the ability to install
314
+ modified object code on the User Product (for example, the work has
315
+ been installed in ROM).
316
+
317
+ The requirement to provide Installation Information does not include a
318
+ requirement to continue to provide support service, warranty, or updates
319
+ for a work that has been modified or installed by the recipient, or for
320
+ the User Product in which it has been modified or installed. Access to a
321
+ network may be denied when the modification itself materially and
322
+ adversely affects the operation of the network or violates the rules and
323
+ protocols for communication across the network.
324
+
325
+ Corresponding Source conveyed, and Installation Information provided,
326
+ in accord with this section must be in a format that is publicly
327
+ documented (and with an implementation available to the public in
328
+ source code form), and must require no special password or key for
329
+ unpacking, reading or copying.
330
+
331
+ 7. Additional Terms.
332
+
333
+ "Additional permissions" are terms that supplement the terms of this
334
+ License by making exceptions from one or more of its conditions.
335
+ Additional permissions that are applicable to the entire Program shall
336
+ be treated as though they were included in this License, to the extent
337
+ that they are valid under applicable law. If additional permissions
338
+ apply only to part of the Program, that part may be used separately
339
+ under those permissions, but the entire Program remains governed by
340
+ this License without regard to the additional permissions.
341
+
342
+ When you convey a copy of a covered work, you may at your option
343
+ remove any additional permissions from that copy, or from any part of
344
+ it. (Additional permissions may be written to require their own
345
+ removal in certain cases when you modify the work.) You may place
346
+ additional permissions on material, added by you to a covered work,
347
+ for which you have or can give appropriate copyright permission.
348
+
349
+ Notwithstanding any other provision of this License, for material you
350
+ add to a covered work, you may (if authorized by the copyright holders of
351
+ that material) supplement the terms of this License with terms:
352
+
353
+ a) Disclaiming warranty or limiting liability differently from the
354
+ terms of sections 15 and 16 of this License; or
355
+
356
+ b) Requiring preservation of specified reasonable legal notices or
357
+ author attributions in that material or in the Appropriate Legal
358
+ Notices displayed by works containing it; or
359
+
360
+ c) Prohibiting misrepresentation of the origin of that material, or
361
+ requiring that modified versions of such material be marked in
362
+ reasonable ways as different from the original version; or
363
+
364
+ d) Limiting the use for publicity purposes of names of licensors or
365
+ authors of the material; or
366
+
367
+ e) Declining to grant rights under trademark law for use of some
368
+ trade names, trademarks, or service marks; or
369
+
370
+ f) Requiring indemnification of licensors and authors of that
371
+ material by anyone who conveys the material (or modified versions of
372
+ it) with contractual assumptions of liability to the recipient, for
373
+ any liability that these contractual assumptions directly impose on
374
+ those licensors and authors.
375
+
376
+ All other non-permissive additional terms are considered "further
377
+ restrictions" within the meaning of section 10. If the Program as you
378
+ received it, or any part of it, contains a notice stating that it is
379
+ governed by this License along with a term that is a further
380
+ restriction, you may remove that term. If a license document contains
381
+ a further restriction but permits relicensing or conveying under this
382
+ License, you may add to a covered work material governed by the terms
383
+ of that license document, provided that the further restriction does
384
+ not survive such relicensing or conveying.
385
+
386
+ If you add terms to a covered work in accord with this section, you
387
+ must place, in the relevant source files, a statement of the
388
+ additional terms that apply to those files, or a notice indicating
389
+ where to find the applicable terms.
390
+
391
+ Additional terms, permissive or non-permissive, may be stated in the
392
+ form of a separately written license, or stated as exceptions;
393
+ the above requirements apply either way.
394
+
395
+ 8. Termination.
396
+
397
+ You may not propagate or modify a covered work except as expressly
398
+ provided under this License. Any attempt otherwise to propagate or
399
+ modify it is void, and will automatically terminate your rights under
400
+ this License (including any patent licenses granted under the third
401
+ paragraph of section 11).
402
+
403
+ However, if you cease all violation of this License, then your
404
+ license from a particular copyright holder is reinstated (a)
405
+ provisionally, unless and until the copyright holder explicitly and
406
+ finally terminates your license, and (b) permanently, if the copyright
407
+ holder fails to notify you of the violation by some reasonable means
408
+ prior to 60 days after the cessation.
409
+
410
+ Moreover, your license from a particular copyright holder is
411
+ reinstated permanently if the copyright holder notifies you of the
412
+ violation by some reasonable means, this is the first time you have
413
+ received notice of violation of this License (for any work) from that
414
+ copyright holder, and you cure the violation prior to 30 days after
415
+ your receipt of the notice.
416
+
417
+ Termination of your rights under this section does not terminate the
418
+ licenses of parties who have received copies or rights from you under
419
+ this License. If your rights have been terminated and not permanently
420
+ reinstated, you do not qualify to receive new licenses for the same
421
+ material under section 10.
422
+
423
+ 9. Acceptance Not Required for Having Copies.
424
+
425
+ You are not required to accept this License in order to receive or
426
+ run a copy of the Program. Ancillary propagation of a covered work
427
+ occurring solely as a consequence of using peer-to-peer transmission
428
+ to receive a copy likewise does not require acceptance. However,
429
+ nothing other than this License grants you permission to propagate or
430
+ modify any covered work. These actions infringe copyright if you do
431
+ not accept this License. Therefore, by modifying or propagating a
432
+ covered work, you indicate your acceptance of this License to do so.
433
+
434
+ 10. Automatic Licensing of Downstream Recipients.
435
+
436
+ Each time you convey a covered work, the recipient automatically
437
+ receives a license from the original licensors, to run, modify and
438
+ propagate that work, subject to this License. You are not responsible
439
+ for enforcing compliance by third parties with this License.
440
+
441
+ An "entity transaction" is a transaction transferring control of an
442
+ organization, or substantially all assets of one, or subdividing an
443
+ organization, or merging organizations. If propagation of a covered
444
+ work results from an entity transaction, each party to that
445
+ transaction who receives a copy of the work also receives whatever
446
+ licenses to the work the party's predecessor in interest had or could
447
+ give under the previous paragraph, plus a right to possession of the
448
+ Corresponding Source of the work from the predecessor in interest, if
449
+ the predecessor has it or can get it with reasonable efforts.
450
+
451
+ You may not impose any further restrictions on the exercise of the
452
+ rights granted or affirmed under this License. For example, you may
453
+ not impose a license fee, royalty, or other charge for exercise of
454
+ rights granted under this License, and you may not initiate litigation
455
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
456
+ any patent claim is infringed by making, using, selling, offering for
457
+ sale, or importing the Program or any portion of it.
458
+
459
+ 11. Patents.
460
+
461
+ A "contributor" is a copyright holder who authorizes use under this
462
+ License of the Program or a work on which the Program is based. The
463
+ work thus licensed is called the contributor's "contributor version".
464
+
465
+ A contributor's "essential patent claims" are all patent claims
466
+ owned or controlled by the contributor, whether already acquired or
467
+ hereafter acquired, that would be infringed by some manner, permitted
468
+ by this License, of making, using, or selling its contributor version,
469
+ but do not include claims that would be infringed only as a
470
+ consequence of further modification of the contributor version. For
471
+ purposes of this definition, "control" includes the right to grant
472
+ patent sublicenses in a manner consistent with the requirements of
473
+ this License.
474
+
475
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
476
+ patent license under the contributor's essential patent claims, to
477
+ make, use, sell, offer for sale, import and otherwise run, modify and
478
+ propagate the contents of its contributor version.
479
+
480
+ In the following three paragraphs, a "patent license" is any express
481
+ agreement or commitment, however denominated, not to enforce a patent
482
+ (such as an express permission to practice a patent or covenant not to
483
+ sue for patent infringement). To "grant" such a patent license to a
484
+ party means to make such an agreement or commitment not to enforce a
485
+ patent against the party.
486
+
487
+ If you convey a covered work, knowingly relying on a patent license,
488
+ and the Corresponding Source of the work is not available for anyone
489
+ to copy, free of charge and under the terms of this License, through a
490
+ publicly available network server or other readily accessible means,
491
+ then you must either (1) cause the Corresponding Source to be so
492
+ available, or (2) arrange to deprive yourself of the benefit of the
493
+ patent license for this particular work, or (3) arrange, in a manner
494
+ consistent with the requirements of this License, to extend the patent
495
+ license to downstream recipients. "Knowingly relying" means you have
496
+ actual knowledge that, but for the patent license, your conveying the
497
+ covered work in a country, or your recipient's use of the covered work
498
+ in a country, would infringe one or more identifiable patents in that
499
+ country that you have reason to believe are valid.
500
+
501
+ If, pursuant to or in connection with a single transaction or
502
+ arrangement, you convey, or propagate by procuring conveyance of, a
503
+ covered work, and grant a patent license to some of the parties
504
+ receiving the covered work authorizing them to use, propagate, modify
505
+ or convey a specific copy of the covered work, then the patent license
506
+ you grant is automatically extended to all recipients of the covered
507
+ work and works based on it.
508
+
509
+ A patent license is "discriminatory" if it does not include within
510
+ the scope of its coverage, prohibits the exercise of, or is
511
+ conditioned on the non-exercise of one or more of the rights that are
512
+ specifically granted under this License. You may not convey a covered
513
+ work if you are a party to an arrangement with a third party that is
514
+ in the business of distributing software, under which you make payment
515
+ to the third party based on the extent of your activity of conveying
516
+ the work, and under which the third party grants, to any of the
517
+ parties who would receive the covered work from you, a discriminatory
518
+ patent license (a) in connection with copies of the covered work
519
+ conveyed by you (or copies made from those copies), or (b) primarily
520
+ for and in connection with specific products or compilations that
521
+ contain the covered work, unless you entered into that arrangement,
522
+ or that patent license was granted, prior to 28 March 2007.
523
+
524
+ Nothing in this License shall be construed as excluding or limiting
525
+ any implied license or other defenses to infringement that may
526
+ otherwise be available to you under applicable patent law.
527
+
528
+ 12. No Surrender of Others' Freedom.
529
+
530
+ If conditions are imposed on you (whether by court order, agreement or
531
+ otherwise) that contradict the conditions of this License, they do not
532
+ excuse you from the conditions of this License. If you cannot convey a
533
+ covered work so as to satisfy simultaneously your obligations under this
534
+ License and any other pertinent obligations, then as a consequence you may
535
+ not convey it at all. For example, if you agree to terms that obligate you
536
+ to collect a royalty for further conveying from those to whom you convey
537
+ the Program, the only way you could satisfy both those terms and this
538
+ License would be to refrain entirely from conveying the Program.
539
+
540
+ 13. Remote Network Interaction; Use with the GNU General Public License.
541
+
542
+ Notwithstanding any other provision of this License, if you modify the
543
+ Program, your modified version must prominently offer all users
544
+ interacting with it remotely through a computer network (if your version
545
+ supports such interaction) an opportunity to receive the Corresponding
546
+ Source of your version by providing access to the Corresponding Source
547
+ from a network server at no charge, through some standard or customary
548
+ means of facilitating copying of software. This Corresponding Source
549
+ shall include the Corresponding Source for any work covered by version 3
550
+ of the GNU General Public License that is incorporated pursuant to the
551
+ following paragraph.
552
+
553
+ Notwithstanding any other provision of this License, you have
554
+ permission to link or combine any covered work with a work licensed
555
+ under version 3 of the GNU General Public License into a single
556
+ combined work, and to convey the resulting work. The terms of this
557
+ License will continue to apply to the part which is the covered work,
558
+ but the work with which it is combined will remain governed by version
559
+ 3 of the GNU General Public License.
560
+
561
+ 14. Revised Versions of this License.
562
+
563
+ The Free Software Foundation may publish revised and/or new versions of
564
+ the GNU Affero General Public License from time to time. Such new versions
565
+ will be similar in spirit to the present version, but may differ in detail to
566
+ address new problems or concerns.
567
+
568
+ Each version is given a distinguishing version number. If the
569
+ Program specifies that a certain numbered version of the GNU Affero General
570
+ Public License "or any later version" applies to it, you have the
571
+ option of following the terms and conditions either of that numbered
572
+ version or of any later version published by the Free Software
573
+ Foundation. If the Program does not specify a version number of the
574
+ GNU Affero General Public License, you may choose any version ever published
575
+ by the Free Software Foundation.
576
+
577
+ If the Program specifies that a proxy can decide which future
578
+ versions of the GNU Affero General Public License can be used, that proxy's
579
+ public statement of acceptance of a version permanently authorizes you
580
+ to choose that version for the Program.
581
+
582
+ Later license versions may give you additional or different
583
+ permissions. However, no additional obligations are imposed on any
584
+ author or copyright holder as a result of your choosing to follow a
585
+ later version.
586
+
587
+ 15. Disclaimer of Warranty.
588
+
589
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597
+
598
+ 16. Limitation of Liability.
599
+
600
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608
+ SUCH DAMAGES.
609
+
610
+ 17. Interpretation of Sections 15 and 16.
611
+
612
+ If the disclaimer of warranty and limitation of liability provided
613
+ above cannot be given local legal effect according to their terms,
614
+ reviewing courts shall apply local law that most closely approximates
615
+ an absolute waiver of all civil liability in connection with the
616
+ Program, unless a warranty or assumption of liability accompanies a
617
+ copy of the Program in return for a fee.
618
+
619
+ END OF TERMS AND CONDITIONS
620
+
621
+ How to Apply These Terms to Your New Programs
622
+
623
+ If you develop a new program, and you want it to be of the greatest
624
+ possible use to the public, the best way to achieve this is to make it
625
+ free software which everyone can redistribute and change under these terms.
626
+
627
+ To do so, attach the following notices to the program. It is safest
628
+ to attach them to the start of each source file to most effectively
629
+ state the exclusion of warranty; and each file should have at least
630
+ the "copyright" line and a pointer to where the full notice is found.
631
+
632
+ <one line to give the program's name and a brief idea of what it does.>
633
+ Copyright (C) <year> <name of author>
634
+
635
+ This program is free software: you can redistribute it and/or modify
636
+ it under the terms of the GNU Affero General Public License as published by
637
+ the Free Software Foundation, either version 3 of the License, or
638
+ (at your option) any later version.
639
+
640
+ This program is distributed in the hope that it will be useful,
641
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
642
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643
+ GNU Affero General Public License for more details.
644
+
645
+ You should have received a copy of the GNU Affero General Public License
646
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
647
+
648
+ Also add information on how to contact you by electronic and paper mail.
649
+
650
+ If your software can interact with users remotely through a computer
651
+ network, you should also make sure that it provides a way for users to
652
+ get its source. For example, if your program is a web application, its
653
+ interface could display a "Source" link that leads users to an archive
654
+ of the code. There are many ways you could offer source, and different
655
+ solutions will be better for different programs; see section 13 for the
656
+ specific requirements.
657
+
658
+ You should also get your employer (if you work as a programmer) or school,
659
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
660
+ For more information on this, and how to apply and follow the GNU AGPL, see
661
+ <https://www.gnu.org/licenses/>.