UNPKG

yolo-tfjs-vision

Version:

A TypeScript-based YOLO object detection library using TensorFlow.js

3 lines (2 loc) 5.39 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@tensorflow/tfjs"),require("@tensorflow/tfjs-backend-webgl")):"function"==typeof define&&define.amd?define(["@tensorflow/tfjs","@tensorflow/tfjs-backend-webgl"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).YOLO=t(e.tf)}(this,(function(e){"use strict";function t(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(o){if("default"!==o){var i=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(t,o,i.get?i:{enumerable:!0,get:function(){return e[o]}})}})),t.default=e,Object.freeze(t)}var o=t(e);function i(e,t,o,i){return new(o||(o=Promise))((function(s,n){function r(e){try{a(i.next(e))}catch(e){n(e)}}function l(e){try{a(i.throw(e))}catch(e){n(e)}}function a(e){var t;e.done?s(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(r,l)}a((i=i.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;return class{constructor(){this.config={modelUrl:"",labels:["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"],colors:["#FF6B6B","#FFA372","#FFDC64","#A3E635","#66D3FA","#82AAFF","#C084FC","#F472B6","#FF9F43","#FFB74D","#AED581","#4DD0E1","#4FC3F7","#9575CD","#F06292","#BA68C8","#FF8A65","#FFD54F","#81C784","#64B5F6"],displayLabels:null,scoreThreshold:.5,boxLineWidth:2,boxLabels:!0}}setup(e){e.modelUrl&&(this.config.modelUrl=e.modelUrl),e.labels&&(this.config.labels=e.labels),e.colors&&(this.config.colors=e.colors),e.displayLabels&&(this.config.displayLabels=new Set(e.displayLabels)),void 0!==e.scoreThreshold&&(this.config.scoreThreshold=e.scoreThreshold),e.boxLineWidth&&(this.config.boxLineWidth=e.boxLineWidth),"boxLabels"in e&&(this.config.boxLabels=Boolean(e.boxLabels))}loadModel(){return i(this,void 0,void 0,(function*(){yield o.ready();try{const e=yield o.loadGraphModel(this.config.modelUrl),t=o.ones(e.inputs[0].shape),i=e.execute(t);return o.dispose([t,i]),{net:e,inputShape:e.inputs[0].shape}}catch(e){return console.error("Error loading model:",e),null}}))}detect(e,t,s){return i(this,arguments,void 0,(function*(e,t,i,s=()=>{}){const[n,r]=t.inputShape.slice(1,3);o.engine().startScope();const[l,{scale:a,dx:c,dy:d,origWidth:h,origHeight:f}]=this.preprocess(e,n,r),u=t.net.execute(l),b=u.transpose([0,2,1]),p=o.tidy((()=>{const e=b.slice([0,0,0],[-1,-1,1]),t=b.slice([0,0,1],[-1,-1,1]),i=b.slice([0,0,2],[-1,-1,1]),s=b.slice([0,0,3],[-1,-1,1]),n=o.sub(e,o.div(i,2)),r=o.sub(t,o.div(s,2)),l=o.add(e,o.div(i,2)),a=o.add(t,o.div(s,2));return o.concat([r,n,a,l],2).squeeze()})),[g,y]=o.tidy((()=>{const e=b.slice([0,0,4],[-1,-1,this.config.labels.length]).squeeze([0]);return[e.max(1),e.argMax(1)]})),m=yield o.image.nonMaxSuppressionAsync(p,g,500,.45,.2),x=p.gather(m,0).dataSync(),F=g.gather(m,0).dataSync(),v=y.gather(m,0).dataSync(),w=[];for(let e=0;e<F.length;e++){let[t,o,i,s]=x.slice(4*e,4*(e+1));o=Math.max(0,(o-c)/a),s=Math.min(h,(s-c)/a),t=Math.max(0,(t-d)/a),i=Math.min(f,(i-d)/a),w.push(t,o,i,s)}HTMLImageElement,i.width=h,i.height=f,this.renderBoxes(i,w,F,v,[1,1]);const k=[];for(let e=0;e<v.length;e++){const t=v[e],o=this.config.labels[t];k.push(o)}s({boxes:w,scores:Array.from(F),classes:Array.from(v),labels:k}),o.dispose([u,b,p,g,y,m]),o.engine().endScope()}))}detectVideo(e,t,o){const s=()=>i(this,void 0,void 0,(function*(){if(0===e.videoWidth&&null===e.srcObject){const e=o.getContext("2d");return e&&e.clearRect(0,0,o.width,o.height),void requestAnimationFrame(s)}yield this.detect(e,t,o,(()=>{requestAnimationFrame(s)}))}));s()}preprocess(e,t,i){const s=o.browser.fromPixels(e),[n,r]=s.shape.slice(0,2),l=Math.min(t/r,i/n),a=Math.round(r*l),c=Math.round(n*l),d=o.image.resizeBilinear(s,[c,a]).div(255),h=Math.floor((t-a)/2),f=Math.floor((i-c)/2),u=o.tidy((()=>o.pad(d,[[f,i-c-f],[h,t-a-h],[0,0]]))),b=u.expandDims(0);return o.dispose([s,d,u]),[b,{scale:l,dx:h,dy:f,origWidth:r,origHeight:n}]}renderBoxes(e,t,o,i,s){const n=e.getContext("2d");if(n){n.clearRect(0,0,e.width,e.height),n.font="16px Arial",n.textBaseline="top";for(let e=0;e<o.length;e++){if(o[e]<this.config.scoreThreshold)continue;const s=this.config.labels[i[e]];if(this.config.displayLabels&&!this.config.displayLabels.has(s))continue;const r=this.config.colors[i[e]%this.config.colors.length],l=(100*o[e]).toFixed(1);let[a,c,d,h]=t.slice(4*e,4*(e+1));const f=h-c,u=d-a;if(n.strokeStyle=r,n.lineWidth=this.config.boxLineWidth,n.strokeRect(c,a,f,u),this.config.boxLabels){const e=`${s} - ${l}%`,t=n.measureText(e).width,o=16;n.fillStyle=r,n.fillRect(c,a-o,t+4,o),n.fillStyle="#ffffff",n.fillText(e,c+2,a-o)}}}}}})); //# sourceMappingURL=yolo.umd.js.map