diff --git a/README.md b/README.md index 00ae495..0d3e857 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ labelme2coco path/to/labelme/dir labelme2coco path/to/labelme/dir --train_split_rate 0.85 ``` +```python +labelme2coco path/to/labelme/dir --category_id_start 1 +``` + ### Advanced Usage ```python @@ -54,8 +58,11 @@ export_dir = "tests/data/" # set train split rate train_split_rate = 0.85 +# set category ID start value +category_id_start = 1 + # convert labelme annotations to coco -labelme2coco.convert(labelme_folder, export_dir, train_split_rate) +labelme2coco.convert(labelme_folder, export_dir, train_split_rate, category_id_start=category_id_start) ``` ```python @@ -71,14 +78,17 @@ labelme_val_folder = "tests/data/labelme_annot" # set path for coco json to be saved export_dir = "tests/data/" +# set category ID start value +category_id_start = 1 + # create train coco object -train_coco = get_coco_from_labelme_folder(labelme_train_folder) +train_coco = get_coco_from_labelme_folder(labelme_train_folder, category_id_start=category_id_start) # export train coco json save_json(train_coco.json, export_dir+"train.json") # create val coco object -val_coco = get_coco_from_labelme_folder(labelme_val_folder, coco_category_list=train_coco.json_categories) +val_coco = get_coco_from_labelme_folder(labelme_val_folder, coco_category_list=train_coco.json_categories, category_id_start=category_id_start) # export val coco json save_json(val_coco.json, export_dir+"val.json") diff --git a/labelme2coco/__init__.py b/labelme2coco/__init__.py index 9bb9787..45f0ae5 100644 --- a/labelme2coco/__init__.py +++ b/labelme2coco/__init__.py @@ -1,6 +1,6 @@ from __future__ import absolute_import -__version__ = "0.2.5" +__version__ = "0.2.6" import logging import os @@ -25,14 +25,16 @@ def convert( export_dir: str = "runs/labelme2coco/", train_split_rate: float = 1, skip_labels: List[str] = [], + category_id_start: int = 0, ): """ Args: labelme_folder: folder that contains labelme annotations and image files export_dir: path for coco jsons to be exported train_split_rate: ration fo train split + category_id_start: starting value for category IDs (default: 0) """ - coco = get_coco_from_labelme_folder(labelme_folder, skip_labels=skip_labels) + coco = get_coco_from_labelme_folder(labelme_folder, skip_labels=skip_labels, category_id_start=category_id_start) if train_split_rate < 1: result = coco.split_coco_as_train_val(train_split_rate) # export train split diff --git a/labelme2coco/labelme2coco.py b/labelme2coco/labelme2coco.py index 7ac47e4..8543771 100644 --- a/labelme2coco/labelme2coco.py +++ b/labelme2coco/labelme2coco.py @@ -15,7 +15,7 @@ def __init__(self): def get_coco_from_labelme_folder( - labelme_folder: str, coco_category_list: List = None, skip_labels: List[str] = [] + labelme_folder: str, coco_category_list: List = None, skip_labels: List[str] = [], category_id_start: int = 0 ) -> Coco: """ Args: @@ -37,19 +37,21 @@ def get_coco_from_labelme_folder( print(f"Will skip the following annotated labels: {skip_labels}") # parse labelme annotations - category_ind = 0 + # depending on cli arguments, will start counting at 1 + category_ind = category_id_start for json_path in tqdm( labelme_json_list, "Converting labelme annotations to COCO format" ): + # Taken from https://github.com/fcakyon/labelme2coco/pull/17 data = load_json(json_path) # get image size - image_path = str(Path(labelme_folder) / data["imagePath"]) + image_path = str(Path(json_path).parent / data["imagePath"]) # use the image sizes provided by labelme (they already account for # things such as EXIF orientation) width = data["imageWidth"] height = data["imageHeight"] # init coco image - coco_image = CocoImage(file_name=data["imagePath"], height=height, width=width) + coco_image = CocoImage(file_name=image_path, height=height, width=width) # iterate over annotations for shape in data["shapes"]: # set category name and id @@ -120,4 +122,4 @@ def get_coco_from_labelme_folder( if __name__ == "__main__": labelme_folder = "tests/data/labelme_annot" - coco = get_coco_from_labelme_folder(labelme_folder) + coco = get_coco_from_labelme_folder(labelme_folder) \ No newline at end of file