thai-ocr-web 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +177 -0
- package/NOTICE +20 -0
- package/README.md +259 -2
- package/models/NOTICE +11 -0
- package/models/config.json +21 -0
- package/models/det.onnx +0 -0
- package/models/det.yml +52 -0
- package/models/rec.onnx +0 -0
- package/models/rec2.onnx +0 -0
- package/models/rec_dict.txt +528 -0
- package/package.json +84 -4
- package/src/browser.d.ts +27 -0
- package/src/browser.js +132 -0
- package/src/index.d.ts +68 -0
- package/src/index.js +5 -0
- package/src/line-ocr.js +1108 -0
- package/src/pdf.d.ts +43 -0
- package/src/pdf.js +112 -0
- package/src/thai-norm.js +52 -0
- package/vendor/opencv/opencv-ocr.mjs +0 -0
package/src/line-ocr.js
ADDED
|
@@ -0,0 +1,1108 @@
|
|
|
1
|
+
// LineOCR: a JavaScript port of OpenThai 2.1 OCR Text's runtime (runtime/line_engine.py, v2.1.4).
|
|
2
|
+
//
|
|
3
|
+
// Ported, with modifications, from iapp/OpenThai2.1-OCR-Text-20261118 (Apache-2.0, iApp Technology).
|
|
4
|
+
// The models (det.onnx, rec.onnx, rec2.onnx) are used unchanged; they derive from PaddlePaddle PP-OCRv6-tiny.
|
|
5
|
+
// This is an unofficial port, not affiliated with iApp Technology.
|
|
6
|
+
//
|
|
7
|
+
// Dependencies are passed in, so the same code runs in a browser, a Web Worker or Node:
|
|
8
|
+
// ort onnxruntime-web (WASM / WebGPU)
|
|
9
|
+
// cv OpenCV.js, initialised (vendor/opencv/opencv-ocr.mjs is a minimal build with what this file uses)
|
|
10
|
+
// ClipperLib clipper-lib (Clipper 6, the library pyclipper wraps)
|
|
11
|
+
// For the browser, src/browser.js wires these up for you.
|
|
12
|
+
//
|
|
13
|
+
// Not ported: word boxes (`words`) and the runtime's multi-process service engines.
|
|
14
|
+
|
|
15
|
+
import { canonical } from "./thai-norm.js";
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------- config
|
|
18
|
+
|
|
19
|
+
export const DEFAULTS = {
|
|
20
|
+
det: { limit: 1280, thresh: 0.3, box_thresh: 0.6, unclip: 1.5, min_size: 3, max_candidates: 3000, v_pad: 0.0,
|
|
21
|
+
short_pad: 0.0, rescan_below: 0.0, rescan_target: 20.0, rescan_max: 3200, split_gap: 1.2 },
|
|
22
|
+
rec: { height: 48, max_w: 3200, batch: 16, drop_below: 0.0, min_chars: 1, min_chars_digits: null,
|
|
23
|
+
min_chars_digits_height: 0.0, min_chars_latin: false, junk_below: 0.0, junk_invalid: 1.0, color: "flatten" },
|
|
24
|
+
ori: { enabled: true, sample: 8, flip_below: 0.8, flip_margin: 0.15, deskew_above: 2.5 },
|
|
25
|
+
marks: { enabled: true, max_gap: 0.35, max_ext: 0.9, max_height: 0.9, margin: 0.0, page_share: 0.25, page_min: 3,
|
|
26
|
+
keep_base: true },
|
|
27
|
+
cols: { list_rep: 0.6, list_facing: 0.0, list_max_w: 10.0, list_enum: 0.0 },
|
|
28
|
+
rec2: { file: "", min_mixed: 1 },
|
|
29
|
+
join: { thai_no_space: false, decimal_comma: false, paragraphs: false, paragraph_gap: 1.0 },
|
|
30
|
+
};
|
|
31
|
+
const DET_MEAN = [0.485, 0.456, 0.406];
|
|
32
|
+
const DET_STD = [0.229, 0.224, 0.225];
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------- small helpers
|
|
35
|
+
|
|
36
|
+
const f32 = Math.fround;
|
|
37
|
+
const clip = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
|
|
38
|
+
const dist = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
39
|
+
const sum = (a) => a.reduce((s, x) => s + x, 0);
|
|
40
|
+
const mean = (a) => sum(a) / a.length;
|
|
41
|
+
|
|
42
|
+
function median(a) {
|
|
43
|
+
const s = Float64Array.from(a).sort();
|
|
44
|
+
const n = s.length;
|
|
45
|
+
return n % 2 ? s[(n - 1) / 2] : (s[n / 2 - 1] + s[n / 2]) / 2;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function pyRound(x) { // Python's round(): halves to even
|
|
49
|
+
const r = Math.round(x);
|
|
50
|
+
return Math.abs(x % 1) === 0.5 && r % 2 !== 0 ? r - 1 : r;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function argmin(a) {
|
|
54
|
+
let k = 0;
|
|
55
|
+
for (let i = 1; i < a.length; i++) if (a[i] < a[k]) k = i;
|
|
56
|
+
return k;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const boxF32 = (b) => b.map(([x, y]) => [f32(x), f32(y)]);
|
|
60
|
+
const xs = (b) => b.map((p) => p[0]);
|
|
61
|
+
const ys = (b) => b.map((p) => p[1]);
|
|
62
|
+
const boxH = (b) => dist(b[0], b[3]);
|
|
63
|
+
const boxW = (b) => dist(b[0], b[1]);
|
|
64
|
+
|
|
65
|
+
// 3x3 matrices (row-major arrays of arrays)
|
|
66
|
+
const eye = () => [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
67
|
+
function matmul(a, b) {
|
|
68
|
+
return a.map((r) => [0, 1, 2].map((j) => r[0] * b[0][j] + r[1] * b[1][j] + r[2] * b[2][j]));
|
|
69
|
+
}
|
|
70
|
+
function inv3(m) {
|
|
71
|
+
const [[a, b, c], [d, e, f], [g, h, i]] = m;
|
|
72
|
+
const A = e * i - f * h, B = -(d * i - f * g), C = d * h - e * g;
|
|
73
|
+
const det = a * A + b * B + c * C;
|
|
74
|
+
return [
|
|
75
|
+
[A / det, -(b * i - c * h) / det, (b * f - c * e) / det],
|
|
76
|
+
[B / det, (a * i - c * g) / det, -(a * f - c * d) / det],
|
|
77
|
+
[C / det, -(a * h - b * g) / det, (a * e - b * d) / det],
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
const applyM = (m, box) => box.map(([x, y]) => [m[0][0] * x + m[0][1] * y + m[0][2], m[1][0] * x + m[1][1] * y + m[1][2]]);
|
|
81
|
+
|
|
82
|
+
function rot90Matrix(k, H, W) { // original -> np.rot90(image, k) coordinates
|
|
83
|
+
if (k === 1) return [[0, 1, 0], [-1, 0, W - 1], [0, 0, 1]];
|
|
84
|
+
if (k === -1) return [[0, -1, H - 1], [1, 0, 0], [0, 0, 1]];
|
|
85
|
+
return [[-1, 0, W - 1], [0, -1, H - 1], [0, 0, 1]];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---------------------------------------------------------------------------- text rules
|
|
89
|
+
|
|
90
|
+
const DIGITS = new Set("0123456789๐๑๒๓๔๕๖๗๘๙");
|
|
91
|
+
const LATIN = new Set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
|
|
92
|
+
// Python's \d on str matches every Unicode decimal digit; \p{Nd} is the JS equivalent.
|
|
93
|
+
const DECIMAL_COMMA = /(?<![\p{Nd}.,])(\p{Nd}{1,3}(?:,\p{Nd}{3})*),(\p{Nd}{2})(?![\p{Nd}.,])/gu;
|
|
94
|
+
const LONE_W = /(?<![A-Za-z])[Ww](?=\.?[ก-ฮเ-ไ])/g;
|
|
95
|
+
const ZERO_IN_THAI = /(?<=[๐-๙])[Oo0]|[Oo0](?=[๐-๙])/g;
|
|
96
|
+
const ENUM = /^\s*\(?([0-9๐-๙]{1,4})\s*[.)]/u;
|
|
97
|
+
const DIGIT_RUN = /[0-9๐-๙]+/g;
|
|
98
|
+
const MARK_RE = /[ัิ-ฺ็-๎\s]/gu;
|
|
99
|
+
const MARKS = new Set("ัิีึืฺุู็่้๊๋์ํ๎");
|
|
100
|
+
const LEADING = new Set("เแโใไ");
|
|
101
|
+
|
|
102
|
+
export function fixScripts(text) {
|
|
103
|
+
return text.replace(LONE_W, "พ").replace(ZERO_IN_THAI, "๐");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function enumOf(text) {
|
|
107
|
+
const m = ENUM.exec(text);
|
|
108
|
+
if (!m) return null;
|
|
109
|
+
return parseInt(m[1].replace(/[๐-๙]/g, (d) => String(d.charCodeAt(0) - 0x0e50)), 10);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function startRep(lines) {
|
|
113
|
+
const st = lines.filter((x) => x.trim()).map((x) => x.trim().replace(DIGIT_RUN, "D").slice(0, 3));
|
|
114
|
+
if (!st.length) return 0;
|
|
115
|
+
const counts = new Map();
|
|
116
|
+
for (const x of st) counts.set(x, (counts.get(x) || 0) + 1);
|
|
117
|
+
const top = [...counts.values()].sort((a, b) => b - a).slice(0, 2);
|
|
118
|
+
return sum(top) / st.length;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function mixed(text) {
|
|
122
|
+
let n = 0;
|
|
123
|
+
for (const tok of text.split(/\s+/).filter(Boolean)) {
|
|
124
|
+
if ([...tok].some((ch) => ch >= "ก" && ch <= "๎")) {
|
|
125
|
+
n += [...tok].filter((ch) => (ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z")).length;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return n;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const digitsOf = (text) => [...text].filter((ch) => /\p{Nd}/u.test(ch)).join("");
|
|
132
|
+
const baseOf = (text) => text.split("ำ").join("า").replace(MARK_RE, "");
|
|
133
|
+
const isThai = (ch) => ch >= "" && ch <= "";
|
|
134
|
+
const isCons = (ch) => ch >= "ก" && ch <= "ฮ";
|
|
135
|
+
|
|
136
|
+
export function invalidShare(text) {
|
|
137
|
+
const t = [...text];
|
|
138
|
+
let thai = 0, bad = 0;
|
|
139
|
+
for (let i = 0; i < t.length; i++) {
|
|
140
|
+
const ch = t[i];
|
|
141
|
+
if (!isThai(ch)) continue;
|
|
142
|
+
thai++;
|
|
143
|
+
const prev = i ? t[i - 1] : " ";
|
|
144
|
+
if (MARKS.has(ch) && !(isCons(prev) || (MARKS.has(prev) && i >= 2 && isCons(t[i - 2])))) bad++;
|
|
145
|
+
else if (LEADING.has(ch) && !(i + 1 < t.length && isCons(t[i + 1]))) bad++;
|
|
146
|
+
}
|
|
147
|
+
return thai ? bad / thai : 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---------------------------------------------------------------------------- geometry
|
|
151
|
+
|
|
152
|
+
function orderPoints(pts) { // 4 points -> TL, TR, BR, BL
|
|
153
|
+
const s = [...pts].sort((a, b) => a[0] - b[0]);
|
|
154
|
+
const left = s.slice(0, 2).sort((a, b) => a[1] - b[1]);
|
|
155
|
+
const right = s.slice(2).sort((a, b) => a[1] - b[1]);
|
|
156
|
+
return boxF32([left[0], right[0], right[1], left[1]]);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function lineHeight(boxes) {
|
|
160
|
+
const hs = boxes.map((b) => Math.max(boxH(b), 1));
|
|
161
|
+
const lines = hs.filter((h, i) => boxW(boxes[i]) >= 2 * h);
|
|
162
|
+
return hs.length ? median(lines.length >= 3 ? lines : hs) : 1;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function pageSkew(boxes) {
|
|
166
|
+
const angles = [];
|
|
167
|
+
for (const b of boxes) {
|
|
168
|
+
const dx = b[1][0] - b[0][0], dy = b[1][1] - b[0][1];
|
|
169
|
+
if (Math.hypot(dx, dy) >= 3 * Math.max(boxH(b), 1)) angles.push((Math.atan2(dy, dx) * 180) / Math.PI);
|
|
170
|
+
}
|
|
171
|
+
return angles.length ? median(angles) : 0;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function vertical(box, ratio) {
|
|
175
|
+
const w = boxW(box), h = boxH(box);
|
|
176
|
+
return h >= ratio * Math.max(w, 1) ? 1 : w >= ratio * Math.max(h, 1) ? -1 : 0;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function fragments(boxes, rec) {
|
|
180
|
+
if (boxes.length < 3) return new Set();
|
|
181
|
+
const hs = boxes.map(boxH);
|
|
182
|
+
const hmed = lineHeight(boxes);
|
|
183
|
+
const out = new Set();
|
|
184
|
+
boxes.forEach((b, i) => {
|
|
185
|
+
if (hs[i] >= 0.35 * hmed || (rec[i] ? rec[i][0] : "").replace(/ /g, "").length > 3) return;
|
|
186
|
+
const x0 = Math.min(...xs(b)), x1 = Math.max(...xs(b)), y0 = Math.min(...ys(b)), y1 = Math.max(...ys(b));
|
|
187
|
+
for (let j = 0; j < boxes.length; j++) {
|
|
188
|
+
const c = boxes[j];
|
|
189
|
+
if (j === i || hs[j] < 0.6 * hmed) continue;
|
|
190
|
+
if (Math.min(...xs(c)) <= x1 && x0 <= Math.max(...xs(c)) &&
|
|
191
|
+
Math.min(Math.abs(y0 - Math.max(...ys(c))), Math.abs(Math.min(...ys(c)) - y1)) <= 0.5 * hmed) {
|
|
192
|
+
out.add(i);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ---------------------------------------------------------------------------- reading order
|
|
201
|
+
|
|
202
|
+
function gaps(iv, minGap) {
|
|
203
|
+
iv = [...iv].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
204
|
+
const out = [];
|
|
205
|
+
let reach = iv[0][1];
|
|
206
|
+
for (const [lo, hi] of iv.slice(1)) {
|
|
207
|
+
if (lo - reach >= minGap) out.push([reach, lo]);
|
|
208
|
+
reach = Math.max(reach, hi);
|
|
209
|
+
}
|
|
210
|
+
return out;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function nextTo(side, ext, hmed, gutterOnRight) {
|
|
214
|
+
if (!side.length) return side;
|
|
215
|
+
const g = gaps(side.map((i) => [ext[i][0], ext[i][1]]), 0.8 * hmed);
|
|
216
|
+
if (!g.length) return side;
|
|
217
|
+
if (gutterOnRight) return side.filter((i) => ext[i][0] >= g[g.length - 1][1]);
|
|
218
|
+
return side.filter((i) => ext[i][1] <= g[0][0]);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function prose(col, ext, hmed, texts) {
|
|
222
|
+
if (col.length < 12) return false;
|
|
223
|
+
const widths = col.map((i) => ext[i][1] - ext[i][0]);
|
|
224
|
+
const span = Math.max(...col.map((i) => ext[i][1])) - Math.min(...col.map((i) => ext[i][0]));
|
|
225
|
+
if (median(widths) < 7 * hmed || mean(widths.map((w) => (w >= 0.75 * span ? 1 : 0))) < 0.35) return false;
|
|
226
|
+
if (texts) {
|
|
227
|
+
const chars = col.flatMap((i) => [...texts[i]].filter((c) => !/\s/.test(c)));
|
|
228
|
+
if (chars.length && chars.filter((c) => DIGITS.has(c)).length > 0.3 * chars.length) return false;
|
|
229
|
+
if (new Set(col.map((i) => texts[i].trim())).size < 0.8 * col.length) return false;
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function numberedAcross(lcol, rcol, ext, hmed, texts) {
|
|
235
|
+
if (!texts) return false;
|
|
236
|
+
let pairs = 0;
|
|
237
|
+
for (const i of lcol) {
|
|
238
|
+
const a = enumOf(texts[i]);
|
|
239
|
+
if (a === null) continue;
|
|
240
|
+
const cy = (ext[i][2] + ext[i][3]) / 2;
|
|
241
|
+
if (rcol.some((j) => enumOf(texts[j]) === a + 1 && Math.abs((ext[j][2] + ext[j][3]) / 2 - cy) <= 0.5 * hmed)) pairs++;
|
|
242
|
+
}
|
|
243
|
+
return pairs >= 0.5 * Math.min(lcol.length, rcol.length);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function listPair(lcol, rcol, ext, hmed, texts, rules) {
|
|
247
|
+
if (!rules || !(rules.list_rep || rules.list_facing || rules.list_enum) || !lcol.length || !rcol.length) return false;
|
|
248
|
+
if (rules.list_rep && texts) {
|
|
249
|
+
if (mean([lcol, rcol].map((col) => startRep(col.map((i) => texts[i])))) >= rules.list_rep) return true;
|
|
250
|
+
}
|
|
251
|
+
const [small, other] = lcol.length <= rcol.length ? [lcol, rcol] : [rcol, lcol];
|
|
252
|
+
const cy = other.map((j) => (ext[j][2] + ext[j][3]) / 2);
|
|
253
|
+
const face = mean(small.map((i) => (cy.some((c) => Math.abs((ext[i][2] + ext[i][3]) / 2 - c) <= 0.5 * hmed) ? 1 : 0)));
|
|
254
|
+
const wide = Math.max(...[lcol, rcol].map((col) => median(col.map((i) => ext[i][1] - ext[i][0])))) / hmed;
|
|
255
|
+
if (rules.list_facing && face >= rules.list_facing && wide < (rules.list_max_w ?? 10)) return true;
|
|
256
|
+
if (rules.list_enum && texts && face >= 0.6) return mean(lcol.map((i) => (enumOf(texts[i]) !== null ? 1 : 0))) >= rules.list_enum;
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function blocks(idx, ext, hmed, texts, depth, rules) {
|
|
261
|
+
if (idx.length < 6 || depth > 12) return [idx];
|
|
262
|
+
const cuts = [];
|
|
263
|
+
for (const [g0, g1] of gaps(idx.map((i) => [ext[i][0], ext[i][1]]), 0.8 * hmed)) {
|
|
264
|
+
const left = idx.filter((i) => ext[i][1] <= g0);
|
|
265
|
+
const right = idx.filter((i) => ext[i][0] >= g1);
|
|
266
|
+
const lcol = nextTo(left, ext, hmed, true), rcol = nextTo(right, ext, hmed, false);
|
|
267
|
+
if (prose(lcol, ext, hmed, texts) && prose(rcol, ext, hmed, texts) &&
|
|
268
|
+
!numberedAcross(lcol, rcol, ext, hmed, texts) && !listPair(lcol, rcol, ext, hmed, texts, rules)) {
|
|
269
|
+
cuts.push([g1 - g0, left, right]);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (cuts.length) {
|
|
273
|
+
let best = cuts[0];
|
|
274
|
+
for (const c of cuts) if (c[0] > best[0]) best = c; // max(): first of equal maxima
|
|
275
|
+
return [...blocks(best[1], ext, hmed, texts, depth + 1, rules), ...blocks(best[2], ext, hmed, texts, depth + 1, rules)];
|
|
276
|
+
}
|
|
277
|
+
const hg = gaps(idx.map((i) => [ext[i][2], ext[i][3]]), 0.8 * hmed);
|
|
278
|
+
if (!hg.length) return bandedColumns(idx, ext, hmed, texts, depth, rules) || [idx];
|
|
279
|
+
let g = hg[0];
|
|
280
|
+
for (const x of hg) if (x[1] - x[0] > g[1] - g[0]) g = x;
|
|
281
|
+
const top = idx.filter((i) => ext[i][3] <= g[0]);
|
|
282
|
+
const bottom = idx.filter((i) => ext[i][2] >= g[1]);
|
|
283
|
+
return [...blocks(top, ext, hmed, texts, depth + 1, rules), ...blocks(bottom, ext, hmed, texts, depth + 1, rules)];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function bandedColumns(idx, ext, hmed, texts, depth, rules) {
|
|
287
|
+
const x0 = Math.min(...idx.map((i) => ext[i][0])), x1 = Math.max(...idx.map((i) => ext[i][1]));
|
|
288
|
+
const wide = idx.filter((i) => ext[i][1] - ext[i][0] >= 0.55 * (x1 - x0));
|
|
289
|
+
if (wide.length > 0.15 * idx.length) return null;
|
|
290
|
+
const wideSet = new Set(wide);
|
|
291
|
+
const rest = idx.filter((i) => !wideSet.has(i));
|
|
292
|
+
if (rest.length < 24) return null;
|
|
293
|
+
const lo = Math.trunc(x0 + 0.15 * (x1 - x0)), n = Math.trunc(0.7 * (x1 - x0)) + 1;
|
|
294
|
+
const cover = new Int32Array(n);
|
|
295
|
+
for (const i of rest) {
|
|
296
|
+
const a = Math.max(0, Math.trunc(ext[i][0]) - lo), b = Math.min(n, Math.ceil(ext[i][1]) - lo);
|
|
297
|
+
for (let x = a; x < b; x++) cover[x]++;
|
|
298
|
+
}
|
|
299
|
+
const lim = Math.max(1, Math.trunc(0.03 * rest.length));
|
|
300
|
+
const runs = [];
|
|
301
|
+
let s = null;
|
|
302
|
+
for (let x = 0; x <= n; x++) {
|
|
303
|
+
const f = x < n && cover[x] <= lim;
|
|
304
|
+
if (f && s === null) s = x;
|
|
305
|
+
else if (!f && s !== null) {
|
|
306
|
+
if (x - s >= 0.4 * hmed) runs.push([lo + s, lo + x]);
|
|
307
|
+
s = null;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
let best = null;
|
|
311
|
+
runs.forEach(([g0, g1], k) => {
|
|
312
|
+
const left = rest.filter((i) => ext[i][1] <= g0);
|
|
313
|
+
const right = rest.filter((i) => ext[i][0] >= g1);
|
|
314
|
+
const lcol = left.filter((i) => k === 0 || ext[i][0] >= runs[k - 1][1]);
|
|
315
|
+
const rcol = right.filter((i) => k === runs.length - 1 || ext[i][1] <= runs[k + 1][0]);
|
|
316
|
+
if (prose(lcol, ext, hmed, texts) && prose(rcol, ext, hmed, texts) &&
|
|
317
|
+
!numberedAcross(lcol, rcol, ext, hmed, texts) && !listPair(lcol, rcol, ext, hmed, texts, rules) &&
|
|
318
|
+
(best === null || g1 - g0 > best[0])) best = [g1 - g0, g0, g1];
|
|
319
|
+
});
|
|
320
|
+
if (best === null) return null;
|
|
321
|
+
const [, g0, g1] = best;
|
|
322
|
+
const mid = rest.filter((i) => ext[i][0] < g0 && ext[i][1] > g1);
|
|
323
|
+
const midSet = new Set(mid);
|
|
324
|
+
const cy = (i) => (ext[i][2] + ext[i][3]) / 2;
|
|
325
|
+
const cx = (i) => (ext[i][0] + ext[i][1]) / 2;
|
|
326
|
+
const seps = [...wide, ...mid].sort((a, b) => cy(a) - cy(b));
|
|
327
|
+
const out = [];
|
|
328
|
+
for (let b = 0; b <= seps.length; b++) {
|
|
329
|
+
const blo = b ? cy(seps[b - 1]) : -Infinity;
|
|
330
|
+
const bhi = b < seps.length ? cy(seps[b]) : Infinity;
|
|
331
|
+
const band = rest.filter((i) => !midSet.has(i) && blo < cy(i) && cy(i) <= bhi);
|
|
332
|
+
for (const side of [band.filter((i) => cx(i) < (g0 + g1) / 2), band.filter((i) => cx(i) >= (g0 + g1) / 2)]) {
|
|
333
|
+
if (side.length) out.push(...blocks(side, ext, hmed, texts, depth + 1, rules));
|
|
334
|
+
}
|
|
335
|
+
if (b < seps.length) out.push([seps[b]]);
|
|
336
|
+
}
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function readingOrder(boxes, angle = null, columns = true, texts = null, rules = null) {
|
|
341
|
+
if (!boxes.length) return [];
|
|
342
|
+
const a = (-(angle === null ? pageSkew(boxes) : angle) * Math.PI) / 180;
|
|
343
|
+
const c = f32(Math.cos(a)), s = f32(Math.sin(a));
|
|
344
|
+
const pts = boxes.map((b) => b.map(([x, y]) => [f32(x * c - y * s), f32(x * s + y * c)]));
|
|
345
|
+
const extOf = (p) => [Math.min(...xs(p)), Math.max(...xs(p)), Math.min(...ys(p)), Math.max(...ys(p))];
|
|
346
|
+
const ext = pts.map(extOf);
|
|
347
|
+
const hmed = lineHeight(boxes);
|
|
348
|
+
const idx = boxes.map((_, i) => i);
|
|
349
|
+
let blks = columns ? blocks(idx, ext, hmed, texts, 0, rules) : [idx];
|
|
350
|
+
if (columns && a) {
|
|
351
|
+
const alt = blocks(idx, boxes.map(extOf), hmed, texts, 0, rules);
|
|
352
|
+
if (alt.length > blks.length) blks = alt;
|
|
353
|
+
}
|
|
354
|
+
const cy = ext.map((e) => (e[2] + e[3]) / 2);
|
|
355
|
+
const rx = ext.map((e) => (e[0] + e[1]) / 2);
|
|
356
|
+
const hs = boxes.map((b) => Math.max(boxH(b), 1));
|
|
357
|
+
const tol = 0.5 * hmed;
|
|
358
|
+
const rows = [];
|
|
359
|
+
for (const blk of blks) {
|
|
360
|
+
let full = blk.filter((i) => hs[i] >= 0.6 * hmed);
|
|
361
|
+
if (!full.length) full = [...blk];
|
|
362
|
+
const fullSet = new Set(full);
|
|
363
|
+
const small = blk.filter((i) => !fullSet.has(i));
|
|
364
|
+
const brows = [];
|
|
365
|
+
for (const i of [...full].sort((p, q) => cy[p] - cy[q])) {
|
|
366
|
+
if (brows.length && Math.abs(cy[i] - mean(brows[brows.length - 1].map((j) => cy[j]))) <= tol) brows[brows.length - 1].push(i);
|
|
367
|
+
else brows.push([i]);
|
|
368
|
+
}
|
|
369
|
+
const centre = brows.map((r) => mean(r.map((j) => cy[j])));
|
|
370
|
+
for (const i of small) {
|
|
371
|
+
const d = centre.map((cc) => Math.abs(cy[i] - cc));
|
|
372
|
+
if (d.length && Math.min(...d) <= tol + 0.5 * hs[i]) brows[argmin(d)].push(i);
|
|
373
|
+
else { brows.push([i]); centre.push(cy[i]); }
|
|
374
|
+
}
|
|
375
|
+
const order = brows.map((_, r) => r).sort((p, q) => centre[p] - centre[q]);
|
|
376
|
+
for (const r of order) rows.push([...brows[r]].sort((p, q) => rx[p] - rx[q]));
|
|
377
|
+
}
|
|
378
|
+
return rows;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ---------------------------------------------------------------------------- engine
|
|
382
|
+
|
|
383
|
+
export class LineOCR {
|
|
384
|
+
/**
|
|
385
|
+
* @param {object} o
|
|
386
|
+
* @param {object} o.ort onnxruntime-web / onnxruntime-node module
|
|
387
|
+
* @param {object} o.cv initialised OpenCV.js
|
|
388
|
+
* @param {object} o.ClipperLib clipper-lib module
|
|
389
|
+
* @param {(name: string) => Promise<Uint8Array|string>} o.loadFile fetches a file of the model directory
|
|
390
|
+
* @param {string[]} [o.executionProviders] e.g. ["webgpu", "wasm"]
|
|
391
|
+
* @param {object} [o.overrides] config sections to override, as LineOCR(**overrides) in Python
|
|
392
|
+
*/
|
|
393
|
+
static async create({ ort, cv, ClipperLib, loadFile, executionProviders, overrides = {}, sessionOptions = {} }) {
|
|
394
|
+
const self = new LineOCR();
|
|
395
|
+
self.ort = ort; self.cv = cv; self.CL = ClipperLib;
|
|
396
|
+
const cfg = JSON.parse(JSON.stringify(DEFAULTS));
|
|
397
|
+
const text = async (n) => { const d = await loadFile(n); return typeof d === "string" ? d : new TextDecoder().decode(d); };
|
|
398
|
+
const det = parsePostprocess(await text("det.yml"));
|
|
399
|
+
for (const [src, dst] of [["thresh", "thresh"], ["box_thresh", "box_thresh"], ["unclip_ratio", "unclip"]]) {
|
|
400
|
+
if (typeof det[src] === "number") cfg.det[dst] = det[src];
|
|
401
|
+
}
|
|
402
|
+
let user = {};
|
|
403
|
+
try { user = JSON.parse(await text("config.json")); } catch { /* optional */ }
|
|
404
|
+
for (const section of [user, overrides]) for (const [k, v] of Object.entries(section)) cfg[k] = { ...(cfg[k] || {}), ...v };
|
|
405
|
+
self.cfg = cfg;
|
|
406
|
+
const chars = (await text("rec_dict.txt")).split("\n").filter((c) => c);
|
|
407
|
+
self.chars = ["", ...chars, " "];
|
|
408
|
+
const opts = { graphOptimizationLevel: "all", ...sessionOptions };
|
|
409
|
+
if (executionProviders) opts.executionProviders = executionProviders;
|
|
410
|
+
const sess = async (n) => ort.InferenceSession.create(await loadFile(n), opts);
|
|
411
|
+
self.det = await sess("det.onnx");
|
|
412
|
+
self.rec = await sess("rec.onnx");
|
|
413
|
+
self.rec2 = cfg.rec2.file ? await sess(cfg.rec2.file) : null;
|
|
414
|
+
return self;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async _run(session, data, dims) {
|
|
418
|
+
const feeds = { [session.inputNames[0]]: new this.ort.Tensor("float32", data, dims) };
|
|
419
|
+
const out = await session.run(feeds);
|
|
420
|
+
const t = out[session.outputNames[0]];
|
|
421
|
+
const res = { data: t.data instanceof Float32Array ? t.data : await t.getData(), dims: t.dims };
|
|
422
|
+
t.dispose?.();
|
|
423
|
+
return res;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// -- image helpers (cv.Mat in, cv.Mat out; caller deletes) -------------------
|
|
427
|
+
|
|
428
|
+
/** Accepts a cv.Mat (BGR or grey), or ImageData-like {data (RGBA), width, height}. Returns a new BGR Mat. */
|
|
429
|
+
toBGR(image) {
|
|
430
|
+
const cv = this.cv;
|
|
431
|
+
if (image instanceof cv.Mat) {
|
|
432
|
+
const out = new cv.Mat();
|
|
433
|
+
if (image.channels() === 3) image.copyTo(out);
|
|
434
|
+
else if (image.channels() === 1) cv.cvtColor(image, out, cv.COLOR_GRAY2BGR);
|
|
435
|
+
else cv.cvtColor(image, out, cv.COLOR_RGBA2BGR);
|
|
436
|
+
return out;
|
|
437
|
+
}
|
|
438
|
+
const rgba = new cv.Mat(image.height, image.width, cv.CV_8UC4);
|
|
439
|
+
rgba.data.set(image.data);
|
|
440
|
+
const out = new cv.Mat();
|
|
441
|
+
cv.cvtColor(rgba, out, cv.COLOR_RGBA2BGR);
|
|
442
|
+
rgba.delete();
|
|
443
|
+
return out;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
cropLine(img, box, turn = 1) {
|
|
447
|
+
const cv = this.cv;
|
|
448
|
+
const w = Math.max(Math.trunc(Math.max(dist(box[0], box[1]), dist(box[2], box[3]))), 1);
|
|
449
|
+
const h = Math.max(Math.trunc(Math.max(dist(box[0], box[3]), dist(box[1], box[2]))), 1);
|
|
450
|
+
const src = cv.matFromArray(4, 1, cv.CV_32FC2, box.flat());
|
|
451
|
+
const dst = cv.matFromArray(4, 1, cv.CV_32FC2, [0, 0, w, 0, w, h, 0, h]);
|
|
452
|
+
const m = cv.getPerspectiveTransform(src, dst);
|
|
453
|
+
const out = new cv.Mat();
|
|
454
|
+
cv.warpPerspective(img, out, m, new cv.Size(w, h), cv.INTER_CUBIC, cv.BORDER_REPLICATE, new cv.Scalar());
|
|
455
|
+
src.delete(); dst.delete(); m.delete();
|
|
456
|
+
if (turn && h / w >= 1.5) return this.rot90(out, turn, true);
|
|
457
|
+
return out;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
rot90(mat, k, del = false) { // np.rot90 semantics: k=1 counter-clockwise
|
|
461
|
+
const cv = this.cv;
|
|
462
|
+
const out = new cv.Mat();
|
|
463
|
+
const code = k === 1 ? cv.ROTATE_90_COUNTERCLOCKWISE : k === -1 ? cv.ROTATE_90_CLOCKWISE : cv.ROTATE_180;
|
|
464
|
+
cv.rotate(mat, out, code);
|
|
465
|
+
if (del) mat.delete();
|
|
466
|
+
return out;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** Dark text on white from a grey line crop (runtime's _flatten). g: Uint8Array h*w. Returns Uint8Array. */
|
|
470
|
+
flatten(g, h, w) {
|
|
471
|
+
const cv = this.cv;
|
|
472
|
+
const k = Math.max(2, Math.trunc(h / 6));
|
|
473
|
+
const bg = new Uint8Array(w);
|
|
474
|
+
const col = new Float64Array(2 * k);
|
|
475
|
+
for (let x = 0; x < w; x++) {
|
|
476
|
+
let n = 0;
|
|
477
|
+
for (let y = 0; y < k; y++) col[n++] = g[y * w + x];
|
|
478
|
+
for (let y = h - k; y < h; y++) col[n++] = g[y * w + x];
|
|
479
|
+
// rows that the top and bottom margins share (h < 2k) count twice, as in np.concatenate
|
|
480
|
+
bg[x] = Math.trunc(median(col.subarray(0, n)));
|
|
481
|
+
}
|
|
482
|
+
const win = Math.max(3, Math.min(h | 1, 255));
|
|
483
|
+
const three = new cv.Mat(3, w, cv.CV_8UC1);
|
|
484
|
+
three.data.set(bg, 0); three.data.set(bg, w); three.data.set(bg, 2 * w);
|
|
485
|
+
const blur = new cv.Mat();
|
|
486
|
+
cv.medianBlur(three, blur, win);
|
|
487
|
+
const bgs = blur.data.slice(w, 2 * w);
|
|
488
|
+
three.delete(); blur.delete();
|
|
489
|
+
const d = new Float32Array(h * w);
|
|
490
|
+
const hist = new Float64Array(256);
|
|
491
|
+
for (let y = 0; y < h; y++) {
|
|
492
|
+
for (let x = 0; x < w; x++) {
|
|
493
|
+
const v = Math.abs(g[y * w + x] - bgs[x]);
|
|
494
|
+
d[y * w + x] = v;
|
|
495
|
+
hist[v | 0]++;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
for (let i = 1; i < 256; i++) hist[i] += hist[i - 1];
|
|
499
|
+
const target = 0.98 * hist[255];
|
|
500
|
+
let pos = 0;
|
|
501
|
+
while (pos < 256 && hist[pos] < target) pos++;
|
|
502
|
+
const hi = Math.max(pos, 24);
|
|
503
|
+
const out = new Uint8Array(h * w);
|
|
504
|
+
for (let i = 0; i < out.length; i++) out[i] = 255 - Math.min(Math.max(d[i] * (255 / hi), 0), 255);
|
|
505
|
+
return out;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// -- detection ---------------------------------------------------------------
|
|
509
|
+
|
|
510
|
+
async detect(img) {
|
|
511
|
+
const c = this.cfg.det;
|
|
512
|
+
let boxes = await this._detectAt(img, c.limit);
|
|
513
|
+
if (c.rescan_below && c.limit && boxes.length) {
|
|
514
|
+
const H0 = img.rows, W0 = img.cols;
|
|
515
|
+
const pad = Math.max(8, Math.trunc(0.05 * Math.min(H0, W0)));
|
|
516
|
+
const s = Math.min(c.limit / Math.max(H0 + 2 * pad, W0 + 2 * pad), 4.0);
|
|
517
|
+
const hdet = lineHeight(boxes) * s;
|
|
518
|
+
if (s < 1 && hdet < c.rescan_below) {
|
|
519
|
+
const limit = Math.min(c.rescan_max, (c.limit * c.rescan_target) / Math.max(hdet, 1));
|
|
520
|
+
if (limit > c.limit * 1.15) boxes = await this._detectAt(img, limit);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
return boxes;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
async _detectAt(img0, limit) {
|
|
527
|
+
const cv = this.cv, c = this.cfg.det;
|
|
528
|
+
const H0 = img0.rows, W0 = img0.cols;
|
|
529
|
+
const pad = Math.max(8, Math.trunc(0.05 * Math.min(H0, W0)));
|
|
530
|
+
const img = new cv.Mat();
|
|
531
|
+
cv.copyMakeBorder(img0, img, pad, pad, pad, pad, cv.BORDER_CONSTANT, new cv.Scalar(255, 255, 255, 255));
|
|
532
|
+
const h = img.rows, w = img.cols;
|
|
533
|
+
const s = limit ? Math.min(limit / Math.max(h, w), 4.0) : 1.0;
|
|
534
|
+
const rh = Math.max(32, pyRound((h * s) / 32) * 32), rw = Math.max(32, pyRound((w * s) / 32) * 32);
|
|
535
|
+
const rs = new cv.Mat();
|
|
536
|
+
cv.resize(img, rs, new cv.Size(rw, rh), 0, 0, cv.INTER_LINEAR);
|
|
537
|
+
img.delete();
|
|
538
|
+
const n = rw * rh, x = new Float32Array(3 * n), px = rs.data;
|
|
539
|
+
for (let i = 0; i < n; i++) {
|
|
540
|
+
for (let ch = 0; ch < 3; ch++) x[ch * n + i] = (f32(px[3 * i + ch] / 255) - DET_MEAN[ch]) / DET_STD[ch];
|
|
541
|
+
}
|
|
542
|
+
rs.delete();
|
|
543
|
+
const { data: prob, dims } = await this._run(this.det, x, [1, 3, rh, rw]);
|
|
544
|
+
const ph = dims[2], pw = dims[3];
|
|
545
|
+
const bitmap = new cv.Mat(ph, pw, cv.CV_8UC1);
|
|
546
|
+
for (let i = 0; i < ph * pw; i++) bitmap.data[i] = prob[i] > c.thresh ? 1 : 0;
|
|
547
|
+
const contours = new cv.MatVector(), hier = new cv.Mat();
|
|
548
|
+
cv.findContours(bitmap, contours, hier, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
|
|
549
|
+
bitmap.delete(); hier.delete();
|
|
550
|
+
const sx = w / pw, sy = h / ph;
|
|
551
|
+
const boxes = [];
|
|
552
|
+
const nc = Math.min(contours.size(), Math.trunc(c.max_candidates));
|
|
553
|
+
for (let k = 0; k < nc; k++) {
|
|
554
|
+
const cnt = contours.get(k);
|
|
555
|
+
let [box, side] = this._miniBox(cnt);
|
|
556
|
+
cnt.delete();
|
|
557
|
+
if (side < c.min_size || this._boxScore(prob, ph, pw, box) < c.box_thresh) continue;
|
|
558
|
+
const core = side;
|
|
559
|
+
const big = this._unclip(box, c.unclip);
|
|
560
|
+
if (!big) continue;
|
|
561
|
+
const bm = cv.matFromArray(big.length, 1, cv.CV_32FC2, big.flat());
|
|
562
|
+
[box, side] = this._miniBox(bm);
|
|
563
|
+
bm.delete();
|
|
564
|
+
if (side < c.min_size + 2) continue;
|
|
565
|
+
let grow = 0;
|
|
566
|
+
if (c.short_pad && dist(box[0], box[1]) >= dist(box[0], box[3])) {
|
|
567
|
+
grow = Math.max(0, c.short_pad * (1 + c.unclip) * core - side) / 2;
|
|
568
|
+
}
|
|
569
|
+
box = boxF32(box.map(([bx, by]) => [clip(f32(bx * sx) - pad, 0, W0 - 1), clip(f32(by * sy) - pad, 0, H0 - 1)]));
|
|
570
|
+
if (grow > 0) {
|
|
571
|
+
let v = [box[3][0] - box[0][0], box[3][1] - box[0][1]];
|
|
572
|
+
const nv = Math.max(Math.hypot(v[0], v[1]), 1e-6);
|
|
573
|
+
v = [(v[0] / nv) * grow * sy, (v[1] / nv) * grow * sy];
|
|
574
|
+
for (const i of [0, 1]) { box[i][0] -= v[0]; box[i][1] -= v[1]; }
|
|
575
|
+
for (const i of [2, 3]) { box[i][0] += v[0]; box[i][1] += v[1]; }
|
|
576
|
+
box = boxF32(box.map(([bx, by]) => [clip(bx, 0, W0 - 1), clip(by, 0, H0 - 1)]));
|
|
577
|
+
}
|
|
578
|
+
if (c.v_pad) {
|
|
579
|
+
const hh = (Math.max(dist(box[0], box[3]), 1) * c.v_pad) / 2;
|
|
580
|
+
for (const i of [0, 1]) box[i][1] = f32(clip(box[i][1] - hh, 0, H0 - 1));
|
|
581
|
+
for (const i of [2, 3]) box[i][1] = f32(clip(box[i][1] + hh, 0, H0 - 1));
|
|
582
|
+
}
|
|
583
|
+
if (Math.max(...xs(box)) - Math.min(...xs(box)) >= 2 && Math.max(...ys(box)) - Math.min(...ys(box)) >= 2) boxes.push(box);
|
|
584
|
+
}
|
|
585
|
+
contours.delete();
|
|
586
|
+
return boxes;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
_miniBox(contourMat) {
|
|
590
|
+
const rect = this.cv.minAreaRect(contourMat);
|
|
591
|
+
const pts = this.cv.RotatedRect.points(rect).map((p) => [p.x, p.y]);
|
|
592
|
+
return [orderPoints(pts), Math.min(rect.size.width, rect.size.height)];
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
_boxScore(prob, h, w, box) {
|
|
596
|
+
const cv = this.cv;
|
|
597
|
+
const x0 = clip(Math.floor(Math.min(...xs(box))), 0, w - 1), x1 = clip(Math.ceil(Math.max(...xs(box))), 0, w - 1);
|
|
598
|
+
const y0 = clip(Math.floor(Math.min(...ys(box))), 0, h - 1), y1 = clip(Math.ceil(Math.max(...ys(box))), 0, h - 1);
|
|
599
|
+
const mh = y1 - y0 + 1, mw = x1 - x0 + 1;
|
|
600
|
+
const mask = cv.Mat.zeros(mh, mw, cv.CV_8UC1);
|
|
601
|
+
const poly = cv.matFromArray(4, 1, cv.CV_32SC2, box.flatMap(([x, y]) => [Math.trunc(x - x0), Math.trunc(y - y0)]));
|
|
602
|
+
const pv = new cv.MatVector();
|
|
603
|
+
pv.push_back(poly);
|
|
604
|
+
cv.fillPoly(mask, pv, new cv.Scalar(1));
|
|
605
|
+
let s = 0, n = 0;
|
|
606
|
+
for (let y = 0; y < mh; y++) {
|
|
607
|
+
for (let x = 0; x < mw; x++) {
|
|
608
|
+
if (mask.data[y * mw + x]) { s += prob[(y + y0) * w + x + x0]; n++; }
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
mask.delete(); poly.delete(); pv.delete();
|
|
612
|
+
return n ? s / n : 0;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
_unclip(box, ratio) {
|
|
616
|
+
let area = 0, length = 0;
|
|
617
|
+
for (let i = 0; i < 4; i++) {
|
|
618
|
+
const [ax, ay] = box[i], [bx, by] = box[(i + 1) % 4];
|
|
619
|
+
area += ax * by - bx * ay;
|
|
620
|
+
length += Math.hypot(bx - ax, by - ay);
|
|
621
|
+
}
|
|
622
|
+
area = Math.abs(area) / 2;
|
|
623
|
+
if (length === 0) return null;
|
|
624
|
+
const CL = this.CL;
|
|
625
|
+
const off = new CL.ClipperOffset(2.0, 0.25);
|
|
626
|
+
off.AddPath(box.map(([x, y]) => ({ X: Math.trunc(x), Y: Math.trunc(y) })), CL.JoinType.jtRound, CL.EndType.etClosedPolygon);
|
|
627
|
+
const out = new CL.Paths();
|
|
628
|
+
off.Execute(out, (area * ratio) / length);
|
|
629
|
+
return out.length ? out[0].map((p) => [p.X, p.Y]) : null;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// -- recognition -------------------------------------------------------------
|
|
633
|
+
|
|
634
|
+
/** Grey line image at the recognizer's height, as the runtime's "flatten" colour mode prepares it. */
|
|
635
|
+
_prepCrop(crop) {
|
|
636
|
+
const cv = this.cv, c = this.cfg.rec, H = Math.trunc(c.height);
|
|
637
|
+
if (!crop.rows || !crop.cols) return { g: new Uint8Array(0), h: 0, w: 0 };
|
|
638
|
+
const mode = c.color;
|
|
639
|
+
let src = crop;
|
|
640
|
+
if (mode === "flatten") {
|
|
641
|
+
src = new cv.Mat();
|
|
642
|
+
const rw = Math.max(1, Math.ceil((H * crop.cols) / Math.max(crop.rows, 1)));
|
|
643
|
+
cv.resize(crop, src, new cv.Size(rw, H), 0, 0, cv.INTER_LINEAR);
|
|
644
|
+
}
|
|
645
|
+
const gm = new cv.Mat();
|
|
646
|
+
if (src.channels() === 3) cv.cvtColor(src, gm, cv.COLOR_BGR2GRAY); else src.copyTo(gm);
|
|
647
|
+
if (src !== crop) src.delete();
|
|
648
|
+
const h = gm.rows, w = gm.cols;
|
|
649
|
+
let g = gm.data.slice();
|
|
650
|
+
gm.delete();
|
|
651
|
+
if (mode === "flatten") g = this.flatten(g, h, w);
|
|
652
|
+
else if (mode !== "gray" && mode !== "bgr") {
|
|
653
|
+
const hist = new Float64Array(256);
|
|
654
|
+
for (const v of g) hist[v]++;
|
|
655
|
+
for (let i = 1; i < 256; i++) hist[i] += hist[i - 1];
|
|
656
|
+
const q = (p) => { let k = 0; while (k < 256 && hist[k] < p * hist[255]) k++; return k; };
|
|
657
|
+
const lo = q(0.02), med = q(0.5), hi = q(0.98);
|
|
658
|
+
let lut = Array.from({ length: 256 }, (_, i) => i);
|
|
659
|
+
if (mode === "stretch" && hi - lo >= 16) lut = lut.map((v) => clip((v - lo) * (255 / (hi - lo)), 0, 255));
|
|
660
|
+
if (lut[med] < 128) lut = lut.map((v) => 255 - v);
|
|
661
|
+
g = g.map((v) => lut[v] | 0);
|
|
662
|
+
}
|
|
663
|
+
return { g, h, w };
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
async recognizeDetail(crops, session = null) {
|
|
667
|
+
const cv = this.cv, c = this.cfg.rec;
|
|
668
|
+
const H = Math.trunc(c.height), maxW = Math.trunc(c.max_w), bs = Math.trunc(c.batch);
|
|
669
|
+
const prepped = crops.map((cr) => this._prepCrop(cr));
|
|
670
|
+
const ratios = prepped.map((p) => p.w / Math.max(p.h, 1));
|
|
671
|
+
const out = new Array(crops.length).fill(null).map(() => ["", 0, []]);
|
|
672
|
+
const order = ratios.map((_, i) => i).sort((a, b) => ratios[a] - ratios[b]);
|
|
673
|
+
for (let b = 0; b < order.length; b += bs) {
|
|
674
|
+
const idx = order.slice(b, b + bs);
|
|
675
|
+
const W = Math.min(maxW, Math.max(16, Math.ceil(H * Math.max(...idx.map((i) => ratios[i])))));
|
|
676
|
+
const batch = new Float32Array(idx.length * 3 * H * W); // 0 = mid-grey after normalising
|
|
677
|
+
const widths = [];
|
|
678
|
+
idx.forEach((i, n) => {
|
|
679
|
+
const p = prepped[i];
|
|
680
|
+
const rw = Math.min(W, Math.max(1, Math.ceil(H * ratios[i])));
|
|
681
|
+
widths.push(rw);
|
|
682
|
+
let g = p.g;
|
|
683
|
+
if (p.w !== rw || p.h !== H) {
|
|
684
|
+
const m = new cv.Mat(p.h, p.w, cv.CV_8UC1);
|
|
685
|
+
m.data.set(p.g);
|
|
686
|
+
const r = new cv.Mat();
|
|
687
|
+
cv.resize(m, r, new cv.Size(rw, H), 0, 0, cv.INTER_LINEAR);
|
|
688
|
+
g = r.data.slice();
|
|
689
|
+
m.delete(); r.delete();
|
|
690
|
+
}
|
|
691
|
+
const base = n * 3 * H * W;
|
|
692
|
+
for (let y = 0; y < H; y++) {
|
|
693
|
+
for (let x = 0; x < rw; x++) {
|
|
694
|
+
const v = (f32(g[y * rw + x] / 255) - 0.5) / 0.5;
|
|
695
|
+
batch[base + y * W + x] = v;
|
|
696
|
+
batch[base + H * W + y * W + x] = v;
|
|
697
|
+
batch[base + 2 * H * W + y * W + x] = v;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
const { data, dims } = await this._run(session || this.rec, batch, [idx.length, 3, H, W]);
|
|
702
|
+
const T = dims[1], K = dims[2];
|
|
703
|
+
idx.forEach((i, n) => {
|
|
704
|
+
out[i] = this._ctcDetail(data.subarray(n * T * K, (n + 1) * T * K), T, K, W / T, widths[n]);
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
return out;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
_ctcDetail(p, T, K, stride, width) {
|
|
711
|
+
const ids = new Int32Array(T), conf = new Float32Array(T);
|
|
712
|
+
for (let t = 0; t < T; t++) {
|
|
713
|
+
let best = 0, bv = p[t * K];
|
|
714
|
+
for (let k = 1; k < K; k++) if (p[t * K + k] > bv) { bv = p[t * K + k]; best = k; }
|
|
715
|
+
ids[t] = best; conf[t] = bv;
|
|
716
|
+
}
|
|
717
|
+
const chars = [], probs = [];
|
|
718
|
+
let t = 0;
|
|
719
|
+
while (t < T) {
|
|
720
|
+
let end = t + 1;
|
|
721
|
+
while (end < T && ids[end] === ids[t]) end++;
|
|
722
|
+
if (ids[t] !== 0) {
|
|
723
|
+
const ch = ids[t] < this.chars.length ? this.chars[ids[t]] : "";
|
|
724
|
+
chars.push([ch, Math.min(1, (t * stride) / width), Math.min(1, (end * stride) / width), conf[t]]);
|
|
725
|
+
probs.push(conf[t]);
|
|
726
|
+
}
|
|
727
|
+
t = end;
|
|
728
|
+
}
|
|
729
|
+
return [chars.map((x) => x[0]).join(""), probs.length ? mean(probs) : 0, chars];
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
async recognize(crops) {
|
|
733
|
+
return (await this.recognizeDetail(crops)).map(([t, c]) => [t, c]);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// -- page --------------------------------------------------------------------
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Read one page.
|
|
740
|
+
* @param image cv.Mat (BGR/grey/RGBA) or ImageData-like {data, width, height}
|
|
741
|
+
* @returns {Promise<{text, lines:[{text,confidence,row,box}], dropped, text_angle, rotation, timing}>}
|
|
742
|
+
*/
|
|
743
|
+
async read(image) {
|
|
744
|
+
const cv = this.cv;
|
|
745
|
+
let img = this.toBGR(image);
|
|
746
|
+
const t0 = performance.now();
|
|
747
|
+
try {
|
|
748
|
+
let boxes = await this.detect(img);
|
|
749
|
+
const h = img.rows, w = img.cols;
|
|
750
|
+
if (!boxes.length && h <= 200 && w >= 1.5 * h) boxes = [[[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]]];
|
|
751
|
+
const oc = this.cfg.ori;
|
|
752
|
+
let rotation = 0, skew = 0, tf = eye();
|
|
753
|
+
if (oc.enabled) {
|
|
754
|
+
const k = await this._sideways(img, boxes);
|
|
755
|
+
if (k) {
|
|
756
|
+
tf = matmul(rot90Matrix(k, img.rows, img.cols), tf);
|
|
757
|
+
img = this.rot90(img, k, true);
|
|
758
|
+
boxes = await this.detect(img);
|
|
759
|
+
rotation = k === -1 ? 90 : 270;
|
|
760
|
+
}
|
|
761
|
+
skew = pageSkew(boxes);
|
|
762
|
+
if (Math.abs(skew) >= oc.deskew_above) {
|
|
763
|
+
const [d, m] = this._deskew(img, skew);
|
|
764
|
+
img.delete();
|
|
765
|
+
img = d;
|
|
766
|
+
tf = matmul(m, tf);
|
|
767
|
+
boxes = await this.detect(img);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
const t1 = performance.now();
|
|
771
|
+
let page = await this._page(img, boxes);
|
|
772
|
+
if (oc.enabled && !rotation && (await this._upsideDown(img, page.boxes, page))) {
|
|
773
|
+
tf = matmul(rot90Matrix(2, img.rows, img.cols), tf);
|
|
774
|
+
img = this.rot90(img, 2, true);
|
|
775
|
+
boxes = await this.detect(img);
|
|
776
|
+
rotation = 180;
|
|
777
|
+
page = await this._page(img, boxes);
|
|
778
|
+
}
|
|
779
|
+
const frag = fragments(page.boxes, page.rec);
|
|
780
|
+
if (this.cfg.marks.enabled) for (const j of await this._attachMarks(img, page, frag)) frag.add(j);
|
|
781
|
+
boxes = page.boxes;
|
|
782
|
+
const back = inv3(tf);
|
|
783
|
+
const t2 = performance.now();
|
|
784
|
+
const lines = [], rowTexts = [], rowGeom = [], dropped = [];
|
|
785
|
+
const hmed = boxes.length ? lineHeight(boxes) : 1;
|
|
786
|
+
const r1 = (v) => Math.round(v * 10) / 10;
|
|
787
|
+
page.rows.forEach((r, ri) => {
|
|
788
|
+
const parts = [];
|
|
789
|
+
for (const i of r) {
|
|
790
|
+
const [raw, conf] = page.rec[i];
|
|
791
|
+
let text = fixScripts(canonical(raw));
|
|
792
|
+
if (this.cfg.join.decimal_comma) text = text.replace(DECIMAL_COMMA, "$1.$2");
|
|
793
|
+
if (frag.has(i)) continue;
|
|
794
|
+
const reason = this._dropReason(text, conf, boxH(boxes[i]) / hmed);
|
|
795
|
+
const entry = { text, confidence: Math.round(conf * 1e4) / 1e4, row: ri,
|
|
796
|
+
box: applyM(back, boxes[i]).map(([x, y]) => [r1(x), r1(y)]) };
|
|
797
|
+
if (reason) { dropped.push({ ...entry, reason }); continue; }
|
|
798
|
+
parts.push(text);
|
|
799
|
+
lines.push(entry);
|
|
800
|
+
}
|
|
801
|
+
if (parts.length) {
|
|
802
|
+
rowTexts.push(parts.join(" "));
|
|
803
|
+
const yv = r.filter((j) => !frag.has(j)).flatMap((j) => ys(boxes[j]));
|
|
804
|
+
rowGeom.push(yv.length ? [Math.min(...yv), Math.max(...yv)] : null);
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
const straightened = Math.abs(skew) >= oc.deskew_above ? skew : 0;
|
|
808
|
+
return { text: this._join(rowTexts, rowGeom, hmed), lines, dropped,
|
|
809
|
+
text_angle: Math.round((page.angle + straightened) * 100) / 100, rotation,
|
|
810
|
+
timing: { det_ms: Math.round((t1 - t0) * 10) / 10, rec_ms: Math.round((t2 - t1) * 10) / 10, n_lines: Object.keys(page.rec).length } };
|
|
811
|
+
} finally {
|
|
812
|
+
img.delete();
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
_deskew(img, angle) {
|
|
817
|
+
const cv = this.cv;
|
|
818
|
+
const H = img.rows, W = img.cols;
|
|
819
|
+
const mm = cv.getRotationMatrix2D(new cv.Point(W / 2, H / 2), angle, 1.0);
|
|
820
|
+
const m = [[mm.doubleAt(0, 0), mm.doubleAt(0, 1), mm.doubleAt(0, 2)], [mm.doubleAt(1, 0), mm.doubleAt(1, 1), mm.doubleAt(1, 2)]];
|
|
821
|
+
const c = Math.abs(m[0][0]), s = Math.abs(m[0][1]);
|
|
822
|
+
const nW = pyRound(H * s + W * c), nH = pyRound(H * c + W * s);
|
|
823
|
+
m[0][2] += nW / 2 - W / 2;
|
|
824
|
+
m[1][2] += nH / 2 - H / 2;
|
|
825
|
+
mm.data64F.set([...m[0], ...m[1]]);
|
|
826
|
+
const px = img.data, ch = [[], [], []];
|
|
827
|
+
const push = (y, x) => { const o = (y * W + x) * 3; for (let k = 0; k < 3; k++) ch[k].push(px[o + k]); };
|
|
828
|
+
for (let x = 0; x < W; x++) push(0, x);
|
|
829
|
+
for (let x = 0; x < W; x++) push(H - 1, x);
|
|
830
|
+
for (let y = 0; y < H; y++) push(y, 0);
|
|
831
|
+
for (let y = 0; y < H; y++) push(y, W - 1);
|
|
832
|
+
const fill = ch.map((a) => Math.trunc(median(a)));
|
|
833
|
+
const out = new cv.Mat();
|
|
834
|
+
cv.warpAffine(img, out, mm, new cv.Size(nW, nH), cv.INTER_CUBIC, cv.BORDER_CONSTANT, new cv.Scalar(fill[0], fill[1], fill[2], 255));
|
|
835
|
+
mm.delete();
|
|
836
|
+
return [out, [...m, [0, 0, 1]]];
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
_splitOne(img, b, ratio) {
|
|
840
|
+
if (!ratio) return [b];
|
|
841
|
+
const cv = this.cv;
|
|
842
|
+
const c = this.cropLine(img, b, 0);
|
|
843
|
+
const h = c.rows, w = c.cols;
|
|
844
|
+
if (w < 4 * h || h < 4) { c.delete(); return [b]; }
|
|
845
|
+
const gm = new cv.Mat();
|
|
846
|
+
cv.cvtColor(c, gm, cv.COLOR_BGR2GRAY);
|
|
847
|
+
c.delete();
|
|
848
|
+
const fl = this.flatten(gm.data.slice(), h, w);
|
|
849
|
+
gm.delete();
|
|
850
|
+
const segs = [];
|
|
851
|
+
let start = null, blank = 0;
|
|
852
|
+
for (let x = 0; x < w; x++) {
|
|
853
|
+
let on = false;
|
|
854
|
+
for (let y = 0; y < h; y++) if (fl[y * w + x] < 128) { on = true; break; }
|
|
855
|
+
if (on) {
|
|
856
|
+
if (start === null) start = x;
|
|
857
|
+
else if (blank >= ratio * h) { segs.push([start, x - blank]); start = x; }
|
|
858
|
+
blank = 0;
|
|
859
|
+
} else if (start !== null) blank++;
|
|
860
|
+
}
|
|
861
|
+
if (start !== null) segs.push([start, w - blank]);
|
|
862
|
+
if (!segs.length || (segs.length === 1 && segs[0][0] < ratio * h && w - segs[0][1] < ratio * h)) return [b];
|
|
863
|
+
const top = [b[1][0] - b[0][0], b[1][1] - b[0][1]], bot = [b[2][0] - b[3][0], b[2][1] - b[3][1]];
|
|
864
|
+
const pad = 0.15 * h;
|
|
865
|
+
return segs.map(([x0, x1]) => {
|
|
866
|
+
const t0 = Math.max(0, (x0 - pad) / w), t1 = Math.min(1, (x1 + pad) / w);
|
|
867
|
+
return boxF32([
|
|
868
|
+
[b[0][0] + t0 * top[0], b[0][1] + t0 * top[1]], [b[0][0] + t1 * top[0], b[0][1] + t1 * top[1]],
|
|
869
|
+
[b[3][0] + t1 * bot[0], b[3][1] + t1 * bot[1]], [b[3][0] + t0 * bot[0], b[3][1] + t0 * bot[1]],
|
|
870
|
+
]);
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
async _page(img, boxes) {
|
|
875
|
+
const groups = boxes.map((b) => this._splitOne(img, b, this.cfg.det.split_gap));
|
|
876
|
+
const changed = boxes.map((b, i) => groups[i].length > 1 || groups[i][0] !== b);
|
|
877
|
+
const pieces = groups.flat();
|
|
878
|
+
const whole = boxes.filter((_, i) => changed[i]);
|
|
879
|
+
const res = await this._readBoxes(img, [...pieces, ...whole]);
|
|
880
|
+
const final = [], out = [];
|
|
881
|
+
let k = 0, e = pieces.length;
|
|
882
|
+
boxes.forEach((b, bi) => {
|
|
883
|
+
const g = groups[bi];
|
|
884
|
+
const r = res.slice(k, k + g.length);
|
|
885
|
+
k += g.length;
|
|
886
|
+
if (changed[bi]) {
|
|
887
|
+
const alt = res[e++];
|
|
888
|
+
const nPieces = sum(r.map((x) => x[0].replace(/ /g, "").length));
|
|
889
|
+
if (Math.min(...r.map((x) => x[1])) < this.cfg.rec.drop_below || nPieces < 0.9 * alt[0].replace(/ /g, "").length) {
|
|
890
|
+
final.push(b); out.push(alt); return;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
final.push(...g); out.push(...r);
|
|
894
|
+
});
|
|
895
|
+
const angle = pageSkew(final);
|
|
896
|
+
const rows = readingOrder(final, angle, true, out.map((x) => x[0]), this.cfg.cols);
|
|
897
|
+
const rec = {}, chars = {};
|
|
898
|
+
out.forEach((x, i) => { rec[i] = [x[0], x[1]]; chars[i] = x[2]; });
|
|
899
|
+
return { rows, angle, rec, chars, boxes: final };
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
async _readBoxes(img, boxes) {
|
|
903
|
+
const raw = boxes.map((b) => this.cropLine(img, b, 0));
|
|
904
|
+
try {
|
|
905
|
+
const tall = raw.map((c, n) => (c.rows >= 1.5 * c.cols ? n : -1)).filter((n) => n >= 0);
|
|
906
|
+
const turned = [1, -1].flatMap((k) => tall.map((n) => this.rot90(raw[n], k)));
|
|
907
|
+
const res = await this.recognizeDetail([...raw, ...turned]);
|
|
908
|
+
turned.forEach((m) => m.delete());
|
|
909
|
+
const out = res.slice(0, raw.length);
|
|
910
|
+
tall.forEach((n, j) => {
|
|
911
|
+
for (const alt of [res[raw.length + j], res[raw.length + tall.length + j]]) {
|
|
912
|
+
if (alt[1] > out[n][1]) out[n] = [alt[0], alt[1], null];
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
if (this.rec2) {
|
|
916
|
+
const sus = out.map((r, n) => (r[2] !== null && mixed(r[0]) >= this.cfg.rec2.min_mixed ? n : -1)).filter((n) => n >= 0);
|
|
917
|
+
if (sus.length) {
|
|
918
|
+
const alts = await this.recognizeDetail(sus.map((n) => raw[n]), this.rec2);
|
|
919
|
+
sus.forEach((n, j) => { if (mixed(alts[j][0]) < mixed(out[n][0])) out[n] = alts[j]; });
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
return out;
|
|
923
|
+
} finally {
|
|
924
|
+
raw.forEach((m) => m.delete());
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
async _sideways(img, boxes) {
|
|
929
|
+
const votes = boxes.map((b) => vertical(b, 2.0));
|
|
930
|
+
let vert = boxes.filter((_, i) => votes[i] === 1);
|
|
931
|
+
if (vert.length < 3 || vert.length <= votes.filter((v) => v === -1).length) return 0;
|
|
932
|
+
vert = [...vert].sort((a, b) => boxH(b) - boxH(a)).slice(0, this.cfg.ori.sample);
|
|
933
|
+
const raw = vert.map((b) => this.cropLine(img, b, 0));
|
|
934
|
+
const turned = [1, -1].flatMap((k) => raw.map((c) => this.rot90(c, k)));
|
|
935
|
+
raw.forEach((m) => m.delete());
|
|
936
|
+
const res = await this.recognize(turned);
|
|
937
|
+
turned.forEach((m) => m.delete());
|
|
938
|
+
const ccw = sum(res.slice(0, raw.length).map((r) => r[1]));
|
|
939
|
+
const cw = sum(res.slice(raw.length).map((r) => r[1]));
|
|
940
|
+
return ccw > cw ? 1 : -1;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
async _upsideDown(img, boxes, page) {
|
|
944
|
+
const oc = this.cfg.ori;
|
|
945
|
+
const confs = Object.values(page.rec).filter(([t]) => [...t].length >= 2).map(([, c]) => c);
|
|
946
|
+
if (confs.length < 3 || median(confs) >= oc.flip_below) return false;
|
|
947
|
+
const idx = Object.keys(page.rec).map(Number).filter((i) => vertical(boxes[i], 2.0) === -1)
|
|
948
|
+
.sort((a, b) => boxW(boxes[b]) - boxW(boxes[a])).slice(0, oc.sample);
|
|
949
|
+
if (idx.length < 3) return false;
|
|
950
|
+
const up = mean(idx.map((i) => page.rec[i][1]));
|
|
951
|
+
const flipped = idx.map((i) => this.rot90(this.cropLine(img, boxes[i], 0), 2, true));
|
|
952
|
+
const res = await this.recognize(flipped);
|
|
953
|
+
flipped.forEach((m) => m.delete());
|
|
954
|
+
return mean(res.map((r) => r[1])) > up + oc.flip_margin;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
_dropReason(text, conf, hratio = null) {
|
|
958
|
+
const rc = this.cfg.rec;
|
|
959
|
+
if (!text) return "empty";
|
|
960
|
+
if (conf < rc.drop_below) return "low_confidence";
|
|
961
|
+
const bare = text.replace(/ /g, "");
|
|
962
|
+
let short = rc.min_chars;
|
|
963
|
+
const inCls = (ch) => DIGITS.has(ch) || (rc.min_chars_latin && LATIN.has(ch));
|
|
964
|
+
if (rc.min_chars_digits !== null && rc.min_chars_digits !== undefined && bare && [...bare].every(inCls) &&
|
|
965
|
+
((rc.min_chars_digits_height || 0) <= 0 || (hratio !== null && hratio >= rc.min_chars_digits_height))) {
|
|
966
|
+
short = rc.min_chars_digits;
|
|
967
|
+
}
|
|
968
|
+
if ([...bare].length < short) return "too_short";
|
|
969
|
+
if (conf < rc.junk_below && invalidShare(text) > rc.junk_invalid) return "invalid_thai";
|
|
970
|
+
return null;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
async _attachMarks(img, page, frag) {
|
|
974
|
+
const mc = this.cfg.marks;
|
|
975
|
+
const { boxes, rec } = page;
|
|
976
|
+
const n = boxes.length;
|
|
977
|
+
let hmed = n ? lineHeight(boxes) : 1;
|
|
978
|
+
const junk = boxes.map((b, i) => frag.has(i) ||
|
|
979
|
+
this._dropReason(fixScripts(canonical(rec[i][0])), rec[i][1], boxH(b) / hmed) !== null);
|
|
980
|
+
if (!junk.some(Boolean) || junk.every(Boolean)) return new Set();
|
|
981
|
+
hmed = lineHeight(boxes.filter((_, i) => !junk[i]));
|
|
982
|
+
const frame = new Map();
|
|
983
|
+
for (let a = 0; a < n; a++) {
|
|
984
|
+
if (junk[a]) continue;
|
|
985
|
+
const b = boxes[a];
|
|
986
|
+
const wa = dist(b[1], b[0]), ha = dist(b[3], b[0]);
|
|
987
|
+
if (wa >= 1 && ha >= 0.5 * hmed) {
|
|
988
|
+
frame.set(a, [b[0], [(b[1][0] - b[0][0]) / wa, (b[1][1] - b[0][1]) / wa], [(b[3][0] - b[0][0]) / ha, (b[3][1] - b[0][1]) / ha], wa, ha]);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
if (!frame.size) return new Set();
|
|
992
|
+
const proj = (a, j) => {
|
|
993
|
+
const [o, u, v] = frame.get(a);
|
|
994
|
+
const s = [], t = [];
|
|
995
|
+
for (const [x, y] of boxes[j]) {
|
|
996
|
+
const px = x - o[0], py = y - o[1];
|
|
997
|
+
s.push(px * u[0] + py * u[1]); t.push(px * v[0] + py * v[1]);
|
|
998
|
+
}
|
|
999
|
+
return [Math.min(...s), Math.max(...s), Math.min(...t), Math.max(...t)];
|
|
1000
|
+
};
|
|
1001
|
+
const reach = new Map([...frame.keys()].map((a) => [a, [0, 0]]));
|
|
1002
|
+
const owner = new Map();
|
|
1003
|
+
for (let j = 0; j < n; j++) {
|
|
1004
|
+
if (!junk[j]) continue;
|
|
1005
|
+
let best = null;
|
|
1006
|
+
for (const [a, [, , , wa, ha]] of frame) {
|
|
1007
|
+
const [s0, s1, t0, t1] = proj(a, j);
|
|
1008
|
+
if (t1 - t0 > mc.max_height * ha || Math.min(s1, wa) - Math.max(s0, 0) < 0.5 * (s1 - s0)) continue;
|
|
1009
|
+
let gap, side, ext;
|
|
1010
|
+
if (t1 <= 0.5 * ha) [gap, side, ext] = [Math.max(0, -t1), 0, -t0];
|
|
1011
|
+
else if (t0 >= 0.5 * ha) [gap, side, ext] = [Math.max(0, t0 - ha), 1, t1 - ha];
|
|
1012
|
+
else continue;
|
|
1013
|
+
if (gap > mc.max_gap * ha || ext > mc.max_ext * ha) continue;
|
|
1014
|
+
if (best === null || gap < best[0] || (gap === best[0] && side < best[1])) best = [gap, side, a, ext];
|
|
1015
|
+
}
|
|
1016
|
+
if (best) {
|
|
1017
|
+
const [, side, a, ext] = best;
|
|
1018
|
+
owner.set(j, a);
|
|
1019
|
+
reach.get(a)[side] = Math.max(reach.get(a)[side], ext);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
if (!owner.size) return new Set();
|
|
1023
|
+
let floating = false;
|
|
1024
|
+
for (const side of [0, 1]) {
|
|
1025
|
+
const lifted = [...frame.keys()].filter((a) => reach.get(a)[side] > 0);
|
|
1026
|
+
if (lifted.length < Math.max(mc.page_min, mc.page_share * frame.size)) continue;
|
|
1027
|
+
floating = true;
|
|
1028
|
+
const typ = median(lifted.map((a) => reach.get(a)[side] / frame.get(a)[4]));
|
|
1029
|
+
for (const a of frame.keys()) {
|
|
1030
|
+
if (reach.get(a)[side] > 0) continue;
|
|
1031
|
+
const [, , , wa, ha] = frame.get(a);
|
|
1032
|
+
let room = typ * ha;
|
|
1033
|
+
for (const b of frame.keys()) {
|
|
1034
|
+
if (b === a) continue;
|
|
1035
|
+
const [s0, s1, t0, t1] = proj(a, b);
|
|
1036
|
+
if (Math.min(s1, wa) - Math.max(s0, 0) <= 0) continue;
|
|
1037
|
+
if (side === 0 && t1 <= 0.5 * ha) room = Math.min(room, Math.max(0, -t1 * 0.9));
|
|
1038
|
+
else if (side === 1 && t0 >= 0.5 * ha) room = Math.min(room, Math.max(0, (t0 - ha) * 0.9));
|
|
1039
|
+
}
|
|
1040
|
+
reach.get(a)[side] = room;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
const grown = [], newBoxes = [];
|
|
1044
|
+
const H = img.rows, W = img.cols;
|
|
1045
|
+
for (const [a, [, , v, , ha]] of frame) {
|
|
1046
|
+
const [up, down] = reach.get(a);
|
|
1047
|
+
if (up <= 0 && down <= 0) continue;
|
|
1048
|
+
const b = boxes[a].map((p) => [...p]);
|
|
1049
|
+
if (up > 0) for (const i of [0, 1]) { b[i][0] -= v[0] * (up + mc.margin * ha); b[i][1] -= v[1] * (up + mc.margin * ha); }
|
|
1050
|
+
if (down > 0) for (const i of [2, 3]) { b[i][0] += v[0] * (down + mc.margin * ha); b[i][1] += v[1] * (down + mc.margin * ha); }
|
|
1051
|
+
grown.push(a);
|
|
1052
|
+
newBoxes.push(boxF32(b.map(([x, y]) => [clip(x, 0, W - 1), clip(y, 0, H - 1)])));
|
|
1053
|
+
}
|
|
1054
|
+
const done = new Set();
|
|
1055
|
+
const reads = newBoxes.length ? await this._readBoxes(img, newBoxes) : [];
|
|
1056
|
+
grown.forEach((a, k) => {
|
|
1057
|
+
const r = reads[k];
|
|
1058
|
+
const nw = fixScripts(canonical(r[0]));
|
|
1059
|
+
if (this._dropReason(nw, r[1]) !== null) return;
|
|
1060
|
+
const old = fixScripts(canonical(rec[a][0]));
|
|
1061
|
+
if (mc.keep_base && (floating ? digitsOf(nw) !== digitsOf(old) : baseOf(nw) !== baseOf(old))) return;
|
|
1062
|
+
boxes[a] = newBoxes[k]; rec[a] = [r[0], r[1]]; page.chars[a] = r[2];
|
|
1063
|
+
done.add(a);
|
|
1064
|
+
});
|
|
1065
|
+
return new Set([...owner].filter(([, a]) => done.has(a)).map(([j]) => j));
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
_join(rows, geom = null, hline = 1) {
|
|
1069
|
+
const jc = this.cfg.join;
|
|
1070
|
+
if (jc.paragraphs && geom && geom.length === rows.length && !jc.thai_no_space) {
|
|
1071
|
+
const out = [];
|
|
1072
|
+
let prev = null;
|
|
1073
|
+
rows.forEach((r, i) => {
|
|
1074
|
+
const g = geom[i];
|
|
1075
|
+
if (out.length && prev && g && (g[0] - prev[1] > jc.paragraph_gap * hline || g[1] < prev[0])) out.push("");
|
|
1076
|
+
out.push(r);
|
|
1077
|
+
prev = g || prev;
|
|
1078
|
+
});
|
|
1079
|
+
return out.join("\n");
|
|
1080
|
+
}
|
|
1081
|
+
if (!jc.thai_no_space) return rows.join("\n");
|
|
1082
|
+
let out = rows.length ? rows[0] : "";
|
|
1083
|
+
for (const r of rows.slice(1)) out += (out && isThai(out[out.length - 1]) && isThai(r[0]) ? "" : "\n") + r;
|
|
1084
|
+
return out;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
async dispose() {
|
|
1088
|
+
for (const s of [this.det, this.rec, this.rec2]) if (s) await s.release?.();
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/** PostProcess block of a PaddleX inference.yml: flat `key: value` pairs (the runtime's _yaml_postprocess). */
|
|
1093
|
+
function parsePostprocess(text) {
|
|
1094
|
+
const out = {};
|
|
1095
|
+
let inPost = false;
|
|
1096
|
+
for (const raw of text.split("\n")) {
|
|
1097
|
+
if (raw.startsWith("PostProcess:")) { inPost = true; continue; }
|
|
1098
|
+
if (inPost && raw && !raw.startsWith(" ")) break;
|
|
1099
|
+
if (!inPost) continue;
|
|
1100
|
+
const line = raw.trim();
|
|
1101
|
+
const k = line.indexOf(":");
|
|
1102
|
+
if (k < 0) continue;
|
|
1103
|
+
const key = line.slice(0, k).trim(), v = line.slice(k + 1).trim();
|
|
1104
|
+
const num = Number(v);
|
|
1105
|
+
out[key] = v !== "" && !Number.isNaN(num) ? num : v;
|
|
1106
|
+
}
|
|
1107
|
+
return out;
|
|
1108
|
+
}
|