YOLO segmentation data set production: use the Labelme tool to create a segmentation data set (.json) and convert it to the format of the YOLO data set (.txt)

1. Labelme production data set

  • Open labelme, label the data, and generate the file directory as follows:
    • data
      • a.jpg
      • a.json
      • b.jpg
      • b.json

2. Convert the data in labelme format to coco format (the converted coco format is still a json file)

  • Code download: instance_segmentation

  • Revise:

    • 78 lines found

      class_name_to_id = {}
      for i, line in enumerate(open(args.labels).readlines()):
          # class_id = i-1  # starts with -1
          class_id = i ## 没有背景,所以索引地址从0开始
          class_name = line.strip()
          if class_id == -1:
              assert class_name == "__ignore__"
              continue
      
    • Create labels.txt (in the same file directory as the data folder)

      • data
        • a.jpg
        • a.json
        • b.jpg
        • b.json
      • labels.txt

      labels.txt content:

      ```dotnetcli
      __ignore__
      class 0
      class 1
      ....
      
      ```
      
  • Start converting json format

    python labelme2coco.py ..path/data ..path/data_dataset_coco --labels labels.txt
    

3. Convert coco format to YOLO format

  • File download: COCO2YOLO

  • Revise

    • Modification 1: Find 306 lines
      ## with open((fn / f).with_suffix('.txt'), 'a') as file:
      with open((fn / f[11:]).with_suffix('.txt'), 'a') as file:
      
    • Modification 2: Find the main function: modify the json path to your file path
      if source == 'COCO':
          convert_coco_json('..path/data_dataset_coco',  # directory with *.json
                            use_segments=True,
                            cls91to80=True)
      
  • Start converting json format

    python general_json2yolo.py
    
  • The generated yolo annotation file is in: ./new_dir/labels/annotations/

Guess you like

Origin blog.csdn.net/qq_40541102/article/details/129882601