predictor.py 3.3 KB

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