Skip to content

Commit 3ec4c95

Browse files
authored
Boost up Image numpy accessing speed through PIL (#2586)
* boost up numpy accessing speed through PIL * update CHANGELOG * resolve precommit error * resolve precommit error * add fallback logic with PIL open * use convert instead of draft
1 parent a4abbed commit 3ec4c95

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
- Update ModelAPI configuration(<https://github.com/openvinotoolkit/training_extensions/pull/2564>)
1010
- Add Anomaly modelAPI changes (<https://github.com/openvinotoolkit/training_extensions/pull/2563>)
11+
- Update Image numpy access (<https://github.com/openvinotoolkit/training_extensions/pull/2586>)
1112

1213
### Bug fixes
1314

src/otx/api/entities/image.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import cv2
1111
import imagesize
1212
import numpy as np
13+
from PIL import Image as PILImage
1314

1415
from otx.api.entities.annotation import Annotation
1516
from otx.api.entities.media import IMedia2DEntity
@@ -91,7 +92,12 @@ def numpy(self) -> np.ndarray:
9192
np.ndarray: NumPy representation of the image.
9293
"""
9394
if self.__data is None:
94-
return cv2.cvtColor(cv2.imread(self.__file_path), cv2.COLOR_BGR2RGB)
95+
try:
96+
image = PILImage.open(self.__file_path)
97+
image = np.asarray(image.convert("RGB"))
98+
except ValueError:
99+
image = cv2.cvtColor(cv2.imread(self.__file_path), cv2.COLOR_BGR2RGB)
100+
return image
95101
if callable(self.__data):
96102
return self.__data()
97103
return self.__data

0 commit comments

Comments
 (0)