Spaces:
Running
Running
Tobias Cornille
commited on
Commit
·
181d680
1
Parent(s):
d241411
Update index.js
Browse files
index.js
CHANGED
@@ -42,11 +42,18 @@ async function detect(img) {
|
|
42 |
imageContainer.style.backgroundImage = `url(${img})`;
|
43 |
|
44 |
status.textContent = 'Analysing...';
|
|
|
|
|
|
|
|
|
45 |
const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag'];
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
}
|
51 |
|
52 |
// Render a bounding box and label on the image
|
@@ -61,10 +68,10 @@ function renderBox({ box, label }) {
|
|
61 |
boxElement.className = 'bounding-box';
|
62 |
Object.assign(boxElement.style, {
|
63 |
borderColor: color,
|
64 |
-
left: xmin + '
|
65 |
-
top: ymin + '
|
66 |
-
width: (xmax - xmin) + '
|
67 |
-
height: (ymax - ymin) + '
|
68 |
})
|
69 |
|
70 |
// Draw label
|
@@ -75,4 +82,4 @@ function renderBox({ box, label }) {
|
|
75 |
|
76 |
boxElement.appendChild(labelElement);
|
77 |
imageContainer.appendChild(boxElement);
|
78 |
-
}
|
|
|
42 |
imageContainer.style.backgroundImage = `url(${img})`;
|
43 |
|
44 |
status.textContent = 'Analysing...';
|
45 |
+
|
46 |
+
var const = new Image();
|
47 |
+
image.src = img;
|
48 |
+
|
49 |
const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag'];
|
50 |
+
image.onload = function () {
|
51 |
+
const output = await detector(img, candidate_labels);
|
52 |
+
const relativeOutput = output.map({box, label} => ({box: {xmax: box.xmax / image.width, xmin: box.xmin / image.width, ymax: box.ymax / image.height, ymin: box.ymin / image.height}, label}))
|
53 |
+
status.textContent = '';
|
54 |
+
output.forEach(renderBox);
|
55 |
+
};
|
56 |
+
|
57 |
}
|
58 |
|
59 |
// Render a bounding box and label on the image
|
|
|
68 |
boxElement.className = 'bounding-box';
|
69 |
Object.assign(boxElement.style, {
|
70 |
borderColor: color,
|
71 |
+
left: 100 * xmin + '%',
|
72 |
+
top: 100 * ymin + '%',
|
73 |
+
width: 100 * (xmax - xmin) + '%',
|
74 |
+
height: 100 * (ymax - ymin) + '%',
|
75 |
})
|
76 |
|
77 |
// Draw label
|
|
|
82 |
|
83 |
boxElement.appendChild(labelElement);
|
84 |
imageContainer.appendChild(boxElement);
|
85 |
+
}
|