Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers"; // import Transformers.js
|
2 |
+
|
3 |
+
env.allowLocalModels = false; // skip the local model check
|
4 |
+
|
5 |
+
// create references to DOM elements
|
6 |
+
const fileUpload = document.getElementById("file-upload");
|
7 |
+
const imageContainer = document.getElementById("image-container");
|
8 |
+
const status = document.getElementById("status");
|
9 |
+
|
10 |
+
status.textContent = "Loading model..."; // status message for user
|
11 |
+
|
12 |
+
// create object detection pipeline
|
13 |
+
const detector = await pipeline("object-detection", "Xenova/detr-resnet-50");
|
14 |
+
|
15 |
+
status.textContent = "Ready"; // status message for user
|
16 |
+
|
17 |
+
// create image uploader
|
18 |
+
fileUpload.addEventListener("change", function (e) {
|
19 |
+
const file = e.target.files[0];
|
20 |
+
if (!file) {
|
21 |
+
return;
|
22 |
+
}
|
23 |
+
|
24 |
+
const reader = new FileReader();
|
25 |
+
|
26 |
+
// Set up a callback when the file is loaded
|
27 |
+
reader.onload = function (e2) {
|
28 |
+
imageContainer.innerHTML = "";
|
29 |
+
const image = document.createElement("img");
|
30 |
+
image.src = e2.target.result;
|
31 |
+
imageContainer.appendChild(image);
|
32 |
+
// detect(image); // Uncomment this line to run the model
|
33 |
+
};
|
34 |
+
reader.readAsDataURL(file);
|
35 |
+
});
|