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/yolov9-c_all
.
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 "car" at [156.96, 133.09, 223.66, 167.14] with score 0.91.
// Found "car" at [63.22, 119.01, 139.68, 145.72] with score 0.88.
// Found "bicycle" at [0.80, 182.37, 39.26, 203.85] with score 0.85.
// Found "bicycle" at [124.02, 184.35, 163.21, 206.10] with score 0.85.
// Found "bicycle" at [158.31, 169.96, 194.58, 189.22] with score 0.80.
// Found "person" at [135.04, 166.16, 156.00, 204.01] with score 0.75.
// Found "person" at [192.14, 90.51, 205.68, 116.73] with score 0.74.
// Found "person" at [11.69, 164.45, 28.37, 200.11] with score 0.74.
// ...
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
).