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