@3sln/emon-ppocr 0.0.0-stage → 0.3.0-preview.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 3sln
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,125 @@
1
- # Temporary Holding Version
1
+ # Experimental PP-OCR in JavaScript
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ PP-OCR is optional and off by default. The browser and Node adapter uses the
4
+ native Rust image preparation, DB geometry, orientation, CTC decoder, frozen
5
+ calibration and integer answer builder. Only ORT inference crosses into
6
+ JavaScript. Dictionary lookups call the pinned Tesseract wasm directly through
7
+ a small wasm shim using separate memories. The native fusion policy
8
+ and its documented receipt precision and CPU limitations remain unchanged.
9
+
10
+ The separately installed `@3sln/emon-ppocr` package supplies the optional module
11
+ as `@3sln/emon-ppocr/emon.wasm`. It has no required ORT dependency; supply the
12
+ pinned runtime and model sources below. The ordinary SDK excludes the PP bridge.
13
+ For a source build, from the repository root:
14
+
15
+ ```sh
16
+ EMON_CS_SHARED_COMPILATION=false EMON_WASM_PPOCR=1 kin/scripts/emon-wasm-build
17
+ ```
18
+
19
+ Install `host/js` development dependencies first. WABT 1.0.39 compiles the two
20
+ small `.wat` framing shims at build time; it is not a runtime dependency.
21
+ The optional module additionally requires WebAssembly multi-memory support.
22
+
23
+ Use the resulting `.cache/emon-wasm-ppocr/target/wasm32-unknown-unknown/release/emon.wasm`
24
+ for both `Emon.load` and the optional port. The ordinary build continues to
25
+ reject `ocr-engine=ppocr`. Node needs the same exception-handling support as
26
+ emon's existing wasm binding (`--experimental-wasm-exnref` on Node 24.6).
27
+
28
+ Install exactly `onnxruntime-web@1.22.0` and `tesseract.js-core@7.0.0` in your
29
+ application. Host their runtime files yourself. Pass model URLs or bytes for
30
+ the three filenames in `tools/ppocr/assets.edn`; the adapter checks their
31
+ SHA-256 pins before creating inference sessions. It has no model, runtime or
32
+ third-party CDN URL defaults. URLs are fetched only when explicitly supplied.
33
+
34
+ ```js
35
+ import { Emon } from '@3sln/emon';
36
+ import { ppocrPort, ppocrLexicon } from '@3sln/emon-ppocr';
37
+
38
+ // Supplied by the application:
39
+ // optionalWasm: compiled WebAssembly.Module, bytes, or a hosted URL
40
+ // incumbent: the existing tesseractPort(...) fulfiller
41
+ // createCore: tesseract-core-simd-lstm.wasm.js's factory
42
+ // coreOptions: local locateFile or wasmBinary for that factory
43
+ // englishData: pinned tessdata_fast eng.traineddata URL or bytes
44
+ // modelSources: { 'ch_PP-OCRv5_det_mobile.onnx': URL_or_bytes, ... }
45
+ const lexicon = ppocrLexicon({ createCore, coreOptions, traineddata: englishData });
46
+ const ocr = ppocrPort({
47
+ incumbent, wasm: optionalWasm, lexicon,
48
+ loadOrt: () => import('onnxruntime-web/wasm'),
49
+ wasmPaths: new URL('/vendor/ort-1.22.0/', location.href).href,
50
+ ortWasm: new URL('/vendor/ort-1.22.0/ort-wasm-simd-threaded.wasm', location.href),
51
+ models: modelSources,
52
+ });
53
+ const emon = await Emon.load(optionalWasm);
54
+ const model = await emon.convert(input, {
55
+ ocrEngine: 'ppocr', ports: { ocr }, record: true,
56
+ producer: ocr.producer,
57
+ });
58
+ // model.transcript() uses the existing portable recording format.
59
+ // Replay with ocrEngine: 'ppocr', transcript: files needs no OCR port or assets.
60
+ model.dispose();
61
+ await ocr.terminate();
62
+ ```
63
+
64
+ To fetch models from a release asset URL chosen by your application, import
65
+ `ppocrAssets` from `@3sln/emon-ppocr` and call `await ppocrAssets(addonUrl)`.
66
+ Use a native `.tar.gz` add-on URL (not the Windows zip); pass the returned
67
+ `models` and `traineddata` into the port and lexicon above. Browsers need a
68
+ CORS-enabled URL or your own proxy. This explicit helper decompresses the
69
+ archive in memory (up to 512 MiB) and ignores native binaries; the inference
70
+ port verifies model hashes before use. Hosting just the extracted model
71
+ files avoids downloading and buffering the native libraries.
72
+
73
+ In Node, supply filesystem bytes for the wasm modules, models and traineddata,
74
+ and a filesystem directory for `wasmPaths`; the adapter does not read files.
75
+ The Node and browser qualification drivers provide executable examples.
76
+ The dedicated dictionary instance never recognizes or learns document words.
77
+ Ordinary OCR requests continue to use the incumbent fulfiller.
78
+
79
+ ORT and its three sessions load on the first PP request. The configuration is
80
+ fixed to the hash-pinned ORT 1.22.0 ordinary SIMD wasm build, one thread,
81
+ sequential execution and graph optimization `all`. The distributed filename
82
+ contains `threaded`; `numThreads=1` prevents worker threads. Relaxed SIMD,
83
+ GPU providers and automatic runtime selection are not used.
84
+ Supply an ORT module/context whose backend has not been initialized by other
85
+ sessions; its wasm configuration flags are global to that module.
86
+
87
+ The three model downloads total 13,277,459 bytes. ORT wasm adds 11,210,254
88
+ bytes plus its JavaScript loader. The optional emon module is about 6 MiB;
89
+ the dedicated dictionary additionally needs the 4,113,088-byte English data
90
+ and the 2,857,601-byte Tesseract core, alongside any incumbent OCR resources.
91
+ These are uncompressed asset sizes, not total resident memory. Session
92
+ weights, tensor workspaces, decoded RGB images and two emon instances increase
93
+ memory use; it depends on page size. Default consumers do not import ORT or
94
+ download these models. Model weights retain Apache-2.0 licensing; ORT is MIT
95
+ (see `release/licenses/native-onnxruntime-1.22.0.txt` and its notices).
96
+
97
+ On macOS arm64 with Node 24.6.0, all 225 OP1c raw port answers were
98
+ byte-identical to the qualified native answers, whose hashes reproduced
99
+ OP1c. This single-process sweep peaked at 485 MiB RSS, measured by Node's
100
+ `resourceUsage().maxRSS`. It includes the PP pipeline, ORT sessions and
101
+ dedicated dictionary, and excludes the incumbent OCR worker and full emon
102
+ conversion. It is not a browser memory measurement or a per-conversion bound.
103
+ The coordinator also qualified Chrome on macOS arm64: 225/225 raw answers
104
+ match native. This is answer equality, not a browser memory measurement.
105
+
106
+ Reproduce the optional live comparison after preparing the pinned assets and
107
+ building the native oracle with `bb tools/ppocr/build_shared.clj`:
108
+
109
+ ```sh
110
+ EMON_CS_SHARED_COMPILATION=false bb tools/ppocr/web_qualify.clj --out EXTERNAL-DIR
111
+ node --experimental-wasm-exnref tools/ppocr/web_browser.mjs EXTERNAL-DIR
112
+ EMON_CS_SHARED_COMPILATION=false bb tools/ppocr/web_qualify.clj --out EXTERNAL-DIR --compare-only true --candidate browser
113
+ ```
114
+
115
+ The integrated recording/replay test additionally needs pinned English and OSD
116
+ traineddata (`bb tessdata-fetch --langs eng,osd`). Run
117
+ `EMON_CS_SHARED_COMPILATION=false bb tools/ppocr/web_interop.clj NEW-EXTERNAL-DIR`.
118
+ It records through each host and replays complete bundles on both hosts while
119
+ all three PP models and both traineddata files are temporarily absent, restoring
120
+ and checking every hash afterward.
121
+
122
+ The comparison checks all 225 retained OP1c qualification bitmaps and verifies
123
+ the oracle's answer hashes against OP1c. Detailed field differences stay in
124
+ external scratch. Qualification is required before release activation; a
125
+ kernel difference does not authorize changing the native integer policy.