transforms.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import numpy as np
  16. from PIL import Image
  17. from ....utils import logging
  18. from ...base import BaseTransform
  19. from ...base.predictor.io.writers import TSWriter
  20. from .keys import TSFCKeys as K
  21. __all__ = ["SaveTSResults"]
  22. class SaveTSResults(BaseTransform):
  23. """SaveSegResults"""
  24. def __init__(self, save_dir):
  25. super().__init__()
  26. self.save_dir = save_dir
  27. self._writer = TSWriter(backend="pandas")
  28. def apply(self, data):
  29. """apply"""
  30. pred_ts = data[K.PRED]
  31. file_name = os.path.basename(data[K.TS_PATH])
  32. ts_save_path = os.path.join(self.save_dir, file_name)
  33. self._write_ts(ts_save_path, pred_ts)
  34. return data
  35. @classmethod
  36. def get_input_keys(cls):
  37. """get input keys"""
  38. return [K.PRED]
  39. @classmethod
  40. def get_output_keys(cls):
  41. """get output keys"""
  42. return []
  43. def _write_ts(self, path, ts):
  44. """write ts"""
  45. if os.path.exists(path):
  46. logging.warning(f"{path} already exists. Overwriting it.")
  47. self._writer.write(path, ts)
  48. @staticmethod
  49. def _add_suffix(path, suffix):
  50. """add suffix"""
  51. stem, ext = os.path.splitext(path)
  52. return stem + suffix + ext
  53. @staticmethod
  54. def _replace_ext(path, new_ext):
  55. """replace ext"""
  56. stem, _ = os.path.splitext(path)
  57. return stem + new_ext