SelfieSegmentation class
Performs selfie/person segmentation using MediaPipe TFLite models.
Generates a per-pixel probability mask separating foreground (person) from background. Works on full images - no face detection required.
See the official model cards for architecture details, training data, and intended use cases:
- Selfie segmentation: https://mediapipe.page.link/selfiesegmentation-mc
(local copy:
doc/model_cards/selfie_segmentation_model_card.pdf) - Multiclass segmentation:
(local copy:
doc/model_cards/multiclass_segmentation_model_card.pdf)
Model Variants
Three model variants are available via SegmentationConfig.model:
- SegmentationModel.general (default): Binary person/background, ~244KB, 256x256 input, single-channel sigmoid output.
- SegmentationModel.landscape: Binary person/background optimized for 16:9 video, ~244KB, 144x256 input.
- SegmentationModel.multiclass: 6-class body part segmentation, ~16MB, 256x256 input. Returns MulticlassSegmentationMask with per-class accessors (hair, face, body, clothes, etc.).
Platform Support
All model variants work on all platforms. Binary models (general/landscape)
use the Convolution2DTransposeBias custom op which is bundled for each platform.
| Platform | Delegate | Notes |
|---|---|---|
| iOS | Metal GPU | Custom ops statically linked via CocoaPods |
| Android | GPU/CPU | Custom ops built via CMake, loaded at runtime |
| macOS | CPU | Custom ops bundled as dylib |
| Linux | CPU | Custom ops bundled as .so |
| Windows | CPU | Custom ops bundled as dll |
Example
// Default: fast binary segmentation (~244KB model)
final segmenter = await SelfieSegmentation.create();
final mask = await segmenter(imageBytes);
final binary = mask.toBinary(threshold: 0.5);
segmenter.dispose();
// Multiclass: per-class body part segmentation (~16MB model)
final multiSeg = await SelfieSegmentation.create(
config: SegmentationConfig(model: SegmentationModel.multiclass),
);
final multiMask = await multiSeg(imageBytes);
if (multiMask is MulticlassSegmentationMask) {
final hair = multiMask.hairMask;
}
multiSeg.dispose();
Memory Considerations (varies by model)
- General/Landscape: ~244KB model + ~768KB buffers
- Multiclass: ~16MB model + ~2.3MB buffers
Integration with FaceDetector
For combined face detection and segmentation, use FaceDetector:
final detector = FaceDetector();
await detector.initialize();
await detector.initializeSegmentation();
final faces = await detector.detectFacesFromBytes(imageBytes);
final mask = await detector.getSegmentationMask(imageBytes);
Properties
- config → SegmentationConfig
-
Current configuration.
no setter
- hasGpuDelegateFailed → bool
-
Whether GPU delegate failed during inference.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- inputHeight → int
-
Model input height in pixels.
no setter
- inputWidth → int
-
Model input width in pixels.
no setter
- isDisposed → bool
-
Whether this instance has been disposed.
no setter
- model → SegmentationModel
-
The segmentation model variant in use.
no setter
- outputChannels → int
-
Number of output channels (1 for binary models, 6 for multiclass).
no setter
- outputHeight → int
-
Output mask height.
no setter
- outputWidth → int
-
Output mask width (may differ from input due to model architecture).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
Mat image, {Float32List? buffer}) → Future< SegmentationMask> - Segments an image to separate foreground (person) from background.
-
callFromBytes(
Uint8List imageBytes, {Float32List? buffer}) → Future< SegmentationMask> - Segments an image from encoded bytes (JPEG, PNG, etc.).
-
dispose(
) → void - Releases all TensorFlow Lite resources held by this model.
-
disposeAsync(
) → Future< void> - Asynchronously releases all resources, ensuring the background isolate is fully terminated before freeing the native interpreter.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
create(
{SegmentationConfig config = const SegmentationConfig()}) → Future< SelfieSegmentation> - Creates and initializes a selfie segmentation model instance.
-
createCompiledFromBuffer(
Uint8List modelBytes, {SegmentationConfig config = const SegmentationConfig(), Set< Accelerator> accelerators = const {Accelerator.gpu, Accelerator.cpu}, Precision precision = Precision.fp16}) → Future<SelfieSegmentation> - Creates a selfie segmentation model backed by LiteRT CompiledModel.
-
createFromBuffer(
Uint8List modelBytes, {SegmentationConfig config = const SegmentationConfig()}) → Future< SelfieSegmentation> - Creates a selfie segmentation model from pre-loaded model bytes.