File size: 4,122 Bytes
d9af197 |
1 2 |
(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const n of document.querySelectorAll('link[rel="modulepreload"]'))s(n);new MutationObserver(n=>{for(const a of n)if(a.type==="childList")for(const r of a.addedNodes)r.tagName==="LINK"&&r.rel==="modulepreload"&&s(r)}).observe(document,{childList:!0,subtree:!0});function o(n){const a={};return n.integrity&&(a.integrity=n.integrity),n.referrerPolicy&&(a.referrerPolicy=n.referrerPolicy),n.crossOrigin==="use-credentials"?a.credentials="include":n.crossOrigin==="anonymous"?a.credentials="omit":a.credentials="same-origin",a}function s(n){if(n.ep)return;n.ep=!0;const a=o(n);fetch(n.href,a)}})();const f=document.getElementById("status"),N=document.getElementById("upload"),g=document.getElementById("container"),j=document.getElementById("example"),c=document.getElementById("mask-output"),M=document.getElementById("upload-button"),S=document.getElementById("reset-image"),X=document.getElementById("clear-points"),y=document.getElementById("cut-mask");let m=null,p=!1,w=!1,h=!1,P=!1,C=null;const L="https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/",_=L+"corgi.jpg",v=new Worker(new URL("/assets/worker-B81g4upz.js",import.meta.url),{type:"module"}),b=new Image;b.src=L+"star-icon.png";b.className="icon";const B=new Image;B.src=L+"cross-icon.png";B.className="icon";v.addEventListener("message",e=>{const{type:t,data:o}=e.data;if(t==="ready")P=!0,f.textContent="Ready";else if(t==="decode_result"){if(w=!1,!p)return;!h&&m&&(k(),m=null);const{mask:s,scores:n}=o;(c.width!==s.width||c.height!==s.height)&&(c.width=s.width,c.height=s.height);const a=c.getContext("2d"),r=a.createImageData(c.width,c.height),x=n.length;let u=0;for(let i=1;i<x;++i)n[i]>n[u]&&(u=i);f.textContent=`Segment score: ${n[u].toFixed(2)}`;const d=r.data;for(let i=0;i<d.length;++i)if(s.data[x*i+u]===1){const l=4*i;d[l]=0,d[l+1]=114,d[l+2]=189,d[l+3]=255}a.putImageData(r,0,0)}else t==="segment_result"&&(o==="start"?f.textContent="Extracting image embedding...":(f.textContent="Embedding extracted!",p=!0))});function k(){w=!0,v.postMessage({type:"decode",data:m})}function O(){h=!1,m=null,document.querySelectorAll(".icon").forEach(e=>e.remove()),y.disabled=!0,c.getContext("2d").clearRect(0,0,c.width,c.height)}X.addEventListener("click",O);S.addEventListener("click",()=>{p=!1,C=null,v.postMessage({type:"reset"}),O(),y.disabled=!0,g.style.backgroundImage="none",M.style.display="flex",f.textContent="Ready"});function U(e){p=!1,P||(f.textContent="Loading model..."),C=e,g.style.backgroundImage=`url(${e})`,M.style.display="none",y.disabled=!0,v.postMessage({type:"segment",data:e})}N.addEventListener("change",function(e){const t=e.target.files[0];if(!t)return;const o=new FileReader;o.onload=s=>U(s.target.result),o.readAsDataURL(t)});j.addEventListener("click",e=>{e.preventDefault(),U(_)});function $({point:e,label:t}){const o=(t===1?b:B).cloneNode();o.style.left=`${e[0]*100}%`,o.style.top=`${e[1]*100}%`,g.appendChild(o)}g.addEventListener("mousedown",e=>{if(e.button!==0&&e.button!==2||!p)return;h||(m=[],h=!0,y.disabled=!1);const t=A(e);m.push(t),$(t),k()});function R(e,t=0,o=1){return Math.max(Math.min(e,o),t)}function A(e){const t=g.getBoundingClientRect(),o=R((e.clientX-t.left)/t.width),s=R((e.clientY-t.top)/t.height);return{point:[o,s],label:e.button===2?0:1}}g.addEventListener("contextmenu",e=>{e.preventDefault()});g.addEventListener("mousemove",e=>{!p||h||(m=[A(e)],w||k())});y.addEventListener("click",()=>{const[e,t]=[c.width,c.height],s=c.getContext("2d").getImageData(0,0,e,t),n=new Image;n.crossOrigin="anonymous",n.onload=async()=>{const r=new OffscreenCanvas(e,t).getContext("2d");r.drawImage(n,0,0,e,t);const x=r.getImageData(0,0,e,t),u=new OffscreenCanvas(e,t),d=u.getContext("2d"),i=d.getImageData(0,0,e,t);for(let E=3;E<s.data.length;E+=4)if(s.data[E]>0)for(let I=0;I<4;++I){const D=E-I;i.data[D]=x.data[D]}d.putImageData(i,0,0);const l=document.createElement("a");l.download="image.png",l.href=URL.createObjectURL(await u.convertToBlob()),l.click(),l.remove()},n.src=C});
|