StubBackgroundSegmenter class
Stub implementation of BackgroundSegmenter for platforms without ML Kit.
This class serves as a reference implementation. To enable real segmentation on mobile:
- Add
google_mlkit_selfie_segmentation: ^0.6.0to pubspec.yaml - Create a class extending BackgroundSegmenter that uses ML Kit
- Set it on VirtualBackgroundProcessor.segmenter
Example implementation with ML Kit:
import 'package:google_mlkit_selfie_segmentation/google_mlkit_selfie_segmentation.dart';
class MLKitSegmenter implements BackgroundSegmenter {
late SelfieSegmenter _segmenter;
bool _isReady = false;
@override
Future<void> initialize() async {
_segmenter = SelfieSegmenter(
mode: SegmenterMode.stream, // Use stream mode for video
enableRawSizeMask: true,
);
_isReady = true;
}
@override
Future<SegmentationResult> processFrame(
Uint8List frameData, {
required int width,
required int height,
}) async {
// Convert frame to InputImage
final inputImage = InputImage.fromBytes(
bytes: frameData,
metadata: InputImageMetadata(
size: Size(width.toDouble(), height.toDouble()),
rotation: InputImageRotation.rotation0deg,
format: InputImageFormat.nv21, // or bgra8888 on iOS
bytesPerRow: width,
),
);
// Process with ML Kit
final mask = await _segmenter.processImage(inputImage);
if (mask == null) {
return SegmentationResult.error('Segmentation failed');
}
// Convert mask to Uint8List
final maskBytes = _convertMaskToBytes(mask, width, height);
return SegmentationResult(
processedFrame: frameData,
mask: maskBytes,
processingTimeMs: 0,
);
}
Uint8List _convertMaskToBytes(SegmentationMask mask, int width, int height) {
final bytes = Uint8List(width * height);
for (int i = 0; i < mask.confidences.length; i++) {
// Convert confidence (0.0-1.0) to grayscale (0-255)
bytes[i] = (mask.confidences[i] * 255).round().clamp(0, 255);
}
return bytes;
}
@override
Future<void> dispose() async {
await _segmenter.close();
_isReady = false;
}
@override
bool get isReady => _isReady;
}
- Implemented types
Constructors
Properties
Methods
-
dispose(
) → Future< void> -
Dispose of resources
override
-
initialize(
) → Future< void> -
Initialize the segmenter
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
processFrame(
Uint8List frameData, {required int width, required int height}) → Future< SegmentationResult> -
Process a frame and return the segmented result
override
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited