predictor.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. StaticInfer,
  24. )
  25. from ..base import BasicPredictor
  26. from .processors import OCRReisizeNormImg, CTCLabelDecode, ToBatch
  27. from .result import TextRecResult
  28. class TextRecPredictor(BasicPredictor):
  29. entities = MODELS
  30. _FUNC_MAP = {}
  31. register = FuncRegister(_FUNC_MAP)
  32. def __init__(self, *args, **kwargs):
  33. super().__init__(*args, **kwargs)
  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 = StaticInfer(
  51. model_dir=self.model_dir,
  52. model_prefix=self.MODEL_FILE_PREFIX,
  53. option=self.pp_option,
  54. )
  55. post_op = self.build_postprocess(**self.config["PostProcess"])
  56. return pre_tfs, infer, post_op
  57. def process(self, batch_data):
  58. batch_raw_imgs = self.pre_tfs["Read"](imgs=batch_data.instances)
  59. batch_imgs = self.pre_tfs["ReisizeNorm"](imgs=batch_raw_imgs)
  60. x = self.pre_tfs["ToBatch"](imgs=batch_imgs)
  61. batch_preds = self.infer(x=x)
  62. texts, scores = self.post_op(batch_preds)
  63. return {
  64. "input_path": batch_data.input_paths,
  65. "page_index": batch_data.page_indexes,
  66. "input_img": batch_raw_imgs,
  67. "rec_text": texts,
  68. "rec_score": scores,
  69. }
  70. @register("DecodeImage")
  71. def build_readimg(self, channel_first, img_mode):
  72. assert channel_first == False
  73. return "Read", ReadImage(format=img_mode)
  74. @register("RecResizeImg")
  75. def build_resize(self, image_shape):
  76. return "ReisizeNorm", OCRReisizeNormImg(rec_image_shape=image_shape)
  77. def build_postprocess(self, **kwargs):
  78. if kwargs.get("name") == "CTCLabelDecode":
  79. return CTCLabelDecode(
  80. character_list=kwargs.get("character_dict"),
  81. )
  82. else:
  83. raise Exception()
  84. @register("MultiLabelEncode")
  85. def foo(self, *args, **kwargs):
  86. return None, None
  87. @register("KeepKeys")
  88. def foo(self, *args, **kwargs):
  89. return None, None