attachMaskBuffers function
Replaces each mask the browser worker named as [width, height, bytes per value, index] in result with [width, height, values] read from
buffers: float32 confidences or uint8 categories.
Implementation
void attachMaskBuffers(Map<String, dynamic> result, List<ByteBuffer> buffers) {
List<Object> mask(List named) {
final [width as int, height as int, depth as int, index as int] = named;
final bytes = buffers[index];
if (bytes.lengthInBytes != width * height * depth) {
throw StateError('Mask $index has ${bytes.lengthInBytes} bytes.');
}
return [
width,
height,
depth == 1 ? bytes.asUint8List() : bytes.asFloat32List(),
];
}
for (final key in const [
'confidenceMasks',
'segmentationMasks',
'poseSegmentationMasks',
]) {
if (result[key] case final List masks) {
result[key] = [for (final named in masks) mask(named as List)];
}
}
if (result['categoryMask'] case final List named) {
result['categoryMask'] = mask(named);
}
}