detectFacesWithSegmentation method

Future<DetectionWithSegmentationResult> detectFacesWithSegmentation(
  1. Uint8List imageBytes, {
  2. FaceDetectionMode mode = FaceDetectionMode.full,
  3. IsolateOutputFormat outputFormat = IsolateOutputFormat.float32,
  4. double binaryThreshold = 0.5,
})

Detects faces and generates segmentation mask in parallel.

This method runs face detection and segmentation simultaneously in separate isolates, returning results as soon as both complete. This provides optimal performance when both features are needed.

Requires withSegmentation: true during spawn.

Parameters:

  • imageBytes: Encoded image data (JPEG, PNG, etc.)
  • mode: Detection mode controlling which features to compute
  • outputFormat: Controls the segmentation mask output format
  • binaryThreshold: Threshold for binary output format

Returns a DetectionWithSegmentationResult containing both faces and mask.

Performance

Processing time is approximately max(detectionTime, segmentationTime) rather than their sum, typically 40-50% faster than sequential calls.

Example:

final detector = await FaceDetectorIsolate.spawn(withSegmentation: true);
final result = await detector.detectFacesWithSegmentation(imageBytes);

print('Found ${result.faces.length} faces');
print('Mask: ${result.segmentationMask?.width}x${result.segmentationMask?.height}');
print('Total time: ${result.totalTimeMs}ms (parallel processing)');

Implementation

Future<DetectionWithSegmentationResult> detectFacesWithSegmentation(
  Uint8List imageBytes, {
  FaceDetectionMode mode = FaceDetectionMode.full,
  IsolateOutputFormat outputFormat = IsolateOutputFormat.float32,
  double binaryThreshold = 0.5,
}) =>
    _detectAndSegmentImpl(
      detectOp: 'detect',
      detectFields: {
        'bytes': TransferableTypedData.fromList([imageBytes]),
        'mode': mode.name,
      },
      segmentOp: 'segment',
      segmentFields: {
        'bytes': TransferableTypedData.fromList([imageBytes]),
        'outputFormat': outputFormat.index,
        'binaryThreshold': binaryThreshold,
      },
    );