Skip to content

Commit

Permalink
Add image name to id transform
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiltsov-max committed Feb 19, 2020
1 parent 1e640a2 commit 4dfbd33
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions datumaro/datumaro/plugins/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from itertools import groupby
import logging as log
import os.path as osp

import pycocotools.mask as mask_utils

Expand Down Expand Up @@ -253,3 +254,11 @@ def __init__(self, extractor, start=1):
def __iter__(self):
for i, item in enumerate(self._extractor):
yield self.wrap_item(item, id=i + self._start)

class IdFromImageName(Transform, CliPlugin):
def transform_item(self, item):
name = item.id
if item.has_image and item.image.filename:
name = osp.splitext(item.image.filename)[0]
return self.wrap_item(item, id=name)

18 changes: 18 additions & 0 deletions datumaro/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,22 @@ def __iter__(self):

actual = transforms.MergeInstanceSegments(SrcExtractor(),
include_polygons=True)
compare_datasets(self, DstExtractor(), actual)

def test_id_from_image(self):
class SrcExtractor(Extractor):
def __iter__(self):
return iter([
DatasetItem(id=1, image='path.jpg'),
DatasetItem(id=2),
])

class DstExtractor(Extractor):
def __iter__(self):
return iter([
DatasetItem(id='path', image='path.jpg'),
DatasetItem(id=2),
])

actual = transforms.IdFromImageName(SrcExtractor())
compare_datasets(self, DstExtractor(), actual)

0 comments on commit 4dfbd33

Please sign in to comment.