code: def add_image_and_boxes(
      parser_builder: builders.BaseParserBuilder,
      sampler_builder: builders.SamplerBuilder,
      decoder_builder: builders.DecoderBuilder,
      preprocessor_builder: builders.PreprocessorBuilder,
      postprocessor_builder: builders.PostprocessorBuilder,
      input_feature_name: str = "image/encoded",
      output_feature_name: str = builders.IMAGE_FEATURE_NAME,
      is_training: bool = True,
      # Video related parameters.
      num_frames: int = 32,
      stride: int = 1,
      num_test_clips: int = 1,
      min_resize: int = 224,
      resize_method: str = tf.image.ResizeMethod.BILINEAR,
      crop_size: int = 200,
      use_crop_and_resize_video_mae: bool = False,
      train_frame_sampling_mode: Optional[str] = None,
      zero_centering_image: bool = False,
      random_flip: bool = True,
      normalization_mean: Union[tf.Tensor, float] = 0,
      normalization_std: Union[tf.Tensor, float] = 1,
      object_configs: ml_collections.ConfigDict = ml_collections.ConfigDict(),
  ) -> None:
      """Same as add_image with additional support boxes."""
      with_boxes = object_configs.get("with_boxes", False)
      keep_full_frames = object_configs.get("keep_full_frames", 0)
      bbox_key = object_configs.get("bbox_key", "gt")
      bbox_num = object_configs.get("bbox_num", 100)
      return_boxes = object_configs.get("return_boxes", -1)
      mask_radius = object_configs.get("mask_radius", -1.0)
      min_box_size = object_configs.get("min_box_size", 0)
      concat_mask = object_configs.get("concat_mask", False)
      add_detections = object_configs.get("add_detections", False)
      detection_stride_spatial = object_configs.get(
          "detection_stride_spatial", 1
      )
      detection_stride_temporal = object_configs.get(
          "detection_stride_temporal", 1
      )
      tracked_objects = object_configs.get("tracked_objects", False)
      add_global_box = object_configs.get("add_global_box", False)
  
      if is_training and num_test_clips != 1:
          logging.info(
              "`num_test_clips` %d is ignored since `is_training` is true.",
              num_test_clips,
          )
      # Parse frames or single image.
      assert isinstance(
          parser_builder, builders.SequenceExampleParserBuilder
      )
      parser_builder.parse_feature(
          feature_name=input_feature_name,
          feature_type=tf.io.FixedLenSequenceFeature(
              (), dtype=tf.string
          ),
          output_name=output_feature_name,
      )
  
      if with_boxes:
          for coord in ["xmax", "xmin", "ymax", "ymin", "score"]:
              parser_builder.parse_feature(
                  feature_name=f"{bbox_key}/bbox/{coord}",
                  feature_type=tf.io.VarLenFeature(dtype=tf.float32),
                  output_name=coord,
              )
              sampler_builder.add_fn(
                  fn=tf.sparse.to_dense,
                  feature_name=coord,
                  fn_name="{}_sparse_to_dense".format(coord),
              )
          preprocessor_builder.add_fn(
              fn=aggregate_bbox_coords,
              feature_name=None,
              fn_name="aggregate_bbox_coords",
          )
  
      # pylint: disable=g-long-lambda
      # Temporal sampler.
      if is_training:
          if train_frame_sampling_mode == "segment_sampling":
              # Sample random clip.
              sampler_builder.add_fn(
                  fn=lambda x, s: sample_sequence_uniformly_with_state(
                      x, num_frames, True, s
                  ),
                  feature_name=output_feature_name,
                  fn_name=f"{output_feature_name}_random_sample",
                  stateful=True,
              )
              if with_boxes:
                  for coord in [
                      "xmax",
                      "xmin",
                      "ymax",
                      "ymin",
                      "score",
                  ]:
                      sampler_builder.add_fn(
                          fn=lambda x, s: sample_sequence_uniformly_with_state(
                              x, num_frames, True, s
                          ),
                          feature_name=coord,
                          fn_name=f"{coord}_random_sample",
                          stateful=True,
                      )
          else:
              # Sample random clip.
              sampler_builder.add_fn(
                  fn=lambda x, s=None: processors.sample_sequence(
                      x, num_frames, True, stride, state=s
                  ),
                  feature_name=output_feature_name,
                  fn_name=f"{output_feature_name}_random_sample",
                  stateful=True,
              )
              if with_boxes:
                  for coord in [
                      "xmax",
                      "xmin",
                      "ymax",
                      "ymin",
                      "score",
                  ]:
                      sampler_builder.add_fn(
                          fn=lambda x, s=None: processors.sample_sequence(
                              x, num_frames, True, stride, state=s
                          ),
                          feature_name=coord,
                          fn_name=f"{coord}_random_sample",
                          stateful=True,
                      )
      else:
          if num_test_clips > 1:
              if train_frame_sampling_mode == "segment_sampling":
                  if num_test_clips != 2:
                      raise ValueError(
                          "For segment_sampling only 2 video clips are allowed."
                      )
                  sampler_builder.add_fn(
                      fn=lambda x: sample_two_sequences_uniformly(
                          x, num_frames
                      ),
                      feature_name=output_feature_name,
                      fn_name=f"{output_feature_name}_segment_sampling_test",
                  )
                  if with_boxes:
                      for coord in [
                          "xmax",
                          "xmin",
                          "ymax",
                          "ymin",
                          "score",
                      ]:
                          sampler_builder.add_fn(
                              fn=lambda x: sample_two_sequences_uniformly(
                                  x, num_frames
                              ),
                              feature_name=coord,
                              fn_name=f"{coord}_segment_sampling_test",
                          )
              else:
                  # Sample linspace clips.
                  sampler_builder.add_fn(
                      fn=lambda x: processors.sample_linspace_sequence(
                          x, num_test_clips, num_frames, stride
                      ),
                      feature_name=output_feature_name,
                      fn_name=f"{output_feature_name}_linspace_sample",
                  )
                  if with_boxes:
                      for coord in [
                          "xmax",
                          "xmin",
                          "ymax",
                          "ymin",
                          "score",
                      ]:
                          sampler_builder.add_fn(
                              fn=lambda x: processors.sample_linspace_sequence(
                                  x, num_test_clips, num_frames, stride
                              ),
                              feature_name=coord,
                              fn_name=f"{coord}_linspace_sample",
                          )
          else:
              if train_frame_sampling_mode == "segment_sampling":
                  sampler_builder.add_fn(
                      fn=lambda x: sample_sequence_uniformly(
                          x, num_frames, is_training=False
                      ),
                      feature_name=output_feature_name,
                      fn_name=f"{output_feature_name}_segment_sampling_train",
                  )
                  if with_boxes:
                      for coord in [
                          "xmax",
                          "xmin",
                          "ymax",
                          "ymin",
                          "score",
                      ]:
                          sampler_builder.add_fn(
                              fn=lambda x: sample_sequence_uniformly(
                                  x, num_frames, is_training=False
                              ),
                              feature_name=coord,
                              fn_name=f"{coord}_segment_sampling_train",
                          )
              else:
                  # Sample middle clip.
                  sampler_builder.add_fn(
                      fn=lambda x: processors.sample_sequence(
                          x, num_frames, False, stride
                      ),
                      feature_name=output_feature_name,
                      fn_name=f"{output_feature_name}_middle_sample",
                  )
                  if with_boxes:
                      for coord in [
                          "xmax",
                          "xmin",
                          "ymax",
                          "ymin",
                          "score",
                      ]:
                          sampler_builder.add_fn(
                              fn=lambda x: processors.sample_sequence(
                                  x, num_frames, False, stride
                              ),
                              feature_name=coord,
                              fn_name=f"{coord}_middle_sample",
                          )
  
      decoder_builder.add_fn(
          fn=lambda x: processors.decode_jpeg(x, channels=3),
          feature_name=output_feature_name,
          fn_name=f"{output_feature_name}_decode_jpeg",
      )
  
      # Resize images (resize happens only if necessary to save compute).
      preprocessor_builder.add_fn(
          fn=lambda x: processors.resize_smallest(
              x, min_resize, is_flow=False, method=resize_method
          ),
          feature_name=output_feature_name,
          fn_name=f"{output_feature_name}_resize_smallest",
      )
      # We don't need to do this for boxes as boxes are relative coordinates
  
      if is_training:
          # Standard image data augmentation: random crop and random flip.
          if use_crop_and_resize_video_mae:
              preprocessor_builder.add_fn(
                  fn=lambda x, s=None: crop_and_resize_image_tong(x),
                  feature_name=output_feature_name,
                  fn_name=f"{output_feature_name}_crop_and_resize",
                  stateful=True,
              )
              assert not with_boxes
          else:
              preprocessor_builder.add_fn(
                  fn=lambda x, s=None: custom_crop_image(
                      x, crop_size, crop_size, True, state=s
                  ),
                  feature_name=output_feature_name,
                  fn_name=f"{output_feature_name}_random_crop",
                  stateful=True,
              )
              if with_boxes:
                  preprocessor_builder.add_fn(
                      fn=lambda x, s: transform_box(
                          x, crop_size, crop_size, state=s
                      ),
                      feature_name="bboxes",
                      fn_name="bboxes_random_crop",
                      stateful=True,
                  )
          if random_flip:
              preprocessor_builder.add_fn(
                  fn=lambda x, s=None: processors.random_flip_left_right(
                      x, state=s, is_flow=False
                  ),
                  feature_name=output_feature_name,
                  fn_name=f"{output_feature_name}_random_flip",
                  stateful=True,
              )
              if with_boxes:
                  preprocessor_builder.add_fn(
                      fn=lambda x, s: random_flip_boxes_left_right(
                          x, state=s
                      ),
                      feature_name="bboxes",
                      fn_name="bboxes_random_flip",
                      stateful=True,
                  )
      else:
          if with_boxes:
              # Central crop of the frames.
              preprocessor_builder.add_fn(
                  fn=lambda x, s: custom_crop_image(
                      x, crop_size, crop_size, False, s
                  ),
                  feature_name=output_feature_name,
                  fn_name=f"{output_feature_name}_central_crop",
                  stateful=True,
              )
              preprocessor_builder.add_fn(
                  fn=lambda x, s: transform_box(
                      x, crop_size, crop_size, state=s
                  ),
                  feature_name="bboxes",
                  fn_name="bboxes_central_crop",
                  stateful=True,
              )
          else:
              preprocessor_builder.add_fn(
                  fn=lambda x: processors.crop_image(
                      x, crop_size, crop_size, False
                  ),
                  feature_name=output_feature_name,
                  fn_name=f"{output_feature_name}_central_crop",
              )
  
      # Cast the frames to `tf.float32`, normalizing according to
      # `zero_centering_image`.
      preprocessor_builder.add_fn(
          fn=lambda x: processors.normalize_image(
              x, zero_centering_image
          ),
          feature_name=output_feature_name,
          fn_name=f"{output_feature_name}_normalize",
      )
  
      preprocessor_builder.add_fn(
          fn=lambda x: x - normalization_mean,
          feature_name=output_feature_name,
          fn_name=f"{output_feature_name}_subtract_given_mean",
      )
  
      preprocessor_builder.add_fn(
          fn=lambda x: x / normalization_std,
          feature_name=output_feature_name,
          fn_name=f"{output_feature_name}_divide_by_given_std",
      )
  
      if with_boxes:
          preprocessor_builder.add_fn(
              fn=lambda x: append_or_add_object_mask(
                  x,
                  num_frames,
                  keep_full_frames,
                  bbox_num,
                  mask_radius,
                  concat_mask,
                  min_box_size=min_box_size,
                  add_detections=add_detections,
                  detection_stride_spatial=detection_stride_spatial,
                  detection_stride_temporal=detection_stride_temporal,
                  tracked_objects=tracked_objects,
                  add_global_box=add_global_box,
              ),
              feature_name=None,
              fn_name="mask_bbox_region_fn",
          )
          if return_boxes > 0:
              preprocessor_builder.add_fn(
                  fn=lambda x: pad_bboxes(x, return_boxes),
                  feature_name=None,
                  fn_name="pad_bboxes",
              )
          else:
              preprocessor_builder.add_fn(
                  fn=remove_bboxes,
                  feature_name=None,
                  fn_name="remove_bboxes",
              )
  
      if num_test_clips > 1 and not is_training:
          # In this case, multiple clips are merged together in batch dimension which
          # will be `B * num_test_clips`.
          postprocessor_builder.add_fn(
              fn=lambda x: tf.reshape(
                  x,
                  (-1, num_frames, x.shape[2], x.shape[3], x.shape[4]),
              ),
              feature_name=output_feature_name,
              fn_name=f"{output_feature_name}_reshape",
          )
          if add_detections:
              postprocessor_builder.add_fn(
                  fn=lambda x: tf.reshape(
                      x,
                      (
                          -1,
                          num_frames
                          // detection_stride_temporal
                          * (crop_size // detection_stride_spatial)
                          ** 2,
                      ),
                  ),
                  feature_name="detections",
                  fn_name="detections_reshape",
              )
          if return_boxes > 0:
              postprocessor_builder.add_fn(
                  fn=lambda x: tf.reshape(
                      x, (-1, num_frames, return_boxes, 4)
                  ),
                  feature_name="bboxes",
                  fn_name="bboxes_reshape",
              )
  
imports: from typing import Optional, Union
  from absl import logging
  from dmvr import builders
  from dmvr import processors
  import ml_collections
  import tensorflow
label_desc: Use image node_id 1 for calling a global function with example usage: add_image_and_boxes(parser_builder, sampler_builder, decoder_builder, preprocessor_builder, postprocessor_builder, input_feature_name, output_feature_name, is_training, num_frames, stride, num_test_clips, min_resize, resize_method, crop_size, use_crop_and_resize_video_mae, train_frame_sampling_mode, zero_centering_image, random_flip, normalization_mean, normalization_std, object_configs) without return types