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 @@
|
|
|
1
|
+
"use strict";(()=>{var H={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function U(o){let e=new DataView(o.buffer?o.buffer:o,o.byteOffset||0,o.byteLength),c=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),r=0;return{u32:()=>{let t=e.getUint32(r,!0);return r+=4,t},f32s:t=>{let s=new Float32Array(t);for(let a=0;a<t;a++)s[a]=e.getFloat32(r,!0),r+=4;return s},i8s:t=>{let s=new Int8Array(t);for(let a=0;a<t;a++)s[a]=c[r+a]<<24>>24;return r+=t,s},ibits:(t,s)=>{if(s===8){let i=new Int8Array(t);for(let n=0;n<t;n++)i[n]=c[r+n]<<24>>24;return r+=t,i}let a=new Int8Array(t),y=(1<<s)-1,h=1<<s-1;for(let i=0;i<t;i++){let n=i*s,f=r+(n>>3),l=n&7,w=(c[f]|c[f+1]<<8)>>l&y;a[i]=w&h?w-(1<<s):w}return r+=t*s+7>>3,a},str:t=>{let s=String.fromCharCode(...c.subarray(r,r+t));return r+=t,s},done:()=>r>=e.byteLength}}function W(o,e=8){let c=o.u32(),r=o.u32(),t=o.f32s(c),s=o.f32s(c),a=o.ibits(c*r,e),y=new Float32Array(c*r);for(let h=0;h<c;h++)for(let i=0;i<r;i++)y[h*r+i]=a[h*r+i]*t[h];return{out:c,in:r,w:y,bias:s}}function Y(o){let e=U(o),c=e.str(6),r=H[c];if(!r)throw new Error("not an MRCNN1/3/4/5/6 model");let t=e.u32();if(t!==1)throw new Error("unknown model version "+t);let s=e.u32(),a=e.u32(),y=e.u32(),h=[];for(let n=0;n<y;n++){let f=e.u32(),l=e.u32(),w=e.u32(),x=e.u32(),d=e.f32s(f),p=e.f32s(f),A=e.ibits(f*l*w*w,r),I=new Float32Array(A.length),M=l*w*w;for(let m=0;m<f;m++)for(let b=0;b<M;b++)I[m*M+b]=A[m*M+b]*d[m];h.push({cout:f,cin:l,k:w,pool:x,w:I,bias:p})}let i=W(e,r);return{size:s,dim:a,convs:h,lin:i,bits:r}}function $(o){let e=new Uint8Array(o.buffer?o.buffer:o,o.byteOffset||0,o.byteLength),c=String.fromCharCode(...e.subarray(0,6)),r=H[c];if(!r)throw new Error("not an MRCNN model");if(r===8)return e;let t=U(o);t.str(6);let s=t.u32(),a=t.u32(),y=t.u32(),h=t.u32(),i=[],n=m=>{let b=new Uint8Array(4);return new DataView(b.buffer).setUint32(0,m,!0),b},f=m=>new Uint8Array(m.buffer,m.byteOffset,m.byteLength);i.push(new TextEncoder().encode("MRCNN1"),n(s),n(a),n(y),n(h));for(let m=0;m<h;m++){let b=t.u32(),u=t.u32(),g=t.u32(),k=t.u32(),N=t.f32s(b),S=t.f32s(b),F=t.ibits(b*u*g*g,r);i.push(n(b),n(u),n(g),n(k),f(N),f(S),new Uint8Array(F.buffer,F.byteOffset,F.length))}let l=t.u32(),w=t.u32(),x=t.f32s(l),d=t.f32s(l),p=t.ibits(l*w,r);i.push(n(l),n(w),f(x),f(d),new Uint8Array(p.buffer,p.byteOffset,p.length));let A=i.reduce((m,b)=>m+b.length,0),I=new Uint8Array(A),M=0;for(let m of i)I.set(m,M),M+=m.length;return I}function X(o){return W(U(o))}function ot(o,e,c,r){let{cout:t,cin:s,k:a,w:y,bias:h}=o,i=c*c,n=a-1>>1,f=s*a*a;for(let l=0;l<t;l++){let w=l*i,x=h[l],d=l*f;for(let p=0;p<c;p++)for(let A=0;A<c;A++){let I=x;for(let M=0;M<s;M++){let m=M*i,b=d+M*a*a;for(let u=0;u<a;u++){let g=p+u-n;if(g<0||g>=c)continue;let k=m+g*c,N=b+u*a;for(let S=0;S<a;S++){let F=A+S-n;F<0||F>=c||(I+=y[N+S]*e[k+F])}}}r[w+p*c+A]=I>0?I:0}}}function rt(o,e,c,r){let t=e>>1;for(let s=0;s<c;s++){let a=s*e*e,y=s*t*t;for(let h=0;h<t;h++)for(let i=0;i<t;i++){let n=a+2*h*e+2*i,f=o[n],l=o[n+1],w=o[n+e],x=o[n+e+1];r[y+h*t+i]=Math.max(f,l,w,x)}}}function q(o,e){let c=o.size,r=Float32Array.from(e),t=c,s=1;for(let n of o.convs){let f=new Float32Array(n.cout*t*t);if(ot(n,r,t,f),r=f,s=n.cout,n.pool){let l=new Float32Array(s*(t>>1)*(t>>1));rt(r,t,s,l),r=l,t>>=1}}let a=t*t,y=new Float32Array(s);for(let n=0;n<s;n++){let f=0;for(let l=0;l<a;l++)f+=r[n*a+l];y[n]=f/a}let{lin:h}=o,i=new Float32Array(h.out);for(let n=0;n<h.out;n++){let f=h.bias[n];for(let l=0;l<h.in;l++)f+=h.w[n*h.in+l]*y[l];i[n]=f}return i}function K(o){let e=0;for(let r=0;r<o.length;r++)e+=o[r]*o[r];e=1/(Math.sqrt(e)+1e-9);let c=new Float32Array(o.length);for(let r=0;r<o.length;r++)c[r]=o[r]*e;return c}function E(o,e){let c=new Float32Array(o.out);for(let r=0;r<o.out;r++){let t=o.bias[r];for(let s=0;s<o.in;s++){let a=e[s];a>0&&(t+=o.w[r*o.in+s]*a)}c[r]=t}return c}function B(o){let e=-1/0;for(let t of o)t>e&&(e=t);let c=new Float32Array(o.length),r=0;for(let t=0;t<o.length;t++)c[t]=Math.exp(o[t]-e),r+=c[t];for(let t=0;t<o.length;t++)c[t]/=r;return c}function Z(o,e,c=120,r=7,t=10){let s=1/0,a=1/0,y=-1/0,h=-1/0;for(let f of e)for(let l of f)l.x<s&&(s=l.x),l.x>y&&(y=l.x),l.y<a&&(a=l.y),l.y>h&&(h=l.y);let i=c/Math.max(y-s,h-a,1),n=c+2*t;o.fillStyle="#fff",o.fillRect(0,0,n,n),o.strokeStyle="#000",o.lineWidth=r,o.lineCap="round",o.lineJoin="round";for(let f of e)if(!(f.length<2)){o.beginPath(),o.moveTo((f[0].x-s)*i+t,(f[0].y-a)*i+t);for(let l=1;l<f.length;l++)o.lineTo((f[l].x-s)*i+t,(f[l].y-a)*i+t);o.stroke()}return n}function V(o,e,c){let r=new Float32Array(e*c);for(let t=0,s=0;t<r.length;t++,s+=4)r[t]=1-(o[s]*.299+o[s+1]*.587+o[s+2]*.114)/255;return r}function J(o,e,c,r=32,t=24){let s=1/0,a=-1,y=1/0,h=-1;for(let u=0;u<c;u++)for(let g=0;g<e;g++)o[u*e+g]>.15&&(g<s&&(s=g),g>a&&(a=g),u<y&&(y=u),u>h&&(h=u));let i=new Float32Array(r*r);if(a<0)return i;let n=a-s+1,f=h-y+1,l=new Float32Array(n*f);for(let u=0;u<f;u++)for(let g=0;g<n;g++)l[u*n+g]=o[(u+y)*e+(g+s)];let w=0;for(let u=0;u<l.length;u++)l[u]>w&&(w=l[u]);if(w>.05&&w<.999)for(let u=0;u<l.length;u++)l[u]=Math.min(1,l[u]/w);let x=t/Math.max(f,n),d=Math.max(1,Math.round(f*x)),p=Math.max(1,Math.round(n*x)),A=new Float32Array(p*d),I=n/p,M=f/d;for(let u=0;u<d;u++){let g=u*M,k=(u+1)*M,N=Math.floor(g),S=Math.min(f,Math.ceil(k));for(let F=0;F<p;F++){let z=F*I,P=(F+1)*I,et=Math.floor(z),nt=Math.min(n,Math.ceil(P)),D=0,L=0;for(let v=N;v<S;v++){let O=Math.min(k,v+1)-Math.max(g,v);if(!(O<=0))for(let C=et;C<nt;C++){let R=Math.min(P,C+1)-Math.max(z,C);R<=0||(D+=l[v*n+C]*R*O,L+=R*O)}}A[u*p+F]=L>0?D/L:0}}let m=r-d>>1,b=r-p>>1;for(let u=0;u<d;u++)for(let g=0;g<p;g++)i[(u+m)*r+g+b]=A[u*p+g];return i}function G(o){let e=o?String(o):"";return e?(e.endsWith("/")||(e+="/"),typeof location<"u"?new URL(e,location.href).href:e):typeof location<"u"?new URL("./",location.href).href:""}var _=o=>Array.isArray(o)&&o.length&&!Array.isArray(o[0])?[o]:o,T=(o,e,c,r)=>new Error(`millirec: ${o} is not ${e} \u2014 the package files are not served at ${c}; copy node_modules/millirec/dist/${r}/ and pass assetBase (see README "Serving the files")`);async function Q(o,{as:e="json",what:c,base:r,recognizer:t}){let s=n=>{let f=T(o,c,r,t);return n&&(f.message+=` (${n})`),f},a;try{a=await fetch(o)}catch(n){throw s(n&&n.message)}let y=a.headers&&a.headers.get("content-type")||"";if(!a.ok)throw s(`HTTP ${a.status}`);if(e==="json")try{return await a.json()}catch(n){throw s(/html/i.test(y)?"an HTML page":n&&n.message)}let h=await a.arrayBuffer(),i=String.fromCharCode(...new Uint8Array(h,0,Math.min(h.byteLength,16))).trimStart().toLowerCase();if(i.startsWith("<!doctype")||i.startsWith("<html"))throw s("an HTML page");return h}function st(){let o=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return o?o.slice(0,o.lastIndexOf("/")+1):""}var it=st(),tt=(o,e)=>Q(o,{as:"buf",what:"a char-cnn model file",base:e,recognizer:"char-cnn"}),at="millirec: recognizer destroyed",ct=o=>typeof document<"u"&&document.createElement?Object.assign(document.createElement("canvas"),{width:o,height:o}):typeof OffscreenCanvas<"u"?new OffscreenCanvas(o,o):null,j=class{static async create(e={}){let r={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...e};if(!r.stages||!r.stages.length)throw new Error("MillirecCharCnn: no stages configured");if(typeof fetch>"u"||typeof document>"u"&&typeof OffscreenCanvas>"u")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");let t=new this(r);return await t.ready,t}constructor(e){this.config=e,this.base=G(e.assetBase||it),this.classes=typeof e.classes=="string"?e.classes.split(""):e.classes,this.stages=[],this.off=ct(140),this.octx=this.off?this.off.getContext("2d",{willReadFrequently:!0}):null,this.ready=this.load()}async load(){let{base:e,config:c}=this,r=null;if(c.kernel){let t=e+c.kernel;try{r=(await import(t)).default}catch(s){let a=T(t,"the char-cnn kernel",e,"char-cnn");throw a.message+=` (${s&&s.message})`,a}}for(let t of c.stages){let[s,a]=await Promise.all([tt(e+t.model,e),tt(e+t.head,e)]);if(this.destroyed)return;let y=X(a),h={theta:t.theta==null?null:t.theta,head:y,headNorm:t.headNorm!==!1,size:0};if(r){let i=await r();if(this.destroyed)return;let n=$(new Uint8Array(s)),f=i._malloc(n.byteLength);i.HEAPU8.set(n,f);let l=i._cnnLoad(f,n.byteLength);if(i._free(f),!l)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let w=i._cnnSize(),x=i._cnnDim();h.size=w,h.embed=d=>{let p=i._cnnInputPtr(1)>>2;i.HEAPF32.set(d,p),i._cnnRun(1);let A=i._cnnEmbedPtr(1)>>2;return i.HEAPF32.slice(A,A+x)},h.logits=d=>E(h.head,h.embed(d))}else{let i=Y(s);h.size=i.size,h.embed=n=>{let f=q(i,n);return h.headNorm?K(f):f},h.logits=n=>E(h.head,h.embed(n))}this.stages.push(h)}}raster(e,c){if(!this.octx)throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");if(this._planeFor!==e){let s=Z(this.octx,e);this._plane={ink:V(this.octx.getImageData(0,0,s,s).data,s,s),W:s},this._planeFor=e}let{ink:r,W:t}=this._plane;return J(r,t,t,c,Math.round(c*.75))}async recognize(e,{only:c=null}={}){if(e=_(e),!e||!e.length||(await this.ready,this.destroyed||!this.stages.length))return null;this._planeFor=null;let r=c?new Set(c):null,t=null,s=0;for(let n=0;n<this.stages.length;n++){let f=this.stages[n];if((this.personal||r)&&n<this.stages.length-1)continue;if(t=f.logits(this.raster(e,f.size)),s=n,f.theta==null||n===this.stages.length-1)break;let l=-1/0,w=-1/0;for(let x of t)x>l?(w=l,l=x):x>w&&(w=x);if(l-w>=f.theta)break}let a=[...t.keys()];if(r&&(a=a.filter(n=>r.has(this.classes[n])),!a.length)){let n=[];return n.stage=s,n}let y=B(a.map(n=>t[n])),i=[...y.keys()].sort((n,f)=>y[f]-y[n]).slice(0,3).map(n=>({char:this.classes[a[n]],prob:y[n]}));return i.stage=s,i}async embedding(e){if(e=_(e),await this.ready,this.destroyed||!this.stages.length)throw new Error(at);this._planeFor=null;let c=this.stages[this.stages.length-1];return c.embed(this.raster(e,c.size))}async personalize(e,{steps:c=250,lr:r=.5,prior:t=1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let s=this.stages[this.stages.length-1],a=s.head0||(s.head0=s.head),y=[],h=[];for(let[w,x]of Object.entries(e||{})){let d=this.classes.indexOf(w);if(!(d<0))for(let p of x||[]){let A=_(p);!A||!A.length||(y.push(await this.embedding(A)),h.push(d))}}if(this.destroyed)return null;if(!y.length)return s.head=a,this.personal=null,null;let i={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},n=y.length,f=new Float32Array(i.w.length),l=new Float32Array(i.out);for(let w=0;w<c;w++){f.fill(0),l.fill(0);for(let d=0;d<n;d++){let p=B(E(i,y[d])),A=y[d];for(let I=0;I<i.out;I++){let M=(p[I]-(I===h[d]?1:0))/n;if(!M)continue;l[I]+=M;let m=I*i.in;for(let b=0;b<i.in;b++){let u=A[b];u>0&&(f[m+b]+=M*u)}}}let x=t/n;for(let d=0;d<i.w.length;d++)i.w[d]-=r*(f[d]+x*(i.w[d]-a.w[d]));for(let d=0;d<i.out;d++)i.bias[d]-=r*(l[d]+x*(i.bias[d]-a.bias[d]))}return s.head=i,this.personal={n,classes:[...new Set(h)].map(w=>this.classes[w]).sort()},this.personal}destroy(){this.destroyed=!0,this.stages=[],this.personal=null,this._plane=null,this._planeFor=null}depersonalize(){let e=this.stages[this.stages.length-1];e&&e.head0&&(e.head=e.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=j);})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";(()=>{var J={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function q(o){let r=new DataView(o.buffer?o.buffer:o,o.byteOffset||0,o.byteLength),l=new Uint8Array(r.buffer,r.byteOffset,r.byteLength),n=0;return{u32:()=>{let t=r.getUint32(n,!0);return n+=4,t},f32s:t=>{let s=new Float32Array(t);for(let a=0;a<t;a++)s[a]=r.getFloat32(n,!0),n+=4;return s},i8s:t=>{let s=new Int8Array(t);for(let a=0;a<t;a++)s[a]=l[n+a]<<24>>24;return n+=t,s},ibits:(t,s)=>{if(s===8){let f=new Int8Array(t);for(let e=0;e<t;e++)f[e]=l[n+e]<<24>>24;return n+=t,f}let a=new Int8Array(t),p=(1<<s)-1,c=1<<s-1;for(let f=0;f<t;f++){let e=f*s,i=n+(e>>3),u=e&7,g=(l[i]|l[i+1]<<8)>>u&p;a[f]=g&c?g-(1<<s):g}return n+=t*s+7>>3,a},str:t=>{let s=String.fromCharCode(...l.subarray(n,n+t));return n+=t,s},done:()=>n>=r.byteLength}}function V(o,r=8){let l=o.u32(),n=o.u32(),t=o.f32s(l),s=o.f32s(l),a=o.ibits(l*n,r),p=new Float32Array(l*n);for(let c=0;c<l;c++)for(let f=0;f<n;f++)p[c*n+f]=a[c*n+f]*t[c];return{out:l,in:n,w:p,bias:s}}function G(o){let r=q(o),l=r.str(6),n=J[l];if(!n)throw new Error("not an MRCNN1/3/4/5/6 model");let t=r.u32();if(t!==1)throw new Error("unknown model version "+t);let s=r.u32(),a=r.u32(),p=r.u32(),c=[];for(let e=0;e<p;e++){let i=r.u32(),u=r.u32(),g=r.u32(),y=r.u32(),m=r.f32s(i),d=r.f32s(i),w=r.ibits(i*u*g*g,n),I=new Float32Array(w.length),A=u*g*g;for(let M=0;M<i;M++)for(let v=0;v<A;v++)I[M*A+v]=w[M*A+v]*m[M];c.push({cout:i,cin:u,k:g,pool:y,w:I,bias:d})}let f=V(r,n);return{size:s,dim:a,convs:c,lin:f,bits:n}}function Q(o){let r=new Uint8Array(o.buffer?o.buffer:o,o.byteOffset||0,o.byteLength),l=String.fromCharCode(...r.subarray(0,6)),n=J[l];if(!n)throw new Error("not an MRCNN model");if(n===8)return r;let t=q(o);t.str(6);let s=t.u32(),a=t.u32(),p=t.u32(),c=t.u32(),f=[],e=M=>{let v=new Uint8Array(4);return new DataView(v.buffer).setUint32(0,M,!0),v},i=M=>new Uint8Array(M.buffer,M.byteOffset,M.byteLength);f.push(new TextEncoder().encode("MRCNN1"),e(s),e(a),e(p),e(c));for(let M=0;M<c;M++){let v=t.u32(),x=t.u32(),b=t.u32(),N=t.u32(),S=t.f32s(v),k=t.f32s(v),F=t.ibits(v*x*b*b,n);f.push(e(v),e(x),e(b),e(N),i(S),i(k),new Uint8Array(F.buffer,F.byteOffset,F.length))}let u=t.u32(),g=t.u32(),y=t.f32s(u),m=t.f32s(u),d=t.ibits(u*g,n);f.push(e(u),e(g),i(y),i(m),new Uint8Array(d.buffer,d.byteOffset,d.length));let w=f.reduce((M,v)=>M+v.length,0),I=new Uint8Array(w),A=0;for(let M of f)I.set(M,A),A+=M.length;return I}function tt(o){return V(q(o))}function pt(o,r,l,n){let{cout:t,cin:s,k:a,w:p,bias:c}=o,f=l*l,e=a-1>>1,i=s*a*a;for(let u=0;u<t;u++){let g=u*f,y=c[u],m=u*i;for(let d=0;d<l;d++)for(let w=0;w<l;w++){let I=y;for(let A=0;A<s;A++){let M=A*f,v=m+A*a*a;for(let x=0;x<a;x++){let b=d+x-e;if(b<0||b>=l)continue;let N=M+b*l,S=v+x*a;for(let k=0;k<a;k++){let F=w+k-e;F<0||F>=l||(I+=p[S+k]*r[N+F])}}}n[g+d*l+w]=I>0?I:0}}}function gt(o,r,l,n){let t=r>>1;for(let s=0;s<l;s++){let a=s*r*r,p=s*t*t;for(let c=0;c<t;c++)for(let f=0;f<t;f++){let e=a+2*c*r+2*f,i=o[e],u=o[e+1],g=o[e+r],y=o[e+r+1];n[p+c*t+f]=Math.max(i,u,g,y)}}}function et(o,r){let l=o.size,n=Float32Array.from(r),t=l,s=1;for(let e of o.convs){let i=new Float32Array(e.cout*t*t);if(pt(e,n,t,i),n=i,s=e.cout,e.pool){let u=new Float32Array(s*(t>>1)*(t>>1));gt(n,t,s,u),n=u,t>>=1}}let a=t*t,p=new Float32Array(s);for(let e=0;e<s;e++){let i=0;for(let u=0;u<a;u++)i+=n[e*a+u];p[e]=i/a}let{lin:c}=o,f=new Float32Array(c.out);for(let e=0;e<c.out;e++){let i=c.bias[e];for(let u=0;u<c.in;u++)i+=c.w[e*c.in+u]*p[u];f[e]=i}return f}function nt(o){let r=0;for(let n=0;n<o.length;n++)r+=o[n]*o[n];r=1/(Math.sqrt(r)+1e-9);let l=new Float32Array(o.length);for(let n=0;n<o.length;n++)l[n]=o[n]*r;return l}function Y(o,r){let l=new Float32Array(o.out);for(let n=0;n<o.out;n++){let t=o.bias[n];for(let s=0;s<o.in;s++){let a=r[s];a>0&&(t+=o.w[n*o.in+s]*a)}l[n]=t}return l}function z(o){let r=-1/0;for(let t of o)t>r&&(r=t);let l=new Float32Array(o.length),n=0;for(let t=0;t<o.length;t++)l[t]=Math.exp(o[t]-r),n+=l[t];for(let t=0;t<o.length;t++)l[t]/=n;return l}function ot(o,r,l=120,n=7,t=10){let s=1/0,a=1/0,p=-1/0,c=-1/0;for(let i of r)for(let u of i)u.x<s&&(s=u.x),u.x>p&&(p=u.x),u.y<a&&(a=u.y),u.y>c&&(c=u.y);let f=l/Math.max(p-s,c-a,1),e=l+2*t;o.fillStyle="#fff",o.fillRect(0,0,e,e),o.strokeStyle="#000",o.lineWidth=n,o.lineCap="round",o.lineJoin="round";for(let i of r)if(!(i.length<2)){o.beginPath(),o.moveTo((i[0].x-s)*f+t,(i[0].y-a)*f+t);for(let u=1;u<i.length;u++)o.lineTo((i[u].x-s)*f+t,(i[u].y-a)*f+t);o.stroke()}return e}function rt(o,r,l){let n=new Float32Array(r*l);for(let t=0,s=0;t<n.length;t++,s+=4)n[t]=1-(o[s]*.299+o[s+1]*.587+o[s+2]*.114)/255;return n}function st(o,r,l,n=32,t=24){let s=1/0,a=-1,p=1/0,c=-1;for(let x=0;x<l;x++)for(let b=0;b<r;b++)o[x*r+b]>.15&&(b<s&&(s=b),b>a&&(a=b),x<p&&(p=x),x>c&&(c=x));let f=new Float32Array(n*n);if(a<0)return f;let e=a-s+1,i=c-p+1,u=new Float32Array(e*i);for(let x=0;x<i;x++)for(let b=0;b<e;b++)u[x*e+b]=o[(x+p)*r+(b+s)];let g=0;for(let x=0;x<u.length;x++)u[x]>g&&(g=u[x]);if(g>.05&&g<.999)for(let x=0;x<u.length;x++)u[x]=Math.min(1,u[x]/g);let y=t/Math.max(i,e),m=Math.max(1,Math.round(i*y)),d=Math.max(1,Math.round(e*y)),w=new Float32Array(d*m),I=e/d,A=i/m;for(let x=0;x<m;x++){let b=x*A,N=(x+1)*A,S=Math.floor(b),k=Math.min(i,Math.ceil(N));for(let F=0;F<d;F++){let B=F*I,j=(F+1)*I,P=Math.floor(B),H=Math.min(e,Math.ceil(j)),O=0,T=0;for(let R=S;R<k;R++){let D=Math.min(N,R+1)-Math.max(b,R);if(!(D<=0))for(let h=P;h<H;h++){let C=Math.min(j,h+1)-Math.max(B,h);C<=0||(O+=u[R*e+h]*C*D,T+=C*D)}}w[x*d+F]=T>0?O/T:0}}let M=n-m>>1,v=n-d>>1;for(let x=0;x<m;x++)for(let b=0;b<d;b++)f[(x+M)*n+b+v]=w[x*d+b];return f}function it(o){let r=o?String(o):"";return r?(r.endsWith("/")||(r+="/"),typeof location<"u"?new URL(r,location.href).href:r):typeof location<"u"?new URL("./",location.href).href:""}var X=o=>Array.isArray(o)&&o.length&&!Array.isArray(o[0])?[o]:o,K=(o,r,l,n)=>new Error(`millirec: ${o} is not ${r} \u2014 the package files are not served at ${l}; copy node_modules/millirec/dist/${n}/ and pass assetBase (see README "Serving the files")`);async function at(o,{as:r="json",what:l,base:n,recognizer:t}){let s=e=>{let i=K(o,l,n,t);return e&&(i.message+=` (${e})`),i},a;try{a=await fetch(o)}catch(e){throw s(e&&e.message)}let p=a.headers&&a.headers.get("content-type")||"";if(!a.ok)throw s(`HTTP ${a.status}`);if(r==="json")try{return await a.json()}catch(e){throw s(/html/i.test(p)?"an HTML page":e&&e.message)}let c=await a.arrayBuffer(),f=String.fromCharCode(...new Uint8Array(c,0,Math.min(c.byteLength,16))).trimStart().toLowerCase();if(f.startsWith("<!doctype")||f.startsWith("<html"))throw s("an HTML page");return c}function mt(){let o=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return o?o.slice(0,o.lastIndexOf("/")+1):""}var xt=mt(),lt=(o,r)=>at(o,{as:"buf",what:"a char-cnn model file",base:r,recognizer:"char-cnn"}),wt="millirec: recognizer destroyed",bt=o=>typeof document<"u"&&document.createElement?Object.assign(document.createElement("canvas"),{width:o,height:o}):typeof OffscreenCanvas<"u"?new OffscreenCanvas(o,o):null,$=class{static async create(r={}){let n={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...r};if(!n.stages||!n.stages.length)throw new Error("MillirecCharCnn: no stages configured");if(typeof fetch>"u"||typeof document>"u"&&typeof OffscreenCanvas>"u")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");let t=new this(n);return await t.ready,t}constructor(r){this.config=r,this.base=it(r.assetBase||xt),this.classes=typeof r.classes=="string"?r.classes.split(""):r.classes,this.stages=[],this.off=bt(140),this.octx=this.off?this.off.getContext("2d",{willReadFrequently:!0}):null,this.ready=this.load()}async load(){let{base:r,config:l}=this,n=null;if(l.kernel){let t=r+l.kernel;try{n=(await import(t)).default}catch(s){let a=K(t,"the char-cnn kernel",r,"char-cnn");throw a.message+=` (${s&&s.message})`,a}}for(let t of l.stages){let[s,a]=await Promise.all([lt(r+t.model,r),lt(r+t.head,r)]);if(this.destroyed)return;let p=tt(a),c={theta:t.theta==null?null:t.theta,head:p,headNorm:t.headNorm!==!1,size:0};if(n){let f=await n();if(this.destroyed)return;let e=Q(new Uint8Array(s)),i=f._malloc(e.byteLength);f.HEAPU8.set(e,i);let u=f._cnnLoad(i,e.byteLength);if(f._free(i),!u)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let g=f._cnnSize(),y=f._cnnDim();c.size=g,c.embed=m=>{let d=f._cnnInputPtr(1)>>2;f.HEAPF32.set(m,d),f._cnnRun(1);let w=f._cnnEmbedPtr(1)>>2;return f.HEAPF32.slice(w,w+y)},c.logits=m=>Y(c.head,c.embed(m))}else{let f=G(s);c.size=f.size,c.embed=e=>{let i=et(f,e);return c.headNorm?nt(i):i},c.logits=e=>Y(c.head,c.embed(e))}this.stages.push(c)}}raster(r,l){if(!this.octx)throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");if(this._planeFor!==r){let s=ot(this.octx,r);this._plane={ink:rt(this.octx.getImageData(0,0,s,s).data,s,s),W:s},this._planeFor=r}let{ink:n,W:t}=this._plane;return st(n,t,t,l,Math.round(l*.75))}async recognize(r,{only:l=null}={}){if(r=X(r),!r||!r.length||(await this.ready,this.destroyed||!this.stages.length))return null;this._planeFor=null;let n=l?new Set(l):null,t=null,s=0;for(let e=0;e<this.stages.length;e++){let i=this.stages[e];if((this.personal||n)&&e<this.stages.length-1)continue;if(t=i.logits(this.raster(r,i.size)),s=e,i.theta==null||e===this.stages.length-1)break;let u=-1/0,g=-1/0;for(let y of t)y>u?(g=u,u=y):y>g&&(g=y);if(u-g>=i.theta)break}let a=[...t.keys()];if(n&&(a=a.filter(e=>n.has(this.classes[e])),!a.length)){let e=[];return e.stage=s,e}let p=z(a.map(e=>t[e])),f=[...p.keys()].sort((e,i)=>p[i]-p[e]).slice(0,3).map(e=>({char:this.classes[a[e]],prob:p[e]}));return f.stage=s,f}async embedding(r){if(r=X(r),await this.ready,this.destroyed||!this.stages.length)throw new Error(wt);this._planeFor=null;let l=this.stages[this.stages.length-1];return l.embed(this.raster(r,l.size))}async personalize(r,{steps:l=250,lr:n=.5,prior:t=1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let s=this.stages[this.stages.length-1],a=s.head0||(s.head0=s.head),p=[],c=[];for(let[g,y]of Object.entries(r||{})){let m=this.classes.indexOf(g);if(!(m<0))for(let d of y||[]){let w=X(d);!w||!w.length||(p.push(await this.embedding(w)),c.push(m))}}if(this.destroyed)return null;if(!p.length)return s.head=a,this.personal=null,null;let f={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},e=p.length,i=new Float32Array(f.w.length),u=new Float32Array(f.out);for(let g=0;g<l;g++){i.fill(0),u.fill(0);for(let m=0;m<e;m++){let d=z(Y(f,p[m])),w=p[m];for(let I=0;I<f.out;I++){let A=(d[I]-(I===c[m]?1:0))/e;if(!A)continue;u[I]+=A;let M=I*f.in;for(let v=0;v<f.in;v++){let x=w[v];x>0&&(i[M+v]+=A*x)}}}let y=t/e;for(let m=0;m<f.w.length;m++)f.w[m]-=n*(i[m]+y*(f.w[m]-a.w[m]));for(let m=0;m<f.out;m++)f.bias[m]-=n*(u[m]+y*(f.bias[m]-a.bias[m]))}return s.head=f,this.personal={n:e,classes:[...new Set(c)].map(g=>this.classes[g]).sort()},this.personal}destroy(){this.destroyed=!0,this.stages=[],this.personal=null,this._plane=null,this._planeFor=null}depersonalize(){let r=this.stages[this.stages.length-1];r&&r.head0&&(r.head=r.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=$);var ht={turns:5,retrace:.5,winding:3.5,cover:.6,minLength:40,minSpan:12,deadband:.22},U=2;function ct(o){return{x0:o.x0-U,y0:o.y0-U,x1:o.x1+U,y1:o.y1+U,w:o.w+2*U,h:o.h+2*U}}function ft(o){let r=1/0,l=1/0,n=-1/0,t=-1/0;for(let s of o){let a=s.x!==void 0?s.x:s[0],p=s.y!==void 0?s.y:s[1];a<r&&(r=a),a>n&&(n=a),p<l&&(l=p),p>t&&(t=p)}return{x0:r,y0:l,x1:n,y1:t,w:n-r,h:t-l}}function ut(o,r){let l=1/0,n=-1/0;for(let c of o)c<l&&(l=c),c>n&&(n=c);let t=(n-l)*r;if(!(t>0))return 0;let s=0,a=o[0],p=0;for(let c of o)s>0?c>a?a=c:c<a-t&&(p++,s=-1,a=c):s<0?c<a?a=c:c>a+t&&(p++,s=1,a=c):c>a+t?(s=1,a=c):c<a-t&&(s=-1,a=c);return p}function Mt(o){let r=0,l=0;for(let u of o)r+=u.x,l+=u.y;r/=o.length,l/=o.length;let n=0,t=0,s=0;for(let u of o){let g=u.x-r,y=u.y-l;n+=g*g,t+=g*y,s+=y*y}let a=n+s,p=n*s-t*t,c=a/2+Math.sqrt(Math.max(0,a*a/4-p)),f=t,e=c-n;Math.hypot(f,e)<1e-9&&(f=1,e=0);let i=Math.hypot(f,e);return{ux:f/i,uy:e/i}}var vt=.09,It=4;function At(o,r){let l=Math.max(2,r*vt),n=l/2,t=[o[0]],s=0;for(let e=1;e<o.length;e++){let i=o[e].x-o[e-1].x,u=o[e].y-o[e-1].y,g=Math.hypot(i,u);if(!(g>0))continue;let y=(n-s)/g;for(;y<=1;)t.push({x:o[e-1].x+i*y,y:o[e-1].y+u*y}),y+=n/g;s=(s+g)%n}if(t.length<8)return 0;let a=Math.round(It*l/n),p=e=>`${Math.floor(e.x/l)},${Math.floor(e.y/l)}`,c=new Map;t.forEach((e,i)=>{let u=p(e);c.has(u)||c.set(u,[]),c.get(u).push(i)});let f=0;for(let e=0;e<t.length;e++){let i=t[e],u=Math.floor(i.x/l),g=Math.floor(i.y/l),y=!1;for(let m=-1;m<=1&&!y;m++)for(let d=-1;d<=1&&!y;d++)for(let w of c.get(`${u+m},${g+d}`)||[])if(!(Math.abs(e-w)<=a)&&Math.hypot(t[w].x-i.x,t[w].y-i.y)<=l){y=!0;break}y&&f++}return f/t.length}function Ct(o,r=[],l={}){let n={...ht,...l},t=[];for(let d of o){let w={x:d.x!==void 0?d.x:d[0],y:d.y!==void 0?d.y:d[1]},I=t[t.length-1];(!I||Math.hypot(w.x-I.x,w.y-I.y)>.75)&&t.push(w)}if(t.length<4)return{turns:0,cross:0,retrace:0,winding:0,cover:0,length:0,span:0};let s=0;for(let d=1;d<t.length;d++)s+=Math.hypot(t[d].x-t[d-1].x,t[d].y-t[d-1].y);let a=ft(t),p=Math.hypot(a.w,a.h),{ux:c,uy:f}=Mt(t),e=t.map(d=>d.x*c+d.y*f),i=t.map(d=>-d.x*f+d.y*c),u=ut(e,n.deadband),g=ut(i,n.deadband),y=0,m=[];for(let d of r)for(let w of d)m.push(w);if(m.length){let d=ct(ft(m)),w=ct(a),I=Math.max(0,Math.min(d.x1,w.x1)-Math.max(d.x0,w.x0)),A=Math.max(0,Math.min(d.y1,w.y1)-Math.max(d.y0,w.y0));y=I/Math.min(d.w,w.w)*(A/Math.min(d.h,w.h))}return{turns:u,cross:g,retrace:At(t,p),winding:p>0?s/p:0,cover:y,length:s,span:p}}function dt(o,r=[],l={}){let n={...ht,...l};if(!r||!r.length)return!1;let t=Ct(o,r,n);return t.length>=n.minLength&&t.span>=n.minSpan&&t.turns>=n.turns&&t.retrace>=n.retrace&&t.winding>=n.winding&&t.cover>=n.cover}var Et=12,W=()=>typeof performance<"u"&&performance.now?performance.now():Date.now();function yt({width:o=160,height:r=90,ink:l="#111827",onStrokeEnd:n,colorOf:t=null,idleOverwrite:s=0,scribbleClear:a=!1,scribble:p=null,onClear:c=null,staleAlpha:f=.3,onStale:e=null}){let i=document.createElement("canvas"),u=typeof devicePixelRatio<"u"&&devicePixelRatio||1;i.width=o*u,i.height=r*u,i.style.width=o+"px",i.style.height=r+"px",i.style.touchAction="none";let g=i.getContext("2d");g.scale(u,u);let y=[],m=[],d=null,w=!1,I=1,A=0,M=0,v=!1,x=null,b=!1;function N(h,C){if(!(h.length<2)){g.strokeStyle=C||l,g.beginPath(),g.moveTo(h[0].x,h[0].y);for(let E=1;E<h.length;E++)g.lineTo(h[E].x,h[E].y);g.stroke()}}function S(){g.clearRect(0,0,o,r),g.lineWidth=2.5,g.lineCap="round",g.lineJoin="round",g.globalAlpha=v?f:1;for(let h of y)N(h,t&&t(h));g.globalAlpha=1,N(m,null)}function k(h){v!==h&&(v=h,S(),e&&e(h))}function F(){clearTimeout(M),M=0;let h=(Number(s)||0)*1e3;if(!h||!y.length){k(!1);return}let C=h-(W()-A);if(C<=0){k(!0);return}k(!1),M=setTimeout(()=>k(!0),C)}function B(h){y.length&&(x=y,y=[],clearTimeout(M),M=0,k(!1),S(),c&&c(h))}function j(h,C){let E=y.filter(L=>!L.some(_=>Math.hypot(_.x-h,_.y-C)<=Et));return E.length===y.length?!1:(y=E,S(),!0)}let P=null,H=h=>{let C=P||(P=i.getBoundingClientRect()),E=o/(i.clientWidth||o),L=r/(i.clientHeight||r);return{x:(h.clientX-C.left-i.clientLeft)*E,y:(h.clientY-C.top-i.clientTop)*L,p:h.pressure||.5}},O=!1,T=null,R=h=>h.preventDefault();i.addEventListener("touchstart",R,{passive:!1}),i.addEventListener("touchmove",R,{passive:!1}),i.addEventListener("contextmenu",R),i.style.userSelect=i.style.webkitUserSelect="none",i.style.webkitTouchCallout="none",i.addEventListener("pointerdown",h=>{if(h.button>0)return;if(d!==null&&d!==h.pointerId){if(T==="pen"&&h.pointerType!=="pen")return;D({pointerId:d})}h.preventDefault(),d=h.pointerId,T=h.pointerType,P=i.getBoundingClientRect();try{i.setPointerCapture(h.pointerId)}catch{}let C=H(h);if(w){O=j(C.x,C.y);return}v&&y.length&&(B("idle"),b=!0),m=[C],S()},{passive:!1}),i.addEventListener("pointermove",h=>{if(d!==h.pointerId)return;h.preventDefault();let C=h.getCoalescedEvents?h.getCoalescedEvents():null,E=C&&C.length?C:[h];if(w){for(let L of E){let _=H(L);j(_.x,_.y)&&(O=!0)}return}for(let L of E)m.push(H(L));S()},{passive:!1});let D=(h,C=!1)=>{if(d===h.pointerId){try{i.releasePointerCapture(h.pointerId)}catch{}if(d=null,T=null,P=null,w){O&&(A=W(),F(),n&&n(y)),O=!1;return}if(m.length>0){let E=m;if(E.length===1){if(C){m=[],S(),b&&(b=!1,n&&n(y));return}let _=E[0];E=[{x:_.x-1,y:_.y,p:_.p},{x:_.x+1,y:_.y,p:_.p}]}let L=y.length?y:b?x:null;if(a&&L&&L.length&&dt(E,L,p||{})){m=[],A=W(),b=!1,y.length?B("scribble"):(S(),c&&c("scribble")),n&&n(y);return}E.id=I++,y=[...y,E],m=[],b=!1,A=W(),F(),n&&n(y)}S()}};return i.addEventListener("pointerup",h=>D(h)),i.addEventListener("pointercancel",h=>D(h,!0)),{el:i,getStrokes:()=>y,setInk(h){l=h,S()},setErasing(h){w=!!h,i.style.cursor=w?"cell":"crosshair"},isErasing:()=>w,repaint:S,isStale:()=>v,undoClear(){return!x||!x.length?!1:(y=y.length?[...x,...y]:x,x=null,A=W(),F(),S(),n&&n(y),!0)},configure(h={}){"idleOverwrite"in h&&(s=Number(h.idleOverwrite)||0),"scribbleClear"in h&&(a=!!h.scribbleClear),"scribble"in h&&(p=h.scribble),"staleAlpha"in h&&(f=Number(h.staleAlpha)),F()},clear(){y=[],m=[],d=null,T=null,P=null,clearTimeout(M),M=0,k(!1),b=!1,S(),n&&n(y)}}}var Z=class extends ${attach(r,{width:l=240,height:n=240,ink:t="#1e293b",idleOverwrite:s=2,scribbleClear:a=!0,scribble:p=null,staleAlpha:c,onStale:f}={}){let e=yt({width:l,height:n,ink:t,idleOverwrite:s,scribbleClear:a,scribble:p,onStale:f,...c===void 0?{}:{staleAlpha:c},onClear:i=>r.dispatchEvent(new CustomEvent("millirec:clear",{detail:{reason:i,undo:()=>e.undoClear()}})),onStrokeEnd:async i=>{if(!i.length){r.dispatchEvent(new CustomEvent("millirec:result",{detail:{results:[],strokes:[]}}));return}let u=await this.recognize(i);r.dispatchEvent(new CustomEvent("millirec:result",{detail:{results:u,strokes:i}}))}});return r.appendChild(e.el),{clear:e.clear,undoClear:e.undoClear,isStale:e.isStale,configure:e.configure,setInk:e.setInk,repaint:e.repaint,getStrokes:e.getStrokes,canvas:e.el}}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=Z);})();
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// accurate: the most accurate, still fast and small
|
|
2
|
+
// 98.49% top-1, 0.24 ms mean via the kernel (worst case 0.9 ms), 20% escalated, 37 KB gzipped with the pad
|
|
3
|
+
window.MILLIREC_CNN = {
|
|
4
|
+
"classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
5
|
+
"stages": [
|
|
6
|
+
{
|
|
7
|
+
"model": "tiny.bin",
|
|
8
|
+
"head": "tiny.head.bin",
|
|
9
|
+
"theta": 2.032
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"model": "base.q4.bin",
|
|
13
|
+
"head": "base.head.bin"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"kernel": "cnn-web.js"
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// fast: most accurate under 0.10 ms per character
|
|
2
|
+
// 97.77% top-1 on EMNIST uppercase (7,800 test images), 0.095 ms mean via the kernel, 5% escalated
|
|
3
|
+
window.MILLIREC_CNN = {
|
|
4
|
+
"classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
5
|
+
"stages": [
|
|
6
|
+
{
|
|
7
|
+
"model": "tiny.bin",
|
|
8
|
+
"head": "tiny.head.bin",
|
|
9
|
+
"theta": 0.89
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"model": "base-p25.bin",
|
|
13
|
+
"head": "base-p25.head.bin"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"kernel": "cnn-web.js"
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// small: most accurate under 10 KB gzipped
|
|
2
|
+
// 96.36% top-1, 0.89 ms in plain JS (0.06 ms with the kernel, which adds 16 KB), 8.0 KB gzipped with the pad-less runtime
|
|
3
|
+
window.MILLIREC_CNN = {
|
|
4
|
+
"classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
5
|
+
"stages": [
|
|
6
|
+
{
|
|
7
|
+
"model": "tiny.bin",
|
|
8
|
+
"head": "tiny.head.bin"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export interface Point { x: number; y: number }
|
|
2
|
+
/** One stroke is a polyline; a drawing is one or more strokes. */
|
|
3
|
+
export type Stroke = Point[]
|
|
4
|
+
export type Strokes = Stroke[]
|
|
5
|
+
/** A delta-encoded sample: first value absolute, then differences. */
|
|
6
|
+
export interface Sample { x: number[]; y: number[] }
|
|
7
|
+
export interface Reading { char: string; prob: number }
|
|
8
|
+
|
|
9
|
+
/** v2 training data (core/format/schema.js). v1 `{ class: Sample[] }` is also accepted.
|
|
10
|
+
* `version` and `mode` are wide (number, string) so a set imported as JSON type-checks. */
|
|
11
|
+
export interface Training {
|
|
12
|
+
/** 2 */
|
|
13
|
+
version: number
|
|
14
|
+
/** 'stroke' | 'image' | 'both' */
|
|
15
|
+
mode?: 'stroke' | 'image' | 'both' | (string & {})
|
|
16
|
+
pointsPerShape?: number
|
|
17
|
+
classes: Record<string, { samples: Sample[]; geometry?: unknown }>
|
|
18
|
+
params?: Record<string, number>
|
|
19
|
+
ext?: Record<string, unknown>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CreateOptions {
|
|
23
|
+
training: Training | Record<string, Sample[]>
|
|
24
|
+
/** Folder holding worker-stroke.js and millirec.wasm. Default: next to this module. Absolute,
|
|
25
|
+
* root-relative or page-relative; the trailing slash is optional. It decides what code is loaded:
|
|
26
|
+
* never fill it from a URL parameter or user input. */
|
|
27
|
+
assetBase?: string
|
|
28
|
+
params?: Record<string, number>
|
|
29
|
+
workerStroke?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface AttachOptions {
|
|
33
|
+
/** Pad size in CSS px (default 240 x 240). */
|
|
34
|
+
width?: number
|
|
35
|
+
height?: number
|
|
36
|
+
/** Ink colour (default '#1e293b'). */
|
|
37
|
+
ink?: string
|
|
38
|
+
/** Seconds of pen-up after which the next stroke starts a new character (default 2; 0 = off). */
|
|
39
|
+
idleOverwrite?: number
|
|
40
|
+
/** Scribbling over the character throws it away (default true). */
|
|
41
|
+
scribbleClear?: boolean
|
|
42
|
+
/** Overrides for the scribble-out detector's bars (turns, retrace, winding, cover). */
|
|
43
|
+
scribble?: Record<string, number> | null
|
|
44
|
+
/** Opacity of ink once the character has settled (default 0.3). */
|
|
45
|
+
staleAlpha?: number
|
|
46
|
+
onStale?: (stale: boolean) => void
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface Pad {
|
|
50
|
+
clear(): void
|
|
51
|
+
/** Put back ink the pad dropped by itself; false when there was none. */
|
|
52
|
+
undoClear(): boolean
|
|
53
|
+
isStale(): boolean
|
|
54
|
+
getStrokes(): Strokes
|
|
55
|
+
/** Change idleOverwrite, scribbleClear, scribble or staleAlpha on a mounted pad. */
|
|
56
|
+
configure(opts: Pick<AttachOptions, 'idleOverwrite' | 'scribbleClear' | 'scribble' | 'staleAlpha'>): void
|
|
57
|
+
setInk(color: string): void
|
|
58
|
+
repaint(): void
|
|
59
|
+
canvas: HTMLCanvasElement
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `millirec:result` — after every stroke (and with empty results when the pad empties). */
|
|
63
|
+
export interface ResultDetail { results: Reading[] | null; strokes: Strokes }
|
|
64
|
+
/** `millirec:clear` — the pad dropped its own ink: 'idle' (idleOverwrite) or 'scribble'. `undo()` puts it back. */
|
|
65
|
+
export interface ClearDetail { reason: 'idle' | 'scribble'; undo: () => boolean }
|
|
66
|
+
|
|
67
|
+
declare global {
|
|
68
|
+
interface HTMLElementEventMap {
|
|
69
|
+
'millirec:result': CustomEvent<ResultDetail>
|
|
70
|
+
'millirec:clear': CustomEvent<ClearDetail>
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export declare const SETS: readonly ['all', 'capitals', 'digits', 'lowercase', 'punctuation']
|
|
75
|
+
export type SetName = (typeof SETS)[number]
|
|
76
|
+
|
|
77
|
+
/** Fetch a shipped training set. */
|
|
78
|
+
export declare function loadSet(name: SetName, opts?: { assetBase?: string }): Promise<Training>
|
|
79
|
+
|
|
80
|
+
export declare class MillirecCharLite {
|
|
81
|
+
static create(opts: CreateOptions): Promise<MillirecCharLite>
|
|
82
|
+
/** Strokes → one sample. Pass the set's pointsPerShape (shipped sets: 25). */
|
|
83
|
+
static strokesToSample(strokes: Strokes | Stroke, pointsPerShape?: number): Sample | null
|
|
84
|
+
/** A copy of `training` with your own drawings added per class (new classes allowed). */
|
|
85
|
+
static withSamples(training: Training | Record<string, Sample[]>, extra: Record<string, Array<Strokes | Stroke | Sample>>, opts?: { replace?: boolean }): Training
|
|
86
|
+
static createDrawCanvas(opts?: object): unknown
|
|
87
|
+
readonly charList: string[]
|
|
88
|
+
readonly ready: Promise<void>
|
|
89
|
+
/** null for an empty drawing, and after destroy(). */
|
|
90
|
+
recognize(strokes: Strokes | Stroke): Promise<Reading[] | null>
|
|
91
|
+
/** Raw per-template costs: costs[i] holds one cost per training sample of classes[i] (lower is closer). */
|
|
92
|
+
rawScores(strokes: Strokes | Stroke): Promise<{ classes: string[]; costs: number[][] } | null>
|
|
93
|
+
use(plugin: { rerank?: (out: unknown, ctx: unknown) => unknown }): this
|
|
94
|
+
/** Mount a drawing pad in `el`; `el` then fires 'millirec:result' and 'millirec:clear'. */
|
|
95
|
+
attach(el: HTMLElement, opts?: AttachOptions): Pad
|
|
96
|
+
/** Stop the instance's Worker (each instance holds one, plus a wasm matcher). Pending and later
|
|
97
|
+
* recognize()/rawScores() calls resolve null. */
|
|
98
|
+
destroy(): void
|
|
99
|
+
}
|