text_detection.py 3.6 KB

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