callFromBytes method

Future<SegmentationMask> callFromBytes(
  1. Uint8List imageBytes, {
  2. Float32List? buffer,
})

Segments an image from encoded bytes (JPEG, PNG, etc.).

Decodes imageBytes to a cv.Mat, runs segmentation, and disposes the intermediate Mat automatically.

Example:

final mask = await segmenter.callFromBytes(imageBytes);

Implementation

Future<SegmentationMask> callFromBytes(
  Uint8List imageBytes, {
  Float32List? buffer,
}) async {
  final cv.Mat mat;
  try {
    mat = cv.imdecode(imageBytes, cv.IMREAD_COLOR);
  } catch (e) {
    throw SegmentationException(
      SegmentationError.imageDecodeFailed,
      'Failed to decode image bytes with OpenCV (length: ${imageBytes.length}): $e',
      e,
    );
  }

  if (mat.isEmpty) {
    throw SegmentationException(
      SegmentationError.imageDecodeFailed,
      'Failed to decode image bytes (length: ${imageBytes.length})',
    );
  }

  try {
    return await call(mat, buffer: buffer);
  } finally {
    mat.dispose();
  }
}