gelan-c / README.md
Xenova's picture
Xenova HF staff
Update README.md
2757f2b verified
metadata
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 JavaScript library from NPM using:

npm i @xenova/transformers

Example: Perform object-detection with Xenova/gelan-c.

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

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

// Load processor
const processor = await AutoProcessor.from_pretrained('Xenova/gelan-c');
// 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 [446.82, 377.56, 639.19, 477.84] with score 0.93.
// Found "car" at [177.22, 336.87, 399.68, 417.72] with score 0.93.
// Found "bicycle" at [1.01, 518.22, 110.25, 584.43] with score 0.91.
// Found "bicycle" at [352.25, 526.08, 463.18, 588.02] with score 0.90.
// Found "person" at [474.38, 430.36, 533.80, 534.33] with score 0.86.
// Found "bicycle" at [449.59, 476.04, 555.38, 537.74] with score 0.86.
// Found "person" at [34.38, 469.56, 79.05, 566.80] with score 0.83.
// Found "traffic light" at [376.79, 66.41, 401.90, 111.34] with score 0.82.
// ...

Demo

Test it out here!


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 and structuring your repo like this one (with ONNX weights located in a subfolder named onnx).