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/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 — 2026-11-22
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- JavaScript port of OpenThai 2.1 OCR Text's runtime (`line_engine.py` v2.1.4): detection, recognition, rec2 for
|
|
8
|
+
loopless fonts, orientation, deskew, column-aware reading order, floating marks, filters, Thai normalisation.
|
|
9
|
+
- Output identical to the reference Python runtime on all 11 test pages (`npm test`, run in CI).
|
|
10
|
+
- `createThaiOCR()` for the browser: WebGPU with WASM fallback, model caching, one WASM thread on iOS / iPadOS,
|
|
11
|
+
WebGPU runtime split into parts for hosts with a 25 MiB file limit.
|
|
12
|
+
- Minimal OpenCV.js 5.0.0 build (0.77 MB gzip) with its build script; OpenCV starts on first use.
|
|
13
|
+
- Demo site on Cloudflare Pages with sample pages, line boxes, copy and .txt download, light theme.
|
|
14
|
+
- Fixed: loading on iPhone — WebKit re-evaluating the worker module, and "RangeError: Out of memory" from ONNX
|
|
15
|
+
Runtime's 4 GB shared-memory reservation (capped at 512 MB on iOS, `maxMemoryMB`).
|
|
16
|
+
- PDF input (`thai-ocr-web/pdf`, pdf.js as an optional peer dependency): a page's own text where it has a usable
|
|
17
|
+
text layer, OCR for scanned pages; progress, page limit, abort. The demo opens PDFs, with a page pager.
|
|
18
|
+
- Demo in Thai (default) and English, light gray theme.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
thai-ocr-web
|
|
2
|
+
Copyright 2026 Awirut
|
|
3
|
+
|
|
4
|
+
Unofficial JavaScript port; not affiliated with or endorsed by iApp Technology.
|
|
5
|
+
|
|
6
|
+
src/line-ocr.js and src/thai-norm.js are a JavaScript port, with modifications, of runtime/line_engine.py and
|
|
7
|
+
runtime/thai_norm.py from
|
|
8
|
+
OpenThai 2.1 OCR Text (iApp Technology), version 2.1.4
|
|
9
|
+
https://huggingface.co/iapp/OpenThai2.1-OCR-Text-20261118
|
|
10
|
+
Licensed under the Apache License, Version 2.0.
|
|
11
|
+
|
|
12
|
+
models/ holds that repository's det.onnx, rec.onnx, rec2.onnx, det.yml, rec_dict.txt and config.json,
|
|
13
|
+
unchanged, with its NOTICE (models/NOTICE), which credits PaddlePaddle PP-OCRv6-tiny (Apache-2.0).
|
|
14
|
+
|
|
15
|
+
vendor/opencv/opencv-ocr.mjs is a build of OpenCV 5.0.0 (Apache License 2.0, Copyright the OpenCV team),
|
|
16
|
+
compiled to WebAssembly from source in which modules/js/src/core_bindings.cpp has one added line
|
|
17
|
+
(an empty namespace declaration); see vendor/opencv/build/build.sh.
|
|
18
|
+
|
|
19
|
+
Runtime dependencies installed from npm: onnxruntime-web (MIT, Microsoft), clipper-lib (Boost Software
|
|
20
|
+
License 1.0, Angus Johnson).
|
package/README.md
CHANGED
|
@@ -1,3 +1,260 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# thai-ocr-web
|
|
4
|
+
|
|
5
|
+
**Thai + English OCR that runs entirely in your browser — images and PDFs.**<br>
|
|
6
|
+
WebGPU or WebAssembly · nothing is uploaded · works offline once loaded
|
|
7
|
+
|
|
8
|
+
[](https://thai-ocr-web.pages.dev/)
|
|
9
|
+
[](https://www.npmjs.com/package/thai-ocr-web)
|
|
10
|
+
[](https://github.com/RiwRiwara/thai-ocr-web/actions/workflows/ci.yml)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
<a href="https://thai-ocr-web.pages.dev/"><img src="docs/demo.gif" width="760" alt="The demo in Thai reading a government letter, a budget table, a two-column article and a loopless font with WebGPU, 0.1–0.7 s a page"></a>
|
|
15
|
+
|
|
16
|
+
**[Try it now →](https://thai-ocr-web.pages.dev/)** · [ภาษาไทย](#ภาษาไทย)
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
thai-ocr-web is a JavaScript port of [OpenThai 2.1 OCR Text](https://huggingface.co/iapp/OpenThai2.1-OCR-Text-20261118),
|
|
23
|
+
iApp Technology's open Thai OCR model. The models run unchanged on [ONNX Runtime Web](https://onnxruntime.ai/), and the
|
|
24
|
+
model's whole reference runtime (`line_engine.py` v2.1.4) is ported to JavaScript — so on every test page **the output
|
|
25
|
+
is identical, character for character, to the reference Python runtime**, and CI checks it on every push.
|
|
26
|
+
|
|
27
|
+
> Unofficial port — not affiliated with or endorsed by iApp Technology.
|
|
28
|
+
|
|
29
|
+
## Why
|
|
30
|
+
|
|
31
|
+
- **Private by design.** Scans of IDs, contracts and medical letters never leave the device. No server to run or pay for.
|
|
32
|
+
- **Accurate Thai.** The model averages **88.3** character accuracy on iApp's 12 printed Thai test sets — Tesseract gets 75.4.
|
|
33
|
+
- **Fast.** ~0.2–0.4 s a page with WebGPU on a Mac mini; WASM works everywhere else.
|
|
34
|
+
- **The full pipeline, not just the model.** Page orientation (90° / 180°), deskew, column-aware reading order, a second
|
|
35
|
+
recognizer for loopless fonts (Kanit, Prompt), floating tone marks of old books, Thai normalisation, decimal commas,
|
|
36
|
+
and a filter for stamps and signatures — with a box and a confidence for every line.
|
|
37
|
+
- **Small.** 6.6 MB of models and a custom OpenCV.js build of 0.77 MB (gzip), 5× smaller than the stock one.
|
|
38
|
+
|
|
39
|
+
## How it compares
|
|
40
|
+
|
|
41
|
+
Character accuracy (100 × (1 − CER)), average of 12 printed Thai sets, from
|
|
42
|
+
[iApp's model card](https://huggingface.co/iapp/OpenThai2.1-OCR-Text-20261118) (higher is better):
|
|
43
|
+
|
|
44
|
+
| Engine | Accuracy | Runs in the browser |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| Typhoon OCR V2 2B | 89.6 | ✗ (GPU server) |
|
|
47
|
+
| **OpenThai 2.1 OCR Text — this project** | **88.3** | **✓ WebGPU / WASM** |
|
|
48
|
+
| Gemini 2.5 Flash | 87.1 | ✗ (cloud API) |
|
|
49
|
+
| Google Cloud Vision | 83.6 | ✗ (cloud API) |
|
|
50
|
+
| PaddleOCR PP-OCRv5 (Thai) | 81.1 | ✓ (PaddleOCR.js) |
|
|
51
|
+
| Tesseract 5 (tha+eng) | 75.4 | ✓ (Tesseract.js) |
|
|
52
|
+
|
|
53
|
+
These are iApp's numbers for the Python runtime; this port gives the same text on every page of its own test set.
|
|
54
|
+
|
|
55
|
+
## Performance
|
|
56
|
+
|
|
57
|
+
Mac mini (Apple Silicon), Edge 154, the 11 test pages, warm:
|
|
58
|
+
|
|
59
|
+
| Backend | ms / page (mean) | range |
|
|
60
|
+
|---|---|---|
|
|
61
|
+
| **WebGPU** (Metal) | **200** | 118–379 |
|
|
62
|
+
| WASM, 12 threads | 323 | 167–702 |
|
|
63
|
+
| WASM, 4 threads (default) | 380 | 190–878 |
|
|
64
|
+
| WASM, 1 thread | 768 | 346–1891 |
|
|
65
|
+
|
|
66
|
+
Pages that need turning take the upper end. The first page after loading WebGPU is slower (~1 s, shader compilation).
|
|
67
|
+
**First load** (gzip): models 6.6 MB + OpenCV.js 0.77 MB + ONNX Runtime 3.9 MB (WASM) or 7.3 MB (WebGPU); cached after that.
|
|
68
|
+
|
|
69
|
+
## Use it
|
|
70
|
+
|
|
71
|
+
### In the browser
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
npm install thai-ocr-web onnxruntime-web
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Serve the files in `node_modules/thai-ocr-web/models/` from your site (e.g. copy them to `public/models/`), then:
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
import { createThaiOCR } from "thai-ocr-web/browser";
|
|
81
|
+
|
|
82
|
+
const ocr = await createThaiOCR({
|
|
83
|
+
modelBaseUrl: "/models/",
|
|
84
|
+
backend: "webgpu", // falls back to WASM when the device has no WebGPU
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const result = await ocr.read(ctx.getImageData(0, 0, width, height));
|
|
88
|
+
result.text; // the page's text in reading order
|
|
89
|
+
result.lines; // [{ text, confidence, row, box: [[x, y] × 4] }] in image coordinates
|
|
90
|
+
result.dropped; // lines filtered out (stamps, signatures, specks) and why
|
|
91
|
+
result.rotation; // 0 / 90 / 180 / 270 if the page was turned
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Run it in a Web Worker to keep the page responsive — copy [`demo/ocr.worker.js`](demo/ocr.worker.js) (it also shows the
|
|
95
|
+
two guards a worker needs for WebKit and for ONNX Runtime's thread workers). For multi-threaded WASM, serve the page
|
|
96
|
+
with `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`; without them it runs
|
|
97
|
+
on one thread.
|
|
98
|
+
|
|
99
|
+
### PDFs
|
|
100
|
+
|
|
101
|
+
Install [pdf.js](https://github.com/mozilla/pdf.js) as well (`npm install pdfjs-dist`). `readPdf` goes through the
|
|
102
|
+
pages one by one: a page that already carries text (a PDF exported from Word, a page saved as PDF) gives that text
|
|
103
|
+
directly — exact and instant — and a scanned page is rendered and read with OCR.
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
import { readPdf } from "thai-ocr-web/pdf";
|
|
107
|
+
import workerSrc from "pdfjs-dist/build/pdf.worker.min.mjs?url"; // Vite; any URL of pdf.worker.min.mjs works
|
|
108
|
+
|
|
109
|
+
for await (const page of readPdf(file, { read: (image) => ocr.read(image), workerSrc })) {
|
|
110
|
+
page.pageNumber; // 1, 2, …
|
|
111
|
+
page.source; // "text-layer" (the PDF's own text) or "ocr"
|
|
112
|
+
page.text;
|
|
113
|
+
page.result; // the full OCR result for "ocr" pages
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Options: `useTextLayer: false` OCRs every page; `maxPages`, `signal` (AbortSignal), `onPage` for progress,
|
|
118
|
+
`targetLongSide` (render size, default 2200 px), and `documentOptions` for pdf.js — for scanned PDFs with JBIG2 or
|
|
119
|
+
JPEG 2000 images, and for PDFs without embedded fonts, serve pdf.js's `wasm/`, `cmaps/` and `standard_fonts/` folders
|
|
120
|
+
and pass `{ wasmUrl, cMapUrl, cMapPacked: true, standardFontDataUrl }` (the demo does this). A text layer whose Thai
|
|
121
|
+
characters did not survive (unmapped glyphs, broken mark order) is not trusted: that page is OCR'd instead.
|
|
122
|
+
|
|
123
|
+
### Core API (any JavaScript runtime)
|
|
124
|
+
|
|
125
|
+
`LineOCR` takes its dependencies as arguments, so it also runs in Node or a custom setup:
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
import { LineOCR } from "thai-ocr-web";
|
|
129
|
+
|
|
130
|
+
const ocr = await LineOCR.create({
|
|
131
|
+
ort, // onnxruntime-web
|
|
132
|
+
cv, // OpenCV.js — `await (await import("thai-ocr-web/vendor/opencv/opencv-ocr.mjs")).default()`
|
|
133
|
+
ClipperLib, // clipper-lib
|
|
134
|
+
loadFile: async (name) => bytesOf(`models/${name}`),
|
|
135
|
+
overrides: { join: { paragraphs: true } }, // optional, same keys as the Python runtime
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
[`test/parity.mjs`](test/parity.mjs) is a complete Node example. TypeScript types are included.
|
|
140
|
+
|
|
141
|
+
## Browser support
|
|
142
|
+
|
|
143
|
+
| Browser | WebGPU | WASM | Checked |
|
|
144
|
+
|---|---|---|---|
|
|
145
|
+
| Chrome / Edge (desktop) | ✓ | ✓ multi-thread | Edge 154 on macOS, Chrome on Linux |
|
|
146
|
+
| Safari / any browser on iPhone, iPad | — | ✓ one thread, 512 MB | WebKit with iPhone 15 Pro emulation |
|
|
147
|
+
| Firefox | Windows, Apple Silicon | ✓ | not yet |
|
|
148
|
+
|
|
149
|
+
WebGPU availability varies by browser, OS and GPU; the library falls back to WASM on its own. On iPhone and iPad it
|
|
150
|
+
uses one thread and caps ONNX Runtime's WASM memory at 512 MB (`maxMemoryMB`): the runtime otherwise reserves 4 GB up
|
|
151
|
+
front, which iOS refuses with "RangeError: Out of memory". All test pages read within 320 MB.
|
|
152
|
+
|
|
153
|
+
## How it works
|
|
154
|
+
|
|
155
|
+
<p align="center"><img src="docs/architecture.svg" width="900" alt="Architecture: the page sends the image to a Web Worker where LineOCR runs with ONNX Runtime Web, OpenCV.js and the models; a static host only serves the files"></p>
|
|
156
|
+
|
|
157
|
+
<p align="center"><img src="docs/pipeline.svg" width="900" alt="Pipeline: page image, detect lines, orient and deskew, cut and split lines, clean crops, recognize, rec2 re-read, floating marks, reading order, text and boxes"></p>
|
|
158
|
+
|
|
159
|
+
| Path | What |
|
|
160
|
+
|---|---|
|
|
161
|
+
| `src/line-ocr.js` | the runtime: detection, recognition, orientation, reading order, filters |
|
|
162
|
+
| `src/thai-norm.js` | Thai canonical form (mark order, sara am, legacy code points) |
|
|
163
|
+
| `src/browser.js` | `createThaiOCR()`: loads ONNX Runtime Web, OpenCV.js and the models (cached) |
|
|
164
|
+
| `src/pdf.js` | `readPdf()`: a PDF's own text where it has one, OCR for scanned pages (pdf.js) |
|
|
165
|
+
| `vendor/opencv/` | the minimal OpenCV.js build and the script that rebuilds it |
|
|
166
|
+
| `models/` | OpenThai 2.1 OCR Text models (v2.1.4), unchanged |
|
|
167
|
+
| `demo/` | the demo site (Vite) |
|
|
168
|
+
| `test/` | test pages, the reference runtime's outputs, the parity test |
|
|
169
|
+
|
|
170
|
+
## Faithful to the reference
|
|
171
|
+
|
|
172
|
+
<p align="center"><img src="docs/parity.svg" width="900" alt="Parity: the 11 test pages go through the Python reference runtime once (golden output) and through thai-ocr-web on every push; CI fails unless the text is identical"></p>
|
|
173
|
+
|
|
174
|
+
## Development
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
npm install
|
|
178
|
+
npm run dev # demo at http://localhost:5173
|
|
179
|
+
npm test # parity with the reference runtime (11 pages, ONNX Runtime Web WASM in Node) + PDF text layer
|
|
180
|
+
npm run build # static site in dist/
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The live demo is on Cloudflare Pages, connected to this repository: every push to `main` is built and deployed.
|
|
184
|
+
Cloudflare limits a file to 25 MiB, so the build splits ONNX Runtime's ~29 MB WebGPU runtime into parts
|
|
185
|
+
(`scripts/split-large-assets.mjs`) that the page joins again (`createThaiOCR({ wasmManifestUrl })`). On hosts that cannot
|
|
186
|
+
send COOP/COEP headers (GitHub Pages) the bundled [coi-serviceworker](https://github.com/gzuidhof/coi-serviceworker)
|
|
187
|
+
adds them.
|
|
188
|
+
|
|
189
|
+
<details>
|
|
190
|
+
<summary>Rebuilding the minimal OpenCV.js</summary>
|
|
191
|
+
|
|
192
|
+
Only needed if `src/line-ocr.js` starts calling a new OpenCV function (add its name to
|
|
193
|
+
`vendor/opencv/build/whitelist.json`):
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
git clone --depth 1 https://github.com/emscripten-core/emsdk.git && (cd emsdk && ./emsdk install latest && ./emsdk activate latest)
|
|
197
|
+
source emsdk/emsdk_env.sh
|
|
198
|
+
git clone --depth 1 --branch 5.0.0 https://github.com/opencv/opencv.git
|
|
199
|
+
OPENCV_SRC=$PWD/opencv npm run build:opencv && npm test
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
OpenCV's own build tool is a Python script, so this step needs Python 3 and CMake; nothing else does.
|
|
203
|
+
</details>
|
|
204
|
+
|
|
205
|
+
## FAQ
|
|
206
|
+
|
|
207
|
+
**Is anything uploaded?** No. The page downloads the models once; the image is read on your device.<br>
|
|
208
|
+
**Does it read handwriting?** No — printed text only. Handwritten parts of a page are left out.<br>
|
|
209
|
+
**Tables?** Cells are read row by row as text; table structure is not kept (iApp's OCR Markdown model does that).<br>
|
|
210
|
+
**PDFs?** Yes — `thai-ocr-web/pdf` (see [PDFs](#pdfs)); the demo opens them too.<br>
|
|
211
|
+
**Can I use only the `.onnx` files?** You can, but on their own they give raw tensors: a probability map and per-position
|
|
212
|
+
character scores. Turning that into text is the work this package does — finding lines (DB post-processing, Clipper),
|
|
213
|
+
cutting and straightening crops, CTC decoding, the second recogniser for loopless fonts, orientation, reading order and
|
|
214
|
+
filters. Use the package, or [`src/line-ocr.js`](src/line-ocr.js) as the reference if you port it elsewhere.<br>
|
|
215
|
+
**Why is my first page slower?** Model and runtime download (cached afterwards) and, with WebGPU, shader compilation.
|
|
216
|
+
|
|
217
|
+
## Roadmap
|
|
218
|
+
|
|
219
|
+
- [x] PDF input (pdf.js)
|
|
220
|
+
- [x] npm package
|
|
221
|
+
- [ ] Searchable-PDF output (text layer over scanned pages)
|
|
222
|
+
- [ ] Word boxes (the reference runtime's `words`)
|
|
223
|
+
- [ ] Several workers for batches of pages
|
|
224
|
+
- [ ] More real-world test pages
|
|
225
|
+
|
|
226
|
+
Ideas and bug reports are welcome — see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
227
|
+
|
|
228
|
+
## Limits (of the model)
|
|
229
|
+
|
|
230
|
+
Printed text only. Heavily decorative fonts can be misread, stamps come out as text, Thai digits occasionally come out
|
|
231
|
+
as Arabic digits. The test pages here are synthetic; check it on your own documents.
|
|
232
|
+
|
|
233
|
+
## License and credits
|
|
234
|
+
|
|
235
|
+
Apache-2.0 — see [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
|
236
|
+
|
|
237
|
+
- Models and reference runtime: [OpenThai 2.1 OCR Text](https://huggingface.co/iapp/OpenThai2.1-OCR-Text-20261118),
|
|
238
|
+
iApp Technology (Apache-2.0), built on PaddlePaddle PP-OCRv6-tiny (Apache-2.0).
|
|
239
|
+
- [OpenCV](https://opencv.org/) (Apache-2.0), [ONNX Runtime Web](https://onnxruntime.ai/) (MIT),
|
|
240
|
+
[Clipper](https://www.npmjs.com/package/clipper-lib) (Boost), [pdf.js](https://github.com/mozilla/pdf.js) (Apache-2.0, optional),
|
|
241
|
+
[coi-serviceworker](https://github.com/gzuidhof/coi-serviceworker) (MIT, demo only).
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## ภาษาไทย
|
|
246
|
+
|
|
247
|
+
**OCR ภาษาไทย + อังกฤษ ที่รันใน browser ทั้งหมด** ผ่าน WebGPU หรือ WebAssembly ไม่มี server รูปไม่ออกจากเครื่อง
|
|
248
|
+
|
|
249
|
+
พอร์ตโมเดล [OpenThai 2.1 OCR Text](https://huggingface.co/iapp/OpenThai2.1-OCR-Text-20261118) ของ iApp Technology
|
|
250
|
+
(Apache-2.0) มาเป็น JavaScript ทั้ง pipeline ผลลัพธ์**ตรงกับ runtime Python ต้นฉบับทุกตัวอักษร**ในทุกหน้าทดสอบ (`npm test`)
|
|
251
|
+
|
|
252
|
+
- **ลองเลย:** <https://thai-ocr-web.pages.dev/> — มีปุ่มตัวอย่างให้กดได้ทันที
|
|
253
|
+
- **เร็ว:** ประมาณ 0.2–0.4 วินาที/หน้า ด้วย WebGPU บน Mac mini และใช้ WASM ได้ทุก browser รวม iPhone
|
|
254
|
+
- **ครบ:** หมุนหน้า 90°/180°, แก้เอียง, อ่านทีละคอลัมน์, ฟอนต์ไม่มีหัว (Kanit/Prompt), วรรณยุกต์ลอยของหนังสือเก่า, ทศนิยมคอมมา, กรองตราประทับ
|
|
255
|
+
- **PDF:** หน้าที่มีข้อความอยู่ในไฟล์แล้วดึงข้อความตรง ๆ หน้าที่เป็นภาพสแกนจะ OCR ให้ (`thai-ocr-web/pdf`)
|
|
256
|
+
- **ใช้ในแอป:** `npm install thai-ocr-web onnxruntime-web` แล้วใช้ `createThaiOCR()` จาก `thai-ocr-web/browser` (ดูตัวอย่างด้านบน)
|
|
257
|
+
- **ใช้แค่ไฟล์โมเดลได้ไหม:** ได้ แต่ไฟล์ `.onnx` ให้ผลเป็นตัวเลขดิบ ต้องมีขั้นตอนก่อนและหลัง (หาบรรทัด ตัดภาพ ถอดรหัส CTC เรียงลำดับการอ่าน) ซึ่งก็คือสิ่งที่แพ็กเกจนี้ทำ
|
|
258
|
+
- **ข้อจำกัด:** อ่านตัวพิมพ์เท่านั้น ไม่เก็บโครงสร้างตาราง ควรทดสอบกับเอกสารจริงของคุณก่อนใช้งาน
|
|
259
|
+
|
|
260
|
+
เป็นงานพอร์ตอิสระ ไม่ได้เกี่ยวข้องหรือได้รับการรับรองจาก iApp Technology
|
package/models/NOTICE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
OpenThai2.0-OCR-Text (iApp Technology)
|
|
2
|
+
|
|
3
|
+
This model includes or is derived from the following Apache-2.0 licensed work:
|
|
4
|
+
|
|
5
|
+
PP-OCRv6_tiny_det, PP-OCRv6_tiny_rec, th_PP-OCRv5_mobile_rec (character list)
|
|
6
|
+
Copyright (c) PaddlePaddle Authors
|
|
7
|
+
https://huggingface.co/PaddlePaddle
|
|
8
|
+
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
det.onnx is PP-OCRv6_tiny_det as distributed. rec.onnx is a fine-tune of
|
|
11
|
+
PP-OCRv6_tiny_rec with a replaced Thai classifier, trained by iApp.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"rec": {
|
|
3
|
+
"drop_below": 0.8,
|
|
4
|
+
"junk_below": 0.9,
|
|
5
|
+
"junk_invalid": 0.05,
|
|
6
|
+
"min_chars": 2,
|
|
7
|
+
"color": "flatten",
|
|
8
|
+
"min_chars_digits": 1,
|
|
9
|
+
"min_chars_digits_height": 0.62,
|
|
10
|
+
"min_chars_latin": true
|
|
11
|
+
},
|
|
12
|
+
"rec2": {
|
|
13
|
+
"file": "rec2.onnx"
|
|
14
|
+
},
|
|
15
|
+
"join": {
|
|
16
|
+
"decimal_comma": true
|
|
17
|
+
},
|
|
18
|
+
"runtime": {
|
|
19
|
+
"ort_low_memory": true
|
|
20
|
+
}
|
|
21
|
+
}
|
package/models/det.onnx
ADDED
|
Binary file
|
package/models/det.yml
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Global:
|
|
2
|
+
model_name: PP-OCRv6_tiny_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.4
|
|
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
|
package/models/rec.onnx
ADDED
|
Binary file
|
package/models/rec2.onnx
ADDED
|
Binary file
|