1
+
2
+ # Copyright (C) 2019-2020 Intel Corporation
3
+ #
4
+ # SPDX-License-Identifier: MIT
5
+
1
6
from collections import OrderedDict
2
7
import os
3
8
import os .path as osp
6
11
7
12
from cvat .apps .annotation .annotation import Annotation
8
13
from cvat .apps .engine .annotation import TaskAnnotation
9
- from cvat .apps .engine .models import Task , ShapeType
14
+ from cvat .apps .engine .models import Task , ShapeType , AttributeType
10
15
11
16
import datumaro .components .extractor as datumaro
12
17
from datumaro .util .image import lazy_image
@@ -128,18 +133,33 @@ def _read_cvat_anno(self, cvat_anno):
128
133
attrs = {}
129
134
db_attributes = db_label .attributespec_set .all ()
130
135
for db_attr in db_attributes :
131
- attrs [db_attr .name ] = db_attr . default_value
136
+ attrs [db_attr .name ] = db_attr
132
137
label_attrs [db_label .name ] = attrs
133
138
map_label = lambda label_db_name : label_map [label_db_name ]
134
139
140
+ def convert_attrs (label , cvat_attrs ):
141
+ cvat_attrs = {a .name : a .value for a in cvat_attrs }
142
+ dm_attr = dict ()
143
+ for attr_name , attr_spec in label_attrs [label ].items ():
144
+ attr_value = cvat_attrs .get (attr_name , attr_spec .default_value )
145
+ try :
146
+ if attr_spec .input_type == AttributeType .NUMBER :
147
+ attr_value = float (attr_value )
148
+ elif attr_spec .input_type == AttributeType .CHECKBOX :
149
+ attr_value = attr_value .lower () == 'true'
150
+ dm_attr [attr_name ] = attr_value
151
+ except Exception as e :
152
+ slogger .task [self ._db_task .id ].error (
153
+ "Failed to convert attribute '%s'='%s': %s" % \
154
+ (attr_name , attr_value , e ))
155
+ return dm_attr
156
+
135
157
for tag_obj in cvat_anno .tags :
136
158
anno_group = tag_obj .group
137
159
if isinstance (anno_group , int ):
138
160
anno_group = anno_group
139
161
anno_label = map_label (tag_obj .label )
140
- anno_attr = dict (label_attrs [tag_obj .label ])
141
- for attr in tag_obj .attributes :
142
- anno_attr [attr .name ] = attr .value
162
+ anno_attr = convert_attrs (tag_obj .label , tag_obj .attributes )
143
163
144
164
anno = datumaro .LabelObject (label = anno_label ,
145
165
attributes = anno_attr , group = anno_group )
@@ -150,9 +170,7 @@ def _read_cvat_anno(self, cvat_anno):
150
170
if isinstance (anno_group , int ):
151
171
anno_group = anno_group
152
172
anno_label = map_label (shape_obj .label )
153
- anno_attr = dict (label_attrs [shape_obj .label ])
154
- for attr in shape_obj .attributes :
155
- anno_attr [attr .name ] = attr .value
173
+ anno_attr = convert_attrs (shape_obj .label , shape_obj .attributes )
156
174
157
175
anno_points = shape_obj .points
158
176
if shape_obj .type == ShapeType .POINTS :
0 commit comments