|
@@ -12,7 +12,6 @@
|
|
|
# See the License for the specific language governing permissions and
|
|
# See the License for the specific language governing permissions and
|
|
|
# limitations under the License.
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
-import os
|
|
|
|
|
import math
|
|
import math
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
@@ -21,10 +20,11 @@ from copy import deepcopy
|
|
|
import numpy as np
|
|
import numpy as np
|
|
|
import cv2
|
|
import cv2
|
|
|
|
|
|
|
|
-from .....utils.download import download
|
|
|
|
|
from .....utils.cache import CACHE_DIR
|
|
from .....utils.cache import CACHE_DIR
|
|
|
from ....utils.io import ImageReader, ImageWriter
|
|
from ....utils.io import ImageReader, ImageWriter
|
|
|
|
|
+from ...utils.mixin import BatchSizeMixin
|
|
|
from ...base import BaseComponent
|
|
from ...base import BaseComponent
|
|
|
|
|
+from ..read_data import _BaseRead
|
|
|
from . import funcs as F
|
|
from . import funcs as F
|
|
|
|
|
|
|
|
__all__ = [
|
|
__all__ = [
|
|
@@ -52,7 +52,7 @@ def _check_image_size(input_):
|
|
|
raise TypeError(f"{input_} cannot represent a valid image size.")
|
|
raise TypeError(f"{input_} cannot represent a valid image size.")
|
|
|
|
|
|
|
|
|
|
|
|
|
-class ReadImage(BaseComponent):
|
|
|
|
|
|
|
+class ReadImage(_BaseRead):
|
|
|
"""Load image from the file."""
|
|
"""Load image from the file."""
|
|
|
|
|
|
|
|
INPUT_KEYS = ["img"]
|
|
INPUT_KEYS = ["img"]
|
|
@@ -71,6 +71,7 @@ class ReadImage(BaseComponent):
|
|
|
"RGB": cv2.IMREAD_COLOR,
|
|
"RGB": cv2.IMREAD_COLOR,
|
|
|
"GRAY": cv2.IMREAD_GRAYSCALE,
|
|
"GRAY": cv2.IMREAD_GRAYSCALE,
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
SUFFIX = ["jpg", "png", "jpeg", "JPEG", "JPG", "bmp"]
|
|
SUFFIX = ["jpg", "png", "jpeg", "JPEG", "JPG", "bmp"]
|
|
|
|
|
|
|
|
def __init__(self, batch_size=1, format="BGR"):
|
|
def __init__(self, batch_size=1, format="BGR"):
|
|
@@ -81,8 +82,7 @@ class ReadImage(BaseComponent):
|
|
|
format (str, optional): Target color format to convert the image to.
|
|
format (str, optional): Target color format to convert the image to.
|
|
|
Choices are 'BGR', 'RGB', and 'GRAY'. Default: 'BGR'.
|
|
Choices are 'BGR', 'RGB', and 'GRAY'. Default: 'BGR'.
|
|
|
"""
|
|
"""
|
|
|
- super().__init__()
|
|
|
|
|
- self.batch_size = batch_size
|
|
|
|
|
|
|
+ super().__init__(batch_size)
|
|
|
self.format = format
|
|
self.format = format
|
|
|
flags = self._FLAGS_DICT[self.format]
|
|
flags = self._FLAGS_DICT[self.format]
|
|
|
self._reader = ImageReader(backend="opencv", flags=flags)
|
|
self._reader = ImageReader(backend="opencv", flags=flags)
|
|
@@ -104,11 +104,10 @@ class ReadImage(BaseComponent):
|
|
|
]
|
|
]
|
|
|
else:
|
|
else:
|
|
|
img_path = img
|
|
img_path = img
|
|
|
- # XXX: auto download for url
|
|
|
|
|
img_path = self._download_from_url(img_path)
|
|
img_path = self._download_from_url(img_path)
|
|
|
- image_list = self._get_image_list(img_path)
|
|
|
|
|
|
|
+ file_list = self._get_files_list(img_path)
|
|
|
batch = []
|
|
batch = []
|
|
|
- for img_path in image_list:
|
|
|
|
|
|
|
+ for img_path in file_list:
|
|
|
img = self._read_img(img_path)
|
|
img = self._read_img(img_path)
|
|
|
batch.append(img)
|
|
batch.append(img)
|
|
|
if len(batch) >= self.batch_size:
|
|
if len(batch) >= self.batch_size:
|
|
@@ -135,34 +134,6 @@ class ReadImage(BaseComponent):
|
|
|
"ori_img_size": deepcopy([blob.shape[1], blob.shape[0]]),
|
|
"ori_img_size": deepcopy([blob.shape[1], blob.shape[0]]),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- def _download_from_url(self, in_path):
|
|
|
|
|
- if in_path.startswith("http"):
|
|
|
|
|
- file_name = Path(in_path).name
|
|
|
|
|
- save_path = Path(CACHE_DIR) / "predict_input" / file_name
|
|
|
|
|
- download(in_path, save_path, overwrite=True)
|
|
|
|
|
- return save_path.as_posix()
|
|
|
|
|
- return in_path
|
|
|
|
|
-
|
|
|
|
|
- def _get_image_list(self, img_file):
|
|
|
|
|
- imgs_lists = []
|
|
|
|
|
- if img_file is None or not os.path.exists(img_file):
|
|
|
|
|
- raise Exception(f"Not found any img file in path: {img_file}")
|
|
|
|
|
-
|
|
|
|
|
- if os.path.isfile(img_file) and img_file.split(".")[-1] in self.SUFFIX:
|
|
|
|
|
- imgs_lists.append(img_file)
|
|
|
|
|
- elif os.path.isdir(img_file):
|
|
|
|
|
- for root, dirs, files in os.walk(img_file):
|
|
|
|
|
- for single_file in files:
|
|
|
|
|
- if single_file.split(".")[-1] in self.SUFFIX:
|
|
|
|
|
- imgs_lists.append(os.path.join(root, single_file))
|
|
|
|
|
- if len(imgs_lists) == 0:
|
|
|
|
|
- raise Exception("not found any img file in {}".format(img_file))
|
|
|
|
|
- imgs_lists = sorted(imgs_lists)
|
|
|
|
|
- return imgs_lists
|
|
|
|
|
-
|
|
|
|
|
- def set_batch_size(self, batch_size):
|
|
|
|
|
- self.batch_size = batch_size
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
class GetImageInfo(BaseComponent):
|
|
class GetImageInfo(BaseComponent):
|
|
|
"""Get Image Info"""
|
|
"""Get Image Info"""
|