code: def generate_mask_RoI(
      data,
      image_root,
      output_path,
      suffix,
      class_id=None,
      pad_scale=0.5,
  ):
      output_image_path = osp.join(output_path, "images")
      _mkdir_p(output_image_path)
      output_anno_path = osp.join(output_path, "anno")
      _mkdir_p(output_anno_path)
      file_list = osp.join(output_path, "RoI.txt")
      f = open(file_list, "w")
  
      if not class_id:
          class_id = data.getCatIds()
      img_ids = list(sorted(data.imgs.keys()))
      for img_id in img_ids:
          im_info = data.loadImgs(img_id)[0]
          ann_ids = data.getAnnIds(
              imgIds=img_id, catIds=class_id, iscrowd=False
          )
          annos = data.loadAnns(ann_ids)
          if len(annos) == 0:
              continue
          img = cv2.imread(osp.join(image_root, im_info["file_name"]))
  
          if img is None:
              raise FileNotFoundError(
                  "{} is not found.".format(
                      osp.join(image_root, im_info["file_name"])
                  )
              )
  
          base_name = os.path.basename(im_info["file_name"]).split(".")[
              0
          ]
          polygons = []
          for anno in annos:
              polygons.extend(anno["segmentation"])
          mask = polygons_to_bitmask(
              polygons, img.shape[0], img.shape[1]
          )
          for idx, anno in enumerate(annos):
              bbox = list(map(int, anno["bbox"]))
              bbox = adjust_bbox(
                  [
                      bbox[0],
                      bbox[1],
                      bbox[0] + bbox[2],
                      bbox[1] + bbox[3],
                  ],
                  img.shape[:2],
                  pad_scale=pad_scale,
              )
              crop_img = img[bbox[1] : bbox[3], bbox[0] : bbox[2], :]
              crop_mask = mask[bbox[1] : bbox[3], bbox[0] : bbox[2]]
              img_save_path = osp.join(
                  output_image_path, f"{base_name}_{idx}.png"
              )
              anno_save_path = osp.join(
                  output_anno_path, f"{base_name}_{idx}{suffix}"
              )
              cv2.imwrite(img_save_path, crop_img)
              cv2.imwrite(anno_save_path, crop_mask)
              line = img_save_path + " " + anno_save_path + "\n"
              f.write(line)
      f.close()
  
file_path: PaddleSeg/contrib/QualityInspector/tools/convert_tools/convert_coco_to_RoI_mask.py
imports: import argparse
  import os
  import os.path
  import sys
  import cv2
  from pycocotools.coco import COCO
  from qinspector.utils.bbox_utils import adjust_bbox
  from qinspector.utils.visualizer import polygons_to_bitmask
label_desc: Use image node_id 3 for calling a global function with example usage: generate_mask_RoI(data, image_root, output_path, suffix, class_id, pad_scale) without return types