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.
- package/CHANGELOG.md +65 -0
- package/README.md +171 -58
- package/dist/char-cnn/cnn-web.wasm +0 -0
- package/dist/char-cnn/core.d.ts +3 -1
- package/dist/char-cnn/core.js +145 -99
- package/dist/char-cnn/index.d.ts +1 -1
- package/dist/char-cnn/index.js +198 -136
- package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -1
- package/dist/char-cnn/millirec-char-cnn.min.js +1 -1
- package/dist/char-cnn/options/accurate/tiny.head.bin +0 -0
- package/dist/char-cnn/options/accurate.js +2 -2
- package/dist/char-cnn/options/accurate.json +1 -1
- package/dist/char-cnn/options/all/all-base.head.bin +0 -0
- package/dist/char-cnn/options/all/all-base.q4.bin +0 -0
- package/dist/char-cnn/options/all/all-tiny.bin +0 -0
- package/dist/char-cnn/options/all/all-tiny.head.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-base-p25.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-tiny.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-tiny.head.bin +0 -0
- package/dist/char-cnn/options/all-fast.js +17 -0
- package/dist/char-cnn/options/all-fast.json +15 -0
- package/dist/char-cnn/options/all-small/all-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/all-small/all-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/all-small.js +11 -0
- package/dist/char-cnn/options/all-small.json +9 -0
- package/dist/char-cnn/options/all.js +17 -0
- package/dist/char-cnn/options/all.json +15 -0
- package/dist/char-cnn/options/digits/digits-base.head.bin +0 -0
- package/dist/char-cnn/options/digits/digits-tiny.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-base-p25.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-tiny.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-tiny.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast.js +17 -0
- package/dist/char-cnn/options/digits-fast.json +15 -0
- package/dist/char-cnn/options/digits-small/digits-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/digits-small/digits-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/digits-small.js +11 -0
- package/dist/char-cnn/options/digits-small.json +9 -0
- package/dist/char-cnn/options/digits.js +2 -2
- package/dist/char-cnn/options/digits.json +1 -1
- package/dist/char-cnn/options/fast/base-p25.head.bin +0 -0
- package/dist/char-cnn/options/fast/tiny.head.bin +0 -0
- package/dist/char-cnn/options/fast.js +2 -2
- package/dist/char-cnn/options/fast.json +1 -1
- package/dist/char-cnn/options/lowercase/lowercase-tiny.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast.js +17 -0
- package/dist/char-cnn/options/lowercase-fast.json +15 -0
- package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/lowercase-small.js +11 -0
- package/dist/char-cnn/options/lowercase-small.json +9 -0
- package/dist/char-cnn/options/lowercase.js +2 -2
- package/dist/char-cnn/options/lowercase.json +1 -1
- package/dist/char-cnn/options/small/tiny.head.bin +0 -0
- package/dist/char-cnn/options/small/tiny.q4.bin +0 -0
- package/dist/char-cnn/options/small.js +2 -2
- package/dist/char-cnn/options/small.json +1 -1
- package/dist/char-lite/core.d.ts +1 -0
- package/dist/char-lite/core.js +628 -0
- package/dist/char-lite/index.d.ts +28 -8
- package/dist/char-lite/index.js +845 -717
- package/dist/char-lite/millirec-char-lite.min.js +1 -1
- package/dist/char-lite/sets.json +17 -17
- package/dist/index.d.ts +35 -0
- package/dist/index.js +88 -0
- package/dist/overlap/millirec-overlap.min.js +7 -7
- package/dist/pad/index.d.ts +8 -0
- package/dist/pad/index.js +482 -0
- package/package.json +21 -7
- package/dist/char-cnn/options/small/tiny.bin +0 -0
package/dist/char-cnn/core.js
CHANGED
|
@@ -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,
|
|
118
|
-
const plane = res * res,
|
|
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 *
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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[
|
|
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++)
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
cur =
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
cur =
|
|
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 =
|
|
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[
|
|
173
|
-
gap[c] = s
|
|
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 =
|
|
179
|
-
for (let j = 0; 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
|
-
|
|
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
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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 (
|
|
261
|
-
const cw =
|
|
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
|
-
|
|
283
|
-
|
|
330
|
+
const wxy = wx * wy;
|
|
331
|
+
ss += ink[srcRow + xx] * wxy;
|
|
332
|
+
wsum += wxy;
|
|
284
333
|
}
|
|
285
334
|
}
|
|
286
|
-
|
|
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
|
|
425
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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'
|
|
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 = {}) {
|
package/dist/char-cnn/index.d.ts
CHANGED
|
@@ -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 {
|