Views
No views yet
npm i @huggingface/transformersXenova/yolov9-e.1import { AutoModel, AutoProcessor, RawImage } from '@huggingface/transformers';
2
3// Load model
4const model = await AutoModel.from_pretrained('Xenova/yolov9-e', {
5 dtype: 'fp32', // (Optional) Use unquantized version.
6});
7
8// Load processor
9const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-e');
10// processor.feature_extractor.do_resize = false; // (Optional) Disable resizing
11// processor.feature_extractor.size = { width: 128, height: 128 } // (Optional) Update resize value
12
13// Read image and run processor
14const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
15const image = await RawImage.read(url);
16const { pixel_values } = await processor(image);
17
18// Run object detection
19const { outputs } = await model({ images: pixel_values });
20const predictions = outputs.tolist();
21
22for (const [xmin, ymin, xmax, ymax, score, id] of predictions) {
23 const bbox = [xmin, ymin, xmax, ymax].map(x => x.toFixed(2)).join(', ');
24 console.log(`Found "${model.config.id2label[id]}" at [${bbox}] with score ${score.toFixed(2)}.`);
25}
26// Found "car" at [179.43, 337.57, 399.15, 418.16] with score 0.94.
27// Found "car" at [447.38, 378.70, 640.22, 477.43] with score 0.93.
28// Found "bicycle" at [352.49, 528.11, 463.47, 588.33] with score 0.90.
29// Found "bicycle" at [0.82, 519.37, 110.09, 584.06] with score 0.89.
30// Found "bicycle" at [448.96, 476.38, 556.01, 538.31] with score 0.89.
31// Found "person" at [550.09, 261.24, 592.19, 331.37] with score 0.88.
32// Found "person" at [472.53, 430.68, 534.50, 532.82] with score 0.87.
33// Found "person" at [393.59, 481.02, 442.97, 587.68] with score 0.85.
34// ...onnx).