Skip to content

Commit

Permalink
Boost up Image numpy accessing speed through PIL (#2586)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wonjuleee authored Nov 7, 2023
1 parent a4abbed commit 3ec4c95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.

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

### Bug fixes

Expand Down
8 changes: 7 additions & 1 deletion src/otx/api/entities/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cv2
import imagesize
import numpy as np
from PIL import Image as PILImage

from otx.api.entities.annotation import Annotation
from otx.api.entities.media import IMedia2DEntity
Expand Down Expand Up @@ -91,7 +92,12 @@ def numpy(self) -> np.ndarray:
np.ndarray: NumPy representation of the image.
"""
if self.__data is None:
return cv2.cvtColor(cv2.imread(self.__file_path), cv2.COLOR_BGR2RGB)
try:
image = PILImage.open(self.__file_path)
image = np.asarray(image.convert("RGB"))
except ValueError:
image = cv2.cvtColor(cv2.imread(self.__file_path), cv2.COLOR_BGR2RGB)
return image
if callable(self.__data):
return self.__data()
return self.__data
Expand Down

0 comments on commit 3ec4c95

Please sign in to comment.