Skip to content

Add category_id_start parameter to support starting category IDs from 1 #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions labelme2coco/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import

__version__ = "0.2.5"
__version__ = "0.2.6"

import logging
import os
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions labelme2coco/labelme2coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)
Loading