SegmentationWorker class
A dedicated background isolate for selfie segmentation inference.
This worker runs TFLite inference in a background isolate to avoid blocking
the main UI thread, while using TransferableTypedData for efficient
zero-copy data transfer (unlike IsolateInterpreter which uses slow
nested list serialization).
Performance
- No UI blocking: Inference runs entirely in background isolate
- Fast data transfer: ~1ms via TransferableTypedData vs ~60-80ms with IsolateInterpreter
- Overall: ~30-50ms per inference (same as direct invoke, but non-blocking)
Usage
final worker = SegmentationWorker();
await worker.initialize();
// Process frames without blocking UI
final mask = await worker.segment(imageBytes);
print('Foreground: ${mask.data.where((v) => v > 0.5).length} pixels');
worker.dispose();
Memory Management
Model memory usage depends on the selected model variant (see SegmentationModel). Call dispose when done to free resources.
Constructors
- SegmentationWorker()
- Creates an uninitialized worker; call initialize before use.
Properties
- 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
- isInitialized → bool
-
Returns true if the worker has been initialized and is ready for inference.
no setter
- isReady → bool
-
Returns true if the worker has been initialized and is ready for requests.
no setterinherited
- outputHeight → int
-
Output mask height in pixels.
no setter
- outputWidth → int
-
Output mask width in pixels.
no setter
- rpc → IsolateRpcClient
-
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- workerDisposeOp → String?
-
The name of the operation to send to the isolate when disposing.
no setterinherited
Methods
-
dispose(
) → Future< void> - Releases all resources held by the worker.
-
disposeGracefully(
{Duration timeout = const Duration(seconds: 5)}) → Future< void> -
Gracefully disposes the worker: sends
workerDisposeOpto the isolate as a request and awaits its acknowledgement — giving the isolate a chance to free native resources — before force-killing it viadispose.inherited -
initialize(
{SegmentationConfig config = const SegmentationConfig()}) → Future< void> - Initializes the worker isolate and loads the segmentation model.
-
initWorker(
Future< Isolate> spawnFn(SendPort sendPort), {Duration timeout = const Duration(seconds: 30), String timeoutMessage = 'Worker initialization timed out', void onResponse(dynamic)?, bool markReady = true}) → Future<void> -
Spawns the background isolate and completes the handshake.
inherited
-
markInitialized(
) → void -
Marks the worker as initialized and ready for requests.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
segment(
Uint8List imageBytes) → Future< SegmentationMask> - Segments encoded image bytes.
-
segmentMat(
Mat mat) → Future< SegmentationMask> - Segments a pre-decoded cv.Mat image.
-
sendRequest<
T> (String operation, Map< String, dynamic> params) → Future<T> -
Sends a typed request to the background isolate.
inherited
-
sendRequestUnchecked<
T> (String operation, Map< String, dynamic> params) → Future<T> -
Sends a typed request directly to the background isolate, bypassing the
isReadyguard.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited