code: def main(args):
      # prepare
      output_dir = args.output_dir
      output_img_dir = osp.join(args.output_dir, "images")
      output_annot_dir = osp.join(args.output_dir, "annotations")
      if not osp.exists(output_dir):
          os.makedirs(output_dir)
          print("Creating directory:", output_dir)
      if not osp.exists(output_img_dir):
          os.makedirs(output_img_dir)
          print("Creating directory:", output_img_dir)
      if not osp.exists(output_annot_dir):
          os.makedirs(output_annot_dir)
          print("Creating directory:", output_annot_dir)
  
      # collect and save class names
      class_names = ["_background_"]
      for label_file in glob.glob(osp.join(args.input_dir, "*.json")):
          with open(label_file) as f:
              data = json.load(f)
              for shape in data["shapes"]:
                  cls_name = shape["label"]
                  if cls_name not in class_names:
                      class_names.append(cls_name)
  
      class_name_to_id = {}
      for i, class_name in enumerate(class_names):
          class_id = i  # starts with 0
          class_name_to_id[class_name] = class_id
      print("class_names:", class_names)
  
      out_class_names_file = osp.join(output_dir, "class_names.txt")
      with open(out_class_names_file, "w") as f:
          f.writelines("\n".join(class_names))
      print("Saved class_names:", out_class_names_file)
  
      # create annotated images and copy origin images
      color_map = get_color_map_list(256)
      for label_file in glob.glob(osp.join(args.input_dir, "*.json")):
          print("Generating dataset from:", label_file)
          filename = osp.splitext(osp.basename(label_file))[0]
          annotated_img_path = osp.join(
              output_annot_dir, filename + ".png"
          )
          with open(label_file) as f:
              data = json.load(f)
              img_path = osp.join(
                  osp.dirname(label_file), data["imagePath"]
              )
              shutil.copy(img_path, output_img_dir)
  
              img = np.asarray(cv2.imread(img_path))
              lbl = shape2label(
                  img_size=img.shape,
                  shapes=data["shapes"],
                  class_name_mapping=class_name_to_id,
              )
  
              # Assume label ranges [0, 255] for uint8,
              if lbl.min() >= 0 and lbl.max() <= 255:
                  lbl_pil = PIL.Image.fromarray(
                      lbl.astype(np.uint8), mode="P"
                  )
                  lbl_pil.putpalette(color_map)
                  lbl_pil.save(annotated_img_path)
              else:
                  raise ValueError(
                      "[%s] Cannot save the pixel-wise class label as PNG. "
                      "Please consider using the .npy format."
                      % annotated_img_path
                  )
  
file_path: PaddleSeg/tools/data/labelme2seg.py
imports: from __future__ import print_function
  import argparse
  import glob
  import math
  import json
  import os
  import os.path
  import shutil
  import numpy
  import PIL.Image
  import PIL.ImageDraw
  import cv2
label_desc: Use image node_id 5 for calling a global function with example usage: main(args) without return types