webClassificationResult function

ClassificationResult webClassificationResult(
  1. Map<Object?, Object?> result
)

Converts a JSON-like MediaPipe web classification result.

Every field is validated so a malformed runtime response surfaces as an MpException instead of a null-check or cast error.

Implementation

ClassificationResult webClassificationResult(Map<Object?, Object?> result) {
  final List<Object?> heads = _requireList(result['classifications'], 'classifications');
  return ClassificationResult(
    timestampMs: webOptionalInt(result['timestampMs']),
    classifications: heads.map((Object? value) {
      final Map<Object?, Object?> head = _requireMap(value, 'classification head');
      final List<Object?> categories = _requireList(head['categories'], 'categories');
      return Classifications(
        headIndex: _requireInt(head['headIndex'], 'headIndex'),
        headName: _optionalString(head['headName'], 'headName'),
        categories: categories.map((Object? categoryValue) {
          final Map<Object?, Object?> category = _requireMap(categoryValue, 'category');
          return Category(
            index: _requireInt(category['index'], 'category index'),
            score: _requireDouble(category['score'], 'category score'),
            categoryName: _optionalString(category['categoryName'], 'categoryName'),
            displayName: _optionalString(category['displayName'], 'displayName'),
          );
        }),
      );
    }),
  );
}