code: def convert_mask_to_coco(args):
      """convert mask to coco format"""
      images, annotations = [], []
      image_id, anno_id = 0, 0
      classid_list = list(
          range(1, args.class_num + 1)
      )  # 0 is background
      for img_name in os.listdir(args.image_path):
          image_id += 1
          file_name = osp.join(args.image_path, img_name)
          img = cv2.imread(file_name)
          if img is None:
              raise FileNotFoundError(
                  "{} is not found.".format(osp.join(file_name))
              )
          image_info = {
              "file_name": file_name,
              "id": image_id,
              "width": img.shape[1],
              "height": img.shape[0],
          }
          images.append(image_info)
          basename = osp.splitext(img_name)[0]
          anno_name = osp.join(args.anno_path, basename + args.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
              _, labels, stats, _ = cv2.connectedComponentsWithStats(
                  class_map
              )
              for i, stat in enumerate(stats):
                  if i == 0:
                      continue  # skip background
                  anno = {}
                  polygon = []
                  stat = list(map(int, stat))
                  contours, _ = cv2.findContours(
                      (labels == i).astype(np.uint8),
                      cv2.RETR_LIST,
                      cv2.CHAIN_APPROX_SIMPLE,
                  )
                  polygon.append(
                      contours[0].flatten().astype(int).tolist()
                  )
                  anno_id += 1
                  anno = {
                      "id": anno_id,
                      "image_id": image_id,
                      "category_id": cls_id,
                      "segmentation": polygon,
                      "area": stat[2] * stat[3],
                      "bbox": [stat[0], stat[1], stat[2], stat[3]],
                      "iscrowd": 0,
                  }
                  annotations.append(anno)
  
      categories = [
          {"supercategory": "defect", "id": cls_id, "name": str(cls_id)}
          for cls_id in classid_list
      ]
      if args.label_file:
          id_to_label = json.load(open(args.label_file))
          for category in categories:
              category["name"] = id_to_label[str(category["id"])]
  
      json_data = {
          "images": images,
          "annotations": annotations,
          "categories": categories,
      }
      with open(args.output_name, "w") as f:
          json.dump(json_data, f, indent=2)
  
file_path: PaddleSeg/contrib/QualityInspector/tools/convert_tools/convert_mask_to_coco.py
imports: import argparse
  import json
  import os
  import os.path
  import cv2
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: convert_mask_to_coco(args) without return types