Xenova HF staff commited on
Commit
91efc35
1 Parent(s): 099bf4d

Upload 4 files

Browse files
Files changed (1) hide show
  1. index.js +10 -5
index.js CHANGED
@@ -23,6 +23,7 @@ const EXAMPLE_URL =
23
  "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/corgi.jpg";
24
 
25
  // State variables
 
26
  let isDecoding = false;
27
  let decodePending = false;
28
  let lastPoints = null;
@@ -135,6 +136,7 @@ resetButton.addEventListener("click", () => {
135
  imageInput = null;
136
  imageProcessed = null;
137
  imageEmbeddings = null;
 
138
  isDecoding = false;
139
 
140
  // Clear points and mask (if present)
@@ -147,11 +149,13 @@ resetButton.addEventListener("click", () => {
147
  statusLabel.textContent = "Ready";
148
  });
149
 
150
- async function segment(url) {
151
- imageInput = await RawImage.fromURL(url);
152
-
153
  statusLabel.textContent = "Extracting image embedding...";
154
 
 
 
155
  // Update UI
156
  imageContainer.style.backgroundImage = `url(${url})`;
157
  uploadButton.style.display = "none";
@@ -162,6 +166,7 @@ async function segment(url) {
162
  imageEmbeddings = await model.get_image_embeddings(imageProcessed);
163
 
164
  statusLabel.textContent = "Embedding extracted!";
 
165
  }
166
 
167
  // Handle file selection
@@ -172,14 +177,14 @@ fileUpload.addEventListener("change", function (e) {
172
  const reader = new FileReader();
173
 
174
  // Set up a callback when the file is loaded
175
- reader.onload = (e2) => segment(e2.target.result);
176
 
177
  reader.readAsDataURL(file);
178
  });
179
 
180
  example.addEventListener("click", (e) => {
181
  e.preventDefault();
182
- segment(EXAMPLE_URL);
183
  });
184
 
185
  // Attach hover event to image container
 
23
  "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/corgi.jpg";
24
 
25
  // State variables
26
+ let isEncoding = false;
27
  let isDecoding = false;
28
  let decodePending = false;
29
  let lastPoints = null;
 
136
  imageInput = null;
137
  imageProcessed = null;
138
  imageEmbeddings = null;
139
+ isEncoding = false;
140
  isDecoding = false;
141
 
142
  // Clear points and mask (if present)
 
149
  statusLabel.textContent = "Ready";
150
  });
151
 
152
+ async function encode(url) {
153
+ if (isEncoding) return;
154
+ isEncoding = true;
155
  statusLabel.textContent = "Extracting image embedding...";
156
 
157
+ imageInput = await RawImage.fromURL(url);
158
+
159
  // Update UI
160
  imageContainer.style.backgroundImage = `url(${url})`;
161
  uploadButton.style.display = "none";
 
166
  imageEmbeddings = await model.get_image_embeddings(imageProcessed);
167
 
168
  statusLabel.textContent = "Embedding extracted!";
169
+ isEncoding = false;
170
  }
171
 
172
  // Handle file selection
 
177
  const reader = new FileReader();
178
 
179
  // Set up a callback when the file is loaded
180
+ reader.onload = (e2) => encode(e2.target.result);
181
 
182
  reader.readAsDataURL(file);
183
  });
184
 
185
  example.addEventListener("click", (e) => {
186
  e.preventDefault();
187
+ encode(EXAMPLE_URL);
188
  });
189
 
190
  // Attach hover event to image container