Skip to content

Commit

Permalink
Merge pull request #4 from fcakyon/refactor
Browse files Browse the repository at this point in the history
refactor codebase to utilize sahi
  • Loading branch information
fcakyon committed Jan 8, 2022
2 parents 8ebf452 + a144251 commit 1e7c7ea
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 373 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.6, 3.7, 3.8]
python-version: [3.7, 3.8, 3.9, "3.10"]
fail-fast: false

steps:
Expand Down Expand Up @@ -60,3 +60,7 @@ jobs:
- name: Test with unittest
run: |
python -m unittest
- name: Test CLI
run: |
pip install .
labelme2coco tests/data/labelme_annot --train_split_rate 0.85
65 changes: 65 additions & 0 deletions .github/workflows/package_testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Package Testing

on:
schedule:
- cron: '0 0 * * *' # Runs at 00:00 UTC every day

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.7, 3.8, 3.9, "3.10"]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Restore Ubuntu cache
uses: actions/cache@v1
if: matrix.operating-system == 'ubuntu-latest'
with:
path: ~/.cache/pip
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-

- name: Restore MacOS cache
uses: actions/cache@v1
if: matrix.operating-system == 'macos-latest'
with:
path: ~/Library/Caches/pip
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-

- name: Restore Windows cache
uses: actions/cache@v1
if: matrix.operating-system == 'windows-latest'
with:
path: ~\AppData\Local\pip\Cache
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-

- name: Update pip
run: python -m pip install --upgrade pip

- name: Install latest labelme2coco package
run: >
pip install --upgrade --force-reinstall labelme2coco
- name: Test with unittest
run: |
pip install pytest
python -m unittest
- name: Test CLI
run: |
pip install .
labelme2coco tests/data/labelme_annot --train_split_rate 0.85
53 changes: 46 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[![Downloads](https://pepy.tech/badge/labelme2coco/month)](https://pepy.tech/project/labelme2coco)
[![Downloads](https://pepy.tech/badge/labelme2coco)](https://pepy.tech/project/labelme2coco)
[![PyPI version](https://badge.fury.io/py/labelme2coco.svg)](https://badge.fury.io/py/labelme2coco)
![CI](https://github.com/fcakyon/labelme2coco/workflows/CI/badge.svg)

# labelme2coco Python Package for Linux/MacOS/Windows
Make your own dataset for object detection/instance segmentation using [labelme](https://github.com/wkentaro/labelme) and transform the format to coco json format
Make your own dataset for object detection/instance segmentation using [labelme](https://github.com/wkentaro/labelme) and transform the format to coco json format.

## Convert LabelMe annotations to COCO format in one step
[labelme](https://github.com/wkentaro/labelme) is a widely used is a graphical image annotation tool that supports classification, segmentation, instance segmentation and object detection formats.
Expand All @@ -14,21 +14,60 @@ You can use this package to convert labelme annotations to COCO format.
## Getting started
### Installation
```
pip install labelme2coco
pip install -U labelme2coco
```

### Usage
### Basic Usage

```python
labelme2coco path/to/labelme/dir
```

```python
labelme2coco path/to/labelme/dir --train_split_rate 0.85
```

### Advanced Usage

```python
# import package
import labelme2coco

# set directory that contains labelme annotations and image files
labelme_folder = "tests/data/labelme_annot"

# set path for coco json to be saved
save_json_path = "tests/data/test_coco.json"
# set export dir
export_dir = "tests/data/"

# set train split rate
train_split_rate = 0.85

# convert labelme annotations to coco
labelme2coco.convert(labelme_folder, save_json_path)
labelme2coco.convert(labelme_folder, export_dir, train_split_rate)
```

```python
# import functions
from labelme2coco import get_coco_from_labelme_folder, save_json

# set labelme training data directory
labelme_train_folder = "tests/data/labelme_annot"

# set labelme validation data directory
labelme_val_folder = "tests/data/labelme_annot"

# set path for coco json to be saved
export_dir = "tests/data/"

# create train coco object
train_coco = get_coco_from_labelme_folder(labelme_train_folder)

# 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)

# export val coco json
save_json(val_coco.json, export_dir+"val.json")
```
42 changes: 37 additions & 5 deletions labelme2coco/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
from __future__ import absolute_import

__version__ = "0.1.2"
__version__ = "0.2.0"

from labelme2coco.labelme2coco import labelme2coco
import logging
import os
from pathlib import Path

from sahi.utils.file import save_json

def convert(labelme_folder: str, save_json_path: str):
from labelme2coco.labelme2coco import get_coco_from_labelme_folder

logger = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=os.environ.get("LOGLEVEL", "INFO").upper(),
)


def convert(
labelme_folder: str,
export_dir: str = "runs/labelme2coco/",
train_split_rate: float = 1,
):
"""
Args:
labelme_folder: folder that contains labelme annotations and image files
save_json_path: oath for coco json to be saved
export_dir: path for coco jsons to be exported
train_split_rate: ration fo train split
"""
labelme2coco(labelme_folder, save_json_path)
coco = get_coco_from_labelme_folder(labelme_folder)
if train_split_rate < 1:
result = coco.split_coco_as_train_val(train_split_rate)
# export train split
save_path = str(Path(export_dir) / "train.json")
save_json(result["train_coco"].json, save_path)
logger.info(f"Training split in COCO format is exported to {save_path}")
# export val split
save_path = str(Path(export_dir) / "val.json")
save_json(result["val_coco"].json, save_path)
logger.info(f"Validation split in COCO format is exported to {save_path}")
else:
save_path = str(Path(export_dir) / "dataset.json")
save_json(coco.json, save_path)
logger.info(f"Converted annotations in COCO format is exported to {save_path}")
12 changes: 12 additions & 0 deletions labelme2coco/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fire

from labelme2coco import convert


def app() -> None:
"""Cli app."""
fire.Fire(convert)


if __name__ == "__main__":
app()
91 changes: 0 additions & 91 deletions labelme2coco/image_utils.py

This file was deleted.

Loading

0 comments on commit 1e7c7ea

Please sign in to comment.