ruchi commited on
Commit
0f22e71
·
1 Parent(s): b4acb11

Init commit

Browse files
Files changed (4) hide show
  1. README.md +4 -4
  2. index.html +23 -18
  3. index.js +74 -0
  4. style.css +48 -18
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Object Detection Detr Resnet 101
3
- emoji: 💻
4
- colorFrom: indigo
5
- colorTo: purple
6
  sdk: static
7
  pinned: false
8
  ---
 
1
  ---
2
+ title: VanillaJS
3
+ emoji: 🏢
4
+ colorFrom: red
5
+ colorTo: green
6
  sdk: static
7
  pinned: false
8
  ---
index.html CHANGED
@@ -1,19 +1,24 @@
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 lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <link rel="stylesheet" href="style.css" />
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
8
+ <title>Transformers.js - Object Detection demo</title>
9
+ </head>
10
+
11
+ <body>
12
+ <main class="container">
13
+ <label class="custom-file-upload">
14
+ <input id="file-upload" type="file" accept="image/*" />
15
+ <img class="upload-icon" src="https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/upload-icon.png" />
16
+ Upload image
17
+ </label>
18
+ <div id="image-container"></div>
19
+ <p id="status"></p>
20
+ </main>
21
+ <script src="index.js" type="module"></script>
22
+ </body>
23
+
24
+ </html>
index.js ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
3
+
4
+ // Since we will download the model from the Hugging Face Hub, we can skip the local model check
5
+ env.allowLocalModels = false;
6
+
7
+ // Reference the elements that we will need
8
+ const status = document.getElementById('status');
9
+ const fileUpload = document.getElementById('file-upload');
10
+ const imageContainer = document.getElementById('image-container');
11
+
12
+ // Create a new object detection pipeline
13
+ status.textContent = 'Loading model...';
14
+ const detector = await pipeline('object-detection', 'Xenova/detr-resnet-101');
15
+ status.textContent = 'Ready';
16
+
17
+ fileUpload.addEventListener('change', function (e) {
18
+ const file = e.target.files[0];
19
+ if (!file) {
20
+ return;
21
+ }
22
+
23
+ const reader = new FileReader();
24
+
25
+ // Set up a callback when the file is loaded
26
+ reader.onload = function (e2) {
27
+ imageContainer.innerHTML = '';
28
+ const image = document.createElement('img');
29
+ image.src = e2.target.result;
30
+ imageContainer.appendChild(image);
31
+ detect(image);
32
+ };
33
+ reader.readAsDataURL(file);
34
+ });
35
+
36
+
37
+ // Detect objects in the image
38
+ async function detect(img) {
39
+ status.textContent = 'Analysing...';
40
+ const output = await detector(img.src, {
41
+ threshold: 0.5,
42
+ percentage: true,
43
+ });
44
+ status.textContent = '';
45
+ output.forEach(renderBox);
46
+ }
47
+
48
+ // Render a bounding box and label on the image
49
+ function renderBox({ box, label }) {
50
+ const { xmax, xmin, ymax, ymin } = box;
51
+
52
+ // Generate a random color for the box
53
+ const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
54
+
55
+ // Draw the box
56
+ const boxElement = document.createElement('div');
57
+ boxElement.className = 'bounding-box';
58
+ Object.assign(boxElement.style, {
59
+ borderColor: color,
60
+ left: 100 * xmin + '%',
61
+ top: 100 * ymin + '%',
62
+ width: 100 * (xmax - xmin) + '%',
63
+ height: 100 * (ymax - ymin) + '%',
64
+ })
65
+
66
+ // Draw label
67
+ const labelElement = document.createElement('span');
68
+ labelElement.textContent = label;
69
+ labelElement.className = 'bounding-box-label';
70
+ labelElement.style.backgroundColor = color;
71
+
72
+ boxElement.appendChild(labelElement);
73
+ imageContainer.appendChild(boxElement);
74
+ }
style.css CHANGED
@@ -1,28 +1,58 @@
 
1
  body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
  }
5
 
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
 
 
 
9
  }
10
 
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
  }
25
 
26
- .card p:last-child {
27
- margin-bottom: 0;
 
 
 
28
  }
 
 
 
 
 
 
 
 
 
 
1
+ html,
2
  body {
3
+ font-family: Arial, Helvetica, sans-serif;
 
4
  }
5
 
6
+ .container {
7
+ margin: 40px auto;
8
+ width: max(50vw, 400px);
9
+ display: flex;
10
+ flex-direction: column;
11
+ align-items: center;
12
  }
13
 
14
+
15
+ .custom-file-upload {
16
+ display: flex;
17
+ align-items: center;
18
+ cursor: pointer;
19
+ gap: 10px;
20
+ border: 2px solid black;
21
+ padding: 8px 16px;
22
+ cursor: pointer;
23
+ border-radius: 6px;
24
+ }
25
+
26
+ #file-upload {
27
+ display: none;
28
+ }
29
+
30
+ .upload-icon {
31
+ width: 30px;
32
+ }
33
+
34
+ #image-container {
35
+ width: 100%;
36
+ margin-top: 20px;
37
+ position: relative;
38
  }
39
 
40
+ #image-container>img {
41
+ width: 100%;
 
 
 
 
42
  }
43
 
44
+ .bounding-box {
45
+ position: absolute;
46
+ box-sizing: border-box;
47
+ border-width: 2px;
48
+ border-style: solid;
49
  }
50
+
51
+ .bounding-box-label {
52
+ color: white;
53
+ position: absolute;
54
+ font-size: 12px;
55
+ margin-top: -16px;
56
+ margin-left: -2px;
57
+ padding: 1px;
58
+ }