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
|
@@ -0,0 +1,1068 @@
|
|
|
1
|
+
// ../../server/public/wordgpu/cnnjs.mjs
|
|
2
|
+
var BITS_OF = { MRCNN1: 8, MRCNN3: 3, MRCNN4: 4, MRCNN5: 5, MRCNN6: 6 };
|
|
3
|
+
function reader(buf) {
|
|
4
|
+
const dv = new DataView(buf.buffer ? buf.buffer : buf, buf.byteOffset || 0, buf.byteLength);
|
|
5
|
+
const u8 = new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength);
|
|
6
|
+
let o = 0;
|
|
7
|
+
return {
|
|
8
|
+
u32: () => {
|
|
9
|
+
const v = dv.getUint32(o, true);
|
|
10
|
+
o += 4;
|
|
11
|
+
return v;
|
|
12
|
+
},
|
|
13
|
+
f32s: (n) => {
|
|
14
|
+
const a = new Float32Array(n);
|
|
15
|
+
for (let i = 0; i < n; i++) {
|
|
16
|
+
a[i] = dv.getFloat32(o, true);
|
|
17
|
+
o += 4;
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
},
|
|
21
|
+
i8s: (n) => {
|
|
22
|
+
const a = new Int8Array(n);
|
|
23
|
+
for (let i = 0; i < n; i++) a[i] = u8[o + i] << 24 >> 24;
|
|
24
|
+
o += n;
|
|
25
|
+
return a;
|
|
26
|
+
},
|
|
27
|
+
// n-bit two's-complement fields packed LSB-first, padded to a whole byte
|
|
28
|
+
ibits: (n, bits) => {
|
|
29
|
+
if (bits === 8) {
|
|
30
|
+
const a2 = new Int8Array(n);
|
|
31
|
+
for (let i = 0; i < n; i++) a2[i] = u8[o + i] << 24 >> 24;
|
|
32
|
+
o += n;
|
|
33
|
+
return a2;
|
|
34
|
+
}
|
|
35
|
+
const a = new Int8Array(n), mask = (1 << bits) - 1, sign = 1 << bits - 1;
|
|
36
|
+
for (let i = 0; i < n; i++) {
|
|
37
|
+
const bp = i * bits, byte = o + (bp >> 3), sh = bp & 7;
|
|
38
|
+
let v = (u8[byte] | u8[byte + 1] << 8) >> sh & mask;
|
|
39
|
+
a[i] = v & sign ? v - (1 << bits) : v;
|
|
40
|
+
}
|
|
41
|
+
o += n * bits + 7 >> 3;
|
|
42
|
+
return a;
|
|
43
|
+
},
|
|
44
|
+
str: (n) => {
|
|
45
|
+
const s = String.fromCharCode(...u8.subarray(o, o + n));
|
|
46
|
+
o += n;
|
|
47
|
+
return s;
|
|
48
|
+
},
|
|
49
|
+
done: () => o >= dv.byteLength
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function readLinear(r, bits = 8) {
|
|
53
|
+
const out = r.u32(), inn = r.u32();
|
|
54
|
+
const scale = r.f32s(out), bias = r.f32s(out), q = r.ibits(out * inn, bits);
|
|
55
|
+
const w = new Float32Array(out * inn);
|
|
56
|
+
for (let o = 0; o < out; o++) for (let i = 0; i < inn; i++) w[o * inn + i] = q[o * inn + i] * scale[o];
|
|
57
|
+
return { out, in: inn, w, bias };
|
|
58
|
+
}
|
|
59
|
+
function parseModel(buf) {
|
|
60
|
+
const r = reader(buf);
|
|
61
|
+
const magic = r.str(6);
|
|
62
|
+
const bits = BITS_OF[magic];
|
|
63
|
+
if (!bits) throw new Error("not an MRCNN1/3/4/5/6 model");
|
|
64
|
+
const version = r.u32();
|
|
65
|
+
if (version !== 1) throw new Error("unknown model version " + version);
|
|
66
|
+
const size = r.u32(), dim = r.u32(), nConv = r.u32();
|
|
67
|
+
const convs = [];
|
|
68
|
+
for (let L = 0; L < nConv; L++) {
|
|
69
|
+
const cout = r.u32(), cin = r.u32(), k = r.u32(), pool = r.u32();
|
|
70
|
+
const scale = r.f32s(cout), bias = r.f32s(cout), q = r.ibits(cout * cin * k * k, bits);
|
|
71
|
+
const w = new Float32Array(q.length);
|
|
72
|
+
const per = cin * k * k;
|
|
73
|
+
for (let o = 0; o < cout; o++) for (let i = 0; i < per; i++) w[o * per + i] = q[o * per + i] * scale[o];
|
|
74
|
+
convs.push({ cout, cin, k, pool, w, bias });
|
|
75
|
+
}
|
|
76
|
+
const lin = readLinear(r, bits);
|
|
77
|
+
return { size, dim, convs, lin, bits };
|
|
78
|
+
}
|
|
79
|
+
function expandToInt8(buf) {
|
|
80
|
+
const u8 = new Uint8Array(buf.buffer ? buf.buffer : buf, buf.byteOffset || 0, buf.byteLength);
|
|
81
|
+
const magic = String.fromCharCode(...u8.subarray(0, 6));
|
|
82
|
+
const bits = BITS_OF[magic];
|
|
83
|
+
if (!bits) throw new Error("not an MRCNN model");
|
|
84
|
+
if (bits === 8) return u8;
|
|
85
|
+
const r = reader(buf);
|
|
86
|
+
r.str(6);
|
|
87
|
+
const version = r.u32(), size = r.u32(), dim = r.u32(), nConv = r.u32();
|
|
88
|
+
const parts = [];
|
|
89
|
+
const u32 = (v) => {
|
|
90
|
+
const b = new Uint8Array(4);
|
|
91
|
+
new DataView(b.buffer).setUint32(0, v, true);
|
|
92
|
+
return b;
|
|
93
|
+
};
|
|
94
|
+
const f32s = (a) => new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
|
|
95
|
+
parts.push(new TextEncoder().encode("MRCNN1"), u32(version), u32(size), u32(dim), u32(nConv));
|
|
96
|
+
for (let L = 0; L < nConv; L++) {
|
|
97
|
+
const cout = r.u32(), cin = r.u32(), k = r.u32(), pool = r.u32();
|
|
98
|
+
const scale2 = r.f32s(cout), bias2 = r.f32s(cout), q2 = r.ibits(cout * cin * k * k, bits);
|
|
99
|
+
parts.push(u32(cout), u32(cin), u32(k), u32(pool), f32s(scale2), f32s(bias2), new Uint8Array(q2.buffer, q2.byteOffset, q2.length));
|
|
100
|
+
}
|
|
101
|
+
const out = r.u32(), inn = r.u32();
|
|
102
|
+
const scale = r.f32s(out), bias = r.f32s(out), q = r.ibits(out * inn, bits);
|
|
103
|
+
parts.push(u32(out), u32(inn), f32s(scale), f32s(bias), new Uint8Array(q.buffer, q.byteOffset, q.length));
|
|
104
|
+
const total = parts.reduce((a, p) => a + p.length, 0);
|
|
105
|
+
const res = new Uint8Array(total);
|
|
106
|
+
let o = 0;
|
|
107
|
+
for (const p of parts) {
|
|
108
|
+
res.set(p, o);
|
|
109
|
+
o += p.length;
|
|
110
|
+
}
|
|
111
|
+
return res;
|
|
112
|
+
}
|
|
113
|
+
function parseHead(buf) {
|
|
114
|
+
return readLinear(reader(buf));
|
|
115
|
+
}
|
|
116
|
+
function conv3(c, inp, res, out) {
|
|
117
|
+
const { cout, cin, k, w, bias } = c;
|
|
118
|
+
const plane = res * res, half = k - 1 >> 1, per = cin * k * k;
|
|
119
|
+
for (let co = 0; co < cout; co++) {
|
|
120
|
+
const ob = co * plane, b = bias[co], wb = co * per;
|
|
121
|
+
for (let y = 0; y < res; y++) {
|
|
122
|
+
for (let x = 0; x < res; x++) {
|
|
123
|
+
let s = b;
|
|
124
|
+
for (let ci = 0; ci < cin; ci++) {
|
|
125
|
+
const ib = ci * plane, wc = wb + ci * k * k;
|
|
126
|
+
for (let ky = 0; ky < k; ky++) {
|
|
127
|
+
const yy = y + ky - half;
|
|
128
|
+
if (yy < 0 || yy >= res) continue;
|
|
129
|
+
const row = ib + yy * res, wr = wc + ky * k;
|
|
130
|
+
for (let kx = 0; kx < k; kx++) {
|
|
131
|
+
const xx = x + kx - half;
|
|
132
|
+
if (xx < 0 || xx >= res) continue;
|
|
133
|
+
s += w[wr + kx] * inp[row + xx];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
out[ob + y * res + x] = s > 0 ? s : 0;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function pool2(inp, res, ch, out) {
|
|
143
|
+
const r2 = res >> 1;
|
|
144
|
+
for (let c = 0; c < ch; c++) {
|
|
145
|
+
const ib = c * res * res, ob = c * r2 * r2;
|
|
146
|
+
for (let y = 0; y < r2; y++) for (let x = 0; x < r2; x++) {
|
|
147
|
+
const i0 = ib + 2 * y * res + 2 * x;
|
|
148
|
+
const a = inp[i0], b = inp[i0 + 1], d = inp[i0 + res], e = inp[i0 + res + 1];
|
|
149
|
+
out[ob + y * r2 + x] = Math.max(a, b, d, e);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function embed(model, img) {
|
|
154
|
+
const S = model.size;
|
|
155
|
+
let cur = Float32Array.from(img), res = S, ch = 1;
|
|
156
|
+
for (const c of model.convs) {
|
|
157
|
+
const out = new Float32Array(c.cout * res * res);
|
|
158
|
+
conv3(c, cur, res, out);
|
|
159
|
+
cur = out;
|
|
160
|
+
ch = c.cout;
|
|
161
|
+
if (c.pool) {
|
|
162
|
+
const p = new Float32Array(ch * (res >> 1) * (res >> 1));
|
|
163
|
+
pool2(cur, res, ch, p);
|
|
164
|
+
cur = p;
|
|
165
|
+
res >>= 1;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const plane = res * res;
|
|
169
|
+
const gap = new Float32Array(ch);
|
|
170
|
+
for (let c = 0; c < ch; c++) {
|
|
171
|
+
let s = 0;
|
|
172
|
+
for (let j = 0; j < plane; j++) s += cur[c * plane + j];
|
|
173
|
+
gap[c] = s / plane;
|
|
174
|
+
}
|
|
175
|
+
const { lin } = model;
|
|
176
|
+
const e = new Float32Array(lin.out);
|
|
177
|
+
for (let o = 0; o < lin.out; o++) {
|
|
178
|
+
let s = lin.bias[o];
|
|
179
|
+
for (let j = 0; j < lin.in; j++) s += lin.w[o * lin.in + j] * gap[j];
|
|
180
|
+
e[o] = s;
|
|
181
|
+
}
|
|
182
|
+
return e;
|
|
183
|
+
}
|
|
184
|
+
function unit(e) {
|
|
185
|
+
let n = 0;
|
|
186
|
+
for (let i = 0; i < e.length; i++) n += e[i] * e[i];
|
|
187
|
+
n = 1 / (Math.sqrt(n) + 1e-9);
|
|
188
|
+
const out = new Float32Array(e.length);
|
|
189
|
+
for (let i = 0; i < e.length; i++) out[i] = e[i] * n;
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
function logits(head, e) {
|
|
193
|
+
const out = new Float32Array(head.out);
|
|
194
|
+
for (let o = 0; o < head.out; o++) {
|
|
195
|
+
let s = head.bias[o];
|
|
196
|
+
for (let j = 0; j < head.in; j++) {
|
|
197
|
+
const v = e[j];
|
|
198
|
+
if (v > 0) s += head.w[o * head.in + j] * v;
|
|
199
|
+
}
|
|
200
|
+
out[o] = s;
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
function softmax(z) {
|
|
205
|
+
let m = -Infinity;
|
|
206
|
+
for (const v of z) if (v > m) m = v;
|
|
207
|
+
const out = new Float32Array(z.length);
|
|
208
|
+
let s = 0;
|
|
209
|
+
for (let i = 0; i < z.length; i++) {
|
|
210
|
+
out[i] = Math.exp(z[i] - m);
|
|
211
|
+
s += out[i];
|
|
212
|
+
}
|
|
213
|
+
for (let i = 0; i < z.length; i++) out[i] /= s;
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
function drawStrokes(ctx, strokes, box = 120, lw = 7, pad = 10) {
|
|
217
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
218
|
+
for (const s of strokes) for (const p of s) {
|
|
219
|
+
if (p.x < minX) minX = p.x;
|
|
220
|
+
if (p.x > maxX) maxX = p.x;
|
|
221
|
+
if (p.y < minY) minY = p.y;
|
|
222
|
+
if (p.y > maxY) maxY = p.y;
|
|
223
|
+
}
|
|
224
|
+
const sc = box / Math.max(maxX - minX, maxY - minY, 1);
|
|
225
|
+
const W = box + 2 * pad;
|
|
226
|
+
ctx.fillStyle = "#fff";
|
|
227
|
+
ctx.fillRect(0, 0, W, W);
|
|
228
|
+
ctx.strokeStyle = "#000";
|
|
229
|
+
ctx.lineWidth = lw;
|
|
230
|
+
ctx.lineCap = "round";
|
|
231
|
+
ctx.lineJoin = "round";
|
|
232
|
+
for (const s of strokes) {
|
|
233
|
+
if (s.length < 2) continue;
|
|
234
|
+
ctx.beginPath();
|
|
235
|
+
ctx.moveTo((s[0].x - minX) * sc + pad, (s[0].y - minY) * sc + pad);
|
|
236
|
+
for (let i = 1; i < s.length; i++) ctx.lineTo((s[i].x - minX) * sc + pad, (s[i].y - minY) * sc + pad);
|
|
237
|
+
ctx.stroke();
|
|
238
|
+
}
|
|
239
|
+
return W;
|
|
240
|
+
}
|
|
241
|
+
function inkPlane(data, W, H) {
|
|
242
|
+
const ink = new Float32Array(W * H);
|
|
243
|
+
for (let i = 0, p = 0; i < ink.length; i++, p += 4) ink[i] = 1 - (data[p] * 0.299 + data[p + 1] * 0.587 + data[p + 2] * 0.114) / 255;
|
|
244
|
+
return ink;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ../../server/public/wordgpu/cnnprep.mjs
|
|
248
|
+
var SIZE = 32;
|
|
249
|
+
var INK = 24;
|
|
250
|
+
function normalizeInk(ink, w, h, size = SIZE, inkBox = INK) {
|
|
251
|
+
let minX = Infinity, maxX = -1, minY = Infinity, maxY = -1;
|
|
252
|
+
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++)
|
|
253
|
+
if (ink[y * w + x] > 0.15) {
|
|
254
|
+
if (x < minX) minX = x;
|
|
255
|
+
if (x > maxX) maxX = x;
|
|
256
|
+
if (y < minY) minY = y;
|
|
257
|
+
if (y > maxY) maxY = y;
|
|
258
|
+
}
|
|
259
|
+
const out = new Float32Array(size * size);
|
|
260
|
+
if (maxX < 0) return out;
|
|
261
|
+
const cw = maxX - minX + 1, ch = maxY - minY + 1;
|
|
262
|
+
const crop = new Float32Array(cw * ch);
|
|
263
|
+
for (let y = 0; y < ch; y++) for (let x = 0; x < cw; x++) crop[y * cw + x] = ink[(y + minY) * w + (x + minX)];
|
|
264
|
+
let peak = 0;
|
|
265
|
+
for (let i = 0; i < crop.length; i++) if (crop[i] > peak) peak = crop[i];
|
|
266
|
+
if (peak > 0.05 && peak < 0.999) for (let i = 0; i < crop.length; i++) crop[i] = Math.min(1, crop[i] / peak);
|
|
267
|
+
const s = inkBox / Math.max(ch, cw);
|
|
268
|
+
const nh = Math.max(1, Math.round(ch * s)), nw = Math.max(1, Math.round(cw * s));
|
|
269
|
+
const r = new Float32Array(nw * nh);
|
|
270
|
+
const fx = cw / nw, fy = ch / nh;
|
|
271
|
+
for (let y = 0; y < nh; y++) {
|
|
272
|
+
const y0 = y * fy, y1 = (y + 1) * fy, iy0 = Math.floor(y0), iy1 = Math.min(ch, Math.ceil(y1));
|
|
273
|
+
for (let x = 0; x < nw; x++) {
|
|
274
|
+
const x0 = x * fx, x1 = (x + 1) * fx, ix0 = Math.floor(x0), ix1 = Math.min(cw, Math.ceil(x1));
|
|
275
|
+
let ss = 0, wsum = 0;
|
|
276
|
+
for (let yy = iy0; yy < iy1; yy++) {
|
|
277
|
+
const wy = Math.min(y1, yy + 1) - Math.max(y0, yy);
|
|
278
|
+
if (wy <= 0) continue;
|
|
279
|
+
for (let xx = ix0; xx < ix1; xx++) {
|
|
280
|
+
const wx = Math.min(x1, xx + 1) - Math.max(x0, xx);
|
|
281
|
+
if (wx <= 0) continue;
|
|
282
|
+
ss += crop[yy * cw + xx] * wx * wy;
|
|
283
|
+
wsum += wx * wy;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
r[y * nw + x] = wsum > 0 ? ss / wsum : 0;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const oy = size - nh >> 1, ox = size - nw >> 1;
|
|
290
|
+
for (let y = 0; y < nh; y++) for (let x = 0; x < nw; x++) out[(y + oy) * size + x + ox] = r[y * nw + x];
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// ../../library/src/assetbase.js
|
|
295
|
+
function assetBaseOf(base) {
|
|
296
|
+
let s = base ? String(base) : "";
|
|
297
|
+
if (!s) return typeof location !== "undefined" ? new URL("./", location.href).href : "";
|
|
298
|
+
if (!s.endsWith("/")) s += "/";
|
|
299
|
+
return typeof location !== "undefined" ? new URL(s, location.href).href : s;
|
|
300
|
+
}
|
|
301
|
+
var asStrokes = (s) => Array.isArray(s) && s.length && !Array.isArray(s[0]) ? [s] : s;
|
|
302
|
+
var notServed = (url2, what, base, recognizer) => new Error(`millirec: ${url2} is not ${what} \u2014 the package files are not served at ${base}; copy node_modules/millirec/dist/${recognizer}/ and pass assetBase (see README "Serving the files")`);
|
|
303
|
+
async function fetchAsset(url2, { as = "json", what, base, recognizer }) {
|
|
304
|
+
const fail = (detail) => {
|
|
305
|
+
const e = notServed(url2, what, base, recognizer);
|
|
306
|
+
if (detail) e.message += ` (${detail})`;
|
|
307
|
+
return e;
|
|
308
|
+
};
|
|
309
|
+
let r;
|
|
310
|
+
try {
|
|
311
|
+
r = await fetch(url2);
|
|
312
|
+
} catch (e) {
|
|
313
|
+
throw fail(e && e.message);
|
|
314
|
+
}
|
|
315
|
+
const type = r.headers && r.headers.get("content-type") || "";
|
|
316
|
+
if (!r.ok) throw fail(`HTTP ${r.status}`);
|
|
317
|
+
if (as === "json") {
|
|
318
|
+
try {
|
|
319
|
+
return await r.json();
|
|
320
|
+
} catch (e) {
|
|
321
|
+
throw fail(/html/i.test(type) ? "an HTML page" : e && e.message);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const buf = await r.arrayBuffer();
|
|
325
|
+
const head = String.fromCharCode(...new Uint8Array(buf, 0, Math.min(buf.byteLength, 16))).trimStart().toLowerCase();
|
|
326
|
+
if (head.startsWith("<!doctype") || head.startsWith("<html")) throw fail("an HTML page");
|
|
327
|
+
return buf;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// ../../library/src/char-cnn-core.js
|
|
331
|
+
function scriptBase() {
|
|
332
|
+
const src = typeof document !== "undefined" && document.currentScript && document.currentScript.src || "";
|
|
333
|
+
return src ? src.slice(0, src.lastIndexOf("/") + 1) : "";
|
|
334
|
+
}
|
|
335
|
+
var ASSET_BASE = scriptBase();
|
|
336
|
+
var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
|
|
337
|
+
var DESTROYED = "millirec: recognizer destroyed";
|
|
338
|
+
var makeCanvas = (n) => typeof document !== "undefined" && document.createElement ? Object.assign(document.createElement("canvas"), { width: n, height: n }) : typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(n, n) : null;
|
|
339
|
+
var MillirecCharCnnCore = class {
|
|
340
|
+
static async create(opts = {}) {
|
|
341
|
+
const g = typeof window !== "undefined" ? window : globalThis;
|
|
342
|
+
const config = { ...g.MILLIREC_CNN || {}, ...opts };
|
|
343
|
+
if (!config.stages || !config.stages.length) throw new Error("MillirecCharCnn: no stages configured");
|
|
344
|
+
if (typeof fetch === "undefined" || typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
|
|
345
|
+
throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");
|
|
346
|
+
}
|
|
347
|
+
const inst = new this(config);
|
|
348
|
+
await inst.ready;
|
|
349
|
+
return inst;
|
|
350
|
+
}
|
|
351
|
+
constructor(config) {
|
|
352
|
+
this.config = config;
|
|
353
|
+
this.base = assetBaseOf(config.assetBase || ASSET_BASE);
|
|
354
|
+
this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
|
|
355
|
+
this.stages = [];
|
|
356
|
+
this.off = makeCanvas(140);
|
|
357
|
+
this.octx = this.off ? this.off.getContext("2d", { willReadFrequently: true }) : null;
|
|
358
|
+
this.ready = this.load();
|
|
359
|
+
}
|
|
360
|
+
async load() {
|
|
361
|
+
const { base, config } = this;
|
|
362
|
+
let factory = null;
|
|
363
|
+
if (config.kernel) {
|
|
364
|
+
const url2 = base + config.kernel;
|
|
365
|
+
try {
|
|
366
|
+
factory = (await import(
|
|
367
|
+
/* @vite-ignore */
|
|
368
|
+
/* webpackIgnore: true */
|
|
369
|
+
url2
|
|
370
|
+
)).default;
|
|
371
|
+
} catch (e) {
|
|
372
|
+
const err = notServed(url2, "the char-cnn kernel", base, "char-cnn");
|
|
373
|
+
err.message += ` (${e && e.message})`;
|
|
374
|
+
throw err;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
for (const st of config.stages) {
|
|
378
|
+
const [modelBuf, headBuf] = await Promise.all([fetchBuf(base + st.model, base), fetchBuf(base + st.head, base)]);
|
|
379
|
+
if (this.destroyed) return;
|
|
380
|
+
const head = parseHead(headBuf);
|
|
381
|
+
const stage = { theta: st.theta == null ? null : st.theta, head, headNorm: st.headNorm !== false, size: 0 };
|
|
382
|
+
if (factory) {
|
|
383
|
+
const M = await factory();
|
|
384
|
+
if (this.destroyed) return;
|
|
385
|
+
const bytes = expandToInt8(new Uint8Array(modelBuf));
|
|
386
|
+
const ptr = M._malloc(bytes.byteLength);
|
|
387
|
+
M.HEAPU8.set(bytes, ptr);
|
|
388
|
+
const loaded = M._cnnLoad(ptr, bytes.byteLength);
|
|
389
|
+
M._free(ptr);
|
|
390
|
+
if (!loaded) throw new Error(`kernel rejected ${st.model} (channel widths must be multiples of four)`);
|
|
391
|
+
const size = M._cnnSize(), dim = M._cnnDim();
|
|
392
|
+
stage.size = size;
|
|
393
|
+
stage.embed = (img) => {
|
|
394
|
+
const ip = M._cnnInputPtr(1) >> 2;
|
|
395
|
+
M.HEAPF32.set(img, ip);
|
|
396
|
+
M._cnnRun(1);
|
|
397
|
+
const o = M._cnnEmbedPtr(1) >> 2;
|
|
398
|
+
return M.HEAPF32.slice(o, o + dim);
|
|
399
|
+
};
|
|
400
|
+
stage.logits = (img) => logits(stage.head, stage.embed(img));
|
|
401
|
+
} else {
|
|
402
|
+
const model = parseModel(modelBuf);
|
|
403
|
+
stage.size = model.size;
|
|
404
|
+
stage.embed = (img) => {
|
|
405
|
+
const e = embed(model, img);
|
|
406
|
+
return stage.headNorm ? unit(e) : e;
|
|
407
|
+
};
|
|
408
|
+
stage.logits = (img) => logits(stage.head, stage.embed(img));
|
|
409
|
+
}
|
|
410
|
+
this.stages.push(stage);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/** drawn strokes ([[{x,y},...],...]) -> the model input raster at `size`.
|
|
414
|
+
* The canvas draw + readback is the dear part (~1 ms) and is shared by every
|
|
415
|
+
* stage of one recognize() call; only the resample differs per size. */
|
|
416
|
+
raster(strokes, size) {
|
|
417
|
+
if (!this.octx) throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");
|
|
418
|
+
if (this._planeFor !== strokes) {
|
|
419
|
+
const W2 = drawStrokes(this.octx, strokes);
|
|
420
|
+
this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
|
|
421
|
+
this._planeFor = strokes;
|
|
422
|
+
}
|
|
423
|
+
const { ink, W } = this._plane;
|
|
424
|
+
return normalizeInk(ink, W, W, size, Math.round(size * 0.75));
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* -> [{char, prob} x3] (best first) with `.stage` = index of the model that decided, or null.
|
|
428
|
+
* `only` (an array or Set of characters) restricts the answer to those: the softmax is taken over them alone, so a box
|
|
429
|
+
* that can hold one of two letters gets the likelier of the two however the rest of the alphabet scores — and the
|
|
430
|
+
* cascade runs to its last stage, since an early exit is judged on the whole alphabet's margin.
|
|
431
|
+
*/
|
|
432
|
+
async recognize(strokes, { only = null } = {}) {
|
|
433
|
+
strokes = asStrokes(strokes);
|
|
434
|
+
if (!strokes || !strokes.length) return null;
|
|
435
|
+
await this.ready;
|
|
436
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
437
|
+
this._planeFor = null;
|
|
438
|
+
const keep = only ? new Set(only) : null;
|
|
439
|
+
let z = null, used = 0;
|
|
440
|
+
for (let s = 0; s < this.stages.length; s++) {
|
|
441
|
+
const st = this.stages[s];
|
|
442
|
+
if ((this.personal || keep) && s < this.stages.length - 1) continue;
|
|
443
|
+
z = st.logits(this.raster(strokes, st.size));
|
|
444
|
+
used = s;
|
|
445
|
+
if (st.theta == null || s === this.stages.length - 1) break;
|
|
446
|
+
let b = -Infinity, b2 = -Infinity;
|
|
447
|
+
for (const v of z) {
|
|
448
|
+
if (v > b) {
|
|
449
|
+
b2 = b;
|
|
450
|
+
b = v;
|
|
451
|
+
} else if (v > b2) b2 = v;
|
|
452
|
+
}
|
|
453
|
+
if (b - b2 >= st.theta) break;
|
|
454
|
+
}
|
|
455
|
+
let idx = [...z.keys()];
|
|
456
|
+
if (keep) {
|
|
457
|
+
idx = idx.filter((c) => keep.has(this.classes[c]));
|
|
458
|
+
if (!idx.length) {
|
|
459
|
+
const out2 = [];
|
|
460
|
+
out2.stage = used;
|
|
461
|
+
return out2;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
const p = softmax(idx.map((c) => z[c]));
|
|
465
|
+
const order = [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3);
|
|
466
|
+
const out = order.map((i) => ({ char: this.classes[idx[i]], prob: p[i] }));
|
|
467
|
+
out.stage = used;
|
|
468
|
+
return out;
|
|
469
|
+
}
|
|
470
|
+
/** The last stage's (unit) embedding of drawn strokes — what the head reads. */
|
|
471
|
+
async embedding(strokes) {
|
|
472
|
+
strokes = asStrokes(strokes);
|
|
473
|
+
await this.ready;
|
|
474
|
+
if (this.destroyed || !this.stages.length) throw new Error(DESTROYED);
|
|
475
|
+
this._planeFor = null;
|
|
476
|
+
const st = this.stages[this.stages.length - 1];
|
|
477
|
+
return st.embed(this.raster(strokes, st.size));
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
|
|
481
|
+
* (`{ 'A': [strokes…], … }`), anchored to the shipped head — the anchor's pull is prior/n, so a few samples barely move
|
|
482
|
+
* a letter and many take it over (Rob, Sep 24 2026: "gradually add and then replace with the player's own"). Plain
|
|
483
|
+
* gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
|
|
484
|
+
* personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
|
|
485
|
+
* Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
|
|
486
|
+
*/
|
|
487
|
+
async personalize(samples, { steps = 250, lr = 0.5, prior = 1 } = {}) {
|
|
488
|
+
await this.ready;
|
|
489
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
490
|
+
const st = this.stages[this.stages.length - 1];
|
|
491
|
+
const H0 = st.head0 || (st.head0 = st.head);
|
|
492
|
+
const X = [], Y = [];
|
|
493
|
+
for (const [cls, list] of Object.entries(samples || {})) {
|
|
494
|
+
const c = this.classes.indexOf(cls);
|
|
495
|
+
if (c < 0) continue;
|
|
496
|
+
for (const drawing of list || []) {
|
|
497
|
+
const strokes = asStrokes(drawing);
|
|
498
|
+
if (!strokes || !strokes.length) continue;
|
|
499
|
+
X.push(await this.embedding(strokes));
|
|
500
|
+
Y.push(c);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (this.destroyed) return null;
|
|
504
|
+
if (!X.length) {
|
|
505
|
+
st.head = H0;
|
|
506
|
+
this.personal = null;
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
const head = { in: H0.in, out: H0.out, w: Float32Array.from(H0.w), bias: Float32Array.from(H0.bias) };
|
|
510
|
+
const n = X.length, gW = new Float32Array(head.w.length), gB = new Float32Array(head.out);
|
|
511
|
+
for (let step = 0; step < steps; step++) {
|
|
512
|
+
gW.fill(0);
|
|
513
|
+
gB.fill(0);
|
|
514
|
+
for (let i = 0; i < n; i++) {
|
|
515
|
+
const p = softmax(logits(head, X[i])), x = X[i];
|
|
516
|
+
for (let o = 0; o < head.out; o++) {
|
|
517
|
+
const g = (p[o] - (o === Y[i] ? 1 : 0)) / n;
|
|
518
|
+
if (!g) continue;
|
|
519
|
+
gB[o] += g;
|
|
520
|
+
const wb = o * head.in;
|
|
521
|
+
for (let j = 0; j < head.in; j++) {
|
|
522
|
+
const v = x[j];
|
|
523
|
+
if (v > 0) gW[wb + j] += g * v;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
const a = prior / n;
|
|
528
|
+
for (let k = 0; k < head.w.length; k++) head.w[k] -= lr * (gW[k] + a * (head.w[k] - H0.w[k]));
|
|
529
|
+
for (let o = 0; o < head.out; o++) head.bias[o] -= lr * (gB[o] + a * (head.bias[o] - H0.bias[o]));
|
|
530
|
+
}
|
|
531
|
+
st.head = head;
|
|
532
|
+
this.personal = { n, classes: [...new Set(Y)].map((c) => this.classes[c]).sort() };
|
|
533
|
+
return this.personal;
|
|
534
|
+
}
|
|
535
|
+
/** Let go of the models and kernels; recognize() and personalize() return null afterwards, embedding() throws. */
|
|
536
|
+
destroy() {
|
|
537
|
+
this.destroyed = true;
|
|
538
|
+
this.stages = [];
|
|
539
|
+
this.personal = null;
|
|
540
|
+
this._plane = null;
|
|
541
|
+
this._planeFor = null;
|
|
542
|
+
}
|
|
543
|
+
depersonalize() {
|
|
544
|
+
const st = this.stages[this.stages.length - 1];
|
|
545
|
+
if (st && st.head0) st.head = st.head0;
|
|
546
|
+
this.personal = null;
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
if (typeof window !== "undefined" && false) window.MillirecCharCnn = MillirecCharCnnCore;
|
|
550
|
+
|
|
551
|
+
// ../../widget/scribble.js
|
|
552
|
+
var SCRIBBLE = {
|
|
553
|
+
turns: 5,
|
|
554
|
+
// reversals along the principal axis (no letter reaches 6; the scribble reaches 8)
|
|
555
|
+
retrace: 0.5,
|
|
556
|
+
// share of the path laid back over its own ground (letters <= 0.54 at ANY turn count)
|
|
557
|
+
winding: 3.5,
|
|
558
|
+
// path length / bbox diagonal (letters at 5 turns reach 3.29; the scribble 6.14)
|
|
559
|
+
cover: 0.6,
|
|
560
|
+
// how completely this stroke and the existing ink overlap, per axis
|
|
561
|
+
minLength: 40,
|
|
562
|
+
// px of path — a small wiggle is not a scribble
|
|
563
|
+
minSpan: 12,
|
|
564
|
+
// px of bbox diagonal — nor is a dense scrub of one point
|
|
565
|
+
deadband: 0.22
|
|
566
|
+
// a reversal must retrace this much of the axis's extent
|
|
567
|
+
};
|
|
568
|
+
var INK_HALF = 2;
|
|
569
|
+
function grow(b) {
|
|
570
|
+
return { x0: b.x0 - INK_HALF, y0: b.y0 - INK_HALF, x1: b.x1 + INK_HALF, y1: b.y1 + INK_HALF, w: b.w + 2 * INK_HALF, h: b.h + 2 * INK_HALF };
|
|
571
|
+
}
|
|
572
|
+
function bbox(pts) {
|
|
573
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
574
|
+
for (const p of pts) {
|
|
575
|
+
const x = p.x !== void 0 ? p.x : p[0], y = p.y !== void 0 ? p.y : p[1];
|
|
576
|
+
if (x < x0) x0 = x;
|
|
577
|
+
if (x > x1) x1 = x;
|
|
578
|
+
if (y < y0) y0 = y;
|
|
579
|
+
if (y > y1) y1 = y;
|
|
580
|
+
}
|
|
581
|
+
return { x0, y0, x1, y1, w: x1 - x0, h: y1 - y0 };
|
|
582
|
+
}
|
|
583
|
+
function turnsAlong(vals, deadband) {
|
|
584
|
+
let lo = Infinity, hi = -Infinity;
|
|
585
|
+
for (const v of vals) {
|
|
586
|
+
if (v < lo) lo = v;
|
|
587
|
+
if (v > hi) hi = v;
|
|
588
|
+
}
|
|
589
|
+
const band = (hi - lo) * deadband;
|
|
590
|
+
if (!(band > 0)) return 0;
|
|
591
|
+
let dir = 0, ext = vals[0], turns = 0;
|
|
592
|
+
for (const v of vals) {
|
|
593
|
+
if (dir > 0) {
|
|
594
|
+
if (v > ext) ext = v;
|
|
595
|
+
else if (v < ext - band) {
|
|
596
|
+
turns++;
|
|
597
|
+
dir = -1;
|
|
598
|
+
ext = v;
|
|
599
|
+
}
|
|
600
|
+
} else if (dir < 0) {
|
|
601
|
+
if (v < ext) ext = v;
|
|
602
|
+
else if (v > ext + band) {
|
|
603
|
+
turns++;
|
|
604
|
+
dir = 1;
|
|
605
|
+
ext = v;
|
|
606
|
+
}
|
|
607
|
+
} else if (v > ext + band) {
|
|
608
|
+
dir = 1;
|
|
609
|
+
ext = v;
|
|
610
|
+
} else if (v < ext - band) {
|
|
611
|
+
dir = -1;
|
|
612
|
+
ext = v;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
return turns;
|
|
616
|
+
}
|
|
617
|
+
function axes(pts) {
|
|
618
|
+
let mx = 0, my = 0;
|
|
619
|
+
for (const p of pts) {
|
|
620
|
+
mx += p.x;
|
|
621
|
+
my += p.y;
|
|
622
|
+
}
|
|
623
|
+
mx /= pts.length;
|
|
624
|
+
my /= pts.length;
|
|
625
|
+
let sxx = 0, sxy = 0, syy = 0;
|
|
626
|
+
for (const p of pts) {
|
|
627
|
+
const dx = p.x - mx, dy = p.y - my;
|
|
628
|
+
sxx += dx * dx;
|
|
629
|
+
sxy += dx * dy;
|
|
630
|
+
syy += dy * dy;
|
|
631
|
+
}
|
|
632
|
+
const tr = sxx + syy, det = sxx * syy - sxy * sxy;
|
|
633
|
+
const l = tr / 2 + Math.sqrt(Math.max(0, tr * tr / 4 - det));
|
|
634
|
+
let ux = sxy, uy = l - sxx;
|
|
635
|
+
if (Math.hypot(ux, uy) < 1e-9) {
|
|
636
|
+
ux = 1;
|
|
637
|
+
uy = 0;
|
|
638
|
+
}
|
|
639
|
+
const n = Math.hypot(ux, uy);
|
|
640
|
+
return { ux: ux / n, uy: uy / n };
|
|
641
|
+
}
|
|
642
|
+
var RETRACE_R = 0.09;
|
|
643
|
+
var RETRACE_SKIP = 4;
|
|
644
|
+
function retraceOf(pts, span) {
|
|
645
|
+
const r = Math.max(2, span * RETRACE_R);
|
|
646
|
+
const step = r / 2;
|
|
647
|
+
const rs = [pts[0]];
|
|
648
|
+
let carry = 0;
|
|
649
|
+
for (let i = 1; i < pts.length; i++) {
|
|
650
|
+
const dx = pts[i].x - pts[i - 1].x, dy = pts[i].y - pts[i - 1].y;
|
|
651
|
+
let d = Math.hypot(dx, dy);
|
|
652
|
+
if (!(d > 0)) continue;
|
|
653
|
+
let t = (step - carry) / d;
|
|
654
|
+
while (t <= 1) {
|
|
655
|
+
rs.push({ x: pts[i - 1].x + dx * t, y: pts[i - 1].y + dy * t });
|
|
656
|
+
t += step / d;
|
|
657
|
+
}
|
|
658
|
+
carry = (carry + d) % step;
|
|
659
|
+
}
|
|
660
|
+
if (rs.length < 8) return 0;
|
|
661
|
+
const skip = Math.round(RETRACE_SKIP * r / step);
|
|
662
|
+
const key = (p) => `${Math.floor(p.x / r)},${Math.floor(p.y / r)}`;
|
|
663
|
+
const grid = /* @__PURE__ */ new Map();
|
|
664
|
+
rs.forEach((p, i) => {
|
|
665
|
+
const k = key(p);
|
|
666
|
+
if (!grid.has(k)) grid.set(k, []);
|
|
667
|
+
grid.get(k).push(i);
|
|
668
|
+
});
|
|
669
|
+
let hit = 0;
|
|
670
|
+
for (let i = 0; i < rs.length; i++) {
|
|
671
|
+
const p = rs[i];
|
|
672
|
+
const cx = Math.floor(p.x / r), cy = Math.floor(p.y / r);
|
|
673
|
+
let found = false;
|
|
674
|
+
for (let ax = -1; ax <= 1 && !found; ax++) {
|
|
675
|
+
for (let ay = -1; ay <= 1 && !found; ay++) {
|
|
676
|
+
for (const j of grid.get(`${cx + ax},${cy + ay}`) || []) {
|
|
677
|
+
if (Math.abs(i - j) <= skip) continue;
|
|
678
|
+
if (Math.hypot(rs[j].x - p.x, rs[j].y - p.y) <= r) {
|
|
679
|
+
found = true;
|
|
680
|
+
break;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
if (found) hit++;
|
|
686
|
+
}
|
|
687
|
+
return hit / rs.length;
|
|
688
|
+
}
|
|
689
|
+
function scribbleFeatures(stroke, prior = [], opts = {}) {
|
|
690
|
+
const k = { ...SCRIBBLE, ...opts };
|
|
691
|
+
const pts = [];
|
|
692
|
+
for (const p of stroke) {
|
|
693
|
+
const q = { x: p.x !== void 0 ? p.x : p[0], y: p.y !== void 0 ? p.y : p[1] };
|
|
694
|
+
const last = pts[pts.length - 1];
|
|
695
|
+
if (!last || Math.hypot(q.x - last.x, q.y - last.y) > 0.75) pts.push(q);
|
|
696
|
+
}
|
|
697
|
+
if (pts.length < 4) return { turns: 0, cross: 0, retrace: 0, winding: 0, cover: 0, length: 0, span: 0 };
|
|
698
|
+
let length = 0;
|
|
699
|
+
for (let i = 1; i < pts.length; i++) length += Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y);
|
|
700
|
+
const b = bbox(pts);
|
|
701
|
+
const span = Math.hypot(b.w, b.h);
|
|
702
|
+
const { ux, uy } = axes(pts);
|
|
703
|
+
const major = pts.map((p) => p.x * ux + p.y * uy);
|
|
704
|
+
const minor = pts.map((p) => -p.x * uy + p.y * ux);
|
|
705
|
+
const turns = turnsAlong(major, k.deadband);
|
|
706
|
+
const cross = turnsAlong(minor, k.deadband);
|
|
707
|
+
let cover = 0;
|
|
708
|
+
const flat = [];
|
|
709
|
+
for (const st of prior) for (const p of st) flat.push(p);
|
|
710
|
+
if (flat.length) {
|
|
711
|
+
const a = grow(bbox(flat));
|
|
712
|
+
const c = grow(b);
|
|
713
|
+
const ox = Math.max(0, Math.min(a.x1, c.x1) - Math.max(a.x0, c.x0));
|
|
714
|
+
const oy = Math.max(0, Math.min(a.y1, c.y1) - Math.max(a.y0, c.y0));
|
|
715
|
+
cover = ox / Math.min(a.w, c.w) * (oy / Math.min(a.h, c.h));
|
|
716
|
+
}
|
|
717
|
+
return { turns, cross, retrace: retraceOf(pts, span), winding: span > 0 ? length / span : 0, cover, length, span };
|
|
718
|
+
}
|
|
719
|
+
function isScribble(stroke, prior = [], opts = {}) {
|
|
720
|
+
const k = { ...SCRIBBLE, ...opts };
|
|
721
|
+
if (!prior || !prior.length) return false;
|
|
722
|
+
const f = scribbleFeatures(stroke, prior, k);
|
|
723
|
+
return f.length >= k.minLength && f.span >= k.minSpan && f.turns >= k.turns && f.retrace >= k.retrace && f.winding >= k.winding && f.cover >= k.cover;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// ../../widget/canvas.js
|
|
727
|
+
var ERASE_RADIUS = 12;
|
|
728
|
+
var now = () => typeof performance !== "undefined" && performance.now ? performance.now() : Date.now();
|
|
729
|
+
function createDrawCanvas({
|
|
730
|
+
width = 160,
|
|
731
|
+
height = 90,
|
|
732
|
+
ink = "#111827",
|
|
733
|
+
onStrokeEnd,
|
|
734
|
+
colorOf = null,
|
|
735
|
+
idleOverwrite = 0,
|
|
736
|
+
scribbleClear = false,
|
|
737
|
+
scribble = null,
|
|
738
|
+
onClear = null,
|
|
739
|
+
staleAlpha = 0.3,
|
|
740
|
+
onStale = null
|
|
741
|
+
}) {
|
|
742
|
+
const canvas = document.createElement("canvas");
|
|
743
|
+
const dpr = typeof devicePixelRatio !== "undefined" && devicePixelRatio || 1;
|
|
744
|
+
canvas.width = width * dpr;
|
|
745
|
+
canvas.height = height * dpr;
|
|
746
|
+
canvas.style.width = width + "px";
|
|
747
|
+
canvas.style.height = height + "px";
|
|
748
|
+
canvas.style.touchAction = "none";
|
|
749
|
+
const ctx = canvas.getContext("2d");
|
|
750
|
+
ctx.scale(dpr, dpr);
|
|
751
|
+
let strokes = [];
|
|
752
|
+
let current = [];
|
|
753
|
+
let activeId = null;
|
|
754
|
+
let erasing = false;
|
|
755
|
+
let nextId = 1;
|
|
756
|
+
let lastEndAt = 0;
|
|
757
|
+
let staleTimer = 0;
|
|
758
|
+
let stale = false;
|
|
759
|
+
let dropped = null;
|
|
760
|
+
let droppedPending = false;
|
|
761
|
+
function paint(st, color) {
|
|
762
|
+
if (st.length < 2) return;
|
|
763
|
+
ctx.strokeStyle = color || ink;
|
|
764
|
+
ctx.beginPath();
|
|
765
|
+
ctx.moveTo(st[0].x, st[0].y);
|
|
766
|
+
for (let i = 1; i < st.length; i++) ctx.lineTo(st[i].x, st[i].y);
|
|
767
|
+
ctx.stroke();
|
|
768
|
+
}
|
|
769
|
+
function render() {
|
|
770
|
+
ctx.clearRect(0, 0, width, height);
|
|
771
|
+
ctx.lineWidth = 2.5;
|
|
772
|
+
ctx.lineCap = "round";
|
|
773
|
+
ctx.lineJoin = "round";
|
|
774
|
+
ctx.globalAlpha = stale ? staleAlpha : 1;
|
|
775
|
+
for (const st of strokes) paint(st, colorOf && colorOf(st));
|
|
776
|
+
ctx.globalAlpha = 1;
|
|
777
|
+
paint(current, null);
|
|
778
|
+
}
|
|
779
|
+
function setStale(v) {
|
|
780
|
+
if (stale === v) return;
|
|
781
|
+
stale = v;
|
|
782
|
+
render();
|
|
783
|
+
if (onStale) onStale(v);
|
|
784
|
+
}
|
|
785
|
+
function armStale() {
|
|
786
|
+
clearTimeout(staleTimer);
|
|
787
|
+
staleTimer = 0;
|
|
788
|
+
const ms = (Number(idleOverwrite) || 0) * 1e3;
|
|
789
|
+
if (!ms || !strokes.length) {
|
|
790
|
+
setStale(false);
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
const left = ms - (now() - lastEndAt);
|
|
794
|
+
if (left <= 0) {
|
|
795
|
+
setStale(true);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
setStale(false);
|
|
799
|
+
staleTimer = setTimeout(() => setStale(true), left);
|
|
800
|
+
}
|
|
801
|
+
function dropAll(reason) {
|
|
802
|
+
if (!strokes.length) return;
|
|
803
|
+
dropped = strokes;
|
|
804
|
+
strokes = [];
|
|
805
|
+
clearTimeout(staleTimer);
|
|
806
|
+
staleTimer = 0;
|
|
807
|
+
setStale(false);
|
|
808
|
+
render();
|
|
809
|
+
if (onClear) onClear(reason);
|
|
810
|
+
}
|
|
811
|
+
function eraseAt(x, y) {
|
|
812
|
+
const kept = strokes.filter((st) => !st.some((p) => Math.hypot(p.x - x, p.y - y) <= ERASE_RADIUS));
|
|
813
|
+
if (kept.length === strokes.length) return false;
|
|
814
|
+
strokes = kept;
|
|
815
|
+
render();
|
|
816
|
+
return true;
|
|
817
|
+
}
|
|
818
|
+
let rect = null;
|
|
819
|
+
const getPos = (e) => {
|
|
820
|
+
const r = rect || (rect = canvas.getBoundingClientRect());
|
|
821
|
+
const sx = width / (canvas.clientWidth || width), sy = height / (canvas.clientHeight || height);
|
|
822
|
+
return { x: (e.clientX - r.left - canvas.clientLeft) * sx, y: (e.clientY - r.top - canvas.clientTop) * sy, p: e.pressure || 0.5 };
|
|
823
|
+
};
|
|
824
|
+
let erasedAny = false;
|
|
825
|
+
let activeType = null;
|
|
826
|
+
const swallow = (e) => e.preventDefault();
|
|
827
|
+
canvas.addEventListener("touchstart", swallow, { passive: false });
|
|
828
|
+
canvas.addEventListener("touchmove", swallow, { passive: false });
|
|
829
|
+
canvas.addEventListener("contextmenu", swallow);
|
|
830
|
+
canvas.style.userSelect = canvas.style.webkitUserSelect = "none";
|
|
831
|
+
canvas.style.webkitTouchCallout = "none";
|
|
832
|
+
canvas.addEventListener("pointerdown", (e) => {
|
|
833
|
+
if (e.button > 0) return;
|
|
834
|
+
if (activeId !== null && activeId !== e.pointerId) {
|
|
835
|
+
if (activeType === "pen" && e.pointerType !== "pen") return;
|
|
836
|
+
finish({ pointerId: activeId });
|
|
837
|
+
}
|
|
838
|
+
e.preventDefault();
|
|
839
|
+
activeId = e.pointerId;
|
|
840
|
+
activeType = e.pointerType;
|
|
841
|
+
rect = canvas.getBoundingClientRect();
|
|
842
|
+
try {
|
|
843
|
+
canvas.setPointerCapture(e.pointerId);
|
|
844
|
+
} catch {
|
|
845
|
+
}
|
|
846
|
+
const pos = getPos(e);
|
|
847
|
+
if (erasing) {
|
|
848
|
+
erasedAny = eraseAt(pos.x, pos.y);
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
if (stale && strokes.length) {
|
|
852
|
+
dropAll("idle");
|
|
853
|
+
droppedPending = true;
|
|
854
|
+
}
|
|
855
|
+
current = [pos];
|
|
856
|
+
render();
|
|
857
|
+
}, { passive: false });
|
|
858
|
+
canvas.addEventListener("pointermove", (e) => {
|
|
859
|
+
if (activeId !== e.pointerId) return;
|
|
860
|
+
e.preventDefault();
|
|
861
|
+
const co = e.getCoalescedEvents ? e.getCoalescedEvents() : null;
|
|
862
|
+
const events = co && co.length ? co : [e];
|
|
863
|
+
if (erasing) {
|
|
864
|
+
for (const ev of events) {
|
|
865
|
+
const p = getPos(ev);
|
|
866
|
+
if (eraseAt(p.x, p.y)) erasedAny = true;
|
|
867
|
+
}
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
for (const ev of events) current.push(getPos(ev));
|
|
871
|
+
render();
|
|
872
|
+
}, { passive: false });
|
|
873
|
+
const finish = (e, cancelled = false) => {
|
|
874
|
+
if (activeId !== e.pointerId) return;
|
|
875
|
+
try {
|
|
876
|
+
canvas.releasePointerCapture(e.pointerId);
|
|
877
|
+
} catch {
|
|
878
|
+
}
|
|
879
|
+
activeId = null;
|
|
880
|
+
activeType = null;
|
|
881
|
+
rect = null;
|
|
882
|
+
if (erasing) {
|
|
883
|
+
if (erasedAny) {
|
|
884
|
+
lastEndAt = now();
|
|
885
|
+
armStale();
|
|
886
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
887
|
+
}
|
|
888
|
+
erasedAny = false;
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
if (current.length > 0) {
|
|
892
|
+
let st = current;
|
|
893
|
+
if (st.length === 1) {
|
|
894
|
+
if (cancelled) {
|
|
895
|
+
current = [];
|
|
896
|
+
render();
|
|
897
|
+
if (droppedPending) {
|
|
898
|
+
droppedPending = false;
|
|
899
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
900
|
+
}
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
const p = st[0];
|
|
904
|
+
st = [{ x: p.x - 1, y: p.y, p: p.p }, { x: p.x + 1, y: p.y, p: p.p }];
|
|
905
|
+
}
|
|
906
|
+
const aimedAt = strokes.length ? strokes : droppedPending ? dropped : null;
|
|
907
|
+
if (scribbleClear && aimedAt && aimedAt.length && isScribble(st, aimedAt, scribble || {})) {
|
|
908
|
+
current = [];
|
|
909
|
+
lastEndAt = now();
|
|
910
|
+
droppedPending = false;
|
|
911
|
+
if (strokes.length) dropAll("scribble");
|
|
912
|
+
else {
|
|
913
|
+
render();
|
|
914
|
+
if (onClear) onClear("scribble");
|
|
915
|
+
}
|
|
916
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
st.id = nextId++;
|
|
920
|
+
strokes = [...strokes, st];
|
|
921
|
+
current = [];
|
|
922
|
+
droppedPending = false;
|
|
923
|
+
lastEndAt = now();
|
|
924
|
+
armStale();
|
|
925
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
926
|
+
}
|
|
927
|
+
render();
|
|
928
|
+
};
|
|
929
|
+
canvas.addEventListener("pointerup", (e) => finish(e));
|
|
930
|
+
canvas.addEventListener("pointercancel", (e) => finish(e, true));
|
|
931
|
+
return {
|
|
932
|
+
el: canvas,
|
|
933
|
+
getStrokes: () => strokes,
|
|
934
|
+
setInk(color) {
|
|
935
|
+
ink = color;
|
|
936
|
+
render();
|
|
937
|
+
},
|
|
938
|
+
setErasing(on) {
|
|
939
|
+
erasing = !!on;
|
|
940
|
+
canvas.style.cursor = erasing ? "cell" : "crosshair";
|
|
941
|
+
},
|
|
942
|
+
isErasing: () => erasing,
|
|
943
|
+
repaint: render,
|
|
944
|
+
/** True once the idle window has passed: the next stroke starts fresh. */
|
|
945
|
+
isStale: () => stale,
|
|
946
|
+
/**
|
|
947
|
+
* Put back what the pad cleared BY ITSELF — an idle overwrite or a
|
|
948
|
+
* scribble. An explicit `clear()` is not stashed: a caller clears the pad
|
|
949
|
+
* because it has banked the drawing, and restoring that would hand back a
|
|
950
|
+
* sample the writer already dealt with.
|
|
951
|
+
*/
|
|
952
|
+
undoClear() {
|
|
953
|
+
if (!dropped || !dropped.length) return false;
|
|
954
|
+
strokes = strokes.length ? [...dropped, ...strokes] : dropped;
|
|
955
|
+
dropped = null;
|
|
956
|
+
lastEndAt = now();
|
|
957
|
+
armStale();
|
|
958
|
+
render();
|
|
959
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
960
|
+
return true;
|
|
961
|
+
},
|
|
962
|
+
/** Change either overwrite behaviour, or the settled look, at runtime. */
|
|
963
|
+
configure(opts = {}) {
|
|
964
|
+
if ("idleOverwrite" in opts) idleOverwrite = Number(opts.idleOverwrite) || 0;
|
|
965
|
+
if ("scribbleClear" in opts) scribbleClear = !!opts.scribbleClear;
|
|
966
|
+
if ("scribble" in opts) scribble = opts.scribble;
|
|
967
|
+
if ("staleAlpha" in opts) staleAlpha = Number(opts.staleAlpha);
|
|
968
|
+
armStale();
|
|
969
|
+
},
|
|
970
|
+
clear() {
|
|
971
|
+
strokes = [];
|
|
972
|
+
current = [];
|
|
973
|
+
activeId = null;
|
|
974
|
+
activeType = null;
|
|
975
|
+
rect = null;
|
|
976
|
+
clearTimeout(staleTimer);
|
|
977
|
+
staleTimer = 0;
|
|
978
|
+
setStale(false);
|
|
979
|
+
droppedPending = false;
|
|
980
|
+
render();
|
|
981
|
+
if (onStrokeEnd) onStrokeEnd(strokes);
|
|
982
|
+
}
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
// ../../library/src/char-cnn.js
|
|
987
|
+
var MillirecCharCnn = class extends MillirecCharCnnCore {
|
|
988
|
+
/**
|
|
989
|
+
* Mount a drawing pad in `el`; fires 'millirec:result' ({results, strokes})
|
|
990
|
+
* after each stroke, and 'millirec:clear' ({reason, undo}) when the pad
|
|
991
|
+
* drops its own ink.
|
|
992
|
+
*
|
|
993
|
+
* The pad reads ONE character, so by default writing in it after a pause
|
|
994
|
+
* starts a fresh one (`idleOverwrite`, seconds — 0 turns it off) and
|
|
995
|
+
* scribbling over it throws it away (`scribbleClear`). Both are measured:
|
|
996
|
+
* see widget/canvas.js and scripts/verify-scribble.mjs.
|
|
997
|
+
*/
|
|
998
|
+
attach(el, { width = 240, height = 240, ink = "#1e293b", idleOverwrite = 2, scribbleClear = true, scribble = null, staleAlpha, onStale } = {}) {
|
|
999
|
+
const pad = createDrawCanvas({
|
|
1000
|
+
width,
|
|
1001
|
+
height,
|
|
1002
|
+
ink,
|
|
1003
|
+
idleOverwrite,
|
|
1004
|
+
scribbleClear,
|
|
1005
|
+
scribble,
|
|
1006
|
+
onStale,
|
|
1007
|
+
...staleAlpha === void 0 ? {} : { staleAlpha },
|
|
1008
|
+
onClear: (reason) => el.dispatchEvent(new CustomEvent("millirec:clear", { detail: { reason, undo: () => pad.undoClear() } })),
|
|
1009
|
+
onStrokeEnd: async (strokes) => {
|
|
1010
|
+
if (!strokes.length) {
|
|
1011
|
+
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results: [], strokes: [] } }));
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
const results = await this.recognize(strokes);
|
|
1015
|
+
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results, strokes } }));
|
|
1016
|
+
}
|
|
1017
|
+
});
|
|
1018
|
+
el.appendChild(pad.el);
|
|
1019
|
+
return { clear: pad.clear, undoClear: pad.undoClear, isStale: pad.isStale, configure: pad.configure, setInk: pad.setInk, repaint: pad.repaint, getStrokes: pad.getStrokes, canvas: pad.el };
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
if (typeof window !== "undefined" && false) window.MillirecCharCnn = MillirecCharCnn;
|
|
1023
|
+
|
|
1024
|
+
// src/here.js
|
|
1025
|
+
var url = (import.meta.url || "").replace(/[?#].*$/, "");
|
|
1026
|
+
var HERE = url.slice(0, url.lastIndexOf("/") + 1);
|
|
1027
|
+
function nodeFileUrl(url2, spec) {
|
|
1028
|
+
const g = globalThis;
|
|
1029
|
+
const node = typeof g.process !== "undefined" && g.process.versions && g.process.versions.node && typeof g.Deno === "undefined" && typeof g.Bun === "undefined";
|
|
1030
|
+
if (url2.startsWith("file:") && node && typeof document === "undefined" && typeof WorkerGlobalScope === "undefined") {
|
|
1031
|
+
throw new Error(`millirec: cannot fetch ${url2} outside a browser \u2014 in Node, import the JSON instead: import x from '${spec}' with { type: 'json' }`);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
// src/options.js
|
|
1036
|
+
var OPTIONS = ["accurate", "fast", "small"];
|
|
1037
|
+
async function loadOption(name, { assetBase = HERE } = {}) {
|
|
1038
|
+
if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
|
|
1039
|
+
const base = assetBaseOf(assetBase);
|
|
1040
|
+
const url2 = new URL(`options/${name}.json`, base).href;
|
|
1041
|
+
nodeFileUrl(url2, `millirec/char-cnn/options/${name}.json`);
|
|
1042
|
+
return fetchAsset(url2, { what: `the ${name} CNN option`, base, recognizer: "char-cnn" });
|
|
1043
|
+
}
|
|
1044
|
+
async function withOption(opts) {
|
|
1045
|
+
if (typeof opts === "string") opts = { option: opts };
|
|
1046
|
+
opts = opts || {};
|
|
1047
|
+
if (typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
|
|
1048
|
+
throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");
|
|
1049
|
+
}
|
|
1050
|
+
const assetBase = assetBaseOf(opts.assetBase || HERE);
|
|
1051
|
+
const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
|
|
1052
|
+
return { ...preset, ...opts, assetBase };
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// src/char-cnn.js
|
|
1056
|
+
var MillirecCharCnn2 = class extends MillirecCharCnn {
|
|
1057
|
+
/** opts: option ('accurate' | 'fast' | 'small', default 'accurate') or your own
|
|
1058
|
+
* { classes, stages, kernel }, plus assetBase (default: this module's folder).
|
|
1059
|
+
* A string is the option's name: create('fast') = create({ option: 'fast' }). */
|
|
1060
|
+
static async create(opts = {}) {
|
|
1061
|
+
return super.create(await withOption(opts));
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
export {
|
|
1065
|
+
MillirecCharCnn2 as MillirecCharCnn,
|
|
1066
|
+
OPTIONS,
|
|
1067
|
+
loadOption
|
|
1068
|
+
};
|