Xenova HF staff commited on
Commit
75ef24f
1 Parent(s): 3e6cb3c

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +15 -5
index.js CHANGED
@@ -44,17 +44,27 @@ fileUpload.addEventListener('change', function (e) {
44
 
45
 
46
  // Detect objects in the image
47
- async function detect(img) {
 
 
 
 
48
  imageContainer.innerHTML = '';
49
- imageContainer.style.backgroundImage = `url(${img})`;
50
 
51
- status.textContent = 'Analysing...';
 
 
 
 
52
 
53
- const image = await RawImage.fromURL(img);
54
 
 
55
  const { pixel_values } = await processor(image);
56
 
57
- const { outputs } = await model({images: pixel_values});
 
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);