YOLOResult.fromMap constructor

YOLOResult.fromMap(
  1. Map map
)

Creates a YOLOResult from a map representation.

This factory constructor is primarily used for deserializing results received from the platform channel. The map should contain keys:

  • 'classIndex': int
  • 'className': String
  • 'confidence': double
  • 'boundingBox': Map with 'left', 'top', 'right', 'bottom'
  • 'normalizedBox': Map with 'left', 'top', 'right', 'bottom'
  • 'mask': (optional) List of List of double
  • 'keypoints': (optional) List of double in x,y,confidence triplets

Implementation

factory YOLOResult.fromMap(Map<dynamic, dynamic> map) {
  final classIndex = map['classIndex'] as int;
  final className = map['className'] as String;
  final confidence = (map['confidence'] as num).toDouble();

  // Parse bounding box
  final boxMap = map['boundingBox'] as Map<dynamic, dynamic>;
  final boundingBox = Rect.fromLTRB(
    (boxMap['left'] as num).toDouble(),
    (boxMap['top'] as num).toDouble(),
    (boxMap['right'] as num).toDouble(),
    (boxMap['bottom'] as num).toDouble(),
  );

  // Parse normalized bounding box
  final normalizedBoxMap = map['normalizedBox'] as Map<dynamic, dynamic>;
  final normalizedBox = Rect.fromLTRB(
    (normalizedBoxMap['left'] as num).toDouble(),
    (normalizedBoxMap['top'] as num).toDouble(),
    (normalizedBoxMap['right'] as num).toDouble(),
    (normalizedBoxMap['bottom'] as num).toDouble(),
  );

  // Parse mask if available
  List<List<double>>? mask;
  if (map.containsKey('mask') && map['mask'] != null) {
    final maskData = map['mask'] as List<dynamic>;
    mask = maskData
        .map(
          (row) => (row as List<dynamic>)
              .map((val) => (val as num).toDouble())
              .toList(),
        )
        .toList();
  }

  // Parse keypoints if available
  List<Point>? keypoints;
  List<double>? keypointConfidences;
  if (map.containsKey('keypoints') && map['keypoints'] != null) {
    final keypointsData = map['keypoints'] as List<dynamic>;
    keypoints = [];
    keypointConfidences = [];

    for (var i = 0; i < keypointsData.length; i += 3) {
      keypoints.add(
        Point(
          (keypointsData[i] as num).toDouble(),
          (keypointsData[i + 1] as num).toDouble(),
        ),
      );
      keypointConfidences.add((keypointsData[i + 2] as num).toDouble());
    }
  }

  return YOLOResult(
    classIndex: classIndex,
    className: className,
    confidence: confidence,
    boundingBox: boundingBox,
    normalizedBox: normalizedBox,
    mask: mask,
    keypoints: keypoints,
    keypointConfidences: keypointConfidences,
  );
}