Spaces:
Running
Running
Tobias Cornille
commited on
Commit
·
f5d33c2
1
Parent(s):
76f738a
Update index.js
Browse files
index.js
CHANGED
@@ -8,6 +8,8 @@ const status = document.getElementById('status');
|
|
8 |
const fileUpload = document.getElementById('upload');
|
9 |
const imageContainer = document.getElementById('container');
|
10 |
const example = document.getElementById('example');
|
|
|
|
|
11 |
|
12 |
const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png';
|
13 |
|
@@ -21,13 +23,13 @@ example.addEventListener('click', (e) => {
|
|
21 |
detect(EXAMPLE_URL);
|
22 |
});
|
23 |
|
|
|
24 |
fileUpload.addEventListener('change', function (e) {
|
25 |
const file = e.target.files[0];
|
26 |
if (!file) {
|
27 |
return;
|
28 |
}
|
29 |
|
30 |
-
const reader = new FileReader();
|
31 |
|
32 |
// Set up a callback when the file is loaded
|
33 |
reader.onload = e2 => detect(e2.target.result);
|
@@ -36,6 +38,13 @@ fileUpload.addEventListener('change', function (e) {
|
|
36 |
});
|
37 |
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
// Detect objects in the image
|
40 |
async function detect(img) {
|
41 |
imageContainer.innerHTML = '';
|
@@ -43,7 +52,6 @@ async function detect(img) {
|
|
43 |
|
44 |
status.textContent = 'Analysing...';
|
45 |
|
46 |
-
const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag'];
|
47 |
const output = await detector(img, candidate_labels, { percentage: true });
|
48 |
console.warn(output);
|
49 |
status.textContent = '';
|
|
|
8 |
const fileUpload = document.getElementById('upload');
|
9 |
const imageContainer = document.getElementById('container');
|
10 |
const example = document.getElementById('example');
|
11 |
+
const labels = document.getElementById('labels');
|
12 |
+
let candidate_labels = ['human face', 'rocket', 'helmet', 'american flag'];
|
13 |
|
14 |
const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png';
|
15 |
|
|
|
23 |
detect(EXAMPLE_URL);
|
24 |
});
|
25 |
|
26 |
+
const reader = new FileReader();
|
27 |
fileUpload.addEventListener('change', function (e) {
|
28 |
const file = e.target.files[0];
|
29 |
if (!file) {
|
30 |
return;
|
31 |
}
|
32 |
|
|
|
33 |
|
34 |
// Set up a callback when the file is loaded
|
35 |
reader.onload = e2 => detect(e2.target.result);
|
|
|
38 |
});
|
39 |
|
40 |
|
41 |
+
labels.addEventListener('blur', (e) => {
|
42 |
+
candidate_labels = e.target.value.split(",");
|
43 |
+
if (reader.readyState === "DONE") {
|
44 |
+
detect(reader.result);
|
45 |
+
}
|
46 |
+
});
|
47 |
+
|
48 |
// Detect objects in the image
|
49 |
async function detect(img) {
|
50 |
imageContainer.innerHTML = '';
|
|
|
52 |
|
53 |
status.textContent = 'Analysing...';
|
54 |
|
|
|
55 |
const output = await detector(img, candidate_labels, { percentage: true });
|
56 |
console.warn(output);
|
57 |
status.textContent = '';
|