predictor.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 ....utils import logging
  17. from ...base.predictor.transforms import image_common
  18. from ...base import BasePredictor
  19. from .keys import TextRecKeys as K
  20. from . import transforms as T
  21. from .utils import InnerConfig
  22. from ..support_models import SUPPORT_MODELS
  23. class TextRecPredictor(BasePredictor):
  24. """ TextRecPredictor """
  25. support_models = SUPPORT_MODELS
  26. def load_other_src(self):
  27. """ load the inner config file """
  28. infer_cfg_file_path = os.path.join(self.model_dir, 'inference.yml')
  29. if not os.path.exists(infer_cfg_file_path):
  30. raise FileNotFoundError(
  31. f"Cannot find config file: {infer_cfg_file_path}")
  32. return InnerConfig(infer_cfg_file_path)
  33. @classmethod
  34. def get_input_keys(cls):
  35. """ get input keys """
  36. return [[K.IMAGE], [K.IM_PATH]]
  37. @classmethod
  38. def get_output_keys(cls):
  39. """ get output keys """
  40. return [K.REC_PROBS]
  41. def _run(self, batch_input):
  42. """ run """
  43. images = [data[K.IMAGE] for data in batch_input]
  44. input_ = np.stack(images, axis=0)
  45. if input_.ndim == 3:
  46. input_ = input_[:, np.newaxis]
  47. input_ = input_.astype(dtype=np.float32, copy=False)
  48. outputs = self._predictor.predict([input_])
  49. probs_res = outputs[0]
  50. # In-place update
  51. pred = batch_input
  52. for dict_, probs in zip(pred, probs_res):
  53. dict_[K.REC_PROBS] = probs[np.newaxis, :]
  54. return pred
  55. def _get_pre_transforms_for_data(self, data):
  56. """ _get_pre_transforms_for_data """
  57. if K.IMAGE not in data and K.IM_PATH not in data:
  58. raise KeyError(
  59. f"Key {repr(K.IMAGE)} or {repr(K.IM_PATH)} is required, but not found."
  60. )
  61. pre_transforms = []
  62. if K.IMAGE not in data:
  63. pre_transforms.append(image_common.ReadImage())
  64. else:
  65. pre_transforms.append(image_common.GetImageInfo())
  66. pre_transforms.append(T.OCRReisizeNormImg())
  67. return pre_transforms
  68. def _get_post_transforms_for_data(self, data):
  69. """ get postprocess transforms """
  70. post_transforms = [T.CTCLabelDecode(self.other_src.PostProcess)]
  71. if data.get('cli_flag', False):
  72. output_dir = data.get("output_dir", "./")
  73. post_transforms.append(T.PrintResult())
  74. return post_transforms