text_recognition.py 2.9 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 numpy as np
  15. from ...utils.func_register import FuncRegister
  16. from ...modules.text_recognition.model_list import MODELS
  17. from ..components import *
  18. from ..results import TextRecResult
  19. from ..utils.process_hook import batchable_method
  20. from .base import BasePredictor
  21. class TextRecPredictor(BasePredictor):
  22. entities = MODELS
  23. INPUT_KEYS = "x"
  24. OUTPUT_KEYS = "text_rec_res"
  25. DEAULT_INPUTS = {"x": "x"}
  26. DEAULT_OUTPUTS = {"text_rec_res": "text_rec_res"}
  27. _FUNC_MAP = {}
  28. register = FuncRegister(_FUNC_MAP)
  29. def _build_components(self):
  30. ops = {}
  31. for cfg in self.config["PreProcess"]["transform_ops"]:
  32. tf_key = list(cfg.keys())[0]
  33. assert tf_key in self._FUNC_MAP
  34. func = self._FUNC_MAP.get(tf_key)
  35. args = cfg.get(tf_key, {})
  36. op = func(self, **args) if args else func(self)
  37. if op:
  38. ops[tf_key] = op
  39. kernel_option = PaddlePredictorOption()
  40. kernel_option.set_device(self.device)
  41. predictor = ImagePredictor(
  42. model_dir=self.model_dir,
  43. model_prefix=self.MODEL_FILE_PREFIX,
  44. option=kernel_option,
  45. )
  46. ops["predictor"] = predictor
  47. key, op = self.build_postprocess(**self.config["PostProcess"])
  48. ops[key] = op
  49. return ops
  50. @register("DecodeImage")
  51. def build_readimg(self, channel_first, img_mode):
  52. assert channel_first == False
  53. return ReadImage(format=img_mode, batch_size=self.kwargs.get("batch_size", 1))
  54. @register("RecResizeImg")
  55. def build_resize(self, image_shape):
  56. return OCRReisizeNormImg(rec_image_shape=image_shape)
  57. def build_postprocess(self, **kwargs):
  58. if kwargs.get("name") == "CTCLabelDecode":
  59. return "CTCLabelDecode", CTCLabelDecode(
  60. character_list=kwargs.get("character_dict"),
  61. )
  62. else:
  63. raise Exception()
  64. @register("MultiLabelEncode")
  65. def foo(self, *args, **kwargs):
  66. return None
  67. @register("KeepKeys")
  68. def foo(self, *args, **kwargs):
  69. return None
  70. @batchable_method
  71. def _pack_res(self, data):
  72. keys = ["img_path", "rec_text", "rec_score"]
  73. return {"text_rec_res": TextRecResult({key: data[key] for key in keys})}