millirec 0.1.2 → 0.2.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.
Files changed (76) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +171 -58
  3. package/dist/char-cnn/cnn-web.wasm +0 -0
  4. package/dist/char-cnn/core.d.ts +3 -1
  5. package/dist/char-cnn/core.js +145 -99
  6. package/dist/char-cnn/index.d.ts +1 -1
  7. package/dist/char-cnn/index.js +198 -136
  8. package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -1
  9. package/dist/char-cnn/millirec-char-cnn.min.js +1 -1
  10. package/dist/char-cnn/options/accurate/tiny.head.bin +0 -0
  11. package/dist/char-cnn/options/accurate.js +2 -2
  12. package/dist/char-cnn/options/accurate.json +1 -1
  13. package/dist/char-cnn/options/all/all-base.head.bin +0 -0
  14. package/dist/char-cnn/options/all/all-base.q4.bin +0 -0
  15. package/dist/char-cnn/options/all/all-tiny.bin +0 -0
  16. package/dist/char-cnn/options/all/all-tiny.head.bin +0 -0
  17. package/dist/char-cnn/options/all-fast/all-base-p25.bin +0 -0
  18. package/dist/char-cnn/options/all-fast/all-base-p25.head.bin +0 -0
  19. package/dist/char-cnn/options/all-fast/all-tiny.bin +0 -0
  20. package/dist/char-cnn/options/all-fast/all-tiny.head.bin +0 -0
  21. package/dist/char-cnn/options/all-fast.js +17 -0
  22. package/dist/char-cnn/options/all-fast.json +15 -0
  23. package/dist/char-cnn/options/all-small/all-tiny-qat4.head.bin +0 -0
  24. package/dist/char-cnn/options/all-small/all-tiny-qat4.q4.bin +0 -0
  25. package/dist/char-cnn/options/all-small.js +11 -0
  26. package/dist/char-cnn/options/all-small.json +9 -0
  27. package/dist/char-cnn/options/all.js +17 -0
  28. package/dist/char-cnn/options/all.json +15 -0
  29. package/dist/char-cnn/options/digits/digits-base.head.bin +0 -0
  30. package/dist/char-cnn/options/digits/digits-tiny.head.bin +0 -0
  31. package/dist/char-cnn/options/digits-fast/digits-base-p25.bin +0 -0
  32. package/dist/char-cnn/options/digits-fast/digits-base-p25.head.bin +0 -0
  33. package/dist/char-cnn/options/digits-fast/digits-tiny.bin +0 -0
  34. package/dist/char-cnn/options/digits-fast/digits-tiny.head.bin +0 -0
  35. package/dist/char-cnn/options/digits-fast.js +17 -0
  36. package/dist/char-cnn/options/digits-fast.json +15 -0
  37. package/dist/char-cnn/options/digits-small/digits-tiny-qat4.head.bin +0 -0
  38. package/dist/char-cnn/options/digits-small/digits-tiny-qat4.q4.bin +0 -0
  39. package/dist/char-cnn/options/digits-small.js +11 -0
  40. package/dist/char-cnn/options/digits-small.json +9 -0
  41. package/dist/char-cnn/options/digits.js +2 -2
  42. package/dist/char-cnn/options/digits.json +1 -1
  43. package/dist/char-cnn/options/fast/base-p25.head.bin +0 -0
  44. package/dist/char-cnn/options/fast/tiny.head.bin +0 -0
  45. package/dist/char-cnn/options/fast.js +2 -2
  46. package/dist/char-cnn/options/fast.json +1 -1
  47. package/dist/char-cnn/options/lowercase/lowercase-tiny.head.bin +0 -0
  48. package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.bin +0 -0
  49. package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.head.bin +0 -0
  50. package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.bin +0 -0
  51. package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.head.bin +0 -0
  52. package/dist/char-cnn/options/lowercase-fast.js +17 -0
  53. package/dist/char-cnn/options/lowercase-fast.json +15 -0
  54. package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.head.bin +0 -0
  55. package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.q4.bin +0 -0
  56. package/dist/char-cnn/options/lowercase-small.js +11 -0
  57. package/dist/char-cnn/options/lowercase-small.json +9 -0
  58. package/dist/char-cnn/options/lowercase.js +2 -2
  59. package/dist/char-cnn/options/lowercase.json +1 -1
  60. package/dist/char-cnn/options/small/tiny.head.bin +0 -0
  61. package/dist/char-cnn/options/small/tiny.q4.bin +0 -0
  62. package/dist/char-cnn/options/small.js +2 -2
  63. package/dist/char-cnn/options/small.json +1 -1
  64. package/dist/char-lite/core.d.ts +1 -0
  65. package/dist/char-lite/core.js +628 -0
  66. package/dist/char-lite/index.d.ts +28 -8
  67. package/dist/char-lite/index.js +845 -717
  68. package/dist/char-lite/millirec-char-lite.min.js +1 -1
  69. package/dist/char-lite/sets.json +17 -17
  70. package/dist/index.d.ts +35 -0
  71. package/dist/index.js +88 -0
  72. package/dist/overlap/millirec-overlap.min.js +7 -7
  73. package/dist/pad/index.d.ts +8 -0
  74. package/dist/pad/index.js +482 -0
  75. package/package.json +21 -7
  76. package/dist/char-cnn/options/small/tiny.bin +0 -0
