Views
No views yet
| Latency-accuracy trade-offs | Size-accuracy trade-offs |
|---|---|
![]() | ![]() |
npm i @xenova/transformers1import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
2
3// Load model
4const model = await AutoModel.from_pretrained('onnx-community/yolov10s', {
5 // quantized: false, // (Optional) Use unquantized version.
6})
7
8// Load processor
9const processor = await AutoProcessor.from_pretrained('onnx-community/yolov10s');
10
11// Read image and run processor
12const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
13const image = await RawImage.read(url);
14const { pixel_values, reshaped_input_sizes } = await processor(image);
15
16// Run object detection
17const { output0 } = await model({ images: pixel_values });
18const predictions = output0.tolist()[0];
19
20const threshold = 0.5;
21const [newHeight, newWidth] = reshaped_input_sizes[0]; // Reshaped height and width
22const [xs, ys] = [image.width / newWidth, image.height / newHeight]; // x and y resize scales
23for (const [xmin, ymin, xmax, ymax, score, id] of predictions) {
24 if (score < threshold) continue;
25
26 // Convert to original image coordinates
27 const bbox = [xmin * xs, ymin * ys, xmax * xs, ymax * ys].map(x => x.toFixed(2)).join(', ');
28 console.log(`Found "${model.config.id2label[id]}" at [${bbox}] with score ${score.toFixed(2)}.`);
29}
30// Found "car" at [559.30, 472.72, 799.58, 598.15] with score 0.95.
31// Found "car" at [221.91, 422.56, 498.09, 521.85] with score 0.94.
32// Found "bicycle" at [1.59, 646.99, 137.72, 730.35] with score 0.92.
33// Found "bicycle" at [561.25, 593.65, 695.01, 671.73] with score 0.91.
34// Found "person" at [687.74, 324.93, 739.70, 415.04] with score 0.89.
35// ...