Xenova HF staff commited on
Commit
2a4fba6
1 Parent(s): fda09a5

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +17 -10
index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.10.1';
2
 
3
  // Since we will download the model from the Hugging Face Hub, we can skip the local model check
4
  env.allowLocalModels = false;
@@ -13,7 +13,11 @@ const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs
13
 
14
  // Create a new object detection pipeline
15
  status.textContent = 'Loading model...';
16
- const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
 
 
 
 
17
  status.textContent = 'Ready';
18
 
19
  example.addEventListener('click', (e) => {
@@ -42,18 +46,21 @@ async function detect(img) {
42
  imageContainer.style.backgroundImage = `url(${img})`;
43
 
44
  status.textContent = 'Analysing...';
45
- const output = await detector(img, {
46
- threshold: 0.5,
47
- percentage: true,
48
- });
 
 
 
 
 
49
  status.textContent = '';
50
  output.forEach(renderBox);
51
  }
52
 
53
  // Render a bounding box and label on the image
54
- function renderBox({ box, label }) {
55
- const { xmax, xmin, ymax, ymin } = box;
56
-
57
  // Generate a random color for the box
58
  const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
59
 
@@ -70,7 +77,7 @@ function renderBox({ box, label }) {
70
 
71
  // Draw label
72
  const labelElement = document.createElement('span');
73
- labelElement.textContent = label;
74
  labelElement.className = 'bounding-box-label';
75
  labelElement.style.backgroundColor = color;
76
 
 
1
+ import { env, AutoModel, AutoProcessor, RawImage } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1';
2
 
3
  // Since we will download the model from the Hugging Face Hub, we can skip the local model check
4
  env.allowLocalModels = false;
 
13
 
14
  // Create a new object detection pipeline
15
  status.textContent = 'Loading model...';
16
+ const model_id = 'onnx-community/yolov10n';
17
+ const model = await AutoModel.from_pretrained(model_id, {
18
+ quantized: false, // (Optional) Use unquantized version.
19
+ });
20
+ const processor = await AutoProcessor.from_pretrained(model_id);
21
  status.textContent = 'Ready';
22
 
23
  example.addEventListener('click', (e) => {
 
46
  imageContainer.style.backgroundImage = `url(${img})`;
47
 
48
  status.textContent = 'Analysing...';
49
+ const image = await RawImage.read(url);
50
+ const { pixel_values } = await processor(image);
51
+ const { output0 } = await model({ images: pixel_values });
52
+ const predictions = output0.tolist()[0];
53
+ const threshold = 0.5;
54
+ for (const [xmin, ymin, xmax, ymax, score, id] of predictions) {
55
+ if (score < threshold) continue;
56
+ renderBox(xmin, ymin, xmax, ymax, score, model.config.id2label[id]);
57
+ }
58
  status.textContent = '';
59
  output.forEach(renderBox);
60
  }
61
 
62
  // Render a bounding box and label on the image
63
+ function renderBox(xmin, ymin, xmax, ymax, score, label) {
 
 
64
  // Generate a random color for the box
65
  const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
66
 
 
77
 
78
  // Draw label
79
  const labelElement = document.createElement('span');
80
+ labelElement.textContent = `${label} (${(score * 100).toFixed(2)}%)`;
81
  labelElement.className = 'bounding-box-label';
82
  labelElement.style.backgroundColor = color;
83