Skip to main content

A dataset format conversion tool for object detection and image segmentation, which supports mutual conversion between labelme, labelImg tools and YOLO, VOC, and COCO dataset formats.

Project description

Label Convert

PyPI SemVer2.0

A dataset format conversion tool for object detection and image segmentation, which supports mutual conversion between labelme, labelImg tools and YOLO, VOC, and COCO dataset formats.

Installation

pip install label_convert

Usage

labelImg label data → YOLOV5 format

Click to expand
  • Convert the yolo data format marked by the labelImg library to YOLOV5 format data with one click.

  • The labelImg label data directory structure is as follows (see dataset/labelImg_dataset for details):

    labelImg_dataset
    ├── classes.txt
    ├── images(13).jpg
    ├── images(13).txt
    ├── images(3).jpg
    ├── images(3).txt
    ├── images4.jpg
    ├── images4.txt
    ├── images5.jpg
    ├── images5.txt
    ├── images6.jpg
    ├── images7.jpg
    └── images7.txt
    
  • Convert

    labelimg_to_yolov5 --src_dir dataset/labelImg_dataset \
                       --out_dir dataset/labelImg_dataset_output \
                       --val_ratio 0.2 \
                       --have_test true \
                       --test_ratio 0.2
    
    • --src_dir: the directory where labelImg is stored after labeling.
    • --out_dir: the location where the data is stored after conversion.
    • --val_ratio: the ratio of the generated validation set to the whole data, default is 0.2.
    • --have_test: whether to generate the test part of the data, the default is True.
    • --test_ratio: percentage of the whole data of the test data, default is 0.2.
  • Converted directory structure (see dataset/labelImg_dataset_output for details):

    labelImg_dataset_output/
    ├── classes.txt
    ├── images
    │   ├── images(13).jpg
    │   ├── images(3).jpg
    │   ├── images4.jpg
    │   ├── images5.jpg
    │   └── images7.jpg
    ├── labels
    │   ├── images(13).txt
    │   ├── images(3).txt
    │   ├── images4.txt
    │   ├── images5.txt
    │   └── images7.txt
    ├── non_labels        # This is the catalog without the labeled images.
    │   └── images6.jpg
    ├── test.txt
    ├── train.txt
    └── val.txt
    
  • You can further directly convert the dataset/labelImg_dataset_output directory to COCO

    yolov5_to_coco --data_dir dataset/labellImg_dataset_output
    

COCO format data → labelImg yolo format

Click to expand
  • One-click conversion of COCO format data to labelImg labeled yolo format data.
  • COCO format directory structure(see dataset/YOLOV5_COCO_format for details):
    YOLOV5_COCO_format
    ├── annotations
    │   ├── instances_train2017.json
    │   └── instances_val2017.json
    ├── train2017
    │   ├── 000000000001.jpg
    │   └── 000000000002.jpg
    └── val2017
        └── 000000000001.jpg
    
  • Convert
    coco_to_labelimg --data_dir dataset/YOLOV5_COCO_format
    
    • --data_dir: the directory where the COCO format dataset is located. Default is dataset/YOLOV5_COCO_format.
  • Converted directory structure (see dataset/COCO_labelImg_format for details):
    COCO_labelImg_format
      ├── train
      │   ├── 000000000001.jpg
      │   ├── 000000000001.txt
      │   |-- 000000000002.jpg
      │   └── classes.txt
      └── val
          ├── 000000000001.jpg
          ├── 000000000001.txt
          └── classes.txt
    
  • For the converted directory, you can directly use the labelImg library to open it directly and change the label. The specific commands are as follows:
    $ cd dataset/COCO_labelImg_format
    $ labelImg train train/classes.txt
    
    # or
    $ labelImg val val/classes.txt
    

YOLOV5 format data → COCO

