Skip to content

Commit d69cee8

Browse files
zhiltsov-maxChris Lee-Messer
authored and
Chris Lee-Messer
committed
[Datumaro] CVAT format import (cvat-ai#974)
* Add label-specific attributes * Add CVAT format import * Register CVAT format * Add little more logs * Little refactoring for tests * Cvat format checks * Add missing check * Refactor datumaro format * Little refactoring * Regularize dataset importer logic * Fix project import issue * Refactor coco extractor * Refactor tests * Codacy
1 parent e7826ea commit d69cee8

25 files changed

+774
-381
lines changed

datumaro/datumaro/cli/project/__init__.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging as log
88
import os
99
import os.path as osp
10+
import shutil
1011

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

30-
if not args.overwrite and osp.isdir(project_dir) and os.listdir(project_dir):
31-
log.error("Directory '%s' already exists "
32-
"(pass --overwrite to force creation)" % project_dir)
33-
return 1
31+
if osp.isdir(project_dir) and os.listdir(project_dir):
32+
if not args.overwrite:
33+
log.error("Directory '%s' already exists "
34+
"(pass --overwrite to force creation)" % project_dir)
35+
return 1
36+
else:
37+
shutil.rmtree(project_dir)
3438
os.makedirs(project_dir, exist_ok=args.overwrite)
3539

3640
if not args.overwrite and osp.isfile(project_path):
@@ -78,10 +82,13 @@ def import_command(args):
7882
project_dir = osp.abspath(args.dst_dir)
7983
project_path = make_project_path(project_dir)
8084

81-
if not args.overwrite and osp.isdir(project_dir) and os.listdir(project_dir):
82-
log.error("Directory '%s' already exists "
83-
"(pass --overwrite to force creation)" % project_dir)
84-
return 1
85+
if osp.isdir(project_dir) and os.listdir(project_dir):
86+
if not args.overwrite:
87+
log.error("Directory '%s' already exists "
88+
"(pass --overwrite to force creation)" % project_dir)
89+
return 1
90+
else:
91+
shutil.rmtree(project_dir)
8592
os.makedirs(project_dir, exist_ok=args.overwrite)
8693

8794
if not args.overwrite and osp.isfile(project_path):
@@ -147,7 +154,11 @@ def export_command(args):
147154
return 1
148155
os.makedirs(dst_dir, exist_ok=args.overwrite)
149156

150-
project.make_dataset().export(
157+
log.info("Loading the project...")
158+
dataset = project.make_dataset()
159+
160+
log.info("Exporting the project...")
161+
dataset.export(
151162
save_dir=dst_dir,
152163
output_format=args.output_format,
153164
filter_expr=args.filter,

datumaro/datumaro/cli/source/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,12 @@ def export_command(args):
210210
return 1
211211
os.makedirs(dst_dir, exist_ok=args.overwrite)
212212

213+
log.info("Loading the project...")
213214
source_project = project.make_source_project(args.name)
214-
source_project.make_dataset().export(
215+
dataset = source_project.make_dataset()
216+
217+
log.info("Exporting the project...")
218+
dataset.export(
215219
save_dir=dst_dir,
216220
output_format=args.output_format,
217221
filter_expr=args.filter,

datumaro/datumaro/components/converters/__init__.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
)
2525

2626
from datumaro.components.converters.yolo import YoloConverter
27-
28-
from datumaro.components.converters.tfrecord import (
29-
DetectionApiConverter,
30-
)
27+
from datumaro.components.converters.tfrecord import DetectionApiConverter
3128

3229

3330
items = [

datumaro/datumaro/components/converters/datumaro.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
from datumaro.components.converter import Converter
1313
from datumaro.components.extractor import (
14-
DEFAULT_SUBSET_NAME,
15-
AnnotationType, Annotation,
14+
DEFAULT_SUBSET_NAME, Annotation,
1615
LabelObject, MaskObject, PointsObject, PolygonObject,
1716
PolyLineObject, BboxObject, CaptionObject,
1817
LabelCategories, MaskCategories, PointsCategories
@@ -52,11 +51,13 @@ def items(self):
5251

5352
def write_item(self, item):
5453
annotations = []
55-
self.items.append({
54+
item_desc = {
5655
'id': item.id,
57-
'path': item.path,
5856
'annotations': annotations,
59-
})
57+
}
58+
if item.path:
59+
item_desc['path'] = item.path
60+
self.items.append(item_desc)
6061

6162
for ann in item.annotations:
6263
if isinstance(ann, LabelObject):

datumaro/datumaro/components/extractor.py

+1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ def __eq__(self, other):
476476
(self.id == other.id) and \
477477
(self.subset == other.subset) and \
478478
(self.annotations == other.annotations) and \
479+
(self.path == other.path) and \
479480
(self.has_image == other.has_image) and \
480481
(self.has_image and np.all(self.image == other.image) or \
481482
not self.has_image)

datumaro/datumaro/components/extractors/__init__.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@
2626
VocComp_9_10_Extractor,
2727
)
2828

29-
from datumaro.components.extractors.yolo import (
30-
YoloExtractor,
31-
)
32-
33-
from datumaro.components.extractors.tfrecord import (
34-
DetectionApiExtractor,
35-
)
36-
29+
from datumaro.components.extractors.yolo import YoloExtractor
30+
from datumaro.components.extractors.tfrecord import DetectionApiExtractor
31+
from datumaro.components.extractors.cvat import CvatExtractor
3732

3833
items = [
3934
('datumaro', DatumaroExtractor),
@@ -59,4 +54,6 @@
5954
('yolo', YoloExtractor),
6055

6156
('tf_detection_api', DetectionApiExtractor),
57+
58+
('cvat', CvatExtractor),
6259
]

0 commit comments

Comments
 (0)