text_recognition.py 3.0 KB

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