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

[Datumaro] CVAT format import #974

Merged
merged 14 commits into from
Dec 18, 2019
29 changes: 20 additions & 9 deletions datumaro/datumaro/cli/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging as log
import os
import os.path as osp
import shutil

from datumaro.components.project import Project
from datumaro.components.comparator import Comparator
Expand All @@ -27,10 +28,13 @@ def create_command(args):
project_dir = osp.abspath(args.dst_dir)
project_path = make_project_path(project_dir)

if not args.overwrite and osp.isdir(project_dir) and os.listdir(project_dir):
log.error("Directory '%s' already exists "
"(pass --overwrite to force creation)" % project_dir)
return 1
if osp.isdir(project_dir) and os.listdir(project_dir):
if not args.overwrite:
log.error("Directory '%s' already exists "
"(pass --overwrite to force creation)" % project_dir)
return 1
else:
shutil.rmtree(project_dir)
os.makedirs(project_dir, exist_ok=args.overwrite)

if not args.overwrite and osp.isfile(project_path):
Expand Down Expand Up @@ -76,10 +80,13 @@ def import_command(args):
project_dir = osp.abspath(args.dst_dir)
project_path = make_project_path(project_dir)

if not args.overwrite and osp.isdir(project_dir) and os.listdir(project_dir):
log.error("Directory '%s' already exists "
"(pass --overwrite to force creation)" % project_dir)
return 1
if osp.isdir(project_dir) and os.listdir(project_dir):
if not args.overwrite:
log.error("Directory '%s' already exists "
"(pass --overwrite to force creation)" % project_dir)
return 1
else:
shutil.rmtree(project_dir)
os.makedirs(project_dir, exist_ok=args.overwrite)

if not args.overwrite and osp.isfile(project_path):
Expand Down Expand Up @@ -137,7 +144,11 @@ def export_command(args):
dst_dir = osp.abspath(args.dst_dir)
os.makedirs(dst_dir, exist_ok=False)

project.make_dataset().export(
log.info("Loading the project...")
dataset = project.make_dataset()

log.info("Exporting the project...")
dataset.export(
save_dir=dst_dir,
output_format=args.output_format,
filter_expr=args.filter,
Expand Down
6 changes: 5 additions & 1 deletion datumaro/datumaro/cli/source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ def export_command(args):
dst_dir = osp.abspath(args.dst_dir)
os.makedirs(dst_dir, exist_ok=False)

log.info("Loading the project...")
source_project = project.make_source_project(args.name)
source_project.make_dataset().export(
dataset = source_project.make_dataset()

log.info("Exporting the project...")
dataset.export(
save_dir=dst_dir,
output_format=args.output_format,
filter_expr=args.filter,
Expand Down
5 changes: 1 addition & 4 deletions datumaro/datumaro/components/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
)

from datumaro.components.converters.yolo import YoloConverter

from datumaro.components.converters.tfrecord import (
DetectionApiConverter,
)
from datumaro.components.converters.tfrecord import DetectionApiConverter


items = [
Expand Down
11 changes: 6 additions & 5 deletions datumaro/datumaro/components/converters/datumaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

from datumaro.components.converter import Converter
from datumaro.components.extractor import (
DEFAULT_SUBSET_NAME,
AnnotationType, Annotation,
DEFAULT_SUBSET_NAME, Annotation,
LabelObject, MaskObject, PointsObject, PolygonObject,
PolyLineObject, BboxObject, CaptionObject,
LabelCategories, MaskCategories, PointsCategories
Expand Down Expand Up @@ -52,11 +51,13 @@ def items(self):

def write_item(self, item):
annotations = []
self.items.append({
item_desc = {
'id': item.id,
'path': item.path,
'annotations': annotations,
})
}
if item.path:
item_desc['path'] = item.path
self.items.append(item_desc)

for ann in item.annotations:
if isinstance(ann, LabelObject):
Expand Down
19 changes: 16 additions & 3 deletions datumaro/datumaro/components/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class Categories:
def __init__(self, attributes=None):
if attributes is None:
attributes = set()
else:
if not isinstance(attributes, set):
attributes = set(attributes)
for attr in attributes:
assert isinstance(attr, str)
self.attributes = attributes

def __eq__(self, other):
Expand All @@ -62,7 +67,7 @@ def __eq__(self, other):
(self.attributes == other.attributes)

class LabelCategories(Categories):
Category = namedtuple('Category', ['name', 'parent'])
Category = namedtuple('Category', ['name', 'parent', 'attributes'])

def __init__(self, items=None, attributes=None):
super().__init__(attributes=attributes)
Expand All @@ -81,11 +86,18 @@ def _reindex(self):
indices[item.name] = index
self._indices = indices

def add(self, name, parent=None):
def add(self, name, parent=None, attributes=None):
assert name not in self._indices
if attributes is None:
attributes = set()
else:
if not isinstance(attributes, set):
attributes = set(attributes)
for attr in attributes:
assert isinstance(attr, str)

index = len(self.items)
self.items.append(self.Category(name, parent))
self.items.append(self.Category(name, parent, attributes))
self._indices[name] = index

def find(self, name):
Expand Down Expand Up @@ -462,6 +474,7 @@ def __eq__(self, other):
(self.id == other.id) and \
(self.subset == other.subset) and \
(self.annotations == other.annotations) and \
(self.path == other.path) and \
(self.has_image == other.has_image) and \
(self.has_image and np.all(self.image == other.image) or \
not self.has_image)
Expand Down
13 changes: 5 additions & 8 deletions datumaro/datumaro/components/extractors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@
VocComp_9_10_Extractor,
)

from datumaro.components.extractors.yolo import (
YoloExtractor,
)

from datumaro.components.extractors.tfrecord import (
DetectionApiExtractor,
)

from datumaro.components.extractors.yolo import YoloExtractor
from datumaro.components.extractors.tfrecord import DetectionApiExtractor
from datumaro.components.extractors.cvat import CvatExtractor

items = [
('datumaro', DatumaroExtractor),
Expand All @@ -59,4 +54,6 @@
('yolo', YoloExtractor),

('tf_detection_api', DetectionApiExtractor),

('cvat', CvatExtractor),
]
Loading