export function validateModelConfig()

in pose-detection/src/movenet/detector_utils.ts [24:84]


export function validateModelConfig(modelConfig: MoveNetModelConfig):
    MoveNetModelConfig {
  const config = modelConfig == null ? MOVENET_CONFIG : {...modelConfig};

  if (config.modelType == null) {
    config.modelType = 'SinglePose.Lightning';
  } else if (VALID_MODELS.indexOf(config.modelType) < 0) {
    throw new Error(
        `Invalid architecture ${config.modelType}. ` +
        `Should be one of ${VALID_MODELS}`);
  }

  if (config.enableSmoothing == null) {
    config.enableSmoothing = true;
  }

  if (config.minPoseScore != null &&
      (config.minPoseScore < 0.0 || config.minPoseScore > 1.0)) {
    throw new Error(`minPoseScore should be between 0.0 and 1.0`);
  }

  if (config.multiPoseMaxDimension != null &&
      (config.multiPoseMaxDimension % 32 !== 0 ||
       config.multiPoseMaxDimension < 32)) {
    throw new Error(
        `multiPoseMaxDimension must be a multiple of 32 and higher than 0`);
  }

  if (config.modelType === MULTIPOSE_LIGHTNING &&
      config.enableTracking == null) {
    config.enableTracking = true;
  }

  if (config.modelType === MULTIPOSE_LIGHTNING &&
      config.enableTracking === true) {
    if (config.trackerType == null) {
      config.trackerType = TrackerType.BoundingBox;
    }
    if (config.trackerType === TrackerType.Keypoint) {
      if (config.trackerConfig != null) {
        config.trackerConfig = mergeKeypointTrackerConfig(config.trackerConfig);
      } else {
        config.trackerConfig = DEFAULT_KEYPOINT_TRACKER_CONFIG;
      }
    } else if (config.trackerType === TrackerType.BoundingBox) {
      if (config.trackerConfig != null) {
        config.trackerConfig =
            mergeBoundingBoxTrackerConfig(config.trackerConfig);
      } else {
        config.trackerConfig = DEFAULT_BOUNDING_BOX_TRACKER_CONFIG;
      }
    } else {
      throw new Error('Tracker type not supported by MoveNet');
    }

    // We don't need to validate the trackerConfig here because the tracker will
    // take care of that.
  }

  return config;
}