text_detection.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 .base import BasicPredictor
  20. class TextDetPredictor(BasicPredictor):
  21. entities = MODELS
  22. _FUNC_MAP = {}
  23. register = FuncRegister(_FUNC_MAP)
  24. def _build_components(self):
  25. for cfg in self.config["PreProcess"]["transform_ops"]:
  26. tf_key = list(cfg.keys())[0]
  27. func = self._FUNC_MAP[tf_key]
  28. args = cfg.get(tf_key, {})
  29. op = func(self, **args) if args else func(self)
  30. if op:
  31. self._add_component(op)
  32. predictor = ImagePredictor(
  33. model_dir=self.model_dir,
  34. model_prefix=self.MODEL_FILE_PREFIX,
  35. option=self.pp_option,
  36. )
  37. self._add_component(predictor)
  38. op = self.build_postprocess(**self.config["PostProcess"])
  39. self._add_component(op)
  40. @register("DecodeImage")
  41. def build_readimg(self, channel_first, img_mode):
  42. assert channel_first == False
  43. return ReadImage(format=img_mode)
  44. @register("DetResizeForTest")
  45. def build_resize(self, **kwargs):
  46. # TODO: align to PaddleOCR
  47. if self.model_name in ("PP-OCRv4_server_det", "PP-OCRv4_mobile_det"):
  48. resize_long = kwargs.get("resize_long", 960)
  49. return DetResizeForTest(limit_side_len=resize_long, limit_type="max")
  50. return DetResizeForTest(**kwargs)
  51. @register("NormalizeImage")
  52. def build_normalize(
  53. self,
  54. mean=[0.485, 0.456, 0.406],
  55. std=[0.229, 0.224, 0.225],
  56. scale=1 / 255,
  57. order="",
  58. channel_num=3,
  59. ):
  60. return NormalizeImage(
  61. mean=mean, std=std, scale=scale, order=order, channel_num=channel_num
  62. )
  63. @register("ToCHWImage")
  64. def build_to_chw(self):
  65. return ToCHWImage()
  66. def build_postprocess(self, **kwargs):
  67. if kwargs.get("name") == "DBPostProcess":
  68. return DBPostProcess(
  69. thresh=kwargs.get("thresh", 0.3),
  70. box_thresh=kwargs.get("box_thresh", 0.7),
  71. max_candidates=kwargs.get("max_candidates", 1000),
  72. unclip_ratio=kwargs.get("unclip_ratio", 2.0),
  73. use_dilation=kwargs.get("use_dilation", False),
  74. score_mode=kwargs.get("score_mode", "fast"),
  75. box_type=kwargs.get("box_type", "quad"),
  76. )
  77. else:
  78. raise Exception()
  79. @register("DetLabelEncode")
  80. def foo(self, *args, **kwargs):
  81. return None
  82. @register("KeepKeys")
  83. def foo(self, *args, **kwargs):
  84. return None
  85. def _pack_res(self, single):
  86. keys = ["input_path", "dt_polys", "dt_scores"]
  87. return TextDetResult({key: single[key] for key in keys})