@@ -114,69 +114,102 @@ function parseHead(buf) {
114
114
  return readLinear(reader(buf));
115
115
  }
116
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;
117
+ const { cout, cin, w, bias } = c;
118
+ const plane = res * res, rm1 = res - 1;
119
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
- }
120
+ const ob = co * plane, b = bias[co], wb = co * cin * 9;
121
+ out.fill(b, ob, ob + plane);
122
+ for (let ci = 0; ci < cin; ci++) {
123
+ const ib = ci * plane, wc = wb + ci * 9;
124
+ const w0 = w[wc], w1 = w[wc + 1], w2 = w[wc + 2];
125
+ const w3 = w[wc + 3], w4 = w[wc + 4], w5 = w[wc + 5];
126
+ const w6 = w[wc + 6], w7 = w[wc + 7], w8 = w[wc + 8];
127
+ for (let y = 1; y < rm1; y++) {
128
+ const r0 = ib + (y - 1) * res, r1 = ib + y * res, r2 = ib + (y + 1) * res, orow = ob + y * res;
129
+ out[orow] += w1 * inp[r0] + w2 * inp[r0 + 1] + w4 * inp[r1] + w5 * inp[r1 + 1] + w7 * inp[r2] + w8 * inp[r2 + 1];
130
+ for (let x = 1; x < rm1; x++) {
131
+ out[orow + x] += w0 * inp[r0 + x - 1] + w1 * inp[r0 + x] + w2 * inp[r0 + x + 1] + w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1] + w6 * inp[r2 + x - 1] + w7 * inp[r2 + x] + w8 * inp[r2 + x + 1];
132
+ }
133
+ out[orow + rm1] += w0 * inp[r0 + rm1 - 1] + w1 * inp[r0 + rm1] + w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1] + w6 * inp[r2 + rm1 - 1] + w7 * inp[r2 + rm1];
134
+ }
135
+ {
136
+ const r1 = ib, r2 = ib + res, orow = ob;
137
+ out[orow] += w4 * inp[r1] + w5 * inp[r1 + 1] + w7 * inp[r2] + w8 * inp[r2 + 1];
138
+ for (let x = 1; x < rm1; x++) {
139
+ out[orow + x] += w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1] + w6 * inp[r2 + x - 1] + w7 * inp[r2 + x] + w8 * inp[r2 + x + 1];
136
140
  }
