File size: 2,749 Bytes
f5d83be
1751340
10f0fc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
---
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/yolov9-c_all`.

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

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

// Load processor
const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-c_all');
// processor.feature_extractor.size = { shortest_edge: 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 inputs = await processor(image);

// Run object detection
const threshold = 0.3;
const { outputs } = await model(inputs);
const predictions = outputs.tolist();

for (const [xmin, ymin, xmax, ymax, score, id] of predictions) {
    if (score < threshold) break;
    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 "bicycle" at [0.64, 181.27, 38.81, 203.94] with score 0.84.
// Found "car" at [157.68, 137.26, 223.67, 167.39] with score 0.78.
// Found "bicycle" at [157.69, 167.86, 195.10, 188.92] with score 0.78.
// Found "bicycle" at [123.69, 184.40, 162.44, 206.26] with score 0.74.
// Found "car" at [62.47, 119.27, 139.17, 145.84] with score 0.73.
// Found "person" at [193.18, 91.03, 206.57, 116.17] with score 0.72.
// Found "traffic light" at [73.08, 20.15, 82.06, 35.85] with score 0.70.
// Found "person" at [11.45, 164.69, 27.88, 199.36] with score 0.69.
// ...
```

## Demo

Test it out [here](https://huggingface.co/spaces/Xenova/video-object-detection)!

<video controls autoplay loop src="https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/AgNFx_3cPMh5zjR91n9Dt.mp4"></video>

---


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`).