Xenova's picture
Xenova HF Staff
Update README.md
441d635 verified
---
license: apache-2.0
---
## Usage (Transformers.js)
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:
```bash
npm i @huggingface/transformers
```
**Example:** Selfie segmentation with `onnx-community/mediapipe_selfie_segmentation-web`.
```js
import { AutoModel, RawImage, Tensor } from '@huggingface/transformers';
// Load model and processor
const model_id = 'onnx-community/mediapipe_selfie_segmentation-web';
const model = await AutoModel.from_pretrained(model_id, { dtype: 'fp32' });
// Load image from URL
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/selfie_segmentation.png';
const image = await RawImage.read(url);
// Predict alpha matte
const { alphas } = await model({
pixel_values: new Tensor(
'uint8',
image.data,
[1, image.height, image.width, 3],
),
});
// Save output mask
const mask = RawImage.fromTensor(alphas[0].mul(255).to('uint8'), 'HWC')
mask.save('mask.png');
// (Optional) Apply mask to original image
const result = image.clone().putAlpha(mask);
result.save('result.png');
```
| Input image | Predicted mask | Output image |
| :----------:|:------------:|:------------:|
| ![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) |