UNPKG

@codait/max-image-segmenter

Version:

Identify objects in an image, additionally assigning each pixel of the image to a particular object.

1 lines 5.29 kB
var imageSegmenter=function(e){"use strict";const t=function(e){return tf.tidy(()=>{const t=tf.browser.fromPixels(e),r=function(e,t){const r=512/Math.max(e,t);return{width:Math.round(r*e),height:Math.round(r*t)}}(t.shape[0],t.shape[1]);return t.resizeBilinear([r.width,r.height]).expandDims()})},r=function(e){return function(e){return new Promise((t,r)=>{if("string"==typeof e){const o=new Image;o.onload=(()=>t(o)),o.onerror=(e=>r(e)),o.src=e}else t(e)})}(e).then(t).then(e=>Promise.resolve(e)).catch(e=>(console.error(e),Promise.reject(e)))};let o=null;o="https://s3.us.cloud-object-storage.appdomain.cloud/codait-cos-max/max-image-segmenter/tfjs/0.1.0/model.json";let n=null,i=!1;const c=function(e){return n?u(e)&&!i?(a(),Promise.resolve(n)):Promise.resolve(n):tf.loadGraphModel(o).then(t=>(n=t,u(e)&&a(),Promise.resolve(n))).catch(e=>(console.error(e),Promise.reject(e)))},s=function(e){if(e){if(n)return tf.tidy(()=>{const t=n.predict(e.toInt());return i=!0,t});throw console.error("model not available"),new Error("model not available")}throw console.error("no image provided"),new Error("no image provided")},a=function(){try{s(tf.zeros([1,512,512,3]))}catch(e){}},u=function(e){return null===e||void 0===e||"string"==typeof e&&"true"===e.toLowerCase()||"boolean"==typeof e&&e};{const e=document.currentScript.getAttribute("data-init-model");u(e)&&c(!0)}const l=["background","airplane","bicycle","bird","boat","bottle","bus","car","cat","chair","cow","dining table","dog","horse","motorbike","person","potted plant","sheep","sofa","train","tv"],h=function(e){return e.unstack()[0].array().then(e=>Promise.resolve({segmentationMap:e,objectsDetected:function(e){const t={};return e.forEach(e=>{e.forEach(e=>{t[l[e]]||(t[l[e]]=!0)})}),Object.keys(t)}(e),imageSize:{width:e[0].length,height:e.length}}))},d=function(e){return r(e)},f=function(e){return t=e,c(!1).then(()=>{try{const e=s(t);return Promise.resolve(e)}catch(e){return Promise.reject(e)}});var t},m=function(e){return h(e)};return e.colorsMap=[[0,0,0],[128,0,0],[0,128,0],[128,128,0],[0,0,128],[128,0,128],[0,128,128],[128,128,128],[64,0,0],[192,0,0],[64,128,0],[192,128,0],[64,0,128],[192,0,128],[64,128,128],[192,128,128],[0,64,0],[128,64,0],[0,192,0],[128,192,0],[0,64,128],[128,64,128],[0,192,128],[128,192,128],[64,64,0],[192,64,0],[64,192,0],[192,192,0],[64,64,128],[192,64,128],[64,192,128],[192,192,128],[0,0,64],[128,0,64],[0,128,64],[128,128,64],[0,0,192],[128,0,192],[0,128,192],[128,128,192],[64,0,64],[192,0,64],[64,128,64],[192,128,64],[64,0,192],[192,0,192],[64,128,192],[192,128,192],[0,64,64],[128,64,64],[0,192,64],[128,192,64],[0,64,192],[128,64,192],[0,192,192],[128,192,192],[64,64,64],[192,64,64],[64,192,64],[192,192,64],[64,64,192],[192,64,192],[64,192,192],[192,192,192],[32,0,0],[160,0,0],[32,128,0],[160,128,0],[32,0,128],[160,0,128],[32,128,128],[160,128,128],[96,0,0],[224,0,0],[96,128,0],[224,128,0],[96,0,128],[224,0,128],[96,128,128],[224,128,128],[32,64,0],[160,64,0],[32,192,0],[160,192,0],[32,64,128],[160,64,128],[32,192,128],[160,192,128],[96,64,0],[224,64,0],[96,192,0],[224,192,0],[96,64,128],[224,64,128],[96,192,128],[224,192,128],[32,0,64],[160,0,64],[32,128,64],[160,128,64],[32,0,192],[160,0,192],[32,128,192],[160,128,192],[96,0,64],[224,0,64],[96,128,64],[224,128,64],[96,0,192],[224,0,192],[96,128,192],[224,128,192],[32,64,64],[160,64,64],[32,192,64],[160,192,64],[32,64,192],[160,64,192],[32,192,192],[160,192,192],[96,64,64],[224,64,64],[96,192,64],[224,192,64],[96,64,192],[224,64,192],[96,192,192],[224,192,192],[0,32,0],[128,32,0],[0,160,0],[128,160,0],[0,32,128],[128,32,128],[0,160,128],[128,160,128],[64,32,0],[192,32,0],[64,160,0],[192,160,0],[64,32,128],[192,32,128],[64,160,128],[192,160,128],[0,96,0],[128,96,0],[0,224,0],[128,224,0],[0,96,128],[128,96,128],[0,224,128],[128,224,128],[64,96,0],[192,96,0],[64,224,0],[192,224,0],[64,96,128],[192,96,128],[64,224,128],[192,224,128],[0,32,64],[128,32,64],[0,160,64],[128,160,64],[0,32,192],[128,32,192],[0,160,192],[128,160,192],[64,32,64],[192,32,64],[64,160,64],[192,160,64],[64,32,192],[192,32,192],[64,160,192],[192,160,192],[0,96,64],[128,96,64],[0,224,64],[128,224,64],[0,96,192],[128,96,192],[0,224,192],[128,224,192],[64,96,64],[192,96,64],[64,224,64],[192,224,64],[64,96,192],[192,96,192],[64,224,192],[192,224,192],[32,32,0],[160,32,0],[32,160,0],[160,160,0],[32,32,128],[160,32,128],[32,160,128],[160,160,128],[96,32,0],[224,32,0],[96,160,0],[224,160,0],[96,32,128],[224,32,128],[96,160,128],[224,160,128],[32,96,0],[160,96,0],[32,224,0],[160,224,0],[32,96,128],[160,96,128],[32,224,128],[160,224,128],[96,96,0],[224,96,0],[96,224,0],[224,224,0],[96,96,128],[224,96,128],[96,224,128],[224,224,128],[32,32,64],[160,32,64],[32,160,64],[160,160,64],[32,32,192],[160,32,192],[32,160,192],[160,160,192],[96,32,64],[224,32,64],[96,160,64],[224,160,64],[96,32,192],[224,32,192],[96,160,192],[224,160,192],[32,96,64],[160,96,64],[32,224,64],[160,224,64],[32,96,192],[160,96,192],[32,224,192],[160,224,192],[96,96,64],[224,96,64],[96,224,64],[224,224,64],[96,96,192],[224,96,192],[96,224,192],[224,224,192]],e.labelsMap=l,e.loadModel=function(e){return c(e)},e.predict=function(e){return d(e).then(f).then(m).catch(e=>{console.error(e)})},e.processInput=d,e.processOutput=m,e.runInference=f,e.version="0.3.0",e}({});