137
- out[ob + y * res + x] = s > 0 ? s : 0;
141
+ out[orow + rm1] += w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1] + w6 * inp[r2 + rm1 - 1] + w7 * inp[r2 + rm1];
142
+ }
143
+ {
144
+ const r0 = ib + (rm1 - 1) * res, r1 = ib + rm1 * res, orow = ob + rm1 * res;
145
+ out[orow] += w1 * inp[r0] + w2 * inp[r0 + 1] + w4 * inp[r1] + w5 * inp[r1 + 1];
146
+ for (let x = 1; x < rm1; x++) {
147
+ out[orow + x] += w0 * inp[r0 + x - 1] + w1 * inp[r0 + x] + w2 * inp[r0 + x + 1] + w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1];
148
+ }
149
+ out[orow + rm1] += w0 * inp[r0 + rm1 - 1] + w1 * inp[r0 + rm1] + w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1];
138
150
  }
139
151
  }
152
+ const end = ob + plane;
153
+ for (let i = ob; i < end; i++) if (out[i] < 0) out[i] = 0;
140
154
  }
141
155
  }
142
156
  function pool2(inp, res, ch, out) {
143
157
  const r2 = res >> 1;
144
158
  for (let c = 0; c < ch; c++) {
145
159
  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);
160
+ for (let y = 0; y < r2; y++) {
161
+ const r0 = ib + (y << 1) * res, r1 = r0 + res, orow = ob + y * r2;
162
+ for (let x = 0; x < r2; x++) {
163
+ const i0 = r0 + (x << 1), i1 = r1 + (x << 1);
164
+ let m = inp[i0], b = inp[i0 + 1], d = inp[i1], e = inp[i1 + 1];
165
+ if (b > m) m = b;
166
+ if (d > m) m = d;
167
+ if (e > m) m = e;
168
+ out[orow + x] = m;
169
+ }
150
170
  }
151
171
  }
152
172
  }
153
173
  function embed(model, img) {
154
174
  const S = model.size;
155
- let cur = Float32Array.from(img), res = S, ch = 1;
175
+ if (!model._bufA) {
176
+ let maxLen = S * S, r = S;
177
+ for (const c of model.convs) {
178
+ if (c.cout * r * r > maxLen) maxLen = c.cout * r * r;
179
+ if (c.pool) r >>= 1;
180
+ }
181
+ model._bufA = new Float32Array(maxLen);
182
+ model._bufB = new Float32Array(maxLen);
183
+ model._gap = new Float32Array(model.lin.in);
184
+ }
185
+ let cur = model._bufA, nxt = model._bufB, res = S, ch = 1;
186
+ cur.set(img);
156
187
  for (const c of model.convs) {
157
- const out = new Float32Array(c.cout * res * res);
158
- conv3(c, cur, res, out);
159
- cur = out;
188
+ conv3(c, cur, res, nxt);
189
+ let t = cur;
190
+ cur = nxt;
191
+ nxt = t;
160
192
  ch = c.cout;
161
193
  if (c.pool) {
162
- const p = new Float32Array(ch * (res >> 1) * (res >> 1));
163
- pool2(cur, res, ch, p);
164
- cur = p;
194
+ pool2(cur, res, ch, nxt);
195
+ t = cur;
196
+ cur = nxt;
197
+ nxt = t;
165
198
  res >>= 1;
166
199
  }
167
200
  }
168
- const plane = res * res;
169
- const gap = new Float32Array(ch);
201
+ const plane = res * res, invPlane = 1 / plane;
202
+ const gap = model._gap;
170
203
  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;
204
+ let s = 0, off = c * plane;
205
+ for (let j = 0; j < plane; j++) s += cur[off + j];
206
+ gap[c] = s * invPlane;
174
207
  }
175
- const { lin } = model;
208
+ const { lin } = model, lw = lin.w, lb = lin.bias, linIn = lin.in;
176
209
  const e = new Float32Array(lin.out);
177
210
  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];
211
+ let s = lb[o], off = o * linIn;
212
+ for (let j = 0; j < linIn; j++) s += lw[off + j] * gap[j];
180
213
  e[o] = s;
181
214
  }
182
215
  return e;
@@ -213,7 +246,8 @@ function softmax(z) {
213
246
  for (let i = 0; i < z.length; i++) out[i] /= s;
214
247
  return out;
215
248
  }
