millirec 0.0.0-stage → 0.1.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/CHANGELOG.md +35 -0
- package/LICENSE +21 -0
- package/README.md +244 -2
- package/dist/char-cnn/base-p25.bin +0 -0
- package/dist/char-cnn/base-p25.head.bin +0 -0
- package/dist/char-cnn/base.head.bin +0 -0
- package/dist/char-cnn/base.q4.bin +0 -0
- package/dist/char-cnn/cnn-web.js +8 -0
- package/dist/char-cnn/cnn-web.wasm +0 -0
- package/dist/char-cnn/core.d.ts +36 -0
- package/dist/char-cnn/core.js +595 -0
- package/dist/char-cnn/index.d.ts +10 -0
- package/dist/char-cnn/index.js +1068 -0
- package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -0
- package/dist/char-cnn/millirec-char-cnn.min.js +1 -0
- package/dist/char-cnn/options/accurate.js +17 -0
- package/dist/char-cnn/options/accurate.json +15 -0
- package/dist/char-cnn/options/fast.js +17 -0
- package/dist/char-cnn/options/fast.json +15 -0
- package/dist/char-cnn/options/small.js +11 -0
- package/dist/char-cnn/options/small.json +9 -0
- package/dist/char-cnn/tiny.bin +0 -0
- package/dist/char-cnn/tiny.head.bin +0 -0
- package/dist/char-lite/index.d.ts +99 -0
- package/dist/char-lite/index.js +962 -0
- package/dist/char-lite/millirec-char-lite.min.js +1 -0
- package/dist/char-lite/millirec.wasm +0 -0
- package/dist/char-lite/sets/all.js +2 -0
- package/dist/char-lite/sets/all.json +1 -0
- package/dist/char-lite/sets/capitals.js +2 -0
- package/dist/char-lite/sets/capitals.json +1 -0
- package/dist/char-lite/sets/digits.js +2 -0
- package/dist/char-lite/sets/digits.json +1 -0
- package/dist/char-lite/sets/lowercase.js +2 -0
- package/dist/char-lite/sets/lowercase.json +1 -0
- package/dist/char-lite/sets/punctuation.js +2 -0
- package/dist/char-lite/sets/punctuation.json +1 -0
- package/dist/char-lite/sets.json +163 -0
- package/dist/char-lite/worker-stroke.js +11 -0
- package/dist/overlap/index.d.ts +139 -0
- package/dist/overlap/millirec-overlap.min.js +45 -0
- package/dist/overlap/millirec.wasm +0 -0
- package/dist/overlap/training.all.json +1 -0
- package/dist/overlap/training.capitals.json +1 -0
- package/dist/overlap/training.digits.json +1 -0
- package/dist/overlap/training.lowercase.json +1 -0
- package/dist/overlap/training.punctuation.json +1 -0
- package/dist/overlap/words.json +1 -0
- package/package.json +87 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
- **Bundlers**: the package found its own folder with `new URL('./', import.meta.url)`, which Vite
|
|
6
|
+
and webpack 5 rewrite into a reference to the module FILE, so `loadSet()` fetched
|
|
7
|
+
`…/index.js/sets/digits.json`. The folder is now sliced from `import.meta.url`. With a bundler,
|
|
8
|
+
copy `dist/char-lite/` / `dist/char-cnn/` to your site and pass `assetBase` (README "Serving the
|
|
9
|
+
files", with Vite and webpack recipes; the old `optimizeDeps` advice did not work in 0.1.0; with this fix `optimizeDeps: { exclude: ['millirec'] }` works in Vite dev only, and builds still need the copy).
|
|
10
|
+
- **Clear errors when the files are not served**: `loadSet`, `loadOption`, the CNN model/kernel
|
|
11
|
+
loader and char-lite's worker/wasm boot now say which URL failed, what came back (a 404, an HTML
|
|
12
|
+
page from an SPA fallback, unparseable JSON) and what to do, instead of failing in `JSON.parse` or
|
|
13
|
+
the matcher.
|
|
14
|
+
- `"sideEffects"` lists the script-tag files (`dist/**/*.min.js`, `sets/*.js`, `options/*.js`) so a
|
|
15
|
+
bundler keeps `import 'millirec/overlap/millirec-overlap.min.js'`.
|
|
16
|
+
- `destroy()`: char-lite resolves pending and later `recognize()`/`rawScores()` with `null`;
|
|
17
|
+
char-cnn's `personalize()` returns `null`, `embedding()` throws `millirec: recognizer destroyed`,
|
|
18
|
+
`depersonalize()` is a no-op (they used to read past an empty stage list).
|
|
19
|
+
- char-cnn runs in a Web Worker (draws on `OffscreenCanvas`); webpack no longer tries to bundle the
|
|
20
|
+
kernel's dynamic import (`webpackIgnore`).
|
|
21
|
+
- Node / SSR: `create()` outside a browser throws a message saying so; `loadSet`/`loadOption` in
|
|
22
|
+
Node say to import the JSON instead.
|
|
23
|
+
- `MillirecCharCnn.create('fast')` = `create({ option: 'fast' })`.
|
|
24
|
+
- char-lite's result cache is an LRU of 256 (it grew without bound).
|
|
25
|
+
- Types: `Training.version` is `number` (and `mode` a string) so an imported set JSON type-checks;
|
|
26
|
+
`millirec:result` / `millirec:clear` are typed on `HTMLElementEventMap`; declarations for the
|
|
27
|
+
overlap widget's global (`import type {} from 'millirec/overlap'`); `typesVersions` for
|
|
28
|
+
`moduleResolution: node10`.
|
|
29
|
+
- package.json: `bugs`, `engines` (node ≥ 18, for tooling that imports the JSON).
|
|
30
|
+
- README: browser-only note, `destroy()`, the pad's options and events, overlap gzip sizes,
|
|
31
|
+
stable paths.
|
|
32
|
+
|
|
33
|
+
## 0.1.0
|
|
34
|
+
|
|
35
|
+
First release.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robert Wilcox
|
|
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,245 @@
|
|
|
1
|
-
#
|
|
1
|
+
# millirec
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Small, fast handwriting recognition in the browser. Draw a character, get the
|
|
4
|
+
three likeliest readings back. Runs entirely on the device — no server, no
|
|
5
|
+
upload of anyone's ink.
|
|
6
|
+
|
|
7
|
+
Three pieces, pick by what you need to read:
|
|
8
|
+
|
|
9
|
+
| | reads | size (gzip) | trainable on your own strokes |
|
|
10
|
+
|---|---|---|---|
|
|
11
|
+
| `millirec/char-lite` | anything you have samples for: digits, letters, punctuation, emoji, made-up symbols | 26 KB + the training set (digits 4 KB, all 27 KB) | **yes** — add samples, add whole new classes |
|
|
12
|
+
| `millirec/char-cnn` | capital letters A–Z | 15 KB (`small`) to 47 KB (`fast`); `accurate` 42 KB | refit on a writer's own letters (`personalize`) |
|
|
13
|
+
| overlap (script tag) | whole words written letter over letter in one box | 34 KB (script 20 + wasm 14) + training (capitals 19 KB, all 42 KB) + the default word list (38 KB, skipped when you pass `words`) | — |
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install millirec
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`import … from 'millirec'` is the same as `'millirec/char-lite'`. The package is ES modules only
|
|
22
|
+
(use `import`, not `require`), or the script-tag builds below.
|
|
23
|
+
|
|
24
|
+
**Browser only.** Recognition needs a browser: char-lite runs its matcher in a Web Worker, char-cnn
|
|
25
|
+
draws strokes on a canvas (an `OffscreenCanvas` inside a worker works too). Importing the package
|
|
26
|
+
is safe during server-side rendering; call `create()` on the client. In Node, `create()` throws a
|
|
27
|
+
message saying so, and the training sets can be imported as JSON
|
|
28
|
+
(`import digits from 'millirec/char-lite/sets/digits.json' with { type: 'json' }`) for tooling.
|
|
29
|
+
|
|
30
|
+
## char-lite
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import { MillirecCharLite, loadSet } from 'millirec/char-lite'
|
|
34
|
+
|
|
35
|
+
const training = await loadSet('digits') // 'all' | 'capitals' | 'digits' | 'lowercase' | 'punctuation'
|
|
36
|
+
const rec = await MillirecCharLite.create({ training })
|
|
37
|
+
|
|
38
|
+
// a drawing pad that reports after every stroke
|
|
39
|
+
const pad = rec.attach(document.querySelector('#pad'))
|
|
40
|
+
document.querySelector('#pad').addEventListener('millirec:result', (e) => {
|
|
41
|
+
console.log(e.detail.results) // [{ char: '7', prob: 0.91 }, ...]
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// or your own strokes: arrays of {x, y} points
|
|
45
|
+
await rec.recognize([[{ x: 50, y: 10 }, /* ... */ { x: 52, y: 120 }]])
|
|
46
|
+
|
|
47
|
+
rec.destroy() // when you are done with it
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Each char-lite instance holds a Web Worker and its own copy of the wasm matcher, so call
|
|
51
|
+
`destroy()` when a recognizer is no longer needed (and share one recognizer between several pads
|
|
52
|
+
rather than creating one per pad). After `destroy()`, `recognize()` and `rawScores()` resolve
|
|
53
|
+
`null`, including calls that were still waiting.
|
|
54
|
+
|
|
55
|
+
### The drawing pad
|
|
56
|
+
|
|
57
|
+
`attach(el, options)` mounts a canvas in `el` and returns a `Pad` (`clear()`, `undoClear()`,
|
|
58
|
+
`getStrokes()`, `isStale()`, `configure()`, `setInk()`, `repaint()`, `canvas`). Options:
|
|
59
|
+
|
|
60
|
+
| option | default | |
|
|
61
|
+
|---|---|---|
|
|
62
|
+
| `width`, `height` | 240, 240 | pad size in CSS px |
|
|
63
|
+
| `ink` | `'#1e293b'` | ink colour |
|
|
64
|
+
| `idleOverwrite` | 2 | seconds of pen-up after which the next stroke starts a new character (0 = off) |
|
|
65
|
+
| `scribbleClear` | true | scribbling over the character throws it away |
|
|
66
|
+
| `scribble` | null | overrides for the scribble detector's bars (`turns`, `retrace`, `winding`, `cover`) |
|
|
67
|
+
| `staleAlpha` | 0.3 | opacity of ink once the character has settled (1 keeps it solid) |
|
|
68
|
+
| `onStale` | — | `(stale) => void`, called on every change of the settled state |
|
|
69
|
+
|
|
70
|
+
`el` fires two events:
|
|
71
|
+
|
|
72
|
+
- `millirec:result` — after every stroke, `detail = { results, strokes }` (`results` is the top
|
|
73
|
+
three `{ char, prob }`; an empty pad reports `results: []`, so a reading of ink that is gone can be
|
|
74
|
+
cleared);
|
|
75
|
+
- `millirec:clear` — the pad dropped its own ink, `detail = { reason: 'idle' | 'scribble', undo }`;
|
|
76
|
+
`undo()` puts it back.
|
|
77
|
+
|
|
78
|
+
Both are typed on `HTMLElementEventMap`, so `el.addEventListener('millirec:result', (e) => e.detail.results)`
|
|
79
|
+
type-checks. char-cnn's `attach()` takes the same options and fires the same events.
|
|
80
|
+
|
|
81
|
+
### Training on your users' strokes
|
|
82
|
+
|
|
83
|
+
A class name is any string, so a recognizer can learn one person's hand, or
|
|
84
|
+
symbols nobody else uses:
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
const mine = MillirecCharLite.withSamples(training, {
|
|
88
|
+
'7': [strokesOfASeven, anotherSeven], // appended to the shipped 7s
|
|
89
|
+
'★': [star1, star2, star3], // a brand-new class
|
|
90
|
+
})
|
|
91
|
+
const rec = await MillirecCharLite.create({ training: mine })
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Drawings are resampled to the set's points-per-shape, so pass raw strokes.
|
|
95
|
+
`{ replace: true }` drops a class's shipped samples instead of appending —
|
|
96
|
+
worth it once you have three or four of the writer's own. Keep the raw strokes
|
|
97
|
+
(in localStorage, IndexedDB, your server — the library never stores anything)
|
|
98
|
+
and rebuild the training object when the recognizer starts. The format is
|
|
99
|
+
plain JSON (`version: 2`, see `Training` in the type declarations), so a
|
|
100
|
+
training set can also be built offline and loaded with `fetch`.
|
|
101
|
+
|
|
102
|
+
## char-cnn
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
import { MillirecCharCnn } from 'millirec/char-cnn'
|
|
106
|
+
|
|
107
|
+
const rec = await MillirecCharCnn.create() // the 'accurate' option
|
|
108
|
+
// or: await MillirecCharCnn.create({ option: 'fast' }) // 'accurate' | 'fast' | 'small'
|
|
109
|
+
// (a bare name works too: create('fast'))
|
|
110
|
+
rec.attach(document.querySelector('#pad'))
|
|
111
|
+
await rec.recognize(strokes, { only: 'AEIOU' }) // restrict the answer to some letters
|
|
112
|
+
await rec.personalize({ A: [a1, a2, a3], R: [r1, r2] }) // lean toward this writer's hand
|
|
113
|
+
rec.destroy() // let go of the models
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`millirec/char-cnn/core` is the same recognizer without the drawing pad; it also runs inside a
|
|
117
|
+
Web Worker (it draws on an `OffscreenCanvas` there). After `destroy()`, `recognize()` and
|
|
118
|
+
`personalize()` return `null` and `embedding()` throws.
|
|
119
|
+
|
|
120
|
+
## overlap: words written in one box
|
|
121
|
+
|
|
122
|
+
Letters written one on top of another in a single box, read against a word list. Script tag only
|
|
123
|
+
for now:
|
|
124
|
+
|
|
125
|
+
```html
|
|
126
|
+
<script src="/millirec/overlap/millirec-overlap.min.js"></script>
|
|
127
|
+
<div id="box"></div>
|
|
128
|
+
<script>
|
|
129
|
+
MillirecOverlap.create(document.getElementById('box'), { set: 'capitals', words: ['HELLO', 'WORLD'] })
|
|
130
|
+
</script>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Copy `dist/overlap/` to your site (or load it from a CDN); the script finds its wasm, training and
|
|
134
|
+
default word list beside itself. `set` is `capitals`, `lowercase`, `digits`, `punctuation` or `all`.
|
|
135
|
+
TypeScript declarations for the `MillirecOverlap` global and its `millirec-overlap:*` events come
|
|
136
|
+
with `import type {} from 'millirec/overlap'` (types only — the widget itself is the script tag).
|
|
137
|
+
|
|
138
|
+
## Serving the files
|
|
139
|
+
|
|
140
|
+
The recognizers load a worker, a wasm file, training sets and model files at run time, by default
|
|
141
|
+
from the folder the module itself is in. That works as-is when the browser loads the package's
|
|
142
|
+
files where they are — no bundler, an import map, or a dev server that serves `node_modules`.
|
|
143
|
+
|
|
144
|
+
**With a bundler** (Vite, webpack, Rollup, esbuild, Parcel …) the JavaScript is moved into your
|
|
145
|
+
bundle and the other files are not emitted with it. Copy the recognizer's folder into the folder
|
|
146
|
+
your site serves as-is, and pass its URL as `assetBase` to every call that loads something:
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
import { MillirecCharLite, loadSet } from 'millirec'
|
|
150
|
+
import { MillirecCharCnn } from 'millirec/char-cnn'
|
|
151
|
+
|
|
152
|
+
const lite = '/millirec/char-lite/' // copied from node_modules/millirec/dist/char-lite/
|
|
153
|
+
const training = await loadSet('digits', { assetBase: lite })
|
|
154
|
+
const rec = await MillirecCharLite.create({ training, assetBase: lite })
|
|
155
|
+
|
|
156
|
+
const cnn = await MillirecCharCnn.create({ option: 'fast', assetBase: '/millirec/char-cnn/' }) // dist/char-cnn/
|
|
157
|
+
// loadOption(name, { assetBase }) takes it too
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Vite — copy into `public/`, which is served (and copied into the build) unchanged:
|
|
161
|
+
|
|
162
|
+
```sh
|
|
163
|
+
mkdir -p public/millirec
|
|
164
|
+
cp -R node_modules/millirec/dist/char-lite node_modules/millirec/dist/char-cnn public/millirec/
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
(a `postinstall` script with those two lines keeps it in step with the installed version).
|
|
168
|
+
In `vite` dev alone, `optimizeDeps: { exclude: ['millirec'] }` also works without copying (the dev
|
|
169
|
+
server then serves the package from `node_modules`), but `vite build` does not emit the files, so a
|
|
170
|
+
production build still needs the copy and `assetBase`.
|
|
171
|
+
|
|
172
|
+
webpack 5 — with `copy-webpack-plugin`:
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
// webpack.config.js
|
|
176
|
+
import CopyPlugin from 'copy-webpack-plugin'
|
|
177
|
+
export default {
|
|
178
|
+
plugins: [new CopyPlugin({ patterns: [
|
|
179
|
+
{ from: 'node_modules/millirec/dist/char-lite', to: 'millirec/char-lite' },
|
|
180
|
+
{ from: 'node_modules/millirec/dist/char-cnn', to: 'millirec/char-cnn' },
|
|
181
|
+
] })],
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
`assetBase` may be absolute, root-relative or relative to the page, with or without the trailing
|
|
186
|
+
slash. If the files are not there, the error names the URL it tried and what came back (a dev
|
|
187
|
+
server's index.html fallback is recognised as such) instead of failing inside the matcher.
|
|
188
|
+
|
|
189
|
+
**`assetBase` (and char-cnn's `kernel`) decide which code is loaded** — the worker and kernel are
|
|
190
|
+
fetched from there and run. Set them in your own code; never fill them from a URL parameter, a
|
|
191
|
+
query string or anything a visitor can type.
|
|
192
|
+
|
|
193
|
+
### Content-Security-Policy
|
|
194
|
+
|
|
195
|
+
If your site sends a CSP, the recognizers need:
|
|
196
|
+
|
|
197
|
+
- `worker-src blob:` — char-lite boots its worker from a blob that loads `worker-stroke.js`;
|
|
198
|
+
- `script-src 'wasm-unsafe-eval' 'unsafe-eval'` — the matcher is WebAssembly, and its generated
|
|
199
|
+
bindings (char-lite's worker, overlap) use `new Function`;
|
|
200
|
+
- `connect-src` for wherever the files are served from, if that is another origin.
|
|
201
|
+
|
|
202
|
+
## Script tag / CDN
|
|
203
|
+
|
|
204
|
+
No build step needed:
|
|
205
|
+
|
|
206
|
+
```html
|
|
207
|
+
<script src="https://cdn.jsdelivr.net/npm/millirec@0.1/dist/char-lite/millirec-char-lite.min.js"></script>
|
|
208
|
+
<script src="https://cdn.jsdelivr.net/npm/millirec@0.1/dist/char-lite/sets/digits.js"></script>
|
|
209
|
+
<script>
|
|
210
|
+
MillirecCharLite.create().then((rec) => rec.attach(document.querySelector('#pad')))
|
|
211
|
+
</script>
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
`sets/<name>.js` sets `window.MILLIREC_TRAINING`; for char-cnn load
|
|
215
|
+
`dist/char-cnn/options/accurate.js` and then `millirec-char-cnn.min.js`. The
|
|
216
|
+
script finds its worker and wasm next to itself, on any origin that sends CORS
|
|
217
|
+
headers (jsDelivr and unpkg do).
|
|
218
|
+
|
|
219
|
+
## Stable paths
|
|
220
|
+
|
|
221
|
+
The documented entry points are the stable API: `millirec`, `millirec/char-lite`,
|
|
222
|
+
`millirec/char-cnn`, `millirec/char-cnn/core`, `millirec/char-lite/sets/<name>.json`,
|
|
223
|
+
`millirec/char-cnn/options/<name>.json`, `millirec/overlap/<file>` and the script-tag files in "Script tag / CDN"
|
|
224
|
+
above. `millirec/dist/*` exposes every built file as an escape hatch; the layout under it may change
|
|
225
|
+
in any release.
|
|
226
|
+
|
|
227
|
+
## Where the data comes from
|
|
228
|
+
|
|
229
|
+
- **char-lite training sets and the overlap templates**: one writer's own handwriting, drawn for
|
|
230
|
+
this project.
|
|
231
|
+
- **char-cnn models**: small CNNs trained on handwritten capitals from EMNIST (derived from NIST
|
|
232
|
+
Special Database 19).
|
|
233
|
+
- **overlap's default word list** (`words.json`): the 10,000 commonest English words by millirec's
|
|
234
|
+
own count over about 1.9 billion words of English, mostly news — names and offensive words
|
|
235
|
+
removed. It leans toward news vocabulary; pass your own `words` for anything specific.
|
|
236
|
+
|
|
237
|
+
Recognition runs entirely in the browser: nothing a user draws is sent anywhere by this package.
|
|
238
|
+
|
|
239
|
+
## Versions
|
|
240
|
+
|
|
241
|
+
0.x: the API may still change between minor versions. 1.0 will mark it stable.
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
MIT
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
async function CnnEmbed(moduleArg={}){var moduleRtn;var b=moduleArg,f=!!globalThis.WorkerGlobalScope,g=import.meta.url,h="",l,m;if(globalThis.window||f){try{h=(new URL(".",g)).href}catch{}f&&(m=a=>{var d=new XMLHttpRequest;d.open("GET",a,!1);d.responseType="arraybuffer";d.send(null);return new Uint8Array(d.response)});l=async a=>{a=await fetch(a,{credentials:"same-origin"});if(a.ok)return a.arrayBuffer();throw Error(a.status+" : "+a.url);}}var n=console.error.bind(console),p,q=!1,r,t,u,v=!1;
|
|
2
|
+
function w(){var a=x.buffer;new Int8Array(a);new Int16Array(a);b.HEAPU8=u=new Uint8Array(a);new Uint16Array(a);new Int32Array(a);new Uint32Array(a);b.HEAPF32=new Float32Array(a);new Float64Array(a);new BigInt64Array(a);new BigUint64Array(a)}var y;async function z(a){if(!p)try{var d=await l(a);return new Uint8Array(d)}catch{}if(a==y&&p)a=new Uint8Array(p);else if(m)a=m(a);else throw"both async and sync fetching of the wasm failed";return a}
|
|
3
|
+
async function A(a,d){try{var c=await z(a);return await WebAssembly.instantiate(c,d)}catch(e){throw n(`failed to asynchronously prepare wasm: ${e}`),a=e,b.onAbort?.(a),a="Aborted("+a+")",n(a),q=!0,a=new WebAssembly.RuntimeError(a+". Build with -sASSERTIONS for more info."),t?.(a),a;}}
|
|
4
|
+
async function B(a){var d=y;if(!p)try{var c=fetch(d,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(c,a)}catch(e){n(`wasm streaming compile failed: ${e}`),n("falling back to ArrayBuffer instantiation")}return A(d,a)}var C=a=>{for(;0<a.length;)a.shift()(b)},D=[],E=[],F=()=>{var a=b.preRun.shift();E.push(a)};b.printErr&&(n=b.printErr);b.wasmBinary&&(p=b.wasmBinary);if(b.preInit)for("function"==typeof b.preInit&&(b.preInit=[b.preInit]);0<b.preInit.length;)b.preInit.shift()();
|
|
5
|
+
var x,G={a:a=>{var d=u.length;a>>>=0;if(2147483648<a)return!1;for(var c=1;4>=c;c*=2){var e=d*(1+.2/c);e=Math.min(e,a+100663296);a:{e=(Math.min(2147483648,65536*Math.ceil(Math.max(a,e)/65536))-x.buffer.byteLength+65535)/65536|0;try{x.grow(e);w();var k=1;break a}catch(I){}k=void 0}if(k)return!0}return!1}},H;
|
|
6
|
+
H=await (async function(){function a(c){c=H=c.exports;b._cnnLoad=c.d;b._free=c.e;b._malloc=c.f;b._cnnSize=c.g;b._cnnDim=c.h;b._cnnInputPtr=c.i;b._cnnEmbedPtr=c.j;b._cnnRun=c.k;x=c.b;w();return H}var d={a:G};if(b.instantiateWasm)return new Promise(c=>{b.instantiateWasm(d,(e,k)=>{c(a(e,k))})});y??=b.locateFile?b.locateFile?b.locateFile("cnn-web.wasm",h):h+"cnn-web.wasm":(new URL("cnn-web.wasm",import.meta.url)).href;return a((await B(d)).instance)}());
|
|
7
|
+
(function(){function a(){b.calledRun=!0;if(!q){v=!0;H.c();r?.(b);b.onRuntimeInitialized?.();if(b.postRun)for("function"==typeof b.postRun&&(b.postRun=[b.postRun]);b.postRun.length;){var d=b.postRun.shift();D.push(d)}C(D)}}if(b.preRun)for("function"==typeof b.preRun&&(b.preRun=[b.preRun]);b.preRun.length;)F();C(E);b.setStatus?(b.setStatus("Running..."),setTimeout(()=>{setTimeout(()=>b.setStatus(""),1);a()},1)):a()})();v?moduleRtn=b:moduleRtn=new Promise((a,d)=>{r=a;t=d});
|
|
8
|
+
;return moduleRtn}export default CnnEmbed;
|
|
Binary file
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Strokes, Stroke, Reading } from '../char-lite/index.js'
|
|
2
|
+
|
|
3
|
+
export interface Stage { model: string; head: string; theta?: number; headNorm?: boolean }
|
|
4
|
+
export interface CnnConfig { classes: string | string[]; stages: Stage[]; kernel?: string }
|
|
5
|
+
|
|
6
|
+
export interface CreateOptions extends Partial<CnnConfig> {
|
|
7
|
+
/** A shipped option, used when `stages` is not given. Default 'accurate'. */
|
|
8
|
+
option?: OptionName
|
|
9
|
+
/** Folder holding the model files and kernel. Default: next to this module. Absolute, root-relative
|
|
10
|
+
* or page-relative; the trailing slash is optional. It (and `kernel`) decide what code is loaded:
|
|
11
|
+
* never fill them from a URL parameter or user input. */
|
|
12
|
+
assetBase?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export declare const OPTIONS: readonly ['accurate', 'fast', 'small']
|
|
16
|
+
export type OptionName = (typeof OPTIONS)[number]
|
|
17
|
+
export declare function loadOption(name: OptionName, opts?: { assetBase?: string }): Promise<CnnConfig>
|
|
18
|
+
|
|
19
|
+
export declare class MillirecCharCnnCore {
|
|
20
|
+
/** A string is a shipped option's name: create('fast') = create({ option: 'fast' }). */
|
|
21
|
+
static create(opts?: CreateOptions | OptionName): Promise<MillirecCharCnnCore>
|
|
22
|
+
readonly classes: string[]
|
|
23
|
+
readonly ready: Promise<void>
|
|
24
|
+
/** `only` restricts the reading to these classes. */
|
|
25
|
+
recognize(strokes: Strokes | Stroke, opts?: { only?: string | string[] | Set<string> }): Promise<(Reading[] & { stage?: number }) | null>
|
|
26
|
+
/** Refit the last stage's head on your own drawings: { 'A': [strokes, ...], ... }.
|
|
27
|
+
* Anchored to the shipped head, so a few samples nudge a letter and many take it over.
|
|
28
|
+
* Classes outside `classes` are ignored (the CNN cannot learn new characters this way). */
|
|
29
|
+
personalize(samples: Record<string, Array<Strokes | Stroke>>, opts?: { steps?: number; lr?: number; prior?: number }): Promise<{ n: number; classes: string[] } | null>
|
|
30
|
+
/** Back to the shipped head. */
|
|
31
|
+
depersonalize(): void
|
|
32
|
+
/** The last stage's unit embedding of a drawing — what the head reads. Throws after destroy(). */
|
|
33
|
+
embedding(strokes: Strokes | Stroke): Promise<Float32Array>
|
|
34
|
+
/** Let go of the models; recognize() and personalize() return null afterwards. */
|
|
35
|
+
destroy(): void
|
|
36
|
+
}
|