-
Notifications
You must be signed in to change notification settings - Fork 135
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 and Market-1501 dataset formats #114
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,9 @@ | |
import os.path as osp | ||
|
||
from datumaro.components.converter import Converter | ||
from datumaro.components.extractor import AnnotationType | ||
from datumaro.components.extractor import AnnotationType, CompiledMask | ||
from datumaro.util.image import save_image | ||
from datumaro.util.mask_tools import paint_mask, merge_masks | ||
from datumaro.util.mask_tools import paint_mask | ||
|
||
from .format import IcdarPath, IcdarTask | ||
|
||
|
@@ -17,7 +17,7 @@ class _WordRecognitionConverter: | |
def __init__(self): | ||
self.annotations = '' | ||
|
||
def save_annotations(self, item): | ||
def save_annotations(self, item, path): | ||
self.annotations += '%s, ' % (item.id + IcdarPath.IMAGE_EXT) | ||
for ann in item.annotations: | ||
if ann.type != AnnotationType.caption: | ||
|
@@ -38,7 +38,7 @@ class _TextLocalizationConverter: | |
def __init__(self): | ||
self.annotations = {} | ||
|
||
def save_annotations(self, item): | ||
def save_annotations(self, item, path): | ||
annotation = '' | ||
for ann in item.annotations: | ||
if ann.type == AnnotationType.bbox: | ||
|
@@ -65,59 +65,62 @@ def is_empty(self): | |
class _TextSegmentationConverter: | ||
def __init__(self): | ||
self.annotations = {} | ||
self.masks = {} | ||
|
||
def save_annotations(self, item): | ||
masks = [] | ||
def save_annotations(self, item, path): | ||
annotation = '' | ||
colormap = [(255, 255, 255)] | ||
anns = [a for a in item.annotations | ||
if a.type == AnnotationType.mask] | ||
anns = sorted(anns, key=lambda a: a.id) | ||
group = anns[0].group | ||
for ann in anns: | ||
if ann.group != group or ann.group == 0: | ||
if anns: | ||
is_not_index = len([p for p in anns if 'index' not in p.attributes]) | ||
if is_not_index: | ||
raise Exception("Item %s: a mask must have" | ||
"'index' attribute" % item.id) | ||
anns = sorted(anns, key=lambda a: a.attributes['index']) | ||
group = anns[0].group | ||
for ann in anns: | ||
if ann.group != group or (not ann.group and anns[0].group != 0): | ||
annotation += '\n' | ||
text = '' | ||
if ann.attributes: | ||
if 'text' in ann.attributes: | ||
text = ann.attributes['text'] | ||
if text == ' ': | ||
annotation += '#' | ||
if 'color' in ann.attributes and \ | ||
len(ann.attributes['color'].split()) == 3: | ||
color = ann.attributes['color'].split() | ||
colormap.append( | ||
(int(color[0]), int(color[1]), int(color[2]))) | ||
annotation += ' '.join(p for p in color) | ||
else: | ||
raise Exception("Item %s: a mask must have " | ||
"an RGB color attribute, e. g. '10 7 50'" % item.id) | ||
if 'center' in ann.attributes: | ||
annotation += ' %s' % ann.attributes['center'] | ||
else: | ||
annotation += ' - -' | ||
bbox = ann.get_bbox() | ||
annotation += ' %s %s %s %s' % (bbox[0], bbox[1], | ||
bbox[0] + bbox[2], bbox[1] + bbox[3]) | ||
annotation += ' \"%s\"' % text | ||
annotation += '\n' | ||
text = '' | ||
if ann.attributes: | ||
if 'text' in ann.attributes: | ||
text = ann.attributes['text'] | ||
if text == ' ': | ||
annotation += '#' | ||
if 'color' in ann.attributes: | ||
colormap.append(ann.attributes['color']) | ||
annotation += ' '.join(str(p) | ||
for p in ann.attributes['color']) | ||
else: | ||
annotation += '- - -' | ||
if 'center' in ann.attributes: | ||
annotation += ' ' | ||
annotation += ' '.join(str(p) | ||
for p in ann.attributes['center']) | ||
else: | ||
annotation += ' - -' | ||
bbox = ann.get_bbox() | ||
annotation += ' %s %s %s %s' % (bbox[0], bbox[1], | ||
bbox[0] + bbox[2], bbox[1] + bbox[3]) | ||
annotation += ' \"%s\"' % text | ||
annotation += '\n' | ||
group = ann.group | ||
masks.append(ann.as_class_mask(ann.id)) | ||
|
||
mask = merge_masks(masks) | ||
mask = paint_mask(mask, | ||
{ i: colormap[i] for i in range(len(colormap)) }) | ||
group = ann.group | ||
|
||
mask = CompiledMask.from_instance_masks(anns, | ||
instance_labels=[m.attributes['index'] + 1 for m in anns]) | ||
mask = paint_mask(mask.class_mask, | ||
{ i: colormap[i] for i in range(len(colormap)) }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can easily consume lots of memory (for instance - imagine an uncompressed 4k video), try not to store masks. |
||
save_image(osp.join(path, item.id + '_GT' + IcdarPath.GT_EXT), | ||
mask, create_dir=True) | ||
self.annotations[item.id] = annotation | ||
self.masks[item.id] = mask | ||
|
||
def write(self, path): | ||
os.makedirs(path, exist_ok=True) | ||
for item in self.annotations: | ||
file = osp.join(path, item + '_GT' + '.txt') | ||
with open(file, 'w') as f: | ||
f.write(self.annotations[item]) | ||
save_image(osp.join(path, item + '_GT' + IcdarPath.GT_EXT), | ||
self.masks[item], create_dir=True) | ||
|
||
def is_empty(self): | ||
return len(self.annotations) == 0 | ||
|
@@ -161,12 +164,13 @@ def apply(self): | |
for subset_name, subset in self._extractor.subsets().items(): | ||
task_converters = self._make_task_converters() | ||
for item in subset: | ||
for task_conv in task_converters.values(): | ||
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, | ||
item.id + IcdarPath.IMAGE_EXT)) | ||
task_conv.save_annotations(item) | ||
task_conv.save_annotations(item, osp.join(self._save_dir, | ||
IcdarPath.TASK_DIR[task], subset_name)) | ||
|
||
for task, task_conv in task_converters.items(): | ||
if task_conv.is_empty() and not self._tasks: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we need to store
color
andcenter
information as text.