create static method
Creates and initializes a selfie segmentation model instance.
This factory method loads the selected segmentation TensorFlow Lite model from package assets and prepares it for inference.
config: Configuration for model selection, delegates, and output limits.
The SegmentationConfig.model field selects which model variant to load.
Returns a fully initialized SelfieSegmentation instance.
Throws SegmentationException if:
- Model file cannot be loaded (SegmentationError.modelNotFound)
- Interpreter creation fails (SegmentationError.interpreterCreationFailed)
- Model validation fails (SegmentationError.unexpectedTensorShape)
Example:
// Default: fast binary model (~244KB)
final segmenter = await SelfieSegmentation.create();
// Multiclass model with per-class masks (~16MB)
final segmenter = await SelfieSegmentation.create(
config: SegmentationConfig(model: SegmentationModel.multiclass),
);
Implementation
static Future<SelfieSegmentation> create({
SegmentationConfig config = const SegmentationConfig(),
}) async {
final effectiveModel = config.model;
final modelFile = segmentationModelFile(effectiveModel);
final obj = await _createWithLoader(
config: config,
loadInterpreter: (options) => Interpreter.fromAsset(
'packages/face_detection_tflite/assets/models/$modelFile',
options: options,
),
loadErrorCode: SegmentationError.modelNotFound,
loadErrorPrefix: 'Failed to load model $modelFile',
);
await obj._initializeTensors();
return obj;
}