utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 codecs
  15. import yaml
  16. from ...base.predictor.transforms import image_common
  17. class InnerConfig(object):
  18. """Inner Config
  19. """
  20. def __init__(self, config_path):
  21. self.inner_cfg = self.load(config_path)
  22. def load(self, config_path):
  23. """ load infer config """
  24. with codecs.open(config_path, 'r', 'utf-8') as file:
  25. dic = yaml.load(file, Loader=yaml.FullLoader)
  26. return dic
  27. @property
  28. def pre_transforms(self):
  29. """ read preprocess transforms from config file """
  30. if "RecPreProcess" in list(self.inner_cfg.keys()):
  31. tfs_cfg = self.inner_cfg['RecPreProcess']['transform_ops']
  32. else:
  33. tfs_cfg = self.inner_cfg['PreProcess']['transform_ops']
  34. tfs = []
  35. for cfg in tfs_cfg:
  36. tf_key = list(cfg.keys())[0]
  37. if tf_key == 'NormalizeImage':
  38. tf = image_common.Normalize(
  39. mean=cfg['NormalizeImage'].get("mean",
  40. [0.485, 0.456, 0.406]),
  41. std=cfg['NormalizeImage'].get("std", [0.229, 0.224, 0.225]))
  42. elif tf_key == 'ResizeImage':
  43. if "resize_short" in list(cfg[tf_key].keys()):
  44. tf = image_common.ResizeByShort(
  45. target_short_edge=cfg['ResizeImage'].get("resize_short",
  46. (224, 224)),
  47. size_divisor=None,
  48. interp='LINEAR')
  49. else:
  50. tf = image_common.Resize(
  51. target_size=cfg['ResizeImage'].get('size', (224, 224)))
  52. elif tf_key == "CropImage":
  53. tf = image_common.Crop(crop_size=cfg["CropImage"].get('size',
  54. 224))
  55. elif tf_key == "ToCHWImage":
  56. tf = image_common.ToCHWImage()
  57. else:
  58. raise RuntimeError(f"Unsupported type: {tf_key}")
  59. tfs.append(tf)
  60. return tfs
  61. @property
  62. def labels(self):
  63. """ the labels in inner config """
  64. return self.inner_cfg["PostProcess"]["Topk"]["label_list"]