radames commited on
Commit
e78b203
·
1 Parent(s): 29c5298

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +86 -17
index.html CHANGED
@@ -1,19 +1,88 @@
1
  <!DOCTYPE html>
2
  <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <!DOCTYPE html>
2
  <html>
3
+
4
+ <head>
5
+ <meta charset="utf-8" />
6
+ <meta name="viewport" content="width=device-width" />
7
+ <link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
8
+ <title>Heelo huggingface.js</title>
9
+ </head>
10
+
11
+ <body>
12
+ <script type="module">
13
+ import { HfInference } from 'https://cdn.skypack.dev/@huggingface/[email protected]';
14
+ let hf = new HfInference();
15
+
16
+ document.querySelector("#token").addEventListener("change", (e) => {
17
+ hf = new HfInference(e.target.value)
18
+ })
19
+
20
+ document.addEventListener("DOMContentLoaded", async () => {
21
+ const image = document.querySelector("#example-img");
22
+ const imgBlob = await fetch(image.src).then((r) => r.blob());
23
+ detectObjects(imgBlob, image.naturalWidth, image.naturalHeight);
24
+ })
25
+
26
+ async function detectObjects(imgBlob, imgW, imgH) {
27
+ console.log(imgBlob, imgW, imgH)
28
+ try {
29
+ const objectDetectionRes = await hf.objectDetection({
30
+ data: imgBlob,
31
+ model: "facebook/detr-resnet-50"
32
+ })
33
+ document.querySelector("#output").innerText = JSON.stringify(objectDetectionRes, null, 2);
34
+ const container = document.querySelector("#image-container");
35
+ container.querySelectorAll(".box").forEach((el) => el.remove());
36
+ const boxes = objectDetectionRes.map((obj) => {
37
+ const w = (100 * (obj.box.xmax - obj.box.xmin)) / imgW;
38
+ const h = (100 * (obj.box.ymax - obj.box.ymin)) / imgH;
39
+ const box = document.createElement("div");
40
+ box.classList.add("box");
41
+ box.style.position = "absolute";
42
+ box.style.border = "solid 2px red";
43
+ box.style.top = (100 * obj.box.ymin) / imgH + "%";
44
+ box.style.left = (100 * obj.box.xmin) / imgW + "%";
45
+ box.style.width = w + "%";
46
+ box.style.height = h + "%";
47
+ box.appendChild(document.createTextNode(obj.label));
48
+ return box;
49
+ })
50
+ boxes.forEach((box) => {
51
+ container.appendChild(box);
52
+ })
53
+ } catch (e) {
54
+ document.querySelector("#output").innerText = e.message;
55
+ }
56
+
57
+ }
58
+ document.querySelector("#image-file").addEventListener("change", async (e) => {
59
+ const file = e.target.files[0];
60
+ const newImage = new Image();
61
+ newImage.src = URL.createObjectURL(file)
62
+
63
+ const img = document.querySelector("#example-img");
64
+ img.src = newImage.src;
65
+ newImage.onload = () => {
66
+ detectObjects(file, newImage.naturalWidth, newImage.naturalHeight);
67
+ }
68
+ });
69
+
70
+ </script>
71
+ <h1> Hello huggingface.js </h1>
72
+ <div>
73
+ <label for="token">HF Token</label>
74
+ <div style="display: flex">
75
+ <input style="flex: 2 1 0%" type="password" id="token" />
76
+ <button style="flex: 1 1 0%">Set Token</button>
77
+ </div>
78
+ </div>
79
+
80
+ <input type="file" id="image-file" accept="image/*" />
81
+ <div style="position: relative;" id="image-container">
82
+ <img id="example-img" width="100%"
83
+ src="https://raw.githubusercontent.com/huggingface/huggingface.js/main/packages/inference/test/cats.png">
84
+ </div>
85
+ <pre><code id="output"></code></pre>
86
+ </body>
87
+
88
+ </html>