Views
No views yet

npm i @huggingface/transformers1import { pipeline } from '@huggingface/transformers';
2
3const segmenter = await pipeline('background-removal', 'Xenova/modnet', { dtype: 'fp32' });
4const url = 'https://images.pexels.com/photos/5965592/pexels-photo-5965592.jpeg?auto=compress&cs=tinysrgb&w=1024';
5const output = await segmenter(url);
6output[0].save('mask.png');
7// You can also use `output[0].toCanvas()` or `await output[0].toBlob()` if you would like to access the output without saving.AutoModel and AutoProcessor APIs:1import { AutoModel, AutoProcessor, RawImage } from '@huggingface/transformers';
2
3// Load model and processor
4const model = await AutoModel.from_pretrained('Xenova/modnet', { dtype: 'fp32' });
5const processor = await AutoProcessor.from_pretrained('Xenova/modnet');
6
7// Load image from URL
8const url = 'https://images.pexels.com/photos/5965592/pexels-photo-5965592.jpeg?auto=compress&cs=tinysrgb&w=1024';
9const image = await RawImage.fromURL(url);
10
11// Pre-process image
12const { pixel_values } = await processor(image);
13
14// Predict alpha matte
15const { output } = await model({ input: pixel_values });
16
17// Save output mask
18const mask = await RawImage.fromTensor(output[0].mul(255).to('uint8')).resize(image.width, image.height);
19mask.save('mask.png');| Input image | Output mask |
|---|---|
![]() | ![]() |
onnx).