predictor.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. from ....utils.func_register import FuncRegister
  15. from ....modules.text_recognition.model_list import MODELS
  16. from ...common.batch_sampler import ImageBatchSampler
  17. from ...common.reader import ReadImage
  18. from ..common import (
  19. Resize,
  20. ResizeByShort,
  21. Normalize,
  22. ToCHWImage,
  23. )
  24. from ..base import BasePredictor
  25. from .processors import OCRReisizeNormImg, CTCLabelDecode, ToBatch
  26. from .result import TextRecResult
  27. class TextRecPredictor(BasePredictor):
  28. entities = MODELS
  29. _FUNC_MAP = {}
  30. register = FuncRegister(_FUNC_MAP)
  31. def __init__(self, *args, input_shape=None, **kwargs):
  32. super().__init__(*args, **kwargs)
  33. self.input_shape = input_shape
  34. self.pre_tfs, self.infer, self.post_op = self._build()
  35. def _build_batch_sampler(self):
  36. return ImageBatchSampler()
  37. def _get_result_class(self):
  38. return TextRecResult
  39. def _build(self):
  40. pre_tfs = {"Read": ReadImage(format="RGB")}
  41. for cfg in self.config["PreProcess"]["transform_ops"]:
  42. tf_key = list(cfg.keys())[0]
  43. assert tf_key in self._FUNC_MAP
  44. func = self._FUNC_MAP[tf_key]
  45. args = cfg.get(tf_key, {})
  46. name, op = func(self, **args) if args else func(self)
  47. if op:
  48. pre_tfs[name] = op
  49. pre_tfs["ToBatch"] = ToBatch()
  50. infer = self.create_static_infer()
  51. post_op = self.build_postprocess(**self.config["PostProcess"])
  52. return pre_tfs, infer, post_op
  53. def process(self, batch_data):
  54. batch_raw_imgs = self.pre_tfs["Read"](imgs=batch_data.instances)
  55. batch_imgs = self.pre_tfs["ReisizeNorm"](imgs=batch_raw_imgs)
  56. x = self.pre_tfs["ToBatch"](imgs=batch_imgs)
  57. batch_preds = self.infer(x=x)
  58. texts, scores = self.post_op(batch_preds)
  59. return {
  60. "input_path": batch_data.input_paths,
  61. "page_index": batch_data.page_indexes,
  62. "input_img": batch_raw_imgs,
  63. "rec_text": texts,
  64. "rec_score": scores,
  65. }
  66. @register("DecodeImage")
  67. def build_readimg(self, channel_first, img_mode):
  68. assert channel_first == False
  69. return "Read", ReadImage(format=img_mode)
  70. @register("RecResizeImg")
  71. def build_resize(self, image_shape):
  72. return "ReisizeNorm", OCRReisizeNormImg(
  73. rec_image_shape=image_shape, input_shape=self.input_shape
  74. )
  75. def build_postprocess(self, **kwargs):
  76. if kwargs.get("name") == "CTCLabelDecode":
  77. return CTCLabelDecode(
  78. character_list=kwargs.get("character_dict"),
  79. )
  80. else:
  81. raise Exception()
  82. @register("MultiLabelEncode")
  83. def foo(self, *args, **kwargs):
  84. return None, None
  85. @register("KeepKeys")
  86. def foo(self, *args, **kwargs):
  87. return None, None