aje6 commited on
Commit
83729f9
·
verified ·
1 Parent(s): 5f847ae

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +65 -27
index.js CHANGED
@@ -1,35 +1,73 @@
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
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0';
2
 
3
+ // Since we will download the model from the Hugging Face Hub, we can skip the local model check
4
+ env.allowLocalModels = false;
5
 
6
+ // Reference the elements that we will need
7
+ const status = document.getElementById('status');
8
+ const fileUpload = document.getElementById('file-upload');
9
+ const imageContainer = document.getElementById('image-container');
10
 
11
+ // Create a new object detection pipeline
12
+ status.textContent = 'Loading model...';
13
+ const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
14
+ status.textContent = 'Ready';
15
 
16
+ fileUpload.addEventListener('change', function (e) {
17
+ const file = e.target.files[0];
18
+ if (!file) {
19
+ return;
20
+ }
21
 
22
+ const reader = new FileReader();
23
 
24
+ // Set up a callback when the file is loaded
25
+ reader.onload = function (e2) {
26
+ imageContainer.innerHTML = '';
27
+ const image = document.createElement('img');
28
+ image.src = e2.target.result;
29
+ imageContainer.appendChild(image);
30
+ detect(image);
31
+ };
32
+ reader.readAsDataURL(file);
33
+ });
34
 
 
35
 
36
+ // Detect objects in the image
37
+ async function detect(img) {
38
+ status.textContent = 'Analysing...';
39
+ const output = await detector(img.src, {
40
+ threshold: 0.5,
41
+ percentage: true,
42
+ });
43
+ status.textContent = '';
44
+ output.forEach(renderBox);
45
+ }
46
+
47
+ // Render a bounding box and label on the image
48
+ function renderBox({ box, label }) {
49
+ const { xmax, xmin, ymax, ymin } = box;
50
+
51
+ // Generate a random color for the box
52
+ const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
53
+
54
+ // Draw the box
55
+ const boxElement = document.createElement('div');
56
+ boxElement.className = 'bounding-box';
57
+ Object.assign(boxElement.style, {
58
+ borderColor: color,
59
+ left: 100 * xmin + '%',
60
+ top: 100 * ymin + '%',
61
+ width: 100 * (xmax - xmin) + '%',
62
+ height: 100 * (ymax - ymin) + '%',
63
+ });
64
+
65
+ // Draw label
66
+ const labelElement = document.createElement('span');
67
+ labelElement.textContent = label;
68
+ labelElement.className = 'bounding-box-label';
69
+ labelElement.style.backgroundColor = color;
70
+
71
+ boxElement.appendChild(labelElement);
72
+ imageContainer.appendChild(boxElement);
73
+ }