code: def get_cocotext_recognizer_dataset(
      split="train",
      cache_dir=None,
      limit=None,
      legible_only=False,
      english_only=False,
      return_raw_labels=False,
  ):
      """Get a list of (filepath, box, word) tuples from the
      COCO-Text dataset.
  
      Args:
          split: Which split to get (train, val, or trainval)
          limit: Limit the number of files included in the download
          cache_dir: The directory in which to cache the file. The default is
              `~/.keras-ocr`.
          return_raw_labels: Whether to return the raw labels object
  
      Returns:
          A recognition dataset as a list of (filepath, box, word) tuples.
          If return_raw_labels is True, you will also get a (labels, images_dir)
          tuple containing the raw COCO data and the directory in which you
          can find the images.
      """
      assert split in [
          "train",
          "val",
          "trainval",
      ], f"Unsupported split: {split}"
      if cache_dir is None:
          cache_dir = tools.get_default_cache_dir()
      main_dir = os.path.join(cache_dir, "coco-text")
      images_dir = os.path.join(main_dir, "images")
      labels_zip = tools.download_and_verify(
          url="https://github.com/bgshih/cocotext/releases/download/dl/cocotext.v2.zip",
          cache_dir=main_dir,
          sha256="1444893ce7dbcd8419b2ec9be6beb0dba9cf8a43bf36cab4293d5ba6cecb7fb1",
      )
      with zipfile.ZipFile(labels_zip) as z:
          with z.open("cocotext.v2.json") as f:
              labels = json.loads(f.read())
      selected_ids = [
          cocoid
          for cocoid, data in labels["imgs"].items()
          if data["set"] in split
      ]
      if limit:
          flatten = lambda l: [
              item for sublist in l for item in sublist
          ]
          selected_ids = selected_ids[:limit]
          labels["imgToAnns"] = {
              k: v
              for k, v in labels["imgToAnns"].items()
              if k in selected_ids
          }
          labels["imgs"] = {
              k: v
              for k, v in labels["imgs"].items()
              if k in selected_ids
          }
          anns = set(flatten(list(labels.values())))
          labels["anns"] = {
              k: v for k, v in labels["anns"].items() if k in anns
          }
      selected_filenames = [
          labels["imgs"][cocoid]["file_name"] for cocoid in selected_ids
      ]
      with concurrent.futures.ThreadPoolExecutor() as executor:
          for future in tqdm.tqdm(
              concurrent.futures.as_completed(
                  [
                      executor.submit(
                          tools.download_and_verify,
                          url=f"http://images.cocodataset.org/train2014/{filename}",
                          cache_dir=images_dir,
                          verbose=False,
                      )
                      for filename in selected_filenames
                  ]
              ),
              total=len(selected_filenames),
              desc="Downloading images",
          ):
              _ = future.result()
      dataset = []
      for selected_id in selected_ids:
          filepath = os.path.join(
              images_dir,
              selected_filenames[selected_ids.index(selected_id)],
          )
          for annIdx in labels["imgToAnns"][selected_id]:
              ann = labels["anns"][str(annIdx)]
              if english_only and ann["language"] != "english":
                  continue
              if legible_only and ann["legibility"] != "legible":
                  continue
              dataset.append(
                  (
                      filepath,
                      np.array(ann["mask"]).reshape(-1, 2),
                      ann["utf8_string"],
                  )
              )
      if return_raw_labels:
          return dataset, (labels, images_dir)
      return dataset
  
imports: import concurrent.futures
  import itertools
  import warnings
  import typing
  import zipfile
  import random
  import glob
  import json
  import os
  import tqdm
  import imgaug
  import PIL.Image
  import numpy
  from .None import tools
label_desc: Use image node_id 2 for calling a global function with example usage: get_cocotext_recognizer_dataset(split, cache_dir, limit, legible_only, english_only, return_raw_labels) and returns: dataset, dataset,  labels,  images_dir