Xenova HF staff commited on
Commit
9d31a14
1 Parent(s): 56876db

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +19 -15
index.js CHANGED
@@ -11,16 +11,16 @@ const example = document.getElementById('example');
11
 
12
  const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
13
 
14
- const IMAGE_SIZE = 256;
15
 
16
  // Create a new object detection pipeline
17
  status.textContent = 'Loading model...';
18
- const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-c');
19
 
20
- // For this demo, we resize the image to IMAGE_SIZE x IMAGE_SIZE
21
- processor.feature_extractor.size = { width: IMAGE_SIZE, height: IMAGE_SIZE }
22
 
23
- const model = await AutoModel.from_pretrained('Xenova/yolov9-c');
24
  status.textContent = 'Ready';
25
 
26
  example.addEventListener('click', (e) => {
@@ -48,7 +48,7 @@ async function detect(url) {
48
  // Update UI
49
  imageContainer.innerHTML = '';
50
  imageContainer.style.backgroundImage = `url(${url})`;
51
-
52
  // Read image
53
  const image = await RawImage.fromURL(url);
54
 
@@ -61,17 +61,21 @@ async function detect(url) {
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);
 
 
71
  }
72
 
73
  // Render a bounding box and label on the image
74
- function renderBox([xmin, ymin, xmax, ymax, score, id]) {
 
 
75
  // Generate a random color for the box
76
  const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
77
 
@@ -80,10 +84,10 @@ function renderBox([xmin, ymin, xmax, ymax, score, id]) {
80
  boxElement.className = 'bounding-box';
81
  Object.assign(boxElement.style, {
82
  borderColor: color,
83
- left: 100 * xmin / IMAGE_SIZE + '%',
84
- top: 100 * ymin / IMAGE_SIZE + '%',
85
- width: 100 * (xmax - xmin) / IMAGE_SIZE + '%',
86
- height: 100 * (ymax - ymin) / IMAGE_SIZE + '%',
87
  })
88
 
89
  // Draw label
 
11
 
12
  const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
13
 
14
+ const THRESHOLD = 0.25;
15
 
16
  // Create a new object detection pipeline
17
  status.textContent = 'Loading model...';
18
+ const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-c_all');
19
 
20
+ // For this demo, we resize the image so that its shortest edge is 256px
21
+ processor.feature_extractor.size = { shortest_edge: 256 }
22
 
23
+ const model = await AutoModel.from_pretrained('Xenova/yolov9-c_all');
24
  status.textContent = 'Ready';
25
 
26
  example.addEventListener('click', (e) => {
 
48
  // Update UI
49
  imageContainer.innerHTML = '';
50
  imageContainer.style.backgroundImage = `url(${url})`;
51
+
52
  // Read image
53
  const image = await RawImage.fromURL(url);
54
 
 
61
  status.textContent = 'Analysing...';
62
 
63
  // Preprocess image
64
+ const inputs = await processor(image);
65
 
66
  // Predict bounding boxes
67
+ const { outputs } = await model(inputs);
68
+
69
  status.textContent = '';
70
+
71
+ const sizes = inputs.reshaped_input_sizes[0].reverse();
72
+ outputs.tolist().forEach(x => renderBox(x, sizes));
73
  }
74
 
75
  // Render a bounding box and label on the image
76
+ function renderBox([xmin, ymin, xmax, ymax, score, id], [w, h]) {
77
+ if (score < THRESHOLD) return; // Skip boxes with low confidence
78
+
79
  // Generate a random color for the box
80
  const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
81
 
 
84
  boxElement.className = 'bounding-box';
85
  Object.assign(boxElement.style, {
86
  borderColor: color,
87
+ left: 100 * xmin / w + '%',
88
+ top: 100 * ymin / h + '%',
89
+ width: 100 * (xmax - xmin) / w + '%',
90
+ height: 100 * (ymax - ymin) / h + '%',
91
  })
92
 
93
  // Draw label