From f26ddc8f5667947c3fe31cfda1fa074acbb98cb2 Mon Sep 17 00:00:00 2001 From: imyhxy Date: Sun, 4 Feb 2024 14:39:58 +0800 Subject: [PATCH 1/5] Use PIL to get size --- src/datumaro/components/media.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/datumaro/components/media.py b/src/datumaro/components/media.py index 1c28634acd..e0320f29f5 100644 --- a/src/datumaro/components/media.py +++ b/src/datumaro/components/media.py @@ -49,6 +49,12 @@ pd = lazy_import("pandas") +try: + from PIL import Image as PILImage + + _HAS_PIL = True +except (ModuleNotFoundError, ImportError): + _HAS_PIL = False AnyData = TypeVar("AnyData", bytes, np.ndarray) @@ -330,6 +336,18 @@ def data(self) -> Optional[np.ndarray]: self._size = tuple(map(int, data.shape[:2])) return data + @property + def size(self) -> Optional[Tuple[int, int]]: + """Returns (H, W)""" + + if self._size is None: + if _HAS_PIL: + w, h = PILImage.open(self.path).size + self._size = (h, w) + else: + _ = super().size + return self._size + def save( self, fp: Union[str, io.IOBase], From bbb941a95cc895bf2f2c42e39e81e6d1e3cd9024 Mon Sep 17 00:00:00 2001 From: imyhxy Date: Mon, 5 Feb 2024 17:41:05 +0800 Subject: [PATCH 2/5] Update src/datumaro/components/media.py Co-authored-by: Vinnam Kim --- src/datumaro/components/media.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/datumaro/components/media.py b/src/datumaro/components/media.py index e0320f29f5..46f886f37b 100644 --- a/src/datumaro/components/media.py +++ b/src/datumaro/components/media.py @@ -337,14 +337,18 @@ def data(self) -> Optional[np.ndarray]: return data @property +import imagesize + +... + def size(self) -> Optional[Tuple[int, int]]: """Returns (H, W)""" if self._size is None: - if _HAS_PIL: - w, h = PILImage.open(self.path).size - self._size = (h, w) - else: + try: + width, height = imagesize.get(self.path) + self._size = (height, width) + except Exception: _ = super().size return self._size From b5a3115879321c4f187fd9d190f0b61ab78f6a79 Mon Sep 17 00:00:00 2001 From: imyhxy Date: Mon, 5 Feb 2024 17:51:17 +0800 Subject: [PATCH 3/5] Add imagesize library --- requirements.txt | 1 + src/datumaro/components/media.py | 12 ++---------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/requirements.txt b/requirements.txt index dd2ea9f2b8..11700b34d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ -r requirements-default.txt opencv-python-headless>=4.1.0.25 +imagesize>=1.4.1 diff --git a/src/datumaro/components/media.py b/src/datumaro/components/media.py index 46f886f37b..2ebe4925b4 100644 --- a/src/datumaro/components/media.py +++ b/src/datumaro/components/media.py @@ -29,6 +29,7 @@ ) import cv2 +import imagesize import numpy as np from datumaro.components.crypter import NULL_CRYPTER, Crypter @@ -49,12 +50,6 @@ pd = lazy_import("pandas") -try: - from PIL import Image as PILImage - - _HAS_PIL = True -except (ModuleNotFoundError, ImportError): - _HAS_PIL = False AnyData = TypeVar("AnyData", bytes, np.ndarray) @@ -337,16 +332,13 @@ def data(self) -> Optional[np.ndarray]: return data @property -import imagesize - -... - def size(self) -> Optional[Tuple[int, int]]: """Returns (H, W)""" if self._size is None: try: width, height = imagesize.get(self.path) + assert width != -1 and height != -1 self._size = (height, width) except Exception: _ = super().size From 6758635c4503e8d445acc633ff811f61bd379a6a Mon Sep 17 00:00:00 2001 From: imyhxy Date: Mon, 5 Feb 2024 19:48:04 +0800 Subject: [PATCH 4/5] Update requirements-core.txt --- requirements-core.txt | 1 + requirements.txt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-core.txt b/requirements-core.txt index 36b76e6692..5d788587ba 100644 --- a/requirements-core.txt +++ b/requirements-core.txt @@ -12,6 +12,7 @@ ruamel.yaml>=0.17.0 shapely>=1.7 typing_extensions>=3.7.4.3 tqdm +imagesize>=1.4.1 pycocotools>=2.0.4; platform_system != "Windows" or python_version >= '3.9' diff --git a/requirements.txt b/requirements.txt index 11700b34d9..dd2ea9f2b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,3 @@ -r requirements-default.txt opencv-python-headless>=4.1.0.25 -imagesize>=1.4.1 From 38caf4dad438e564627badfea885e7de94f4e48c Mon Sep 17 00:00:00 2001 From: imyhxy Date: Mon, 5 Feb 2024 19:51:27 +0800 Subject: [PATCH 5/5] Fix order --- requirements-core.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-core.txt b/requirements-core.txt index 5d788587ba..66bd2c4646 100644 --- a/requirements-core.txt +++ b/requirements-core.txt @@ -1,6 +1,7 @@ attrs>=21.3.0 defusedxml>=0.7.0 h5py>=2.10.0 +imagesize>=1.4.1 lxml>=4.4.1 matplotlib>=3.3.1 networkx>=2.6 @@ -12,7 +13,6 @@ ruamel.yaml>=0.17.0 shapely>=1.7 typing_extensions>=3.7.4.3 tqdm -imagesize>=1.4.1 pycocotools>=2.0.4; platform_system != "Windows" or python_version >= '3.9'