code: def main():
      parser = argparse.ArgumentParser(
          formatter_class=argparse.ArgumentDefaultsHelpFormatter
      )
      parser.add_argument(
          "--dataset_type",
          help="the type of dataset, can be `voc`, `widerface`, `labelme` or `cityscape`",
      )
      parser.add_argument(
          "--json_input_dir", help="input annotated directory"
      )
      parser.add_argument("--image_input_dir", help="image directory")
      parser.add_argument(
          "--output_dir", help="output dataset directory", default="./"
      )
      parser.add_argument(
          "--train_proportion",
          help="the proportion of train dataset",
          type=float,
          default=1.0,
      )
      parser.add_argument(
          "--val_proportion",
          help="the proportion of validation dataset",
          type=float,
          default=0.0,
      )
      parser.add_argument(
          "--test_proportion",
          help="the proportion of test dataset",
          type=float,
          default=0.0,
      )
      parser.add_argument(
          "--voc_anno_dir",
          help="In Voc format dataset, path to annotation files directory.",
          type=str,
          default=None,
      )
      parser.add_argument(
          "--voc_anno_list",
          help="In Voc format dataset, path to annotation files ids list.",
          type=str,
          default=None,
      )
      parser.add_argument(
          "--voc_label_list",
          help="In Voc format dataset, path to label list. The content of each line is a category.",
          type=str,
          default=None,
      )
      parser.add_argument(
          "--voc_out_name",
          type=str,
          default="voc.json",
          help="In Voc format dataset, path to output json file",
      )
      parser.add_argument(
          "--widerface_root_dir",
          help="The root_path for wider face dataset, which contains `wider_face_split`, `WIDER_train` and `WIDER_val`.And the json file will save in this path",
          type=str,
          default=None,
      )
      args = parser.parse_args()
      try:
          assert args.dataset_type in [
              "voc",
              "labelme",
              "cityscape",
              "widerface",
          ]
      except AssertionError as e:
          print(
              "Now only support the voc, cityscape dataset and labelme dataset!!"
          )
          os._exit(0)
  
      if args.dataset_type == "voc":
          assert (
              args.voc_anno_dir
              and args.voc_anno_list
              and args.voc_label_list
          )
          label2id, ann_paths = voc_get_label_anno(
              args.voc_anno_dir, args.voc_anno_list, args.voc_label_list
          )
          voc_xmls_to_cocojson(
              annotation_paths=ann_paths,
              label2id=label2id,
              output_dir=args.output_dir,
              output_file=args.voc_out_name,
          )
      elif args.dataset_type == "widerface":
          assert args.widerface_root_dir
          widerface_to_cocojson(args.widerface_root_dir)
      else:
          try:
              assert os.path.exists(args.json_input_dir)
          except AssertionError as e:
              print("The json folder does not exist!")
              os._exit(0)
          try:
              assert os.path.exists(args.image_input_dir)
          except AssertionError as e:
              print("The image folder does not exist!")
              os._exit(0)
          try:
              assert (
                  abs(
                      args.train_proportion
                      + args.val_proportion
                      + args.test_proportion
                      - 1.0
                  )
                  < 1e-5
              )
          except AssertionError as e:
              print(
                  "The sum of pqoportion of training, validation and test datase must be 1!"
              )
              os._exit(0)
  
          # Allocate the dataset.
          total_num = len(
              glob.glob(osp.join(args.json_input_dir, "*.json"))
          )
          if args.train_proportion != 0:
              train_num = int(total_num * args.train_proportion)
              out_dir = args.output_dir + "/train"
              if not os.path.exists(out_dir):
                  os.makedirs(out_dir)
          else:
              train_num = 0
          if args.val_proportion == 0.0:
              val_num = 0
              test_num = total_num - train_num
              out_dir = args.output_dir + "/test"
              if args.test_proportion != 0.0 and not os.path.exists(
                  out_dir
              ):
                  os.makedirs(out_dir)
          else:
              val_num = int(total_num * args.val_proportion)
              test_num = total_num - train_num - val_num
              val_out_dir = args.output_dir + "/val"
              if not os.path.exists(val_out_dir):
                  os.makedirs(val_out_dir)
              test_out_dir = args.output_dir + "/test"
              if args.test_proportion != 0.0 and not os.path.exists(
                  test_out_dir
              ):
                  os.makedirs(test_out_dir)
          count = 1
          for img_name in os.listdir(args.image_input_dir):
              if count <= train_num:
                  if osp.exists(args.output_dir + "/train/"):
                      shutil.copyfile(
                          osp.join(args.image_input_dir, img_name),
                          osp.join(
                              args.output_dir + "/train/", img_name
                          ),
                      )
              else:
                  if count <= train_num + val_num:
                      if osp.exists(args.output_dir + "/val/"):
                          shutil.copyfile(
                              osp.join(args.image_input_dir, img_name),
                              osp.join(
                                  args.output_dir + "/val/", img_name
                              ),
                          )
                  else:
                      if osp.exists(args.output_dir + "/test/"):
                          shutil.copyfile(
                              osp.join(args.image_input_dir, img_name),
                              osp.join(
                                  args.output_dir + "/test/", img_name
                              ),
                          )
              count = count + 1
  
          # Deal with the json files.
          if not os.path.exists(args.output_dir + "/annotations"):
              os.makedirs(args.output_dir + "/annotations")
          if args.train_proportion != 0:
              train_data_coco = deal_json(
                  args.dataset_type,
                  args.output_dir + "/train",
                  args.json_input_dir,
              )
              train_json_path = osp.join(
                  args.output_dir + "/annotations",
                  "instance_train.json",
              )
              json.dump(
                  train_data_coco,
                  open(train_json_path, "w"),
                  indent=4,
                  cls=MyEncoder,
              )
          if args.val_proportion != 0:
              val_data_coco = deal_json(
                  args.dataset_type,
                  args.output_dir + "/val",
                  args.json_input_dir,
              )
              val_json_path = osp.join(
                  args.output_dir + "/annotations", "instance_val.json"
              )
              json.dump(
                  val_data_coco,
                  open(val_json_path, "w"),
                  indent=4,
                  cls=MyEncoder,
              )
          if args.test_proportion != 0:
              test_data_coco = deal_json(
                  args.dataset_type,
                  args.output_dir + "/test",
                  args.json_input_dir,
              )
              test_json_path = osp.join(
                  args.output_dir + "/annotations", "instance_test.json"
              )
              json.dump(
                  test_data_coco,
                  open(test_json_path, "w"),
                  indent=4,
                  cls=MyEncoder,
              )
  
imports: import argparse
  import glob
  import json
  import os
  import os.path
  import shutil
  import xml.etree.ElementTree
  import numpy
  import PIL.ImageDraw
  from tqdm import tqdm
  import cv2
label_desc: Use image node_id 16 for calling a global function with example usage: main() without return types