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

Fix bugs for Tile transform #1123

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
(<https://github.com/openvinotoolkit/datumaro/pull/1120>)

### Bug fixes
- Fix bugs for Tile transform
(<https://github.com/openvinotoolkit/datumaro/pull/1123>)

## 27/07/2023 - Release 1.4.1
### Bug fixes
Expand Down
36 changes: 19 additions & 17 deletions src/datumaro/components/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,24 @@ def _get_roi_data(self, data: np.ndarray) -> np.ndarray:
data = data.astype(np.float32)
return data[y : y + h, x : x + w]

def save(
self,
fp: Union[str, io.IOBase],
ext: Optional[str] = None,
crypter: Crypter = NULL_CRYPTER,
):
if not crypter.is_null_crypter:
raise NotImplementedError(
f"{self.__class__.__name__} does not implement save() with non NullCrypter."
)
data = self.data
if data is None:
raise ValueError(f"{self.__class__.__name__} is empty.")
new_ext = self._get_ext_to_save(fp, ext)
if isinstance(fp, str):
os.makedirs(osp.dirname(fp), exist_ok=True)
save_image(fp, data, ext=new_ext, crypter=crypter)


class RoIImageFromFile(FromFileMixin, RoIImage):
def __init__(
Expand All @@ -1067,23 +1085,7 @@ def data(self) -> Optional[np.ndarray]:


class RoIImageFromData(FromDataMixin, RoIImage):
def save(
self,
fp: Union[str, io.IOBase],
ext: Optional[str] = None,
crypter: Crypter = NULL_CRYPTER,
):
if not crypter.is_null_crypter:
raise NotImplementedError(
f"{self.__class__.__name__} does not implement save() with non NullCrypter."
)
data = self.data
if data is None:
raise ValueError(f"{self.__class__.__name__} is empty.")
new_ext = self._get_ext_to_save(fp, ext)
if isinstance(fp, str):
os.makedirs(osp.dirname(fp), exist_ok=True)
save_image(fp, data, ext=new_ext, crypter=crypter)
pass


class RoIImageFromBytes(RoIImageFromData):
Expand Down
2 changes: 1 addition & 1 deletion src/datumaro/plugins/tiling/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Tile(Transform, CliPlugin):
--------
The following example is the CLI command for a 3x2 tiling with a width and height overlap of 10%::
$ datum transform -t tile --grid-size 3 2 --overlap 0.1 0.1 --threshold-drop-ann 0.1
$ datum transform -t tile -- --grid-size 3 2 --overlap 0.1 0.1 --threshold-drop-ann 0.1
:obj:`--threshold-drop-ann` means an area threshold to remove bboxes and polygons
when they are in the boundary of the tiled image and cropped by tiling. In this example,
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def test_ext_detection_failure(self):


class RoIImageTest(TestCase):
def _test_ctors(self, img_ctor, args_list, is_bytes=False):
def _test_ctors(self, img_ctor, args_list, test_dir, is_bytes=False):
for args in args_list:
# Case 1. Retrieve roi_img.data without retrieving the original image
with self.subTest(**args):
Expand Down Expand Up @@ -303,12 +303,18 @@ def _test_ctors(self, img_ctor, args_list, is_bytes=False):
self.assertEqual(roi_img.size, (new_h, new_w))
self.assertEqual(roi_img.data.shape[:2], (new_h, new_w))

with self.subTest(**args):
try:
roi_img.save(osp.join(test_dir, "test.png"))
except:
self.fail("Cannot save RoIImage")

def test_ctors_from_image(self):
with TestDir() as test_dir:
_, args_list = ImageTest._gen_image_and_args_list(test_dir)
self._test_ctors(Image, args_list)
self._test_ctors(Image, args_list, test_dir, False)
_, _, args_list = ImageTest._gen_bytes_image_and_args_list()
self._test_ctors(Image, args_list, True)
self._test_ctors(Image, args_list, test_dir, True)


class ImageMetaTest(TestCase):
Expand Down
Loading