text_detection.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. import numpy as np
  15. from ...utils.func_register import FuncRegister
  16. from ...modules.text_detection.model_list import MODELS
  17. from ..components import *
  18. from .base import BasePredictor
  19. class TextDetPredictor(BasePredictor):
  20. entities = MODELS
  21. INPUT_KEYS = "x"
  22. OUTPUT_KEYS = "text_det_res"
  23. DEAULT_INPUTS = {"x": "x"}
  24. DEAULT_OUTPUTS = {"text_det_res": "text_det_res"}
  25. _FUNC_MAP = {}
  26. register = FuncRegister(_FUNC_MAP)
  27. def _build_components(self):
  28. ops = {}
  29. for cfg in self.config["PreProcess"]["transform_ops"]:
  30. tf_key = list(cfg.keys())[0]
  31. func = self._FUNC_MAP.get(tf_key)
  32. args = cfg.get(tf_key, {})
  33. op = func(self, **args) if args else func(self)
  34. if op:
  35. ops[tf_key] = op
  36. kernel_option = PaddlePredictorOption()
  37. # kernel_option.set_device(self.device)
  38. predictor = ImagePredictor(
  39. model_dir=self.model_dir,
  40. model_prefix=self.MODEL_FILE_PREFIX,
  41. option=kernel_option,
  42. )
  43. predictor.set_inputs({"imgs": "img"})
  44. ops["predictor"] = predictor
  45. key, op = self.build_postprocess(**self.config["PostProcess"])
  46. ops[key] = op
  47. return ops
  48. @register("DecodeImage")
  49. def build_readimg(self, channel_first, img_mode):
  50. assert channel_first == False
  51. return ReadImage(format=img_mode, batch_size=self.kwargs.get("batch_size", 1))
  52. @register("DetResizeForTest")
  53. def build_resize(self, resize_long=960):
  54. return DetResizeForTest(limit_side_len=resize_long, limit_type="max")
  55. @register("NormalizeImage")
  56. def build_normalize(
  57. self,
  58. mean=[0.485, 0.456, 0.406],
  59. std=[0.229, 0.224, 0.225],
  60. scale=1 / 255,
  61. order="",
  62. channel_num=3,
  63. ):
  64. return NormalizeImage(
  65. mean=mean, std=std, scale=scale, order=order, channel_num=channel_num
  66. )
  67. @register("ToCHWImage")
  68. def build_to_chw(self):
  69. return ToCHWImage()
  70. def build_postprocess(self, **kwargs):
  71. if kwargs.get("name") == "DBPostProcess":
  72. return "DBPostProcess", DBPostProcess(
  73. thresh=kwargs.get("thresh", 0.3),
  74. box_thresh=kwargs.get("box_thresh", 0.7),
  75. max_candidates=kwargs.get("max_candidates", 1000),
  76. unclip_ratio=kwargs.get("unclip_ratio", 2.0),
  77. use_dilation=kwargs.get("use_dilation", False),
  78. score_mode=kwargs.get("score_mode", "fast"),
  79. box_type=kwargs.get("box_type", "quad"),
  80. )
  81. else:
  82. raise Exception()
  83. @register("DetLabelEncode")
  84. def foo(self, *args, **kwargs):
  85. return None
  86. @register("KeepKeys")
  87. def foo(self, *args, **kwargs):
  88. return None