code: def convert_mask_to_roi(args):
      image_path = args.image_path
      anno_path = args.anno_path
      suffix = args.suffix
      output_path = args.output_path
  
      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)
  
      classid_list = list(range(1, args.class_num + 1))
      file_list = osp.join(output_path, "RoI.txt")
      f = open(file_list, "w")
  
      for img_name in os.listdir(image_path):
          file_name = osp.join(image_path, img_name)
          img = cv2.imread(file_name)
  
          if img is None:
              raise FileNotFoundError(
                  "{} is not found.".format(osp.join(file_name))
              )
  
          base_name = img_name.split(".")[0]
          anno_name = osp.join(anno_path, base_name + suffix)
          mask = cv2.imread(anno_name, -1)
  
          if mask is None:
              raise FileNotFoundError(
                  "{} is not found.".format(osp.join(anno_name))
              )
          if mask.sum() == 0:
              continue
  
          for cls_id in classid_list:
              class_map = np.equal(mask, cls_id).astype(np.uint8)
              if class_map.sum() == 0:
                  continue
              _, _, stats, _ = cv2.connectedComponentsWithStats(
                  class_map
              )
              for i, stat in enumerate(stats):
                  if i == 0:
                      continue
                  stat = list(map(int, stat))
                  bbox = adjust_bbox(
                      [
                          stat[0],
                          stat[1],
                          stat[0] + stat[2],
                          stat[1] + stat[3],
                      ],
                      img.shape[:2],
                      pad_scale=args.pad_scale,
                  )
                  img_crop = img[
                      bbox[1] : bbox[3], bbox[0] : bbox[2], :
                  ]
                  mask_crop = mask[bbox[1] : bbox[3], bbox[0] : bbox[2]]
                  if args.to_binary:
                      mask_crop[mask_crop > 0] = 1
                  # save crop img, mask and write to txt file
                  img_save_path = osp.join(
                      output_image_path, f"{base_name}_{cls_id}_{i}.png"
                  )
                  anno_save_path = osp.join(
                      output_anno_path,
                      f"{base_name}_{cls_id}_{i}{suffix}",
                  )
                  cv2.imwrite(img_save_path, img_crop)
                  cv2.imwrite(anno_save_path, mask_crop)
                  line = img_save_path + " " + anno_save_path + "\n"
                  f.write(line)
      f.close()
  
file_path: PaddleSeg/contrib/QualityInspector/tools/convert_tools/convert_mask_to_RoI.py
imports: import argparse
  import os
  import os.path
  import sys
  import cv2
  import numpy
  from qinspector.utils.bbox_utils import adjust_bbox
label_desc: Use image node_id 3 for calling a global function with example usage: convert_mask_to_roi(args) without return types