Views
No views yet
npm i @huggingface/transformersXenova/yolov9-e_all.1import { AutoModel, AutoProcessor, RawImage } from '@huggingface/transformers';
2
3// Load model
4const model = await AutoModel.from_pretrained('Xenova/yolov9-e_all', {
5 dtype: "fp32", // (Optional) Use unquantized version.
6});
7
8// Load processor
9const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-e_all');
10// processor.feature_extractor.size = { shortest_edge: 128 } // (Optional) Update resize value
11
12// Read image and run processor
13const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
14const image = await RawImage.read(url);
15const inputs = await processor(image);
16
17// Run object detection
18const threshold = 0.3;
19const { outputs } = await model(inputs);
20const predictions = outputs.tolist();
21
22for (const [xmin, ymin, xmax, ymax, score, id] of predictions) {
23 if (score < threshold) break;
24 const bbox = [xmin, ymin, xmax, ymax].map(x => x.toFixed(2)).join(', ');
25 console.log(`Found "${model.config.id2label[id]}" at [${bbox}] with score ${score.toFixed(2)}.`);
26}
27// Found "car" at [156.96, 133.09, 223.66, 167.14] with score 0.91.
28// Found "car" at [63.22, 119.01, 139.68, 145.72] with score 0.88.
29// Found "bicycle" at [0.80, 182.37, 39.26, 203.85] with score 0.85.
30// Found "bicycle" at [124.02, 184.35, 163.21, 206.10] with score 0.85.
31// Found "bicycle" at [158.31, 169.96, 194.58, 189.22] with score 0.80.
32// Found "person" at [135.04, 166.16, 156.00, 204.01] with score 0.75.
33// Found "person" at [192.14, 90.51, 205.68, 116.73] with score 0.74.
34// Found "person" at [11.69, 164.45, 28.37, 200.11] with score 0.74.
35// ...onnx).