At a glance
| 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.
Webcam frames stay strictly in browser memory. Grant camera permission when prompted, then capture a portrait snapshot.
Camera idle.
Initialising model loader…
No photo segmented yet.
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
-
Instant video-call bokeh on any laptop or phone: At
0.22 MBand ~20 ms CPU WASM latency, background blur loads faster than a hero JPEG and requires zero WebGPU flags. -
True continuous alpha (not binary masks): The
[1, 1, 256, 256]float32matte preserves soft hair and shoulder transitions for natural feathering. - Know its domain boundary: Trained specifically on selfies/people — non-person objects (e.g. cats or empty rooms) reveal honest false-positive limits you can probe in the Wild lab.
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
Basics: Threshold Sweep & q8 vs fp32
Sweep alpha thresholds (0.1 → 0.9), inspect the probability histogram, and run a live q8 vs fp32 precision diff.
Practical: Batch Headshot Cutter
Upload multiple photos, batch-cut transparent PNG avatars sequentially with Cancel, and export CSV/JSON.
Wild: Stress & Failure-Mode Lab
Run a live 6-perturbation robustness sweep (low-light, flip, blur, contrast, occlusion, crop) + non-person failure probes.
Multi-model: Selfie Seg + YuNet Privacy
Chain MediaPipe Selfie Seg (0.22 MB) + OpenCV YuNet (0.23 MB) in 0.45 MB total for face-aware bokeh and bystander blur.