Lfm3ImageProcessor.fromJson constructor

Lfm3ImageProcessor.fromJson(
  1. Map<String, Object?> json
)

Implementation

factory Lfm3ImageProcessor.fromJson(Map<String, Object?> json) {
  final image = json['image_processor'];
  if (image is! Map<String, Object?>) {
    throw const FormatException(
      'processor_config.json lacks image_processor',
    );
  }
  for (final entry in {
    'image_processor_type': 'Lfm3VlImageProcessor',
    'do_convert_rgb': true,
    'do_resize': true,
    'do_rescale': true,
    'do_normalize': true,
    'resample': 3,
    'rescale_factor': 1 / 255,
  }.entries) {
    if (image[entry.key] != entry.value) {
      throw FormatException(
        'unsupported image processor ${entry.key}: ${image[entry.key]}',
      );
    }
  }
  for (final key in ['image_mean', 'image_std']) {
    final values = image[key];
    if (values is! List ||
        values.length != 3 ||
        values.any((v) => v != 0.5)) {
      throw FormatException('unsupported image processor $key');
    }
  }
  final size = image['size'];
  if (size is! Map<String, Object?>) {
    throw const FormatException('missing image size budget');
  }
  int integer(Map<String, Object?> map, String key) {
    final value = map[key];
    if (value is! int) throw FormatException('image $key must be an integer');
    return value;
  }

  return Lfm3ImageProcessor(
    patchSize: integer(image, 'patch_size'),
    mergeSize: integer(image, 'merge_size'),
    temporalPatchSize: integer(image, 'temporal_patch_size'),
    minPixels: integer(size, 'shortest_edge'),
    maxPixels: integer(size, 'longest_edge'),
  );
}