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