Skip to content

Commit 0744c6a

Browse files
authored
[Datumaro] MOT format (#1289)
* Add mot format base * Add mot format * Extract common code
1 parent 8efaf58 commit 0744c6a

File tree

6 files changed

+540
-50
lines changed

6 files changed

+540
-50
lines changed

datumaro/datumaro/plugins/coco_format/converter.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,14 @@
1717
AnnotationType, Points
1818
)
1919
from datumaro.components.cli_plugin import CliPlugin
20-
from datumaro.util import find
20+
from datumaro.util import find, cast
2121
from datumaro.util.image import save_image
2222
import datumaro.util.mask_tools as mask_tools
2323
import datumaro.util.annotation_tools as anno_tools
2424

2525
from .format import CocoTask, CocoPath
2626

2727

28-
def _cast(value, type_conv, default=None):
29-
if value is None:
30-
return default
31-
try:
32-
return type_conv(value)
33-
except Exception:
34-
return default
35-
36-
3728
SegmentationMode = Enum('SegmentationMode', ['guess', 'polygons', 'mask'])
3829

3930
class _TaskConverter:
@@ -82,7 +73,7 @@ def save_image_info(self, item, filename):
8273
'id': self._get_image_id(item),
8374
'width': int(w),
8475
'height': int(h),
85-
'file_name': _cast(filename, str, ''),
76+
'file_name': cast(filename, str, ''),
8677
'license': 0,
8778
'flickr_url': '',
8879
'coco_url': '',
@@ -162,8 +153,8 @@ def save_categories(self, dataset):
162153
for idx, cat in enumerate(label_categories.items):
163154
self.categories.append({
164155
'id': 1 + idx,
165-
'name': _cast(cat.name, str, ''),
166-
'supercategory': _cast(cat.parent, str, ''),
156+
'name': cast(cat.name, str, ''),
157+
'supercategory': cast(cat.parent, str, ''),
167158
})
168159

169160
@classmethod
@@ -309,7 +300,7 @@ def convert_instance(self, instance, item):
309300
elem = {
310301
'id': self._get_ann_id(ann),
311302
'image_id': self._get_image_id(item),
312-
'category_id': _cast(ann.label, int, -1) + 1,
303+
'category_id': cast(ann.label, int, -1) + 1,
313304
'segmentation': segmentation,
314305
'area': float(area),
315306
'bbox': list(map(float, bbox)),
@@ -334,10 +325,11 @@ def save_categories(self, dataset):
334325
for idx, label_cat in enumerate(label_categories.items):
335326
cat = {
336327
'id': 1 + idx,
337-
'name': _cast(label_cat.name, str, ''),
338-
'supercategory': _cast(label_cat.parent, str, ''),
328+
'name': cast(label_cat.name, str, ''),
329+
'supercategory': cast(label_cat.parent, str, ''),
339330
'keypoints': [],
340331
'skeleton': [],
332+
341333
}
342334

343335
if point_categories is not None:
@@ -416,8 +408,8 @@ def save_categories(self, dataset):
416408
for idx, cat in enumerate(label_categories.items):
417409
self.categories.append({
418410
'id': 1 + idx,
419-
'name': _cast(cat.name, str, ''),
420-
'supercategory': _cast(cat.parent, str, ''),
411+
'name': cast(cat.name, str, ''),
412+
'supercategory': cast(cat.parent, str, ''),
421413
})
422414

423415
def save_annotations(self, item):
@@ -504,7 +496,7 @@ def _make_task_converters(self):
504496
def _get_image_id(self, item):
505497
image_id = self._image_ids.get(item.id)
506498
if image_id is None:
507-
image_id = _cast(item.id, int, len(self._image_ids) + 1)
499+
image_id = cast(item.id, int, len(self._image_ids) + 1)
508500
self._image_ids[item.id] = image_id
509501
return image_id
510502

datumaro/datumaro/plugins/cvat_format/converter.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,12 @@
1212
from datumaro.components.cli_plugin import CliPlugin
1313
from datumaro.components.converter import Converter
1414
from datumaro.components.extractor import DEFAULT_SUBSET_NAME, AnnotationType
15+
from datumaro.util import cast
1516
from datumaro.util.image import save_image
1617

1718
from .format import CvatPath
1819

1920

20-
def _cast(value, type_conv, default=None):
21-
if value is None:
22-
return default
23-
try:
24-
return type_conv(value)
25-
except Exception:
26-
return default
27-
2821
def pairwise(iterable):
2922
a = iter(iterable)
3023
return zip(a, a)
@@ -188,7 +181,7 @@ def _save_image(self, item):
188181

189182
def _write_item(self, item, index):
190183
image_info = OrderedDict([
191-
("id", str(_cast(item.id, int, index))),
184+
("id", str(cast(item.id, int, index))),
192185
])
193186
if item.has_image:
194187
size = item.image.size

datumaro/datumaro/plugins/datumaro_format/converter.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616
Label, Mask, RleMask, Points, Polygon, PolyLine, Bbox, Caption,
1717
LabelCategories, MaskCategories, PointsCategories
1818
)
19+
from datumaro.util import cast
1920
from datumaro.util.image import save_image
2021
import pycocotools.mask as mask_utils
2122
from datumaro.components.cli_plugin import CliPlugin
2223

