Skip to content
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

Annotation file support #32

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b717c2d
(WIP) Annotation structures being constructed.
devrimcavusoglu Feb 14, 2023
5211e84
Type hint variables are corrected.
devrimcavusoglu Feb 26, 2023
92f4710
sketched persistence methods for different format in Annotations class
gauravparajuli Sep 8, 2024
2600f47
updated annotation Dataclass to include more comprehensive detail of …
gauravparajuli Sep 10, 2024
c39d96f
implemented load_from_coco and persist_to_yolo method in Annotations …
gauravparajuli Sep 10, 2024
1e9b9f7
annotations are now grouped by imagename in the internal representati…
gauravparajuli Sep 11, 2024
24a8fa7
implemented persist_as_voc and load_from_voc methods in Annotations c…
gauravparajuli Sep 11, 2024
0de8314
implemented persist_as_coco in Annotations class
gauravparajuli Sep 11, 2024
1690fbd
added function to extract image dimesions from the image
gauravparajuli Sep 11, 2024
0ea0edd
load_from_yolo method added in Annotations class
gauravparajuli Sep 11, 2024
fea3b5b
defined docstring for all the class methods
gauravparajuli Sep 17, 2024
b8713c2
documented the conversion process of annotations file across differen…
gauravparajuli Sep 17, 2024
500dd7c
added pytest-depends as development dependency
gauravparajuli Sep 18, 2024
f04aa23
using a small coco dataset, implemented unit tests for annotations fi…
gauravparajuli Sep 18, 2024
13e92ab
renamed persist_as_* methods to save_as_* methods (which are used by …
gauravparajuli Oct 4, 2024
1bca511
updated README.md, renamed persist_as_* to save_as_*
gauravparajuli Oct 4, 2024
904558c
migrated the test dataset (used for unit testing) in the repository t…
gauravparajuli Oct 4, 2024
82b1375
linting with flake8 and black
gauravparajuli Oct 6, 2024
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ dmypy.json
# IDE
.idea/
.vscode/

# fixtures for annotation file conversion testing
testing_data_coco
testing_data_images
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,96 @@ pbf.compute_area(coco_bbox, bbox_type="coco") # 12
pbf.compute_area(voc_bbox, bbox_type="voc") # 4
```

## Annotation file conversion
`pybboxes` now supports the conversion of annotation file(s) across different annotation formats. (yolo, voc and coco are currently supported)

This is a 3 step process.

### 1. Instantiate the Annotations class
```python
from pybboxes.annotations import Annotations

anns = Annotations(annotation_type='yolo')
```

**Important** you have to explicitly declare `annotation_type` beforehand. post declaration, *you will be only able to load annotation in declared format* but you will be able to export to other annotation formats.

### 2. Load the annotations file
After you have instantiated the `Annotations` class declaring `annotation_type`, you can now load the annotations using appropriate method of the `Annotations` class.

#### 2.1 Load from yolo
```python
from pybboxes.annotations import Annotations

anns = Annotations(annotation_type='yolo')

anns.load_from_yolo(labels_dir='./labels', images_dir='./images', classes_file='./classes.txt')
```

As yolo normalizes the bounding box metadata, path to corresponding images directory must be provided (via `images_dir`) so that physical dimension of image data can be inferred.

Also, path to `classes_file` (usually classes.txt) should be provided that lists all the class labels that is used for the annotation. Without this, `pybboxes` will fail to assign appropriate class labels when converting across different annotations format.

#### 2.2 Load from voc
```python
from pybboxes.annotations import Annotations

anns = Annotations(annotation_type='voc')

anns.load_from_voc(labels_dir='./labels')
```

#### 2.3 Load from coco
```python
from pybboxes.annotations import Annotations

anns = Annotations(annotation_type='coco')

anns.load_from_coco(json_path='./validation.json')
```

### 3. Saving annotations to different format
#### 3.1 Saving annotations to yolo format
As every image data has its own corresponding annotation file in yolo format, you have to provide path to `export_dir` where all the annotation files will be written.

```python
from pybboxes.annotations import Annotations

anns = Annotations(annotation_type='coco') # just for the demonstration purpose

anns.load_from_coco(json_path='./validation.json') # we could have loaded the annotation data from other format as well

anns.save_as_yolo(export_dir='./labels')
```
This will create all the required annotation files (in yolo format) in given directory. Additionally, it will also create `classes.txt` in the given folder which will list all the class labels used for the annotation.

#### 3.2 Saving annotations to voc format
Just like yolo format, in voc format, every image data has also its own corresponding annotation file. So, you have to provide path to `export_dir` where all the annotation files will be written.

```python
from pybboxes.annotations import Annotations

anns = Annotations(annotation_type='coco') # just for the demonstration purpose

anns.load_from_coco(json_path='./validation.json') # we could have loaded the annotation data from other format as well

anns.save_as_voc(export_dir='./labels')
```


#### 3.3 Saving annotations to coco format
To export annotations in coco format, you just have to provide name (or path) of the output file (in json format) via `export_file`.

```python
from pybboxes.annotations import Annotations

anns = Annotations(annotation_type='voc') # just for the demonstration purpose

anns.load_from_voc(labels_dir='./labels') # we could have loaded the annotation data from other format as well

anns.save_as_coco(export_file='./validation.json')
```

## Contributing

### Installation
Expand Down
5 changes: 5 additions & 0 deletions pybboxes/annotations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
https://medium.com/red-buffer/converting-a-custom-dataset-from-coco-format-to-yolo-format-6d98a4fd43fc
https://blog.roboflow.com/train-yolov7-instance-segmentation-on-custom-data/
"""
from pybboxes.annotations.base import Annotations
Loading
Loading