216
- function drawStrokes(ctx, strokes, box = 120, lw = 7, pad = 10) {
249
+ var _planeBuf = null;
250
+ function strokePlane(strokes, box = 120, lw = 7, pad = 10) {
217
251
  let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
218
252
  for (const s of strokes) for (const p of s) {
219
253
  if (p.x < minX) minX = p.x;
@@ -221,73 +255,86 @@ function drawStrokes(ctx, strokes, box = 120, lw = 7, pad = 10) {
221
255
  if (p.y < minY) minY = p.y;
222
256
  if (p.y > maxY) maxY = p.y;
223
257
  }
224
- const sc = box / Math.max(maxX - minX, maxY - minY, 1);
225
258
  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";
259
+ const ink = _planeBuf && _planeBuf.length === W * W ? _planeBuf : _planeBuf = new Float32Array(W * W);
260
+ ink.fill(0);
261
+ if (!(minX <= maxX)) return { ink, W, iMinX: 0, iMaxX: -1, iMinY: 0, iMaxY: -1 };
262
+ const sc = box / Math.max(maxX - minX, maxY - minY, 1);
263
+ const r = lw * 0.5, rOut = r + 0.5, rIn = r - 0.5, rOut2 = rOut * rOut, rIn2 = rIn * rIn;
264
+ let iMinX = W, iMaxX = -1, iMinY = W, iMaxY = -1;
265
+ const drawSeg = (ax, ay, bx, by) => {
266
+ const x0 = Math.max(0, Math.floor(Math.min(ax, bx) - rOut)), x1 = Math.min(W - 1, Math.ceil(Math.max(ax, bx) + rOut));
267
+ const y0 = Math.max(0, Math.floor(Math.min(ay, by) - rOut)), y1 = Math.min(W - 1, Math.ceil(Math.max(ay, by) + rOut));
268
+ const vx = bx - ax, vy = by - ay, l2 = vx * vx + vy * vy, invL2 = l2 > 1e-6 ? 1 / l2 : 0;
269
+ for (let y = y0; y <= y1; y++) {
270
+ const py = y + 0.5 - ay, row = y * W;
271
+ for (let x = x0; x <= x1; x++) {
272
+ const cur = ink[row + x];
273
+ if (cur >= 1) continue;
274
+ const px = x + 0.5 - ax;
275
+ let t = (px * vx + py * vy) * invL2;
276
+ if (t < 0) t = 0;
277
+ else if (t > 1) t = 1;
278
+ const dx = px - t * vx, dy = py - t * vy, d2 = dx * dx + dy * dy;
279
+ if (d2 >= rOut2) continue;
280
+ const v = d2 <= rIn2 ? 1 : rOut - Math.sqrt(d2);
281
+ if (v > cur) {
282
+ ink[row + x] = v;
283
+ if (v > 0.15) {
284
+ if (x < iMinX) iMinX = x;
285
+ if (x > iMaxX) iMaxX = x;
286
+ if (y < iMinY) iMinY = y;
287
+ if (y > iMaxY) iMaxY = y;
288
+ }
289
+ }
290
+ }
291
+ }
292
+ };
232
293
  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();
294
+ if (!s.length) continue;
295
+ let ax = (s[0].x - minX) * sc + pad, ay = (s[0].y - minY) * sc + pad;
296
+ if (s.length === 1) {
297
+ drawSeg(ax, ay, ax, ay);
298
+ continue;
299
+ }
300
+ for (let i = 1; i < s.length; i++) {
301
+ const bx = (s[i].x - minX) * sc + pad, by = (s[i].y - minY) * sc + pad;
302
+ drawSeg(ax, ay, bx, by);
303
+ ax = bx;
304
+ ay = by;
305
+ }
238
306
  }
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;
307
+ return { ink, W, iMinX, iMaxX, iMinY, iMaxY };
245
308
  }
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
- }
309
+ function normalizePlane({ ink, W, iMinX, iMaxX, iMinY, iMaxY }, size = 32, inkBox = Math.round(size * 0.75)) {
259
310
  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);
311
+ if (iMaxX < 0) return out;
312
+ const cw = iMaxX - iMinX + 1, ch = iMaxY - iMinY + 1;
267
313
  const s = inkBox / Math.max(ch, cw);
