Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -44,17 +44,27 @@ fileUpload.addEventListener('change', function (e) {
|
|
44 |
|
45 |
|
46 |
// Detect objects in the image
|
47 |
-
async function detect(
|
|
|
|
|
|
|
|
|
48 |
imageContainer.innerHTML = '';
|
49 |
-
imageContainer.style.backgroundImage = `url(${
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
|
|
|
55 |
const { pixel_values } = await processor(image);
|
56 |
|
57 |
-
|
|
|
58 |
|
59 |
status.textContent = '';
|
60 |
outputs.tolist().forEach(renderBox);
|
|
|
44 |
|
45 |
|
46 |
// Detect objects in the image
|
47 |
+
async function detect(url) {
|
48 |
+
// Read image
|
49 |
+
const image = await RawImage.fromURL(url);
|
50 |
+
|
51 |
+
// Update UI
|
52 |
imageContainer.innerHTML = '';
|
53 |
+
imageContainer.style.backgroundImage = `url(${url})`;
|
54 |
|
55 |
+
// Set container width and height depending on the image aspect ratio
|
56 |
+
const ar = image.width / image.height;
|
57 |
+
const [cw, ch] = (ar > 720 / 480) ? [720, 720 / ar] : [480 * ar, 480];
|
58 |
+
imageContainer.style.width = `${cw}px`;
|
59 |
+
imageContainer.style.height = `${ch}px`;
|
60 |
|
61 |
+
status.textContent = 'Analysing...';
|
62 |
|
63 |
+
// Preprocess image
|
64 |
const { pixel_values } = await processor(image);
|
65 |
|
66 |
+
// Predict bounding boxes
|
67 |
+
const { outputs } = await model({ images: pixel_values });
|
68 |
|
69 |
status.textContent = '';
|
70 |
outputs.tolist().forEach(renderBox);
|