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/src/browser.js ADDED
@@ -0,0 +1,132 @@
1
+ // Browser entry: wires up onnxruntime-web, the minimal OpenCV.js build and clipper-lib, loads the models
2
+ // (cached with the Cache API) and returns a ready LineOCR. Works on the main thread or in a Web Worker.
3
+ //
4
+ // import { createThaiOCR } from "thai-ocr-web/browser";
5
+ // const ocr = await createThaiOCR({ modelBaseUrl: "/models/", backend: "webgpu" });
6
+ // const { text, lines } = await ocr.read(imageData);
7
+ import ClipperLib from "clipper-lib";
8
+ import loadOpenCV from "../vendor/opencv/opencv-ocr.mjs";
9
+ import { LineOCR } from "./line-ocr.js";
10
+
11
+ export const MODEL_FILES = ["det.yml", "config.json", "rec_dict.txt", "det.onnx", "rec.onnx", "rec2.onnx"];
12
+
13
+ /**
14
+ * @param {object} [o]
15
+ * @param {string} [o.modelBaseUrl] URL of the folder that holds the model files (MODEL_FILES). Default "models/".
16
+ * @param {"webgpu"|"wasm"} [o.backend] "webgpu" falls back to WASM when the device has no WebGPU. Default "wasm".
17
+ * @param {number} [o.threads] WASM threads; only used when the page is cross-origin isolated. Default min(4, cores),
18
+ * or 1 on iOS / iPadOS, where multi-threaded onnxruntime-web fails to create sessions.
19
+ * @param {string|false} [o.cacheName] Cache API bucket for the model files; false to skip caching.
20
+ * @param {object} [o.overrides] runtime config overrides, e.g. { join: { paragraphs: true } }.
21
+ * @param {number} [o.maxMemoryMB] Cap on onnxruntime-web's WASM memory. Its runtime reserves a 4 GB shared memory
22
+ * up front, which iOS refuses ("RangeError: Out of memory"); the default on iPhone / iPad is 512. The model needs
23
+ * far less (all test pages read within 320 MB). Must be set before the first createThaiOCR in a worker.
24
+ * @param {string} [o.wasmManifestUrl] For hosts with a per-file size limit (Cloudflare: 25 MiB): a JSON file,
25
+ * as written by scripts/split-large-assets.mjs, listing the parts of onnxruntime-web's .wasm. The parts are
26
+ * joined here and handed to onnxruntime-web. A missing manifest (404) is ignored.
27
+ * @returns {Promise<LineOCR & { backend: "webgpu"|"wasm" }>}
28
+ */
29
+ export async function createThaiOCR({
30
+ modelBaseUrl = "models/",
31
+ backend = "wasm",
32
+ threads = isAppleMobile() ? 1 : Math.min(4, globalThis.navigator?.hardwareConcurrency || 1),
33
+ cacheName = "thai-ocr-web-models-2.1.4",
34
+ overrides = {},
35
+ wasmManifestUrl,
36
+ maxMemoryMB = isAppleMobile() ? 512 : undefined,
37
+ } = {}) {
38
+ if (maxMemoryMB) capWasmMemory(maxMemoryMB);
39
+ // Only the chosen build is loaded: WASM ~15 MB (~4 MB gzip), WebGPU ~29 MB (~7 MB gzip).
40
+ // Notes for Web Worker hosts (see demo/ocr.worker.js):
41
+ // - onnxruntime-web starts its WASM threads from the chunk it was bundled into, and that chunk can import the
42
+ // worker's entry module, so the entry must not take over `onmessage` when self.name starts with "em-pthread";
43
+ // - WebKit (every iOS browser) can evaluate a worker's entry module a second time when a chunk imports it, so
44
+ // install the message handler only once.
45
+ const ort = backend === "webgpu" ? await import("onnxruntime-web/webgpu") : await import("onnxruntime-web/wasm");
46
+ ort.env.wasm.numThreads = globalThis.crossOriginIsolated ? threads : 1;
47
+ const cv = await loadOpenCV();
48
+ const base = new URL(modelBaseUrl, globalThis.location?.href).href;
49
+ const cache = cacheName && globalThis.caches ? await caches.open(cacheName).catch(() => null) : null;
50
+ const loadFile = async (name) => {
51
+ const url = new URL(name, base).href;
52
+ let res = cache && (await cache.match(url));
53
+ if (!res) {
54
+ res = await fetch(url);
55
+ if (!res.ok) throw new Error(`thai-ocr-web: cannot load ${url} (${res.status})`);
56
+ if (cache) await cache.put(url, res.clone());
57
+ }
58
+ return new Uint8Array(await res.arrayBuffer());
59
+ };
60
+ if (wasmManifestUrl) {
61
+ const binary = await joinedWasm(new URL(wasmManifestUrl, globalThis.location?.href).href, backend, cache);
62
+ if (binary) ort.env.wasm.wasmBinary = binary;
63
+ }
64
+ const make = (eps) => LineOCR.create({ ort, cv, ClipperLib, loadFile, executionProviders: eps, overrides });
65
+ let ocr, used = backend;
66
+ if (backend === "webgpu") {
67
+ try {
68
+ if (!globalThis.navigator?.gpu || !(await navigator.gpu.requestAdapter())) throw new Error("no WebGPU adapter");
69
+ ocr = await make(["webgpu", "wasm"]);
70
+ } catch {
71
+ used = "wasm";
72
+ ocr = await make(["wasm"]);
73
+ }
74
+ } else {
75
+ ocr = await make(["wasm"]);
76
+ }
77
+ ocr.backend = used;
78
+ return ocr;
79
+ }
80
+
81
+ /**
82
+ * Lower the maximum of every shared WebAssembly.Memory created from now on in this realm (worker or page).
83
+ * onnxruntime-web asks for { initial: 256, maximum: 65536 pages (4 GB), shared: true }; WebKit on iOS cannot reserve
84
+ * that much address space. A smaller maximum is still a valid import for the module (its limit is an upper bound).
85
+ */
86
+ export function capWasmMemory(maxMB) {
87
+ const Memory = globalThis.WebAssembly?.Memory;
88
+ if (!Memory) return;
89
+ const maxPages = Math.max(1, Math.floor((maxMB * 1024 * 1024) / 65536));
90
+ const Original = Memory.__thaiOcrOriginal || Memory;
91
+ function CappedMemory(desc) {
92
+ if (desc && desc.shared && (desc.maximum === undefined || desc.maximum > maxPages)) {
93
+ desc = { ...desc, maximum: Math.max(maxPages, desc.initial || 0) };
94
+ }
95
+ return new Original(desc);
96
+ }
97
+ CappedMemory.prototype = Original.prototype;
98
+ CappedMemory.__thaiOcrOriginal = Original;
99
+ globalThis.WebAssembly.Memory = CappedMemory;
100
+ }
101
+
102
+ /** iPhone / iPad (every iOS browser is WebKit; iPadOS reports itself as a Mac with touch). */
103
+ export function isAppleMobile() {
104
+ const n = globalThis.navigator;
105
+ if (!n) return false;
106
+ return /iPhone|iPad|iPod/.test(n.userAgent) || (/Macintosh/.test(n.userAgent) && n.maxTouchPoints > 1);
107
+ }
108
+
109
+ async function joinedWasm(manifestUrl, backend, cache) {
110
+ const res = await fetch(manifestUrl).catch(() => null);
111
+ if (!res || !res.ok) return null;
112
+ const entry = (await res.json())[backend];
113
+ if (!entry) return null;
114
+ const out = new Uint8Array(entry.size);
115
+ let off = 0;
116
+ for (const part of entry.parts) {
117
+ const url = new URL(part, manifestUrl).href;
118
+ let r = cache && (await cache.match(url));
119
+ if (!r) {
120
+ r = await fetch(url);
121
+ if (!r.ok) throw new Error(`thai-ocr-web: cannot load ${url} (${r.status})`);
122
+ if (cache) await cache.put(url, r.clone());
123
+ }
124
+ const b = new Uint8Array(await r.arrayBuffer());
125
+ out.set(b, off);
126
+ off += b.length;
127
+ }
128
+ if (off !== entry.size) throw new Error("thai-ocr-web: wasm parts do not add up");
129
+ return out.buffer;
130
+ }
131
+
132
+ export { LineOCR };
package/src/index.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ export type Point = [number, number];
2
+ export type Quad = [Point, Point, Point, Point];
3
+
4
+ export interface OcrLine {
5
+ /** Recognised text of the line (Thai-normalised). */
6
+ text: string;
7
+ /** Mean per-character probability, 0..1. */
8
+ confidence: number;
9
+ /** Index of the reading-order row the line belongs to. */
10
+ row: number;
11
+ /** Corners TL, TR, BR, BL in the coordinates of the image passed to read(). */
12
+ box: Quad;
13
+ }
14
+
15
+ export interface DroppedLine extends OcrLine {
16
+ reason: "empty" | "low_confidence" | "too_short" | "invalid_thai";
17
+ }
18
+
19
+ export interface OcrResult {
20
+ /** The page's text in reading order, one row per line. */
21
+ text: string;
22
+ lines: OcrLine[];
23
+ /** Lines the filter removed (stamps, signatures, specks) and why. */
24
+ dropped: DroppedLine[];
25
+ /** Median text angle after straightening, degrees. */
26
+ text_angle: number;
27
+ /** Clockwise turn applied to an upright page: 0, 90, 180 or 270. */
28
+ rotation: 0 | 90 | 180 | 270;
29
+ timing: { det_ms: number; rec_ms: number; n_lines: number };
30
+ }
31
+
32
+ /** Pixels in RGBA order, e.g. CanvasRenderingContext2D.getImageData(). */
33
+ export interface ImageDataLike {
34
+ data: Uint8ClampedArray | Uint8Array;
35
+ width: number;
36
+ height: number;
37
+ }
38
+
39
+ export type ConfigOverrides = Partial<Record<keyof typeof DEFAULTS, Record<string, unknown>>>;
40
+
41
+ export interface CreateOptions {
42
+ ort: any;
43
+ cv: any;
44
+ ClipperLib: any;
45
+ /** Returns the bytes (or text) of one file of the model folder: det.yml, config.json, rec_dict.txt, *.onnx. */
46
+ loadFile(name: string): Promise<Uint8Array | string>;
47
+ executionProviders?: string[];
48
+ overrides?: ConfigOverrides;
49
+ sessionOptions?: Record<string, unknown>;
50
+ }
51
+
52
+ export declare const DEFAULTS: Record<string, Record<string, unknown>>;
53
+
54
+ export declare class LineOCR {
55
+ static create(options: CreateOptions): Promise<LineOCR>;
56
+ /** Read one page: ImageData-like RGBA pixels, or a cv.Mat (BGR, grey or RGBA). */
57
+ read(image: ImageDataLike | any): Promise<OcrResult>;
58
+ dispose(): Promise<void>;
59
+ cfg: Record<string, Record<string, unknown>>;
60
+ }
61
+
62
+ export declare function canonical(text: string): string;
63
+ export declare function fixScripts(text: string): string;
64
+ export declare function invalidShare(text: string): number;
65
+ export declare function lineHeight(boxes: Quad[]): number;
66
+ export declare function pageSkew(boxes: Quad[]): number;
67
+ export declare function readingOrder(boxes: Quad[], angle?: number | null, columns?: boolean,
68
+ texts?: string[] | null, rules?: Record<string, number> | null): number[][];
package/src/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // thai-ocr-web: Thai + English printed-text OCR (OpenThai 2.1 OCR Text) in JavaScript.
2
+ // Core API, no bundled dependencies: pass in onnxruntime-web, OpenCV.js and clipper-lib yourself.
3
+ // For a ready-made browser setup, import from "thai-ocr-web/browser" instead.
4
+ export { LineOCR, DEFAULTS, fixScripts, invalidShare, lineHeight, pageSkew, readingOrder } from "./line-ocr.js";
5
+ export { canonical } from "./thai-norm.js";