File size: 2,658 Bytes
f6ad9b3
 
5bd0eef
7c922a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
library_name: transformers.js
license: gpl-3.0
pipeline_tag: object-detection
---

https://github.com/WongKinYiu/yolov9 with ONNX weights to be compatible with Transformers.js.


## Usage (Transformers.js)

If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@xenova/transformers) using:
```bash
npm i @xenova/transformers
```

**Example:** Perform object-detection with `Xenova/gelan-e`.

```js
import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';

// Load model
const model = await AutoModel.from_pretrained('Xenova/gelan-e', {
    // quantized: false,    // (Optional) Use unquantized version.
})

// Load processor
const processor = await AutoProcessor.from_pretrained('Xenova/gelan-e');
// processor.feature_extractor.do_resize = false;                   // (Optional) Disable resizing
// processor.feature_extractor.size = { width: 128, height: 128 }   // (Optional) Update resize value

// Read image and run processor
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
const image = await RawImage.read(url);
const { pixel_values } = await processor(image);

// Run object detection
const { outputs } = await model({ images: pixel_values })
const predictions = outputs.tolist();

for (const [xmin, ymin, xmax, ymax, score, id] of predictions) {
    const bbox = [xmin, ymin, xmax, ymax].map(x => x.toFixed(2)).join(', ')
    console.log(`Found "${model.config.id2label[id]}" at [${bbox}] with score ${score.toFixed(2)}.`)
}
// Found "car" at [177.60, 337.36, 398.55, 416.89] with score 0.93.
// Found "car" at [447.15, 378.75, 639.80, 477.55] with score 0.93.
// Found "bicycle" at [1.58, 518.34, 109.99, 584.37] with score 0.90.
// Found "person" at [551.19, 261.01, 591.45, 330.76] with score 0.89.
// Found "bicycle" at [449.09, 477.33, 555.91, 537.40] with score 0.89.
// Found "bicycle" at [352.70, 528.23, 463.36, 588.13] with score 0.88.
// Found "traffic light" at [376.77, 65.71, 401.59, 111.02] with score 0.86.
// Found "traffic light" at [208.46, 55.44, 233.45, 101.43] with score 0.85.
// ...
```

## Demo

Test it out [here](https://huggingface.co/spaces/Xenova/yolov9-web)!

---


Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [🤗 Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).