Xenova HF Staff commited on
Commit
6d8b63c
·
verified ·
1 Parent(s): d1eb4b9

Update README.md

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