predictor.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 ..model_list import MODELS
  23. class TextRecPredictor(BasePredictor):
  24. """TextRecPredictor"""
  25. entities = 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(f"Cannot find config file: {infer_cfg_file_path}")
  31. return InnerConfig(infer_cfg_file_path)
  32. @classmethod
  33. def get_input_keys(cls):
  34. """get input keys"""
  35. return [[K.IMAGE], [K.IM_PATH]]
  36. @classmethod
  37. def get_output_keys(cls):
  38. """get output keys"""
  39. return [K.REC_PROBS]
  40. def _run(self, batch_input):
  41. """run"""
  42. images = [data[K.IMAGE] for data in batch_input]
  43. input_ = np.stack(images, axis=0)
  44. if input_.ndim == 3:
  45. input_ = input_[:, np.newaxis]
  46. input_ = input_.astype(dtype=np.float32, copy=False)
  47. outputs = self._predictor.predict([input_])
  48. probs_res = outputs[0]
  49. # In-place update
  50. pred = batch_input
  51. for dict_, probs in zip(pred, probs_res):
  52. dict_[K.REC_PROBS] = probs[np.newaxis, :]
  53. return pred
  54. def _get_pre_transforms_from_config(self):
  55. """_get_pre_transforms_from_config"""
  56. if self.model_name == "LaTeX_OCR_rec":
  57. return [
  58. image_common.ReadImage(),
  59. image_common.GetImageInfo(),
  60. T.LaTeXOCRReisizeNormImg(),
  61. ]
  62. else:
  63. return [
  64. image_common.ReadImage(),
  65. image_common.GetImageInfo(),
  66. T.OCRReisizeNormImg(),
  67. ]
  68. def _get_post_transforms_from_config(self):
  69. """get postprocess transforms"""
  70. if self.model_name == "LaTeX_OCR_rec":
  71. post_transforms = [T.LaTeXOCRDecode(self.other_src.PostProcess)]
  72. else:
  73. post_transforms = [T.CTCLabelDecode(self.other_src.PostProcess)]
  74. if not self.disable_print:
  75. post_transforms.append(T.PrintResult())
  76. return post_transforms