read_data.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. from pathlib import Path
  16. from ....utils.download import download
  17. from ....utils.cache import CACHE_DIR
  18. from ..utils.mixin import BatchSizeMixin
  19. from ..base import BaseComponent
  20. class _BaseRead(BaseComponent, BatchSizeMixin):
  21. """Load image from the file."""
  22. SUFFIX = []
  23. def __init__(self, batch_size=1):
  24. super().__init__()
  25. BatchSizeMixin.__init__(self, batch_size)
  26. # XXX: auto download for url
  27. def _download_from_url(self, in_path):
  28. if in_path.startswith("http"):
  29. file_name = Path(in_path).name
  30. save_path = Path(CACHE_DIR) / "predict_input" / file_name
  31. download(in_path, save_path, overwrite=True)
  32. return save_path.as_posix()
  33. return in_path
  34. def _get_files_list(self, fp):
  35. file_list = []
  36. if fp is None or not os.path.exists(fp):
  37. raise Exception(f"Not found any img file in path: {fp}")
  38. if os.path.isfile(fp) and fp.split(".")[-1] in self.SUFFIX:
  39. file_list.append(fp)
  40. elif os.path.isdir(fp):
  41. for root, dirs, files in os.walk(fp):
  42. for single_file in files:
  43. if single_file.split(".")[-1] in self.SUFFIX:
  44. file_list.append(os.path.join(root, single_file))
  45. if len(file_list) == 0:
  46. raise Exception("Not found any file in {}".format(fp))
  47. file_list = sorted(file_list)
  48. return file_list