code: def custom_crop_image(
      frames: tf.Tensor,
      height: int,
      width: int,
      random: bool,
      state: Optional[builders.ProcessorState] = None,
  ) -> tf.Tensor:
      """Add more metadata to processors.crop_image."""
      if random:
          # Random spatial crop. tf.image.random_crop is not used since the offset is
          # needed to ensure consistency between crops on different modalities.
          shape = tf.shape(input=frames)
          # If a static_shape is available (e.g. when using this method from add_image
          # method), it will be used to have an output tensor with static shape.
          static_shape = frames.shape.as_list()
          seq_len = (
              shape[0] if static_shape[0] is None else static_shape[0]
          )
          channels = (
              shape[3] if static_shape[3] is None else static_shape[3]
          )
          size = tf.convert_to_tensor(
              value=(seq_len, height, width, channels)
          )
  
          if state and "crop_offset_proportion" in state:
              # Use offset set by a previous cropping: [0, offset_h, offset_w, 0].
              offset = state["crop_offset_proportion"] * tf.cast(
                  shape, tf.float32
              )
              offset = tf.cast(tf.math.round(offset), tf.int32)
          else:
              # Limit of possible offset in order to fit the entire crop:
              # [1, input_h - target_h + 1, input_w - target_w + 1, 1].
              limit = shape - size + 1
              offset = (
                  tf.random.uniform(
                      shape=(4,),
                      dtype=tf.int32,
                      maxval=tf.int32.max,
                  )
                  % limit
              )  # [0, offset_h, offset_w, 0]
  
              if state is not None:
                  # Update state.
                  offset_proportion = tf.cast(
                      offset, tf.float32
                  ) / tf.cast(shape, tf.float32)
                  state[
                      "crop_offset_proportion"
                  ] = offset_proportion  # 4
                  state["crop_ori_shape"] = shape  # 4
                  state["crop_offset_abs"] = offset  # 4
  
          frames = tf.slice(frames, offset, size)
      else:
          # Central crop or pad.
          shape = tf.shape(input=frames)
          static_shape = frames.shape.as_list()
          seq_len = (
              shape[0] if static_shape[0] is None else static_shape[0]
          )
          channels = (
              shape[3] if static_shape[3] is None else static_shape[3]
          )
          size = tf.convert_to_tensor(
              value=(seq_len, height, width, channels)
          )
          offset = tf.convert_to_tensor(
              value=(
                  0,
                  (shape[1] - height) // 2,
                  (shape[2] - width) // 2,
                  0,
              )
          )
          frames = tf.slice(frames, offset, size)
          if state is not None:
              state["crop_ori_shape"] = shape
              state["crop_offset_abs"] = offset
      return frames
  
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 6 for calling a global function with example usage: custom_crop_image(frames, height, width, random, state) and returns: frames