createFromBuffer static method

Future<SelfieSegmentation> createFromBuffer(
  1. Uint8List modelBytes, {
  2. SegmentationConfig config = const SegmentationConfig(),
})

Creates a selfie segmentation model from pre-loaded model bytes.

This is primarily used by FaceDetector to initialize models in a background isolate where asset loading is not available. The IsolateInterpreter is skipped since the model is already running inside a background isolate.

modelBytes: Raw TFLite model file contents. config: Configuration for model selection, delegates, and output limits. The SegmentationConfig.model field must match the model contained in modelBytes.

Example:

final bytes = await rootBundle.load('assets/models/selfie_segmenter.tflite');
final segmenter = await SelfieSegmentation.createFromBuffer(
  bytes.buffer.asUint8List(),
);

Implementation

static Future<SelfieSegmentation> createFromBuffer(
  Uint8List modelBytes, {
  SegmentationConfig config = const SegmentationConfig(),
}) async {
  final obj = await _createWithLoader(
    config: config,
    loadInterpreter: (options) =>
        Interpreter.fromBuffer(modelBytes, options: options),
    loadErrorCode: SegmentationError.interpreterCreationFailed,
    loadErrorPrefix: 'Failed to create interpreter from buffer',
  );
  await obj._initializeTensors(useIsolateInterpreter: false);
  return obj;
}