Views
No views yet
npm i @huggingface/transformersXenova/gelan-c_all.1import { AutoModel, AutoProcessor, RawImage } from '@huggingface/transformers';
2
3// Load model
4const model = await AutoModel.from_pretrained('Xenova/gelan-c_all', {
5 dtype: "fp32", // (Optional) Use unquantized version.
6})
7
8// Load processor
9const processor = await AutoProcessor.from_pretrained('Xenova/gelan-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 "car" at [63.06, 118.80, 139.61, 146.78] with score 0.84.
28// Found "bicycle" at [158.32, 166.13, 195.02, 189.03] with score 0.81.
29// Found "bicycle" at [123.22, 183.83, 162.71, 206.30] with score 0.79.
30// Found "bicycle" at [0.56, 180.92, 39.26, 203.94] with score 0.78.
31// Found "car" at [157.10, 132.38, 223.72, 167.69] with score 0.77.
32// Found "person" at [193.04, 90.98, 207.09, 116.78] with score 0.77.
33// Found "person" at [12.49, 164.97, 27.63, 197.55] with score 0.66.
34// Found "traffic light" at [102.80, 74.25, 124.12, 95.75] with score 0.62.
35// ...onnx).