encodeAsync method

Future<Float32List> encodeAsync(
  1. VisionImageInput image, {
  2. void capture(
    1. String stage,
    2. Float32List rows
    )?,
  3. void checkCancelled()?,
  4. void onProgress(
    1. int completed,
    2. int total
    )?,
})

Same arithmetic as encode, yielding to the event loop between stages. checkCancelled may throw to abort before the next stage is computed. A stage itself is synchronous CPU work; run inference in a worker isolate when integrating with a UI.

Implementation

Future<Float32List> encodeAsync(
  VisionImageInput image, {
  void Function(String stage, Float32List rows)? capture,
  void Function()? checkCancelled,
  void Function(int completed, int total)? onProgress,
}) async {
  await Future<void>.delayed(Duration.zero);
  checkCancelled?.call();
  Float32List? result;
  var completed = 0;
  for (final (stage, rows) in _stages(image)) {
    capture?.call(stage, Float32List.fromList(rows));
    result = rows;
    onProgress?.call(++completed, config.vision.numLayers + 2);
    await Future<void>.delayed(Duration.zero);
    checkCancelled?.call();
  }
  return result!;
}