268
314
  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
315
  const fx = cw / nw, fy = ch / nh;
316
+ const oy = size - nh >> 1, ox = size - nw >> 1;
271
317
  for (let y = 0; y < nh; y++) {
272
318
  const y0 = y * fy, y1 = (y + 1) * fy, iy0 = Math.floor(y0), iy1 = Math.min(ch, Math.ceil(y1));
319
+ const outRow = (y + oy) * size + ox;
273
320
  for (let x = 0; x < nw; x++) {
274
321
  const x0 = x * fx, x1 = (x + 1) * fx, ix0 = Math.floor(x0), ix1 = Math.min(cw, Math.ceil(x1));
275
322
  let ss = 0, wsum = 0;
276
323
  for (let yy = iy0; yy < iy1; yy++) {
277
324
  const wy = Math.min(y1, yy + 1) - Math.max(y0, yy);
278
325
  if (wy <= 0) continue;
326
+ const srcRow = (yy + iMinY) * W + iMinX;
279
327
  for (let xx = ix0; xx < ix1; xx++) {
280
328
  const wx = Math.min(x1, xx + 1) - Math.max(x0, xx);
281
329
  if (wx <= 0) continue;
282
- ss += crop[yy * cw + xx] * wx * wy;
283
- wsum += wx * wy;
330
+ const wxy = wx * wy;
331
+ ss += ink[srcRow + xx] * wxy;
332
+ wsum += wxy;
284
333
  }
285
334
  }
286
- r[y * nw + x] = wsum > 0 ? ss / wsum : 0;
335
+ out[outRow + x] = wsum > 0 ? ss / wsum : 0;
287
336
  }
288
337
  }
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
338
  return out;
292
339
  }
293
340
 
@@ -328,6 +375,7 @@ async function fetchAsset(url2, { as = "json", what, base, recognizer }) {
328
375
  }
329
376
 
330
377
  // ../../library/src/char-cnn-core.js
