Lfm3VideoProcessor.fromJson constructor

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

Implementation

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

  final size = video['size'];
  final fps = video['fps'];
  if (size is! Map<String, Object?> || fps is! num) {
    throw const FormatException('missing video size budget or fps');
  }
  return Lfm3VideoProcessor(
    patchSize: integer(video, 'patch_size'),
    mergeSize: integer(video, 'merge_size'),
    temporalPatchSize: integer(video, 'temporal_patch_size'),
    fps: fps.toDouble(),
    minFrames: integer(video, 'min_frames'),
    maxFrames: integer(video, 'max_frames'),
    minPixels: integer(size, 'shortest_edge'),
    maxPixels: integer(size, 'longest_edge'),
    capPixelsPerFrame: video['cap_pixels_per_frame'] == true,
    maxVideoTokens: integer(video, 'max_video_tokens'),
  );
}