Views
No views yet
npm i @huggingface/transformersonnx-community/mediapipe_selfie_segmentation_landscape-web.1import { AutoModel, RawImage, Tensor } from '@huggingface/transformers';
2
3// Load model and processor
4const model_id = 'onnx-community/mediapipe_selfie_segmentation_landscape-web';
5const model = await AutoModel.from_pretrained(model_id, { dtype: 'fp32' });
6
7// Load image from URL
8const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/selfie_segmentation_landscape.png';
9const image = await RawImage.read(url);
10
11// Predict alpha matte
12const { alphas } = await model({
13 pixel_values: new Tensor(
14 'uint8',
15 image.data,
16 [1, image.height, image.width, 3],
17 ),
18});
19
20// Save output mask
21const mask = RawImage.fromTensor(alphas[0].mul(255).to('uint8'), 'HWC')
22mask.save('mask.png');
23
24// (Optional) Apply mask to original image
25const result = image.clone().putAlpha(mask);
26result.save('result.png');| Input image | Predicted mask | Output image |
|---|---|---|
![]() | ![]() | ![]() |