Views
No views yet
npm i @huggingface/transformersXenova/yolov9-c_all.1import { AutoModel, AutoProcessor, RawImage } from '@huggingface/transformers';
2
3// Load model
4const model = await AutoModel.from_pretrained('Xenova/yolov9-c_all', {
5 dtype: "fp32", // (Optional) Use unquantized version.
6})
7
8// Load processor
9const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-c_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 "bicycle" at [0.64, 181.27, 38.81, 203.94] with score 0.84.
28// Found "car" at [157.68, 137.26, 223.67, 167.39] with score 0.78.
29// Found "bicycle" at [157.69, 167.86, 195.10, 188.92] with score 0.78.
30// Found "bicycle" at [123.69, 184.40, 162.44, 206.26] with score 0.74.
31// Found "car" at [62.47, 119.27, 139.17, 145.84] with score 0.73.
32// Found "person" at [193.18, 91.03, 206.57, 116.17] with score 0.72.
33// Found "traffic light" at [73.08, 20.15, 82.06, 35.85] with score 0.70.
34// Found "person" at [11.45, 164.69, 27.88, 199.36] with score 0.69.
35// ...onnx).