predict method

Future<Map<String, dynamic>> predict(
  1. Uint8List imageBytes, {
  2. double? confidenceThreshold,
  3. double? iouThreshold,
})

Implementation

Future<Map<String, dynamic>> predict(
  Uint8List imageBytes, {
  double? confidenceThreshold,
  double? iouThreshold,
}) async {
  if (imageBytes.isEmpty) {
    throw InvalidInputException('Image data is empty');
  }

  if (confidenceThreshold != null &&
      (confidenceThreshold < 0.0 || confidenceThreshold > 1.0)) {
    throw InvalidInputException(
      'Confidence threshold must be between 0.0 and 1.0',
    );
  }
  if (iouThreshold != null && (iouThreshold < 0.0 || iouThreshold > 1.0)) {
    throw InvalidInputException('IoU threshold must be between 0.0 and 1.0');
  }

  try {
    final Map<String, dynamic> arguments = {'image': imageBytes};

    if (confidenceThreshold != null) {
      arguments['confidenceThreshold'] = confidenceThreshold;
    }
    if (iouThreshold != null) {
      arguments['iouThreshold'] = iouThreshold;
    }

    if (_instanceId != 'default') {
      arguments['instanceId'] = _instanceId;
    }

    final result = await _channel.invokeMethod(
      'predictSingleImage',
      arguments,
    );

    if (result is Map) {
      return _processInferenceResult(result);
    }

    throw InferenceException('Invalid result format returned from inference');
  } on PlatformException catch (e) {
    throw YOLOErrorHandler.handleError(e, 'Error during image prediction');
  } catch (e) {
    throw YOLOErrorHandler.handleError(e, 'Error during image prediction');
  }
}