@3sln/emon-ppocr 0.3.0-preview.1 → 0.3.0-preview.2
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/README.md +11 -7
- package/THIRD_PARTY_NOTICES.md +163 -850
- package/emon.wasm +0 -0
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +2 -2
- package/ppocr.d.ts +3 -4
- package/ppocr.js +12 -14
- package/wasm.js +73 -0
package/emon.wasm
CHANGED
|
Binary file
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ppocrEngine, ppocrAdapter, ppocrLexicon } from './ppocr.js';
|
|
2
2
|
/** Caller-selected native release .tar.gz URL, compressed bytes or byte stream.
|
|
3
3
|
* The PP port and lexicon verify all returned model bytes before inference.
|
|
4
4
|
*/
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// No default URLs, automatic inference downloads or required ORT dependency.
|
|
2
|
-
export {
|
|
2
|
+
export { ppocrEngine, ppocrAdapter, ppocrLexicon } from './ppocr.js';
|
|
3
3
|
|
|
4
4
|
/** Read the pinned model payload from a caller-selected native release tar.gz.
|
|
5
5
|
* Keep ORT's platform files out of the browser filesystem. The port and lexicon
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@3sln/emon-ppocr",
|
|
3
|
-
"version": "0.3.0-preview.
|
|
3
|
+
"version": "0.3.0-preview.2",
|
|
4
4
|
"description": "Experimental opt-in PP-OCR adapter; caller-provided pinned ORT Web and model URLs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": { "type": "git", "url": "git+https://github.com/3sln/emon.git", "directory": "release/pkg/ppocr" },
|
|
@@ -15,6 +15,6 @@
|
|
|
15
15
|
"files": ["*.js", "*.d.ts", "emon.wasm", "LICENSE", "THIRD_PARTY_NOTICES.md", "README.md"],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"publishConfig": { "access": "public" },
|
|
18
|
-
"peerDependencies": { "@3sln/emon": "^0.3.0-preview.
|
|
18
|
+
"peerDependencies": { "@3sln/emon": "^0.3.0-preview.2" },
|
|
19
19
|
"engines": { "node": ">=22" }
|
|
20
20
|
}
|
package/ppocr.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OcrAdapter } from '@3sln/emon';
|
|
2
2
|
|
|
3
3
|
type AssetSource = string | URL | Uint8Array | ArrayBuffer;
|
|
4
4
|
type WasmSource = AssetSource | WebAssembly.Module | Response | Promise<Response>;
|
|
@@ -17,9 +17,8 @@ interface AdapterOptions {
|
|
|
17
17
|
lexicon: Lexicon;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
/** Experimental optional
|
|
21
|
-
export function
|
|
22
|
-
OcrPort & { terminate(): Promise<void>; producer: Record<string, string> };
|
|
20
|
+
/** Experimental optional engine, registered with `await emon.engines().add(ppocrEngine(options))` (D925); requires the optional emon wasm build. */
|
|
21
|
+
export function ppocrEngine(options: AdapterOptions): OcrAdapter & { name: 'ppocr'; engine: 2; producer: Record<string, string>; close(): Promise<void> };
|
|
23
22
|
|
|
24
23
|
/** Dedicated pinned English dictionary. Supply tesseract.js-core 7.0.0's
|
|
25
24
|
* ordinary SIMD LSTM factory and local runtime options. */
|
package/ppocr.js
CHANGED
|
@@ -152,22 +152,20 @@ export function ppocrAdapter({ wasm, loadOrt, wasmPaths, ortWasm, models, lexico
|
|
|
152
152
|
return read;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
/**
|
|
156
|
-
*
|
|
157
|
-
* canonical answer, so JS never reconstructs boxes, confidence or extensions.
|
|
155
|
+
/** PP-OCR as a REGISTERED ADAPTER (D925): `await emon.engines().add(ppocrEngine(options))`, beside the Tesseract adapter or alone. The registry routes each request to the engine
|
|
156
|
+
* it names; a PP request is answered by Rust's complete canonical answer, so JS never reconstructs boxes, confidence or extensions.
|
|
158
157
|
*/
|
|
159
|
-
export function
|
|
158
|
+
export function ppocrEngine(options) {
|
|
160
159
|
const read = ppocrAdapter(options);
|
|
161
|
-
const
|
|
162
|
-
// kin identified the canonical trailer. A view hash may contain any cell.
|
|
163
|
-
if (req.engine !== 2) return incumbent(req);
|
|
164
|
-
const answer = await read({ png: req.bitmap(), id: req.id, rect: req.rect, record: req.record });
|
|
165
|
-
return answer === null ? null : { [canonicalOcr]: answer };
|
|
166
|
-
};
|
|
167
|
-
port.terminate = async () => { try { await read.terminate(); } finally { await incumbent.terminate?.(); } };
|
|
168
|
-
port.producer = { ...incumbent.producer, 'ocr-engine': 'tesseract+ppocr',
|
|
169
|
-
'ocr-model': 'ppocr5-en-0420cd75efc786ba37d1e028a4c9396d2f17a3b4264880f1296a7f186faced7f',
|
|
160
|
+
const producer = { 'ocr-model': 'ppocr5-en-0420cd75efc786ba37d1e028a4c9396d2f17a3b4264880f1296a7f186faced7f',
|
|
170
161
|
'pp-lexicon': 'tessdata_fast/eng sha256=7d4322bd2a7749724879683fc3912cb542f19906c83bcc1a52132556427170b2',
|
|
171
162
|
'onnxruntime': '1.22.0 WASM fixed SIMD sequential threads=1 graph=all', host: 'host/js/src/ppocr.js' };
|
|
172
|
-
return
|
|
163
|
+
return {
|
|
164
|
+
name: 'ppocr', engine: 2, caps: 0, measured: 0, needs: 4 | 8 | 1, flags: 0, producer,
|
|
165
|
+
async read(req) {
|
|
166
|
+
const answer = await read({ png: req.bitmap(), id: req.id, rect: req.rect, record: req.record });
|
|
167
|
+
return answer === null ? null : { [canonicalOcr]: answer };
|
|
168
|
+
},
|
|
169
|
+
close: () => read.terminate(),
|
|
170
|
+
};
|
|
173
171
|
}
|
package/wasm.js
CHANGED
|
@@ -69,6 +69,79 @@ export class Wasm {
|
|
|
69
69
|
return n < 0 ? [] : Array.from(out);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
// THE ADAPTER REGISTRY'S RULES ARE KIN'S (`emon.adapters`, D925): `Engines` asks the module.
|
|
73
|
+
/** 0 when an adapter of that shape may register (kin's `ad-register-check`), else the reason's code. */
|
|
74
|
+
adapterCheck(abi, oen, caps, measured, needs, flags) {
|
|
75
|
+
return this.x.emon_adapter_check(abi, oen, caps, measured, needs, flags);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The reason's name for a refusal code (`adapter-engine-unknown`, ...). */
|
|
79
|
+
adapterReason(code) {
|
|
80
|
+
this.x.emon_adapter_reason(code);
|
|
81
|
+
const t = this.outText();
|
|
82
|
+
this.outFree();
|
|
83
|
+
return t;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The index, among the OIL engines `oens` (in registration order), of the adapter a request of the OIL engine `oen` goes to, or -1 (kin's `ad-route`). */
|
|
87
|
+
adapterRoute(oen, oens) {
|
|
88
|
+
return this.withBytes(new Uint8Array(Int32Array.from(oens).buffer), (p) => this.x.emon_adapter_route(oen, p, oens.length));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// THE PACK RULES ARE KIN'S TOO (`emon.engines`, `emon.adapters`, P13.16): the catalogue and what to do about a pack are asked of the module.
|
|
92
|
+
/** The pack catalogue (`pack_catalogue` of the module): the platform class, kin's codes and every pack with its files' SHA-256. */
|
|
93
|
+
packCatalogue() {
|
|
94
|
+
this.x.emon_pack_catalogue();
|
|
95
|
+
const t = this.outText();
|
|
96
|
+
this.outFree();
|
|
97
|
+
return JSON.parse(t);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `ad-pack-action` (and `ad-asks-hook`): `{ action, asks }` for a pack that is `present`, under `mode`, `offline`, a hook or none, and what the hook decided (0: nothing). */
|
|
101
|
+
packAction(present, mode, offline, hook, decision) {
|
|
102
|
+
const r = this.x.emon_pack_action(present ? 1 : 0, mode, offline ? 1 : 0, hook ? 1 : 0, decision | 0);
|
|
103
|
+
return { action: r & 255, asks: (r >> 8) === 1 };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The kin engine an OIL engine answers as, or -1. */
|
|
107
|
+
packOenEng(oen) {
|
|
108
|
+
return this.x.emon_pack_oen_eng(oen);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#i32s(a) {
|
|
112
|
+
return new Uint8Array(Int32Array.from(a).buffer);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#json(n) {
|
|
116
|
+
const t = this.outText();
|
|
117
|
+
this.outFree();
|
|
118
|
+
return n < 0 ? Object.assign(new Error(t), { packError: true }) : JSON.parse(t);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `pk-resolve`: rows `[state, engine, pack, lang]` for the kin engines, packed languages and held pack codes; throws kin's explanation. */
|
|
122
|
+
packResolve(osd, langs, engines, installed) {
|
|
123
|
+
const n = this.withBytes(this.#i32s(langs), (lp) => this.withBytes(this.#i32s(engines), (ep) => this.withBytes(this.#i32s(installed), (ip) => this.x.emon_pack_resolve(osd ? 1 : 0, lp, langs.length, ep, engines.length, ip, installed.length))));
|
|
124
|
+
const r = this.#json(n);
|
|
125
|
+
if (r.error !== undefined) throw new Error(r.error);
|
|
126
|
+
return r.rows;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** `engines()` as the module's JSON (the shape of the C ABI's `emon_engines_describe`). */
|
|
130
|
+
packDescribe(oens, renderer, installed, mode, offline, store) {
|
|
131
|
+
const sb = new TextEncoder().encode(store);
|
|
132
|
+
const n = this.withBytes(this.#i32s(oens), (op) => this.withBytes(this.#i32s(installed), (ip) => this.withBytes(sb, (sp) => this.x.emon_pack_describe(op, oens.length, renderer ? 1 : 0, ip, installed.length, mode, offline ? 1 : 0, sp, sb.length))));
|
|
133
|
+
return this.#json(n);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** `prepare(options)` over the option lines, as the module's JSON (the shape of the C ABI's `emon_prepare`); throws the options' error. */
|
|
137
|
+
packPrepare(optionLines, installed, mode, offline) {
|
|
138
|
+
const tb = new TextEncoder().encode(optionLines);
|
|
139
|
+
const n = this.withBytes(tb, (tp) => this.withBytes(this.#i32s(installed), (ip) => this.x.emon_pack_prepare(tp, tb.length, ip, installed.length, mode, offline ? 1 : 0)));
|
|
140
|
+
const r = this.#json(n);
|
|
141
|
+
if (r instanceof Error) throw r;
|
|
142
|
+
return r;
|
|
143
|
+
}
|
|
144
|
+
|
|
72
145
|
/** The language a model read a word in, as a request packs it. */
|
|
73
146
|
ocrWordLang(model) {
|
|
74
147
|
const cps = Int32Array.from(Array.from(model, (c) => c.codePointAt(0)));
|