Click to expand
  • Some background images can be added to the training by directly placing them into the backgroud_images directory.

  • The conversion program will automatically scan this directory and add it to the training set, allowing seamless integration with subsequent YOLOX training.

  • YOLOV5 training format directory structure (see dataset/YOLOV5 for details).

    YOLOV5
    ├── classes.txt
    ├── background_images  # usually images that are easily confused with the object to be detected
    │   └── bg1.jpeg
    ├── images
    │   ├── images(13).jpg
    │   └── images(3).jpg
    ├── labels
    │   ├── images(13).txt
    │   └── images(3).txt
    ├── train.txt
    └── val.txt
    
  • The image paths in train.txt and val.txt can be either:

    • Path relative to root directory:
      dataset/YOLOV5/images/images(3).jpg
      
    • Path relative to dataset/YOLOV5:
      images/images(3).jpg
      
  • Convert

    yolov5_to_coco --data_dir dataset/YOLOV5 --mode_list train,val
    
    • --data_dir: the directory where the collated dataset is located
    • --mode_list: specify the generated json, provided that there is a corresponding txt file, which can be specified separately. (e.g. train,val,test)
  • The structure of the converted directory (see dataset/YOLOV5_COCO_format for details)

    YOLOV5_COCO_format
    ├── annotations
    │   ├── instances_train2017.json
    │   └── instances_val2017.json
    ├── train2017
    │   ├── 000000000001.jpg
    │   └── 000000000002.jpg  # This is the background image.
    └── val2017
        └── 000000000001.jpg
    

YOLOV5 YAML description file → COCO

Click to expand
  • The YOLOV5 yaml data file needs to contain.

    YOLOV5_yaml
    ├── images
    │   ├── train
    │   │   ├── images(13).jpg
    │   │   └── images(3).jpg
    │   └── val
    │       ├── images(13).jpg
    │       └── images(3).jpg
    ├── labels
    │   ├── train
    │   │   ├── images(13).txt
    │   │   └── images(3).txt
    │   └── val
    │       ├── images(13).txt
    │       └── images(3).txt
    └── sample.yaml
    
  • Convert

    yolov5_yaml_to_coco --yaml_path dataset/YOLOV5_yaml/sample.yaml
    

darknet format data → COCO

  • Darknet training data directory structure (see dataset/darknet for details).
    darknet
    ├── class.names
    ├── gen_config.data
    ├── gen_train.txt
    ├── gen_valid.txt
    └── images
        ├── train
        └── valid
    
  • Convert
    darknet_to_coco --data_path dataset/darknet/gen_config.data
    

Visualize images in COCO format

Click to expand
coco_visual --vis_num 1 \
            --json_path dataset/YOLOV5_COCO_format/annotations/instances_train2017.json \
            --img_dir dataset/YOLOV5_COCO_format/train2017
  • --vis_num: specify the index of the image to be viewed
  • --json_path: path to the json file of the image to view
  • --img_dir: view the directory where the image is located

Object Instance demo of COCO

Click to expand
{
    "info": {
      "year": 2022,
      "version": "1.0",
      "description": "For object detection",
      "date_created": "2022"
    },
    "licenses":  [{
        "id": 1,
        "name": "Apache License v2.0",
        "url": "https://github.com/RapidAI/YOLO2COCO/LICENSE"
    }],
    "images": [{
        "date_captured": "2022",
        "file_name": "000000000001.jpg",
        "id": 1,
        "height": 224,
        "width": 224
    }, {
        "date_captured": "2022",
        "file_name": "000000000002.jpg",
        "id": 2,
        "height": 424,
        "width": 550
    }],
    "annotations": [{
        "segmentation": [[18.00, 2.99, 105.00, 2.99, 105.00, 89.00, 18.00, 89.00]],
        "area": 7482.011,
        "iscrowd": 0,
        "image_id": 1,  // Corresponding to the ID in images
        "bbox": [18.00, 2.99, 87.00, 86.00],  // [x, y, w, h], (x,y) is the left top point of the box. w,h is the width and height of the box.
        "category_id": 1,  // Corresponding to the ID in categories.
        "id": 1  // Number that uniquely distinguishes different dimension instances
    }, {
        "segmentation": [
            [126.99, 3.99, 210.99, 3.99, 210.99, 88.99, 126.99, 88.99]
        ],
        "area": 7139.994,
        "iscrowd": 0,
        "image_id": 1,
        "bbox": [126.99, 3.99, 84.0, 84.99],
        "category_id": 1,
        "id": 2
    }],
    "categories": [{
        "supercategory": "stamp",
        "id": 1,
        "name": "stamp"
    }]
}

Related information

See details for LabelConvert

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

label_convert-0.0.2-py3-none-any.whl (21.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page