# MediaPipe Objectron object detection CPU subgraph.

type: "ObjectDetectionOidV4Subgraph"

input_stream: "IMAGE:input_video"
input_side_packet: "LABELS_CSV:allowed_labels"
output_stream: "DETECTIONS:detections"

# Crops, resizes, and converts the input video into tensor.
# Preserves aspect ratio of the images.
node {
  calculator: "ImageToTensorCalculator"
  input_stream: "IMAGE:input_video"
  output_stream: "TENSORS:image_tensor"
  output_stream: "LETTERBOX_PADDING:letterbox_padding"
  options {
    [mediapipe.ImageToTensorCalculatorOptions.ext] {
      output_tensor_width: 300
      output_tensor_height: 300
      keep_aspect_ratio: false
      output_tensor_float_range {
        min: -1.0
        max: 1.0
      }
    }
  }
}

# Runs a TensorFlow Lite model on CPU that takes an image tensor and outputs a
# vector of tensors representing, for instance, detection boxes/keypoints and
# scores.
node {
  calculator: "InferenceCalculator"
  input_stream: "TENSORS:image_tensor"
  output_stream: "TENSORS:detection_tensors"
  options: {
    [mediapipe.InferenceCalculatorOptions.ext] {
      model_path: "mediapipe/modules/objectron/object_detection_ssd_mobilenetv2_oidv4_fp16.tflite"
      delegate { xnnpack {} }
    }
  }
}

# Generates a single side packet containing a vector of SSD anchors based on
# the specification in the options.
node {
  calculator: "SsdAnchorsCalculator"
  output_side_packet: "anchors"
  options: {
    [mediapipe.SsdAnchorsCalculatorOptions.ext] {
      num_layers: 6
      min_scale: 0.2
      max_scale: 0.95
      input_size_height: 300
      input_size_width: 300
      anchor_offset_x: 0.5
      anchor_offset_y: 0.5
      strides: 16
      strides: 32
      strides: 64
      strides: 128
      strides: 256
      strides: 512
      aspect_ratios: 1.0
      aspect_ratios: 2.0
      aspect_ratios: 0.5
      aspect_ratios: 3.0
      aspect_ratios: 0.3333
      reduce_boxes_in_lowest_layer: true
    }
  }
}

# Decodes the detection tensors generated by the TensorFlow Lite model, based on
# the SSD anchors and the specification in the options, into a vector of
# detections. Each detection describes a detected object.
node {
  calculator: "TensorsToDetectionsCalculator"
  input_stream: "TENSORS:detection_tensors"
  input_side_packet: "ANCHORS:anchors"
  output_stream: "DETECTIONS:all_detections"
  options: {
    [mediapipe.TensorsToDetectionsCalculatorOptions.ext] {
      num_classes: 24
      num_boxes: 1917
      num_coords: 4
      ignore_classes: 0
      sigmoid_score: true
      apply_exponential_on_box_size: true
      x_scale: 10.0
      y_scale: 10.0
      h_scale: 5.0
      w_scale: 5.0
      min_score_thresh: 0.5
    }
  }
}

# Maps detection label IDs to the corresponding label text. The label map is
# provided in the label_map_path option.
node {
  calculator: "DetectionLabelIdToTextCalculator"
  input_stream: "all_detections"
  output_stream: "labeled_detections"
  options: {
    [mediapipe.DetectionLabelIdToTextCalculatorOptions.ext] {
      label_map_path: "mediapipe/modules/objectron/object_detection_oidv4_labelmap.txt"
    }
  }
}

# Filters the detections to only those with valid scores
# for the specified allowed labels.
node {
  calculator: "FilterDetectionCalculator"
  input_stream: "DETECTIONS:labeled_detections"
  output_stream: "DETECTIONS:filtered_detections"
  input_side_packet: "LABELS_CSV:allowed_labels"
}

# Performs non-max suppression to remove excessive detections.
node {
  calculator: "NonMaxSuppressionCalculator"
  input_stream: "filtered_detections"
  output_stream: "detections"
  options: {
    [mediapipe.NonMaxSuppressionCalculatorOptions.ext] {
      min_suppression_threshold: 0.5
      max_num_detections: 100
      overlap_type: INTERSECTION_OVER_UNION
      return_empty_detections: true
    }
  }
}
