-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcbl.min.js
1 lines (1 loc) · 25.8 KB
/
cbl.min.js
1
var CBL=function(g){var s={preprocess:function(){D("You should define a preprocess method!")},model_file:"",model_string:"",model_loaded:function(){},training_complete:function(){},blob_min_pixels:1,blob_max_pixels:99999,blob_min_width:1,blob_min_height:1,blob_max_width:99999,blob_max_height:99999,pattern_width:20,pattern_height:20,pattern_maintain_ratio:false,pattern_auto_rotate:false,incorrect_segment_char:"\\",blob_debug:"",blob_console_debug:false,allow_console_log:false,allow_console_warn:true,perceptive_colorspace:false,character_set:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",fixed_blob_locations:[],exact_characters:-1,exact_characters_width:-1,exact_characters_play:-1};g=g||{};for(var b in s){if(s.hasOwnProperty(b)&&!g.hasOwnProperty(b)){g[b]=s[b]}}var u={solve:function(H){return u.train(H,true)},done:function(H){y(function(){H(w);o()})},train:function(H,I){if(typeof I==="undefined"){I=false}y(function(){var K;var J=false;if(document.getElementById(H)!=null){K=document.getElementById(H)}else{K=document.createElement("img");J=true}var L=function(){var M="";var N=document.createElement("canvas");N.width=K.width;N.height=K.height;N.getContext("2d").drawImage(K,0,0);var P=new A(N);g.preprocess(P);var S;if(g.fixed_blob_locations.length>0){S=P.segmentBlocks(g.pattern_width,g.pattern_height,g.fixed_blob_locations,g.blob_debug)}else{S=P.segmentBlobs(g.blob_min_pixels,g.blob_max_pixels,g.pattern_width,g.pattern_height,g.blob_debug)}if(!I){for(var O=0;O<S.length;O++){var R=S[O].toDataURL();var Q=t(S[O]);C.push({imgSrc:R,pattern:Q,imgId:j,txtId:f,self:u,onComplete:g.training_complete})}if(!G){u.loadNextPattern()}G=true}else{for(var O=0;O<S.length;O++){M+=m(t(S[O]))}p("Solution = "+M)}w=M;o()};if(K.complete&&!J){L()}else{K.onload=L;if(J){K.src=H}}});return this},loadNextPattern:function(){var H=C.pop();if(H){p("Loading a pattern for human classification.");B();document.getElementById(H.imgId).src=H.imgSrc;document.getElementById(H.txtId).focus();document.getElementById(H.txtId).onkeyup=function(I){var J=document.getElementById(H.txtId).value;if((g.character_set.indexOf(J)>-1&&J.length)||J==g.incorrect_segment_char){if(J!=g.incorrect_segment_char){h.push({pattern:H.pattern,solution:document.getElementById(H.txtId).value});p('Added "'+document.getElementById(H.txtId).value+'" pattern to model!')}else{p("Did not add bad segment to model.")}document.getElementById(H.txtId).value="";if(C.length){H.self.loadNextPattern()}else{G=false;document.getElementById(H.txtId).onkeyup=function(){};if(typeof H.onComplete==="function"){H.onComplete();e()}}}else{document.getElementById(H.txtId).value=""}}}},loadModelString:function(K){K=v.decompressFromBase64(K);h=new Array();var J=K.replace(/\[/g,"").split("]");for(var H=0;H<J.length;H++){var M=J[H].split("=");if(M.length==2){var L=M[1];var I=M[0];h.push({pattern:L,solution:I})}}if(!h.length){D("No patterns to load in provided model.")}else{p("Model loaded with "+h.length+" patterns!");g.model_loaded()}},loadModel:function(H){try{var J=new XMLHttpRequest();J.open("GET",H,true);J.send();J.onreadystatechange=function(){if(J.readyState==4&&J.status==200&&J.responseText){u.loadModelString(J.responseText)}}}catch(I){D('Could not load model from "'+H+'"! ('+I.message+")")}},serializeModel:function(){var I="";for(var H=0;H<h.length;H++){I+="["+h[H].solution+"="+h[H].pattern+"]"}I=v.compressToBase64(I);return I},saveModel:function(){var I=u.serializeModel();var H=document.createElement("a");H.href="data:application/octet-stream,"+encodeURIComponent(I);H.setAttribute("download","cbl-model.dat");H.click()},debugModel:function(){for(var H=0;H<h.length;H++){p(h[H].solution+" pattern length = "+h[H].pattern.split(".").length)}},sortModel:function(){h=h.sort(function(I,H){return I.solution.localeCompare(H.solution)})},visualizeModel:function(K){for(var I=0;I<h.length;I++){var M=document.createElement("canvas");M.width=g.pattern_width;M.height=g.pattern_height;var O=M.getContext("2d").getImageData(0,0,g.pattern_width,g.pattern_height);var Q=h[I].pattern.split(".");for(var P=0;P<g.pattern_width;P++){for(var N=0;N<g.pattern_height;N++){var J=P*4+N*4*g.pattern_width;var H=N+P*g.pattern_width;O.data[J]=Q[H];O.data[J+1]=Q[H];O.data[J+2]=Q[H];O.data[J+3]=255}}M.getContext("2d").putImageData(O,0,0);var L=document.createElement("img");L.src=M.toDataURL();document.getElementById(K).appendChild(L)}},condenseModel:function(){var M=new Array();var J=h.length;for(var L=0;L<h.length;L++){var I=h[L].pattern.split(".");var N=false;for(var K=0;K<M.length;K++){if(M[K].solution==h[L].solution){for(var H=0;H<M[K].tempArray.length;H++){M[K].tempArray[H]=parseInt(M[K].tempArray[H])+parseInt(I[H])}M[K].tempCount++;N=true;break}}if(!N){M.push({pattern:h[L].pattern,solution:h[L].solution,tempArray:I,tempCount:1})}}for(var L=0;L<M.length;L++){for(var H=0;H<M[L].tempArray.length;H++){M[L].tempArray[H]=Math.round(M[L].tempArray[H]/M[L].tempCount)}M[L].pattern=M[L].tempArray.join(".")}h=M;p("Condensed model from "+J+" patterns to "+h.length+" patterns!");return this}};var A=function(H){var I={colorRegions:function(L,O,Q){if(typeof O==="undefined"){O=false}if(typeof Q==="undefined"){Q=0}var K=new Array();var N=H.getContext("2d").getImageData(0,0,H.width,H.height);for(var J=0;J<N.width;J++){for(var P=0;P<N.height;P++){var M=J*4+P*4*N.width;if(!E(K,M)){I.floodfill(J,P,z(),L,N,K,O,Q)}}}H.getContext("2d").putImageData(N,0,0);return this},display:function(J){document.getElementById(J).src=H.toDataURL();return this},debugImage:function(J){var K=document.createElement("img");K.src=H.toDataURL();document.getElementById(J).appendChild(K);return this},floodfill:function(P,N,ae,Z,U,ab,X,S){var V=false;if(typeof U==="undefined"){V=true;U=H.getContext("2d").getImageData(0,0,H.width,H.height)}if(typeof S==="undefined"){S=0}var ad=U.data;var K=ad.length;var L=[];var Y=(P+N*U.width)*4;var aa=Y,R=Y,ac,T,O=U.width*4;var J=[ad[Y],ad[Y+1],ad[Y+2],ad[Y+3]];var M=ad[Y]+ad[Y+1]+ad[Y+2]+ad[Y+3];if(!a(Y,J,M,ae,ad,K,Z)){return false}L.push(Y);while(L.length){Y=L.pop();if(typeof ab!=="undefined"){if(E(ab,Y)){continue}}if(r(Y,J,M,ae,ad,K,Z,ab,X)){aa=Y;R=Y;T=(Y/O)*O;ac=T+O;while(T<(R-=4)&&r(R,J,M,ae,ad,K,Z,ab,X)){}while(ac>(aa+=4)&&r(aa,J,M,ae,ad,K,Z,ab,X)){}if(S>0){R-=S*4;aa+=S*4}for(var W=R;W<aa;W+=4){if(W-O>=0&&a(W-O,J,M,ae,ad,K,Z)){L.push(W-O)}if(W+O<K&&a(W+O,J,M,ae,ad,K,Z)){L.push(W+O)}}}}if(V){H.getContext("2d").putImageData(U,0,0)}},blur:function(J){if(typeof J==="undefined"){J=1}if(J==2){return this.convolute([[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]],1/25)}else{if(J==3){return this.convolute([[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1]],1/49)}else{return this.convolute([[1,1,1],[1,1,1],[1,1,1]],1/9)}}},sharpen:function(){return this.convolute([[0,-1,0],[-1,5,-1],[0,-1,0]])},grayscale:function(){var M=H.getContext("2d").getImageData(0,0,H.width,H.height);for(var J=0;J<M.width;J++){for(var N=0;N<M.height;N++){var K=J*4+N*4*M.width;var L=0.34*M.data[K]+0.5*M.data[K+1]+0.16*M.data[K+2];M.data[K]=L;M.data[K+1]=L;M.data[K+2]=L;M.data[K+3]=255}}H.getContext("2d").putImageData(M,0,0);return this},removeGray:function(K){var N=H.getContext("2d").getImageData(0,0,H.width,H.height);for(var J=0;J<N.width;J++){for(var O=0;O<N.height;O++){var L=J*4+O*4*N.width;var M=Math.max(Math.abs(N.data[L]-N.data[L+1]),Math.abs(N.data[L+1]-N.data[L+2]),Math.abs(N.data[L+2]-N.data[L]));if(M<K){N.data[L]=255;N.data[L+1]=255;N.data[L+2]=255;N.data[L+3]=255}}}H.getContext("2d").putImageData(N,0,0);return this},removeLight:function(N){var M=H.getContext("2d").getImageData(0,0,H.width,H.height);for(var J=0;J<M.width;J++){for(var O=0;O<M.height;O++){var K=J*4+O*4*M.width;var L=Math.max(M.data[K],M.data[K+1],M.data[K+2]);if(L>N){M.data[K]=255;M.data[K+1]=255;M.data[K+2]=255;M.data[K+3]=255}}}H.getContext("2d").putImageData(M,0,0);return this},binarize:function(K){var N=H.getContext("2d").getImageData(0,0,H.width,H.height);for(var J=0;J<N.width;J++){for(var O=0;O<N.height;O++){var L=J*4+O*4*N.width;var M=0.34*N.data[L]+0.5*N.data[L+1]+0.16*N.data[L+2];N.data[L]=M>=K?255:0;N.data[L+1]=M>=K?255:0;N.data[L+2]=M>=K?255:0;N.data[L+3]=255}}H.getContext("2d").putImageData(N,0,0);return this},convolute:function(W,S){var L=H.getContext("2d").getImageData(0,0,H.width,H.height);var O=H.getContext("2d").getImageData(0,0,H.width,H.height);var Y=W[0].length;var P=W.length;var Z=Math.floor(P/2);if(typeof S==="undefined"){S=1}var R=0;for(var U=0;U<L.height-1;U++){for(var V=0;V<L.width-1;V++){var X=V*4+U*4*L.width;var J=0;var Q=0;var T=0;for(var K=0;K<Y;K++){for(var N=0;N<P;N++){var M=((U+(K-Z))*L.width+(V+(N-Z)))*4;J+=L.data[(M+L.data.length)%L.data.length]*W[K][N];Q+=L.data[(M+1+L.data.length)%L.data.length]*W[K][N];T+=L.data[(M+2+L.data.length)%L.data.length]*W[K][N]}}O.data[X+0]=S*J+R;O.data[X+1]=S*Q+R;O.data[X+2]=S*T+R;O.data[X+3]=255}}H.getContext("2d").putImageData(O,0,0);return this},erode:function(){return this.convolute([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])},foreach:function(M){var N=H.getContext("2d").getImageData(0,0,H.width,H.height);for(var J=0;J<N.width;J++){for(var O=0;O<N.height;O++){var L=J*4+O*4*N.width;var K={r:N.data[L+0],g:N.data[L+1],b:N.data[L+2]};K=M(K);N.data[L+0]=K.r;N.data[L+1]=K.g;N.data[L+2]=K.b;N.data[L+3]=255}}H.getContext("2d").putImageData(N,0,0);return this},removeTransparency:function(L,K){if(typeof L==="undefined"){L=128}if(typeof K==="undefined"){K={r:255,g:255,b:255}}var N=H.getContext("2d").getImageData(0,0,H.width,H.height);for(var J=0;J<N.width;J++){for(var O=0;O<N.height;O++){var M=J*4+O*4*N.width;if(N.data[M+3]<=L){N.data[M+0]=K.r;N.data[M+1]=K.g;N.data[M+2]=K.b;N.data[M+3]=255}}}H.getContext("2d").putImageData(N,0,0);return this},invert:function(J){return this.foreach(function(K){K.r=255-K.r;K.g=255-K.g;K.b=255-K.b;return K})},cropRelative:function(N,M,K,J){var L=H.getContext("2d").getImageData(N,M,H.width-N-K,H.height-M-J);H.width=H.width-N-K;H.height=H.height-M-J;H.getContext("2d").putImageData(L,0,0);return this},removeHorizontalLine:function(J,Y){if(typeof Y==="undefined"){Y={r:0,g:0,b:0}}if(typeof J==="undefined"){J=1}var U=H.getContext("2d").getImageData(0,0,H.width,H.height);var aa=[0,-1,1];var L=[];for(var M=0;M<H.height;M++){var K=this.getPixel(0,M);if(K.r==Y.r&&K.g==Y.g&&K.b==Y.b){L.push({x:0,y:M})}}var Q=[];for(var M=0;M<H.height;M++){var K=this.getPixel(H.width-1,M);if(K.r==Y.r&&K.g==Y.g&&K.b==Y.b){Q.push({x:H.width-1,y:M})}}var T=this;var R=10000;var W=[];var N=function(ah,ag,aj){if(false){var ae=ah*4+ag*4*U.width;U.data[ae+0]=255;U.data[ae+1]=0;U.data[ae+2]=0;U.data[ae+3]=255}if(R--<=0){return null}var ai=[];var ab=JSON.parse(JSON.stringify(aj));ab.points.push({x:ah,y:ag});if(ah>=H.width-1&&Q.some(function(ak){return ak.x==ab.points[ab.points.length-1].x&&ak.y==ab.points[ab.points.length-1].y})){return ab}var ac=T.getPixel(ah,ag);if(ac.r!=Y.r||ac.g!=Y.g||ac.b!=Y.b){return null}for(var af=0;af<aa.length;af++){if(ag+aa[af]>=0&&ag+aa[af]<H.height){ab.vertical+=Math.abs(aa[af]);if(W.some(function(ak){return ak.x==ah+1&&ak.y==ag+aa[af]})){continue}var ad=N(ah+1,ag+aa[af],ab);if(ad!=null){ai.push(ad);return ad}else{W.push({x:ah+1,y:ag+aa[af]})}}}return ai&&ai.length?ai.reduce(function(al,ak){return al.vertical<ak.vertical?al:ak}):null};for(var S=0;S<L.length;S++){var O=N(L[S].x,L[S].y,{points:[],vertical:0});if(O&&O.points){for(var V=0;V<O.points.length;V++){for(var P=-J;P<=J;P++){var Z=O.points[V].x*4+(O.points[V].y+P)*4*U.width;var X=O.points[V].x*4+(O.points[V].y+P-1)*4*U.width;U.data[Z+0]=U.data[X+0];U.data[Z+1]=U.data[X+1];U.data[Z+2]=U.data[X+2];U.data[Z+3]=255}}}}H.getContext("2d").putImageData(U,0,0);return this},getPixel:function(J,N){var M=H.getContext("2d").getImageData(0,0,H.width,H.height);var L=J*4+N*4*M.width;var K={r:M.data[L+0],g:M.data[L+1],b:M.data[L+2]};return K},segmentBlocks:function(W,L,U,V){if(typeof W==="undefined"){W=20}if(typeof L==="undefined"){L=20}if(typeof U==="undefined"){U=[]}var R=H.getContext("2d").getImageData(0,0,H.width,H.height);var aa=new Array();for(var ab=0;ab<U.length;ab++){var J=document.createElement("canvas");J.width=R.width;J.height=R.height;var T=J.getContext("2d").getImageData(0,0,H.width,H.height);var M=T.data;var S=0;var N=U[ab].x1;var X=U[ab].x2;var O=U[ab].y1;var Q=U[ab].y2;var Z=document.createElement("canvas");Z.width=X-N+1;Z.height=Q-O+1;Z.getContext("2d").putImageData(R,-N,-O,N,O,Z.width,Z.height);J.width=W;J.height=L;if(g.pattern_maintain_ratio){var K=Z.width;var P=Z.height;if(K/W>P/L){J.getContext("2d").drawImage(Z,0,0,W,P*(W/K))}else{J.getContext("2d").drawImage(Z,0,0,K*(L/P),L)}}else{J.getContext("2d").drawImage(Z,0,0,W,L)}aa.push(J);if(typeof V!=="undefined"&&V.length){if(g.blob_console_debug){p("Blob size = "+S)}var Y=document.createElement("img");Y.src=J.toDataURL();document.getElementById(V).appendChild(Y)}}return aa},segmentBlobs:function(ak,aB,ao,N,ad){if(typeof ak==="undefined"){ak=1}if(typeof aB==="undefined"){aB=100000}if(typeof ao==="undefined"){ao=20}if(typeof N==="undefined"){N=20}var M=H.getContext("2d").getImageData(0,0,H.width,H.height);var ax=function(aD,aC){return aD[aC]*255*255+aD[aC+1]*256+aD[aC+2]};var J=ax([255,255,255],0);var av=new Array();for(var ar=0;ar<M.width;ar++){for(var aq=0;aq<M.height;aq++){var ay=ar*4+aq*4*M.width;var P=ax(M.data,ay);if(!E(av,P)&&P!=J){av.push(P)}}}var ae=new Array();for(var az=0;az<av.length;az++){var af=document.createElement("canvas");af.width=M.width;af.height=M.height;var L=af.getContext("2d").getImageData(0,0,H.width,H.height);var T=L.data;var S=0;var at=M.width;var O=0;var K=M.height;var al=0;for(var ar=0;ar<M.width;ar++){for(var aq=0;aq<M.height;aq++){var ay=ar*4+aq*4*M.width;var P=ax(M.data,ay);if(P==av[az]){T[ay]=0;T[ay+1]=0;T[ay+2]=0;T[ay+3]=255;S++;if(ar<at){at=ar}if(ar>O){O=ar}if(aq<K){K=aq}if(aq>al){al=aq}}else{T[ay]=255;T[ay+1]=255;T[ay+2]=255;T[ay+3]=255}}}if(S>=ak&&S<=aB&&O-at>=g.blob_min_width&&al-K>=g.blob_min_height&&O-at<=g.blob_max_width&&al-K<=g.blob_max_height){var R=document.createElement("canvas");R.width=O-at+1;R.height=al-K+1;R.getContext("2d").putImageData(L,-at,-K,at,K,R.width,R.height);af.width=ao;af.height=N;af.orig_width=R.width;af.orig_image=R;if(g.pattern_maintain_ratio){var aa=R.width;var au=R.height;if(aa/ao>au/N){af.getContext("2d").drawImage(R,0,0,ao,au*(ao/aa))}else{af.getContext("2d").drawImage(R,0,0,aa*(N/au),N)}}else{af.getContext("2d").drawImage(R,0,0,ao,N)}if(g.pattern_auto_rotate){af=I.histogramRotate(af)}ae.push(af)}}if(g.exact_characters>0){while(ae.length<g.exact_characters){var ai=0;var aA=ae[0].orig_width;for(var ay=1;ay<ae.length;ay++){if(ae[ay].orig_width>aA){aA=ae[ay].orig_width;ai=ay}}var ap=2;if(g.exact_characters_width>0){ap=Math.ceil(aA/g.exact_characters_width);ap=Math.max(ap,2);ap=Math.min(ap,g.exact_characters-ae.length+1)}for(var ag=1;ag<=ap;ag++){var ac=l(ae[ai].orig_image);var L=ac.getContext("2d").getImageData(0,0,ac.width,ac.height);var W=ac.width/ap;at=Math.floor(W*(ag-1));O=Math.floor(W*ag);K=0;al=ac.height;if(g.exact_characters_play>0){var ah=0;var aw=0;for(var Z=at-g.exact_characters_play;Z<at+g.exact_characters_play;Z++){var V=0;for(var X=K;X<al;X++){var Y=x(L,Z,X);if(Y.r==255&&Y.g==255&&Y.b==255){V++}}if(V>ah){aw=Z;ah=V}}at=aw;var ab=0;var an=0;for(var Z=O+g.exact_characters_play;Z>O-g.exact_characters_play;Z--){var U=0;for(var X=K;X<al;X++){var Y=x(L,Z,X);if(Y.r==255&&Y.g==255&&Y.b==255){U++}}if(U>ab){an=Z;ab=U}}O=an}for(var X=K;X<al&&K==0;X++){for(var Z=at;Z<O&&K==0;Z++){var Y=x(L,Z,X);if(Y.r!=255||Y.g!=255||Y.b!=255){K=X}}}for(var X=al-1;X>K&&al==ac.height;X--){for(var Z=at;Z<O&&al==ac.height;Z++){var Y=x(L,Z,X);if(Y.r!=255||Y.g!=255||Y.b!=255){al=X}}}var R=document.createElement("canvas");R.width=O-at+1;R.height=al-K+1;R.getContext("2d").putImageData(L,-at,-K,at,K,R.width,R.height);ac.width=ao;ac.height=N;ac.orig_width=R.width;ac.orig_image=R;if(g.pattern_maintain_ratio){var aa=R.width;var au=R.height;if(aa/ao>au/N){ac.getContext("2d").drawImage(R,0,0,ao,au*(ao/aa))}else{ac.getContext("2d").drawImage(R,0,0,aa*(N/au),N)}}else{ac.getContext("2d").drawImage(R,0,0,ao,N)}if(g.pattern_auto_rotate){ac=I.histogramRotate(ac)}ae.splice(ai+ag,0,ac)}ae.splice(ai,1);if(g.blob_console_debug){p("Large blob divided")}}while(ae.length>g.exact_characters){var aj=0;var am=ae[0].orig_width;for(var ay=1;ay<ae.length;ay++){if(ae[ay].orig_width<am){am=ae[ay].orig_width;aj=ay}}ae.splice(aj,1);if(g.blob_console_debug){p("Small blob removed")}}}if(typeof ad!=="undefined"&&ad.length){for(var ay=0;ay<ae.length;ay++){if(g.blob_console_debug){p("Blob size = "+S)}var Q=document.createElement("img");Q.src=ae[ay].toDataURL();document.getElementById(ad).appendChild(Q)}}return ae},histogramRotate:function(J){var R=new Image();R.src=J.toDataURL();var Q=90;var N=5;var O=J;var L=J.width;for(var M=-Q/2;M<=Q/2;M+=N){var T=document.createElement("canvas");var S=T.getContext("2d");T.width=J.width;T.height=J.height;S.save();S.translate(J.width/2,J.height/2);S.rotate(M*Math.PI/180);S.drawImage(R,-R.width/2,-R.width/2);S.restore();var K=S.getImageData(0,0,T.width,T.height);var W=0;for(var V=0;V<K.width;V++){for(var U=0;U<K.height;U++){var P=V*4+U*4*K.width;if(K.data[P]!=255&&K.data[P+3]!=0){W++;break}}}S.putImageData(K,0,0);if(W<L){L=W;O=T}}return O}};return I};var h=new Array();var C=new Array();var G=false;var i=new Array();var d=false;var w="";var y=function(H){i.push(H);if(!d){o()}};var o=function(){if(i.length){d=true;i.shift()()}else{d=false}};var m=function(J){var K=4000000000;var H="?";for(var I=0;I<h.length;I++){var L=k(h[I].pattern,J);if(L<K){K=L;H=h[I].solution}}return H};var t=function(I){var K=new Array();var M=I.getContext("2d").getImageData(0,0,I.width,I.height);for(var H=0;H<M.width;H++){for(var N=0;N<M.height;N++){var J=H*4+N*4*M.width;var L=Math.round(0.34*M.data[J]+0.5*M.data[J+1]+0.16*M.data[J+2]);if(M.data[J+3]<255){L=255}K.push(L)}}return K.join(".")};var l=function(H){var J=document.createElement("canvas");var I=J.getContext("2d");J.width=H.width;J.height=H.height;I.drawImage(H,0,0);return J};var k=function(M,L){var J=M.split(".");var I=L.split(".");var K=0;for(var H=0;H<J.length;H++){K+=(J[H]-I[H])*(J[H]-I[H])}return Math.sqrt(K/J.length)};var a=function(M,H,N,J,L,I,P){if(M<0||M>=I){return false}var Q=q(H,0);var K=q(L,M);var O=J;if(F(Q,O)==0){return false}else{if(F(Q,K)==0){return true}}if(g.perceptive_colorspace){if(n(Q,K)<=P){return true}}else{if(F(Q,K)<=P){return true}}return false};var r=function(L,H,M,J,K,I,P,O,N){if(a(L,H,M,J,K,I,P)){if(typeof O!=="undefined"){if(E(O,L)){return false}}if(!(N&&K[L]==255&&K[L+1]==255&&K[L+2]==255)){K[L]=J.r;K[L+1]=J.g;K[L+2]=J.b;K[L+3]=J.a}if(typeof O!=="undefined"){O.push(L)}return true}return false};var q=function(I,H){return{r:I[H],g:I[H+1],b:I[H+2]}};var F=function(I,H){return Math.sqrt((Math.pow(I.r-H.r,2),Math.pow(I.g-H.g,2),Math.pow(I.g-H.g,2))/3)};var n=function(J,H){var K=function(P,N){var O=I(L(P));var M=I(L(N));return Math.sqrt(Math.pow(O.l-M.l,2)+Math.pow(O.a-M.a,2)+Math.pow(O.b-M.b,2))};var L=function(P){var N=P.r/255;var M=P.g/255;var O=P.b/255;N=N>0.04045?Math.pow((N+0.055)/1.055,2.4):(N/12.92);M=M>0.04045?Math.pow((M+0.055)/1.055,2.4):(M/12.92);O=O>0.04045?Math.pow((O+0.055)/1.055,2.4):(O/12.92);N=N*100;M=M*100;O=O*100;return{x:N*0.4124+M*0.3576+O*0.1805,y:N*0.2126+M*0.7152+O*0.0722,z:N*0.0193+M*0.1192+O*0.9505}};var I=function(P){var O=P.x/95.047;var N=P.y/100;var M=P.z/108.883;O=O>0.008856?Math.pow(O,1/3):(7.787*O)+(16/116);N=N>0.008856?Math.pow(N,1/3):(7.787*N)+(16/116);M=M>0.008856?Math.pow(M,1/3):(7.787*M)+(16/116);return{l:(116*N)-16,a:500*(O-N),b:200*(N-M)}};return K(J,H)};var E=function(H,J){for(var I=0;I<H.length;I++){if(H[I]===J){return true}}return false};var c=function(J,I,H){return{r:J,g:I,b:H,a:255}};var z=function(){var J=Math.round(Math.random()*200)+55;var I;var H;while((I=Math.round(Math.random()*200)+55)==J){}while((H=Math.round(Math.random()*200)+55)==J||H==I){}return c(J,I,H)};var x=function(K,H,L){var J=H*4+L*4*K.width;var I={r:K.data[J+0],g:K.data[J+1],b:K.data[J+2]};return I};var j="cbl-pattern";var f="cbl-solution";var e=function(){document.getElementById("cbl-trainer").style.display="none"};var B=function(){if(document.getElementById("cbl-trainer")!=null){document.getElementById("cbl-trainer").style.display="flex"}else{var H=function(I,J){var K=document.createElement("div");K.innerHTML=J;while(K.children.length>0){I.appendChild(K.children[0])}};H(document.body,'<div id="cbl-trainer"> <div id="cbl-trainer-dialog"> <span id="cbl-close" onclick="">✗</span> <h1>CBL-js Pattern Classifier</h1> <p>Identify the character in the image below by typing it into the textbox.</p> <p>Type <span class="cbl-discard">'+g.incorrect_segment_char+'</span> to discard a pattern if the image was not segmented properly.</p> <div class="cbl-row"> <div class="cbl-cell-50 cbl-right"> <img id="'+j+'" /> </div> <div class="cbl-cell-50"> <input id="'+f+'" type="text" /> </div> </div> </div> <small><a href="https://github.com/skotz/cbl-js" target="_blank">CBL-js © Scott Clayton</a></small></div>');document.getElementById("cbl-close").addEventListener("click",function(I){e();I.preventDefault()})}};var p=function(H){if(g.allow_console_log){console.log("CBL: "+H)}};var D=function(H){if(g.allow_console_warn){console.warn("CBL: "+H)}};var v=function(){function L(O,N){if(!I[O]){I[O]={};for(var P=0;P<O.length;P++){I[O][O.charAt(P)]=P}}return I[O][N]}var J=String.fromCharCode,M="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",K="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$",I={},H={compressToBase64:function(O){if(null==O){return""}var N=H._compress(O,6,function(P){return M.charAt(P)});switch(N.length%4){default:case 0:return N;case 1:return N+"===";case 2:return N+"==";case 3:return N+"="}},decompressFromBase64:function(N){return null==N?"":""==N?null:H._decompress(N.length,32,function(O){return L(M,N.charAt(O))})},compressToUTF16:function(N){return null==N?"":H._compress(N,15,function(O){return J(O+32)})+" "},decompressFromUTF16:function(N){return null==N?"":""==N?null:H._decompress(N.length,16384,function(O){return N.charCodeAt(O)-32})},compressToUint8Array:function(R){for(var P=H.compress(R),S=new Uint8Array(2*P.length),Q=0,N=P.length;N>Q;Q++){var O=P.charCodeAt(Q);S[2*Q]=O>>>8,S[2*Q+1]=O%256}return S},decompressFromUint8Array:function(Q){if(null===Q||void 0===Q){return H.decompress(Q)}for(var R=new Array(Q.length/2),P=0,N=R.length;N>P;P++){R[P]=256*Q[2*P]+Q[2*P+1]}var O=[];return R.forEach(function(S){O.push(J(S))}),H.decompress(O.join(""))},compressToEncodedURIComponent:function(N){return null==N?"":H._compress(N,6,function(O){return K.charAt(O)})},decompressFromEncodedURIComponent:function(N){return null==N?"":""==N?null:(N=N.replace(/ /g,"+"),H._decompress(N.length,32,function(O){return L(K,N.charAt(O))}))},compress:function(N){return H._compress(N,16,function(O){return J(O)})},_compress:function(P,N,Q){if(null==P){return""}var W,ac,T,ad={},O={},ab="",Y="",Z="",S=2,V=3,U=2,X=[],R=0,aa=0;for(T=0;T<P.length;T+=1){if(ab=P.charAt(T),Object.prototype.hasOwnProperty.call(ad,ab)||(ad[ab]=V++,O[ab]=!0),Y=Z+ab,Object.prototype.hasOwnProperty.call(ad,Y)){Z=Y}else{if(Object.prototype.hasOwnProperty.call(O,Z)){if(Z.charCodeAt(0)<256){for(W=0;U>W;W++){R<<=1,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++}for(ac=Z.charCodeAt(0),W=0;8>W;W++){R=R<<1|1&ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac>>=1}}else{for(ac=1,W=0;U>W;W++){R=R<<1|ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac=0}for(ac=Z.charCodeAt(0),W=0;16>W;W++){R=R<<1|1&ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac>>=1}}S--,0==S&&(S=Math.pow(2,U),U++),delete O[Z]}else{for(ac=ad[Z],W=0;U>W;W++){R=R<<1|1&ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac>>=1}}S--,0==S&&(S=Math.pow(2,U),U++),ad[Y]=V++,Z=String(ab)}}if(""!==Z){if(Object.prototype.hasOwnProperty.call(O,Z)){if(Z.charCodeAt(0)<256){for(W=0;U>W;W++){R<<=1,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++}for(ac=Z.charCodeAt(0),W=0;8>W;W++){R=R<<1|1&ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac>>=1}}else{for(ac=1,W=0;U>W;W++){R=R<<1|ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac=0}for(ac=Z.charCodeAt(0),W=0;16>W;W++){R=R<<1|1&ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac>>=1}}S--,0==S&&(S=Math.pow(2,U),U++),delete O[Z]}else{for(ac=ad[Z],W=0;U>W;W++){R=R<<1|1&ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac>>=1}}S--,0==S&&(S=Math.pow(2,U),U++)}for(ac=2,W=0;U>W;W++){R=R<<1|1&ac,aa==N-1?(aa=0,X.push(Q(R)),R=0):aa++,ac>>=1}for(;;){if(R<<=1,aa==N-1){X.push(Q(R));break}aa++}return X.join("")},decompress:function(N){return null==N?"":""==N?null:H._decompress(N.length,32768,function(O){return N.charCodeAt(O)})},_decompress:function(U,V,ab){var Q,Y,R,T,P,ad,ae,X,aa=[],Z=4,ac=4,W=3,O="",N=[],S={val:ab(0),position:V,index:1};for(Y=0;3>Y;Y+=1){aa[Y]=Y}for(T=0,ad=Math.pow(2,2),ae=1;ae!=ad;){P=S.val&S.position,S.position>>=1,0==S.position&&(S.position=V,S.val=ab(S.index++)),T|=(P>0?1:0)*ae,ae<<=1}switch(Q=T){case 0:for(T=0,ad=Math.pow(2,8),ae=1;ae!=ad;){P=S.val&S.position,S.position>>=1,0==S.position&&(S.position=V,S.val=ab(S.index++)),T|=(P>0?1:0)*ae,ae<<=1}X=J(T);break;case 1:for(T=0,ad=Math.pow(2,16),ae=1;ae!=ad;){P=S.val&S.position,S.position>>=1,0==S.position&&(S.position=V,S.val=ab(S.index++)),T|=(P>0?1:0)*ae,ae<<=1}X=J(T);break;case 2:return""}for(aa[3]=X,R=X,N.push(X);;){if(S.index>U){return""}for(T=0,ad=Math.pow(2,W),ae=1;ae!=ad;){P=S.val&S.position,S.position>>=1,0==S.position&&(S.position=V,S.val=ab(S.index++)),T|=(P>0?1:0)*ae,ae<<=1}switch(X=T){case 0:for(T=0,ad=Math.pow(2,8),ae=1;ae!=ad;){P=S.val&S.position,S.position>>=1,0==S.position&&(S.position=V,S.val=ab(S.index++)),T|=(P>0?1:0)*ae,ae<<=1}aa[ac++]=J(T),X=ac-1,Z--;break;case 1:for(T=0,ad=Math.pow(2,16),ae=1;ae!=ad;){P=S.val&S.position,S.position>>=1,0==S.position&&(S.position=V,S.val=ab(S.index++)),T|=(P>0?1:0)*ae,ae<<=1}aa[ac++]=J(T),X=ac-1,Z--;break;case 2:return N.join("")}if(0==Z&&(Z=Math.pow(2,W),W++),aa[X]){O=aa[X]}else{if(X!==ac){return null}O=R+R.charAt(0)}N.push(O),aa[ac++]=R+O.charAt(0),Z--,R=O,0==Z&&(Z=Math.pow(2,W),W++)}}};return H}();"function"==typeof define&&define.amd?define(function(){return v}):"undefined"!=typeof module&&null!=module&&(module.exports=v);if(g.model_file.length){u.loadModel(g.model_file)}else{if(g.model_string.length){u.loadModelString(g.model_string)}}return u};