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

Make image dirs data format streamable #1576

Merged
merged 2 commits into from
Aug 5, 2024
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
4 changes: 4 additions & 0 deletions src/datumaro/plugins/data_formats/image_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def __init__(
)
self._ann_types = set()

@property
def is_stream(self) -> bool:
return True


class ImageDirExporter(Exporter):
DEFAULT_IMAGE_EXT = ".jpg"
Expand Down
Binary file added tests/assets/image_dir_dataset/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/image_dir_dataset/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 31 additions & 12 deletions tests/unit/test_image_dir_format.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
from unittest import TestCase

import numpy as np
import pytest

from datumaro.components.dataset import Dataset, StreamDataset
from datumaro.components.dataset_base import DatasetItem
from datumaro.components.media import Image
from datumaro.components.project import Dataset
from datumaro.plugins.data_formats.image_dir import ImageDirExporter

from ..requirements import Requirements, mark_requirement

from tests.utils.test_utils import TestDir, check_save_and_load
from tests.utils.assets import get_test_asset_path
from tests.utils.test_utils import TestDir, check_save_and_load, compare_datasets

DUMMY_DATASET_DIR = get_test_asset_path("image_dir_dataset")
FORMAT_NAME = "image_dir"


class ImageDirFormatTest(TestCase):
class ImageDirFormatTest:
@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_load(self):
def test_can_load(self, helper_tc):
dataset = Dataset.from_iterable(
[
DatasetItem(id=1, media=Image.from_numpy(data=np.ones((10, 6, 3)))),
Expand All @@ -24,16 +28,31 @@ def test_can_load(self):

with TestDir() as test_dir:
check_save_and_load(
self,
helper_tc,
dataset,
ImageDirExporter.convert,
test_dir,
importer="image_dir",
importer=FORMAT_NAME,
require_media=True,
)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_save_dataset_with_cyrillic_and_spaces_in_filename(self):
@pytest.mark.parametrize("dataset_cls, is_stream", [(Dataset, False), (StreamDataset, True)])
def test_can_import(self, dataset_cls, is_stream, helper_tc):
expected_dataset = Dataset.from_iterable(
[
DatasetItem(id="1", media=Image.from_numpy(data=np.zeros((4, 3, 3)), ext=".JPEG")),
DatasetItem(id="2", media=Image.from_numpy(data=np.zeros((3, 4, 3)), ext=".bmp")),
]
)

actual_dataset = dataset_cls.import_from(DUMMY_DATASET_DIR, FORMAT_NAME)

assert actual_dataset.is_stream == is_stream
compare_datasets(helper_tc, expected_dataset, actual_dataset)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_save_dataset_with_cyrillic_and_spaces_in_filename(self, helper_tc):
dataset = Dataset.from_iterable(
[
DatasetItem(
Expand All @@ -44,11 +63,11 @@ def test_can_save_dataset_with_cyrillic_and_spaces_in_filename(self):

with TestDir() as test_dir:
check_save_and_load(
self, dataset, ImageDirExporter.convert, test_dir, importer="image_dir"
helper_tc, dataset, ImageDirExporter.convert, test_dir, importer=FORMAT_NAME
)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_save_and_load_image_with_arbitrary_extension(self):
def test_can_save_and_load_image_with_arbitrary_extension(self, helper_tc):
dataset = Dataset.from_iterable(
[
DatasetItem(id="1", media=Image.from_numpy(data=np.zeros((4, 3, 3)), ext=".JPEG")),
Expand All @@ -58,10 +77,10 @@ def test_can_save_and_load_image_with_arbitrary_extension(self):

with TestDir() as test_dir:
check_save_and_load(
self,
helper_tc,
dataset,
ImageDirExporter.convert,
test_dir,
importer="image_dir",
importer=FORMAT_NAME,
require_media=True,
)
Loading