Skip to content

Commit

Permalink
Fix bugs for Tile transform (openvinotoolkit#1123)
Browse files Browse the repository at this point in the history
- Resolve this issue,
openvinotoolkit#1118
 - Fix documentation error on CLI guide for Tile transform
 - Fix a problem that RoIImageFromFile cannot be saved properly

I updated the existing test to test saving `RoIImage` as well.

<!-- Put an 'x' in all the boxes that apply -->
- [x] I have added unit tests to cover my changes.​
- [ ] I have added integration tests to cover my changes.​
- [x] I have added the description of my changes into
[CHANGELOG](https://github.com/openvinotoolkit/datumaro/blob/develop/CHANGELOG.md).​
- [x] I have updated the
[documentation](https://github.com/openvinotoolkit/datumaro/tree/develop/docs)
accordingly

- [x] I submit _my code changes_ under the same [MIT
License](https://github.com/openvinotoolkit/datumaro/blob/develop/LICENSE)
that covers the project.
  Feel free to contact the maintainers if that's a concern.
- [x] I have updated the license header for each file (see an example
below).

```python
```

---------

Signed-off-by: Kim, Vinnam <vinnam.kim@intel.com>
  • Loading branch information
vinnamkim committed Aug 21, 2023
1 parent 6d65cec commit c9ad773
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
36 changes: 19 additions & 17 deletions src/datumaro/components/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,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 @@ -1063,23 +1081,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

0 comments on commit c9ad773

Please sign in to comment.