code: def map_attentions(
      img_data,
      attentions,
      pred,
      pad_width,
      pad_height,
      threshold=1,
      normalize=False,
      binarize=True,
  ):
      """Map the attentions to the image."""
      img_out_agg = np.zeros(img_data.shape)
      img_out_frames = []
  
      width, height = img_data.shape[1], img_data.shape[0]
  
      # Calculate the image resizing proportions.
      width_resize_ratio, height_resize_ratio = 1, 1
      max_width = math.ceil((width / height) * pad_height)
      max_height = math.ceil((pad_width / max_width) * pad_height)
      if pad_width >= max_width:
          if pad_height < height:
              width_resize_ratio = width / max_width
              height_resize_ratio = height / pad_height
      else:
          width_resize_ratio = width / pad_width
          height_resize_ratio = height / max_height
  
      # Map the attention for each predicted character.
      for idx in range(len(pred)):
          attention = attentions[0][idx]
  
          # Get maximal attentional focus.
          score = attention.max()
  
          # Reshape the attention vector.
          nrows = 1  # should match number of encoded rows
          attention = attention.reshape((nrows, -1))
  
          # Map attention to fixed value.
          if normalize:
              attention *= 1.0 / attention.max()
              if binarize:
                  attention[attention < threshold] = 0
          elif binarize:
              attention[attention >= score * threshold] = 1
              attention[attention < score] = 0
  
          # Resize attention to the image size, cropping padded regions.
          attention = Image.fromarray(attention)
          attention = attention.resize(
              (
                  int(pad_width * width_resize_ratio),
                  int(pad_height * height_resize_ratio),
              ),
              Image.NEAREST,
          )
          attention = attention.crop((0, 0, width, height))
          attention = np.asarray(attention)
  
          # Add new axis as needed (e.g., RGB images).
          if len(img_data.shape) == 3:
              attention = attention[..., np.newaxis]
  
          # Update the image frame with attended region(s).
          img_out_i = (img_data * np.maximum(attention, 0.3)).astype(
              np.uint8
          )
          img_out_i = Image.fromarray(img_out_i)
          img_out_i = img_out_i.convert("RGB")
  
          # Add animation frame to list.
          img_out_frames.append(img_out_i)
  
          # Add attention to aggregate.
          img_out_agg += attention
  
      return img_out_frames, img_out_agg
  
file_path: attention-ocr/aocr/util/visualizations.py
imports: from __future__ import absolute_import
  from __future__ import division
  import math
  import os
  from io import BytesIO
  import numpy
  from PIL import Image
label_desc: Use image node_id 2 for calling a global function with example usage: map_attentions(img_data, attentions, pred, pad_width, pad_height, threshold, normalize, binarize) and returns: img_out_frames,  img_out_agg