378
+ var finitePoints = (strokes) => strokes.every((st) => Array.isArray(st) && st.every((p) => p && Number.isFinite(p.x) && Number.isFinite(p.y)));
331
379
  function scriptBase() {
332
380
  const src = typeof document !== "undefined" && document.currentScript && document.currentScript.src || "";
333
381
  return src ? src.slice(0, src.lastIndexOf("/") + 1) : "";
@@ -335,7 +383,6 @@ function scriptBase() {
335
383
  var ASSET_BASE = scriptBase();
336
384
  var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
337
385
  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
386
  var TEACH_SHARPNESS = 18;
340
387
  var unitOf = (e) => {
341
388
  let n = 0;
@@ -363,8 +410,6 @@ var MillirecCharCnnCore = class {
363
410
  this.base = assetBaseOf(config.assetBase || ASSET_BASE);
364
411
  this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
365
412
  this.stages = [];
366
- this.off = makeCanvas(140);
367
- this.octx = this.off ? this.off.getContext("2d", { willReadFrequently: true }) : null;
368
413
  this.ready = this.load();
369
414
  }
370
415
  async load() {
@@ -420,18 +465,19 @@ var MillirecCharCnnCore = class {
420
465
  this.stages.push(stage);
421
466
  }
422
467
  }
468
+ /** Which engine this is (createRecognizer() picks one). */
469
+ get engine() {
470
+ return "char-cnn";
471
+ }
423
472
  /** drawn strokes ([[{x,y},...],...]) -> the model input raster at `size`.
424
- * The canvas draw + readback is the dear part (~1 ms) and is shared by every
425
- * stage of one recognize() call; only the resample differs per size. */
473
+ * The 140x140 stroke plane is computed once per recognize() call and shared
474
+ * by every stage; only the resample differs per size. */
426
475
  raster(strokes, size) {
427
- if (!this.octx) throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");
428
476
  if (this._planeFor !== strokes) {
429
- const W2 = drawStrokes(this.octx, strokes);
430
- this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
477
+ this._plane = strokePlane(strokes);
431
478
  this._planeFor = strokes;
432
479
  }
433
- const { ink, W } = this._plane;
434
- return normalizeInk(ink, W, W, size, Math.round(size * 0.75));
480
+ return normalizePlane(this._plane, size, Math.round(size * 0.75));
435
481
  }
436
482
  /**
437
483
  * -> [{char, prob} x3] (best first) with `.stage` = index of the model that decided, or null.
@@ -441,7 +487,7 @@ var MillirecCharCnnCore = class {
441
487
  */
442
488
  async recognize(strokes, { only = null } = {}) {
443
489
  strokes = asStrokes(strokes);
444
- if (!strokes || !strokes.length) return null;
490
+ if (!strokes || !strokes.length || !finitePoints(strokes)) return null;
445
491
  await this.ready;
446
492
  if (this.destroyed || !this.stages.length) return null;
447
493
  this._planeFor = null;
@@ -537,6 +583,8 @@ var MillirecCharCnnCore = class {
537
583
  /** The last stage's (unit) embedding of drawn strokes — what the head reads. */
538
584
  async embedding(strokes) {
539
585
  strokes = asStrokes(strokes);
586
+ if (!strokes || !strokes.length) throw new TypeError("MillirecCharCnn: embedding() needs at least one stroke");
587
+ if (!finitePoints(strokes)) throw new TypeError("MillirecCharCnn: embedding() got a point that is not a finite number");
540
588
  await this.ready;
541
589
  if (this.destroyed || !this.stages.length) throw new Error(DESTROYED);
542
590
  this._planeFor = null;
@@ -628,7 +676,7 @@ function nodeFileUrl(url2, spec) {
628
676
  }
629
677
 
630
678
  // src/options.js
631
- var OPTIONS = ["accurate", "fast", "small", "digits", "lowercase"];
679
+ var OPTIONS = ["accurate", "fast", "small", "digits", "digits-fast", "digits-small", "lowercase", "lowercase-fast", "lowercase-small", "all", "all-fast", "all-small"];
632
680
  async function loadOption(name, { assetBase = HERE } = {}) {
633
681
  if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
634
682
  const base = assetBaseOf(assetBase);
@@ -639,9 +687,7 @@ async function loadOption(name, { assetBase = HERE } = {}) {
639
687
  async function withOption(opts) {
640
688
  if (typeof opts === "string") opts = { option: opts };
641
689
  opts = opts || {};
642
- if (typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
643
- 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");
644
- }
690
+ if (typeof fetch === "undefined") throw new Error("millirec: char-cnn needs fetch to load its model files");
645
691
  const assetBase = assetBaseOf(opts.assetBase || HERE);
646
692
  const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
647
693
  return { ...preset, ...opts, assetBase };
@@ -649,7 +695,7 @@ async function withOption(opts) {
649
695
 
650
696
  // src/char-cnn-core.js
651
697
  var MillirecCharCnnCore2 = class extends MillirecCharCnnCore {
652
- /** opts: option ('accurate' | 'fast' | 'small', default 'accurate') or your own
698
+ /** opts: option (one of OPTIONS: 'accurate', 'fast', 'small', 'digits', 'lowercase', 'all', each set also -fast and -small; default 'accurate') or your own
653
699
  * { classes, stages, kernel }, plus assetBase (default: this module's folder).
654
700
  * A string is the option's name: create('fast') = create({ option: 'fast' }). */
655
701
  static async create(opts = {}) {
@@ -1,7 +1,7 @@
1
1
  import type { AttachOptions, Pad } from '../char-lite/index.js'
2
2
  import { MillirecCharCnnCore, type CreateOptions, type OptionName } from './core.js'
3
3
 
4
- export type { ResultDetail, ClearDetail } from '../char-lite/index.js'
4
+ export type { ResultDetail, ClearDetail, Point, Stroke, Strokes, Reading } from '../char-lite/index.js'
5
5
  export { OPTIONS, loadOption, type OptionName, type CnnConfig, type Stage, type CreateOptions } from './core.js'
6
6
 
7
7
  export declare class MillirecCharCnn extends MillirecCharCnnCore {