2324
from .format import DatumaroPath
2425

2526

26-
def _cast(value, type_conv, default=None):
27-
if value is None:
28-
return default
29-
try:
30-
return type_conv(value)
31-
except Exception:
32-
return default
33-
3427
class _SubsetWriter:
3528
def __init__(self, name, context):
3629
self._name = name
@@ -108,18 +101,18 @@ def _convert_annotation(self, obj):
108101
assert isinstance(obj, Annotation)
109102

110103
ann_json = {
111-
'id': _cast(obj.id, int),
112-
'type': _cast(obj.type.name, str),
104+
'id': cast(obj.id, int),
105+
'type': cast(obj.type.name, str),
113106
'attributes': obj.attributes,
114-
'group': _cast(obj.group, int, 0),
107+
'group': cast(obj.group, int, 0),
115108
}
116109
return ann_json
117110

118111
def _convert_label_object(self, obj):
119112
converted = self._convert_annotation(obj)
120113

121114
converted.update({
122-
'label_id': _cast(obj.label, int),
115+
'label_id': cast(obj.label, int),
123116
})
124117
return converted
125118

@@ -133,7 +126,7 @@ def _convert_mask_object(self, obj):
133126
np.require(obj.image, dtype=np.uint8, requirements='F'))
134127

135128
converted.update({
136-
'label_id': _cast(obj.label, int),
129+
'label_id': cast(obj.label, int),
137130
'rle': {
138131
# serialize as compressed COCO mask
139132
'counts': rle['counts'].decode('ascii'),
@@ -146,7 +139,7 @@ def _convert_polyline_object(self, obj):
146139
converted = self._convert_annotation(obj)
147140

148141
converted.update({
149-
'label_id': _cast(obj.label, int),
142+
'label_id': cast(obj.label, int),
150143
'points': [float(p) for p in obj.points],
151144
})
152145
return converted
@@ -155,7 +148,7 @@ def _convert_polygon_object(self, obj):
155148
converted = self._convert_annotation(obj)
156149

157150
converted.update({
158-
'label_id': _cast(obj.label, int),
151+
'label_id': cast(obj.label, int),
159152
'points': [float(p) for p in obj.points],
160153
})
161154
return converted
@@ -164,7 +157,7 @@ def _convert_bbox_object(self, obj):
164157
converted = self._convert_annotation(obj)
165158

166159
converted.update({
167-
'label_id': _cast(obj.label, int),
160+
'label_id': cast(obj.label, int),
168161
'bbox': [float(p) for p in obj.get_bbox()],
169162
})
170163
return converted
@@ -173,7 +166,7 @@ def _convert_points_object(self, obj):
173166
converted = self._convert_annotation(obj)
174167

175168
converted.update({
176-
'label_id': _cast(obj.label, int),
169+
'label_id': cast(obj.label, int),
177170
'points': [float(p) for p in obj.points],
178171
'visibility': [int(v.value) for v in obj.visibility],
179172
})
@@ -183,7 +176,7 @@ def _convert_caption_object(self, obj):
183176
converted = self._convert_annotation(obj)
184177

185178
converted.update({
186-
'caption': _cast(obj.caption, str),
179+
'caption': cast(obj.caption, str),
187180
})
188181
return converted
189182

@@ -193,8 +186,8 @@ def _convert_label_categories(self, obj):
193186
}
194187
for label in obj.items:
195188
converted['labels'].append({
196-
'name': _cast(label.name, str),
197-
'parent': _cast(label.parent, str),
189+
'name': cast(label.name, str),
190+
'parent': cast(label.parent, str),
198191
})
199192
return converted
200193

@@ -218,7 +211,7 @@ def _convert_points_categories(self, obj):
218211
for label_id, item in obj.items.items():
219212
converted['items'].append({
220213
'label_id': int(label_id),
221-
'labels': [_cast(label, str) for label in item.labels],
214+
'labels': [cast(label, str) for label in item.labels],
222215
'adjacent': [int(v) for v in item.adjacent],
223216
})
224217
return converted

0 commit comments

Comments
 (0)