image-segmentation · vision MobileNetV3-Small · 256×256 Alpha Matte WASM q8 · 0.22 MB License: Apache-2.0

MediaPipe Selfie Segmentation — 224 KB Instant Portrait Matte Lab

Google's MediaPipe Selfie Segmentation predicts a continuous 256×256 float32 foreground alpha matte in just 0.22 MB (224,004 B q8 on CPU WASM, ~20 ms per frame). Cut out portraits, apply live background bokeh, inspect pixel-level matte probabilities, or snap a webcam portrait entirely on your device.

At a glance

MediaPipe Selfie Segmentation model specification and runtime grounding
Upstream & ONNX repo onnx-community/mediapipe_selfie_segmentation (onnx/model_quantized.onnx)
Pipeline task image-segmentation · AutoModel + AutoProcessor ({ alphas })
Architecture MobileNetV3-Small encoder + lightweight segmentation head — outputs continuous [1, 1, 256, 256] float32 foreground matte in [0, 1]
Image processor ImageFeatureExtractor — 256×256 bilinear resize, rescale 1/255, do_normalize: false
Backend & size CPU WASM q8 — 0.22 MB (224,004 B) via shared @huggingface/transformers@3.7.5 in lib/webai.js (~20–41 ms typical WASM latency)
License Apache-2.0 (upstream and ONNX weights)

Live Portrait Matte & Background Replacement Studio

Choose a rights-cleared starter photo, upload or drag-and-drop your own, press Ctrl/Cmd+V to paste, or capture a webcam portrait. All preprocessing, inference, and alpha compositing stay on your device.

Initialising model loader…

No photo segmented yet.

Foreground area–
Mean alpha–
Edge uncertainty–
Inference latency–
Output tensor[1, 1, 256, 256]
Backend / dtypewasm · q8
NaN / invalid0

See inside: 10-bin histogram of the 65,536 continuous alphas probabilities (0.0 background → 1.0 person).

Why a 224 KB portrait matte matters

How the API works

In Transformers.js, AutoModel and AutoProcessor expose the exact raw alphas probability tensor ([1, 1, 256, 256]), while pipeline("image-segmentation", "onnx-community/mediapipe_selfie_segmentation") wraps it into an 8-bit RawImage mask:

import { AutoModel, AutoProcessor, RawImage } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.5";

const model = await AutoModel.from_pretrained("onnx-community/mediapipe_selfie_segmentation", {
  device: "wasm",
  dtype: "q8",
});
const processor = await AutoProcessor.from_pretrained("onnx-community/mediapipe_selfie_segmentation");
const inputs = await processor(rawImage);
const { alphas } = await model(inputs); // Float32Array [1, 1, 256, 256] in [0..1]

See inside the code

Inspect the exact off-main-thread Web Worker inference path, continuous alpha analysis, and composite pipeline.

Show the code — worker.js: ensureLoaded() (shared 3.7.5 WASM + epoch guards)
Show the code — worker.js: segmentImage() (AutoProcessor + AutoModel alphas extraction)
Show the code — selfie-core.mjs: analyzeAlphaField() (coverage, edge uncertainty, real nanCount)

Explore the 5-route use-case ladder

References & model card