code: def obtain_dataset_cfg(
      cfg: Config, phase: str, mode: str, task: str
  ) -> Tuple:
      """Obtain dataset and visualizer from config. Two modes are supported:
      1. Model Config Mode:
          In this mode, the input config should be a complete model config, which
          includes a dataset within pipeline and a visualizer.
      2. Dataset Config Mode:
          In this mode, the input config should be a complete dataset config,
          which only includes basic dataset information, and it may does not
          contain a visualizer and dataset pipeline.
  
      Examples:
          Typically, the model config files are stored in
          `configs/textdet/dbnet/xxx.py` and should look like:
          >>> train_dataloader = dict(
          >>>     batch_size=16,
          >>>     num_workers=8,
          >>>     persistent_workers=True,
          >>>     sampler=dict(type='DefaultSampler', shuffle=True),
          >>>     dataset=icdar2015_textdet_train)
  
          while the dataset config files are stored in
          `configs/textdet/_base_/datasets/xxx.py` and should be like:
          >>> icdar2015_textdet_train = dict(
          >>>     type='OCRDataset',
          >>>     data_root=ic15_det_data_root,
          >>>     ann_file='textdet_train.json',
          >>>     filter_cfg=dict(filter_empty_gt=True, min_size=32),
          >>>     pipeline=None)
  
      Args:
          cfg (Config): Config object.
          phase (str): The dataset phase to visualize.
          mode (str): Script mode.
          task (str): The current task type.
  
      Returns:
          Tuple: Tuple of (dataset, visualizer).
      """
      default_cfgs = dict(
          textdet=dict(
              visualizer=dict(
                  type="TextDetLocalVisualizer",
                  name="visualizer",
                  vis_backends=[dict(type="LocalVisBackend")],
              ),
              pipeline=[
                  dict(
                      type="LoadImageFromFile",
                      color_type="color_ignore_orientation",
                  ),
                  dict(
                      type="LoadOCRAnnotations",
                      with_polygon=True,
                      with_bbox=True,
                      with_label=True,
                  ),
                  dict(
                      type="PackTextDetInputs",
                      meta_keys=("img_path", "ori_shape", "img_shape"),
                  ),
              ],
          ),
          textrecog=dict(
              visualizer=dict(
                  type="TextRecogLocalVisualizer",
                  name="visualizer",
                  vis_backends=[dict(type="LocalVisBackend")],
              ),
              pipeline=[
                  dict(
                      type="LoadImageFromFile",
                      ignore_empty=True,
                      min_size=2,
                  ),
                  dict(type="LoadOCRAnnotations", with_text=True),
                  dict(
                      type="PackTextRecogInputs",
                      meta_keys=(
                          "img_path",
                          "ori_shape",
                          "img_shape",
                          "valid_ratio",
                      ),
                  ),
              ],
          ),
      )
  
      # Model config mode
      dataloader_name = f"{phase}_dataloader"
      if dataloader_name in cfg:
          dataset = cfg.get(dataloader_name).dataset
          visualizer = cfg.visualizer
  
          if mode == "original":
              default_cfg = default_cfgs[
                  infer_dataset_task(task, dataset)
              ]
              # Image can be stored in other methods, like LMDB,
              # which LoadImageFromFile can not handle
              if dataset.pipeline is not None:
                  all_transform_types = [
                      tfm["type"] for tfm in dataset.pipeline
                  ]
                  if any(
                      [
                          tfm_type.startswith("LoadImageFrom")
                          for tfm_type in all_transform_types
                      ]
                  ):
                      for tfm in dataset.pipeline:
                          if tfm["type"].startswith("LoadImageFrom"):
                              # update LoadImageFrom** transform
                              default_cfg["pipeline"][0] = tfm
              dataset.pipeline = default_cfg["pipeline"]
          else:
              # In test_pipeline LoadOCRAnnotations is placed behind
              # other transforms. Transform will not be applied on
              # gt annotation.
              if phase == "test":
                  all_transform_types = [
                      tfm["type"] for tfm in dataset.pipeline
                  ]
                  load_ocr_ann_tfm_index = all_transform_types.index(
                      "LoadOCRAnnotations"
                  )
                  load_ocr_ann_tfm = dataset.pipeline.pop(
                      load_ocr_ann_tfm_index
                  )
                  dataset.pipeline.insert(1, load_ocr_ann_tfm)
  
          return dataset, visualizer
  
      # Dataset config mode
  
      for key in cfg.keys():
          if key.endswith(phase) and cfg[key]["type"].endswith(
              "Dataset"
          ):
              dataset = cfg[key]
              default_cfg = default_cfgs[
                  infer_dataset_task(task, dataset, key.lower())
              ]
              visualizer = default_cfg["visualizer"]
              dataset["pipeline"] = (
                  default_cfg["pipeline"]
                  if dataset["pipeline"] is None
                  else dataset["pipeline"]
              )
  
              return dataset, visualizer
  
      raise ValueError(
          f'Unable to find "{phase}_dataloader" or any dataset variable ending '
          f'with "{phase}". Please check your config file or --phase argument '
          "and try again. More details can be found in the docstring of "
          "obtain_dataset_cfg function. Or, you may visit the documentation via "
          "https://mmocr.readthedocs.io/en/dev-1.x/user_guides/useful_tools.html#dataset-visualization-tool"  # noqa: E501
      )
  
imports: import argparse
  import os.path
  import sys
  from typing import Optional, Tuple
  import cv2
  import mmcv
  import numpy
  from mmengine.config import Config, DictAction
  from mmengine.dataset import Compose
  from mmengine.registry import init_default_scope
  from mmengine.utils import ProgressBar
  from mmengine.visualization import Visualizer
  from mmocr.registry import DATASETS, VISUALIZERS
label_desc: Use image node_id 5 for calling a global function with example usage: obtain_dataset_cfg(cfg, phase, mode, task) and returns: dataset,  visualizer, dataset,  visualizer