Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,44 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
## Usage (Transformers.js)
|
6 |
+
|
7 |
+
If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@huggingface/transformers) using:
|
8 |
+
```bash
|
9 |
+
npm i @huggingface/transformers
|
10 |
+
```
|
11 |
+
|
12 |
+
**Example:** Selfie segmentation with `onnx-community/mediapipe_selfie_segmentation_landscape`.
|
13 |
+
|
14 |
+
```js
|
15 |
+
import { AutoModel, AutoProcessor, RawImage } from '@huggingface/transformers';
|
16 |
+
|
17 |
+
// Load model and processor
|
18 |
+
const model_id = 'onnx-community/mediapipe_selfie_segmentation_landscape';
|
19 |
+
const model = await AutoModel.from_pretrained(model_id, { dtype: 'fp32' });
|
20 |
+
const processor = await AutoProcessor.from_pretrained(model_id);
|
21 |
+
|
22 |
+
// Load image from URL
|
23 |
+
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/selfie_segmentation_landscape.png';
|
24 |
+
const image = await RawImage.read(url);
|
25 |
+
|
26 |
+
// Pre-process image
|
27 |
+
const inputs = await processor(image);
|
28 |
+
|
29 |
+
// Predict alpha matte
|
30 |
+
const { alphas } = await model(inputs);
|
31 |
+
|
32 |
+
// Save output mask
|
33 |
+
const mask = await RawImage.fromTensor(alphas[0].mul(255).to('uint8')).resize(image.width, image.height);
|
34 |
+
mask.save('mask.png');
|
35 |
+
|
36 |
+
// (Optional) Apply mask to original image
|
37 |
+
const result = image.clone().putAlpha(mask);
|
38 |
+
result.save('result.png');
|
39 |
+
```
|
40 |
+
|
41 |
+
| Input image | Predicted mask | Output image |
|
42 |
+
| :----------:|:------------:|:------------:|
|
43 |
+
|  |  |  |
|
44 |
+
|