Xenova HF staff commited on
Commit
441d635
·
verified ·
1 Parent(s): f2fc464

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +46 -3
README.md CHANGED
@@ -1,3 +1,46 @@
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-web`.
13
+
14
+ ```js
15
+ import { AutoModel, RawImage, Tensor } from '@huggingface/transformers';
16
+
17
+ // Load model and processor
18
+ const model_id = 'onnx-community/mediapipe_selfie_segmentation-web';
19
+ const model = await AutoModel.from_pretrained(model_id, { dtype: 'fp32' });
20
+
21
+ // Load image from URL
22
+ const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/selfie_segmentation.png';
23
+ const image = await RawImage.read(url);
24
+
25
+ // Predict alpha matte
26
+ const { alphas } = await model({
27
+ pixel_values: new Tensor(
28
+ 'uint8',
29
+ image.data,
30
+ [1, image.height, image.width, 3],
31
+ ),
32
+ });
33
+
34
+ // Save output mask
35
+ const mask = RawImage.fromTensor(alphas[0].mul(255).to('uint8'), 'HWC')
36
+ mask.save('mask.png');
37
+
38
+ // (Optional) Apply mask to original image
39
+ const result = image.clone().putAlpha(mask);
40
+ result.save('result.png');
41
+ ```
42
+
43
+ | Input image | Predicted mask | Output image |
44
+ | :----------:|:------------:|:------------:|
45
+ | ![image/png](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/NR--WRELcGKsY8c7dI7s5.png) | ![image/png](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/tAsPevxCzxGank2iHXo7o.png) | ![image/png](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/8RMmqdfcgr4cclN5Dv6ae.png) |
46
+