Spaces:
Running
Running
Upload 4 files
Browse files- assets/index-Bbb1uBOJ.css +1 -0
- assets/index-Dh-wG5Rt.js +1 -0
- assets/worker-B81g4upz.js +0 -0
- index.html +42 -19
assets/index-Bbb1uBOJ.css
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*{box-sizing:border-box;padding:0;margin:0;font-family:sans-serif}html,body{height:100%}body{padding:16px 32px}body,#container,#upload-button{display:flex;flex-direction:column;justify-content:center;align-items:center}h1,h3{text-align:center}#container{position:relative;width:640px;height:420px;max-width:100%;max-height:100%;border:2px dashed #D1D5DB;border-radius:.75rem;overflow:hidden;cursor:pointer;margin-top:1rem;background-size:100% 100%;background-position:center;background-repeat:no-repeat}#mask-output{position:absolute;width:100%;height:100%;pointer-events:none}#upload-button{gap:.4rem;font-size:18px;cursor:pointer}#upload{display:none}svg{pointer-events:none}#example{font-size:14px;text-decoration:underline;cursor:pointer}#example:hover{color:#2563eb}canvas{position:absolute;width:100%;height:100%;opacity:.6}#status{min-height:16px;margin:8px 0}.icon{height:16px;width:16px;position:absolute;transform:translate(-50%,-50%)}#controls>button{padding:6px 12px;background-color:#3498db;color:#fff;border:1px solid #2980b9;border-radius:5px;cursor:pointer;font-size:16px}#controls>button:disabled{background-color:#d1d5db;color:#6b7280;border:1px solid #9ca3af;cursor:not-allowed}#information{margin-top:.25rem;font-size:15px}
|
assets/index-Dh-wG5Rt.js
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
(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});
|
assets/worker-B81g4upz.js
ADDED
The diff for this file is too large to render.
See raw diff
|
|
index.html
CHANGED
@@ -1,19 +1,42 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<meta charset="UTF-8" />
|
6 |
+
|
7 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
8 |
+
<title>Transformers.js - Segment Anything WebGPU</title>
|
9 |
+
<script type="module" crossorigin src="/assets/index-Dh-wG5Rt.js"></script>
|
10 |
+
<link rel="stylesheet" crossorigin href="/assets/index-Bbb1uBOJ.css">
|
11 |
+
</head>
|
12 |
+
|
13 |
+
<body>
|
14 |
+
<h1>Segment Anything WebGPU</h1>
|
15 |
+
<h3>In-browser image segmentation w/ <a href="https://hf.co/docs/transformers.js" target="_blank">🤗
|
16 |
+
Transformers.js</a></h3>
|
17 |
+
<div id="container">
|
18 |
+
<label id="upload-button" for="upload">
|
19 |
+
<svg width="25" height="25" viewBox="0 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg">
|
20 |
+
<path fill="#000"
|
21 |
+
d="M3.5 24.3a3 3 0 0 1-1.9-.8c-.5-.5-.8-1.2-.8-1.9V2.9c0-.7.3-1.3.8-1.9.6-.5 1.2-.7 2-.7h18.6c.7 0 1.3.2 1.9.7.5.6.7 1.2.7 2v18.6c0 .7-.2 1.4-.7 1.9a3 3 0 0 1-2 .8H3.6Zm0-2.7h18.7V2.9H3.5v18.7Zm2.7-2.7h13.3c.3 0 .5 0 .6-.3v-.7l-3.7-5a.6.6 0 0 0-.6-.2c-.2 0-.4 0-.5.3l-3.5 4.6-2.4-3.3a.6.6 0 0 0-.6-.3c-.2 0-.4.1-.5.3l-2.7 3.6c-.1.2-.2.4 0 .7.1.2.3.3.6.3Z">
|
22 |
+
</path>
|
23 |
+
</svg>
|
24 |
+
Click to upload image
|
25 |
+
<label id="example">(or try example)</label>
|
26 |
+
</label>
|
27 |
+
<canvas id="mask-output"></canvas>
|
28 |
+
</div>
|
29 |
+
<label id="status"></label>
|
30 |
+
<div id="controls">
|
31 |
+
<button id="reset-image">Reset image</button>
|
32 |
+
<button id="clear-points">Clear points</button>
|
33 |
+
<button id="cut-mask" disabled>Cut mask</button>
|
34 |
+
</div>
|
35 |
+
<p id="information">
|
36 |
+
Left click = positive points, right click = negative points.
|
37 |
+
</p>
|
38 |
+
<input id="upload" type="file" accept="image/*" />
|
39 |
+
|
40 |
+
</body>
|
41 |
+
|
42 |
+
</html>
|