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

Fixes in ICDAR dataset format #174

Merged
merged 9 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion datumaro/plugins/icdar_format/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def write(self, path):
os.makedirs(path, exist_ok=True)
for item in self.annotations:
file = osp.join(path, 'gt_' + item + '.txt')
os.makedirs(osp.dirname(file), exist_ok=True)
zhiltsov-max marked this conversation as resolved.
Show resolved Hide resolved
with open(file, 'w') as f:
f.write(self.annotations[item])

Expand Down Expand Up @@ -119,6 +120,7 @@ def write(self, path):
os.makedirs(path, exist_ok=True)
for item in self.annotations:
file = osp.join(path, item + '_GT' + '.txt')
os.makedirs(osp.dirname(file), exist_ok=True)
with open(file, 'w') as f:
f.write(self.annotations[item])

Expand Down Expand Up @@ -167,7 +169,8 @@ def apply(self):
for task, task_conv in task_converters.items():
if item.has_image and self._save_images:
self._save_image(item, osp.join(
self._save_dir, subset_name, IcdarPath.IMAGES_DIR,
self._save_dir, IcdarPath.TASK_DIR[task],
zhiltsov-max marked this conversation as resolved.
Show resolved Hide resolved
subset_name, IcdarPath.IMAGES_DIR,
item.id + IcdarPath.IMAGE_EXT))
task_conv.save_annotations(item, osp.join(self._save_dir,
IcdarPath.TASK_DIR[task], subset_name))
Expand Down
10 changes: 6 additions & 4 deletions datumaro/plugins/icdar_format/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ def _load_recognition_items(self):
def _load_localization_items(self):
items = {}

for path in glob(osp.join(self._path, '*.txt')):
item_id = osp.splitext(osp.basename(path))[0]
for path in glob(osp.join(self._path, '**', '*.txt'), recursive=True):
item_id = osp.splitext(osp.relpath(path, self._path))[0]
item_id = item_id.replace('\\', '/')
if item_id.startswith('gt_'):
item_id = item_id[3:]
image_path = osp.join(self._path, IcdarPath.IMAGES_DIR,
Expand Down Expand Up @@ -115,8 +116,9 @@ def _load_localization_items(self):
def _load_segmentation_items(self):
items = {}

for path in glob(osp.join(self._path, '*.txt')):
item_id = osp.splitext(osp.basename(path))[0]
for path in glob(osp.join(self._path, '**', '*.txt'), recursive=True):
item_id = osp.splitext(osp.relpath(path, self._path))[0]
item_id = item_id.replace('\\', '/')
if item_id.endswith('_GT'):
item_id = item_id[:-3]
image_path = osp.join(self._path, IcdarPath.IMAGES_DIR,
Expand Down
37 changes: 21 additions & 16 deletions tests/test_icdar_format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path as osp
from functools import partial
from unittest import TestCase

import numpy as np
Expand Down Expand Up @@ -102,35 +103,36 @@ def _test_save_and_load(self, source_dataset, converter, test_dir,

def test_can_save_and_load_captions(self):
expected_dataset = Dataset.from_iterable([
DatasetItem(id=1, subset='train',
annotations=[
DatasetItem(id='a/b/1', subset='train',
image=np.ones((10, 15, 3)), annotations=[
Caption('caption_0'),
]),
DatasetItem(id=2, subset='train',
annotations=[
image=np.ones((10, 15, 3)), annotations=[
Caption('caption_1'),
]),
])

with TestDir() as test_dir:
self._test_save_and_load(expected_dataset,
IcdarWordRecognitionConverter.convert, test_dir)
partial(IcdarWordRecognitionConverter.convert, save_images=True),
test_dir)

def test_can_save_and_load_bboxes(self):
expected_dataset = Dataset.from_iterable([
DatasetItem(id=1, subset='train',
annotations=[
DatasetItem(id='a/b/1', subset='train',
image=np.ones((10, 15, 3)), annotations=[
Bbox(1, 3, 6, 10),
Bbox(0, 1, 3, 5, attributes={'text': 'word_0'}),
]),
DatasetItem(id=2, subset='train',
annotations=[
image=np.ones((10, 15, 3)), annotations=[
Polygon([0, 0, 3, 0, 4, 7, 1, 8],
attributes={'text': 'word_1'}),
Polygon([1, 2, 5, 3, 6, 8, 0, 7]),
]),
DatasetItem(id=3, subset='train',
annotations=[
image=np.ones((10, 15, 3)), annotations=[
Polygon([2, 2, 8, 3, 7, 10, 2, 9],
attributes={'text': 'word_2'}),
Bbox(0, 2, 5, 9, attributes={'text': 'word_3'}),
Expand All @@ -139,12 +141,13 @@ def test_can_save_and_load_bboxes(self):

with TestDir() as test_dir:
self._test_save_and_load(expected_dataset,
IcdarTextLocalizationConverter.convert, test_dir)
partial(IcdarTextLocalizationConverter.convert, save_images=True),
test_dir)

def test_can_save_and_load_masks(self):
expected_dataset = Dataset.from_iterable([
DatasetItem(id=1, subset='train',
annotations=[
DatasetItem(id='a/b/1', subset='train',
image=np.ones((10, 15, 3)), annotations=[
Mask(image=np.array([[0, 0, 0, 1, 1]]), group=1,
attributes={ 'index': 1, 'color': '82 174 214', 'text': 'j',
'center': '0 3' }),
Expand All @@ -153,7 +156,7 @@ def test_can_save_and_load_masks(self):
'center': '0 1' }),
]),
DatasetItem(id=2, subset='train',
annotations=[
image=np.ones((10, 15, 3)), annotations=[
Mask(image=np.array([[0, 0, 0, 0, 0, 1]]), group=0,
attributes={ 'index': 3, 'color': '183 6 28', 'text': ' ',
'center': '0 5' }),
Expand All @@ -171,7 +174,8 @@ def test_can_save_and_load_masks(self):

with TestDir() as test_dir:
self._test_save_and_load(expected_dataset,
IcdarTextSegmentationConverter.convert, test_dir)
partial(IcdarTextSegmentationConverter.convert, save_images=True),
test_dir)

def test_can_save_and_load_with_no_subsets(self):
expected_dataset = Dataset.from_iterable([
Expand All @@ -187,12 +191,13 @@ def test_can_save_and_load_with_no_subsets(self):

def test_can_save_dataset_with_cyrillic_and_spaces_in_filename(self):
expected_dataset = Dataset.from_iterable([
DatasetItem(id='кириллица с пробелом', image=np.ones((8, 8, 3)),
annotations=[
DatasetItem(id='кириллица с пробелом',
image=np.ones((8, 8, 3)), annotations=[
Bbox(0, 1, 3, 5),
]),
])

with TestDir() as test_dir:
self._test_save_and_load(expected_dataset,
IcdarTextLocalizationConverter.convert, test_dir)
partial(IcdarTextLocalizationConverter.convert, save_images=True),
test_dir)