predictor.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. from operator import le
  15. import os
  16. import numpy as np
  17. from . import transforms as T
  18. from ....utils import logging
  19. from ...base import BasePredictor
  20. from ...base.predictor.transforms import image_common
  21. from .keys import TextDetKeys as K
  22. from ..model_list import MODELS
  23. class TextDetPredictor(BasePredictor):
  24. """TextDetPredictor"""
  25. entities = MODELS
  26. @classmethod
  27. def get_input_keys(cls):
  28. """get input keys"""
  29. return [[K.IMAGE], [K.IM_PATH]]
  30. @classmethod
  31. def get_output_keys(cls):
  32. """get output keys"""
  33. return [K.PROB_MAP, K.SHAPE]
  34. def _run(self, batch_input):
  35. """_run"""
  36. if len(batch_input) != 1:
  37. raise ValueError(
  38. f"For `{self.__class__.__name__}`, batch size can only be set to 1."
  39. )
  40. images = [data[K.IMAGE] for data in batch_input]
  41. input_ = np.stack(images, axis=0)
  42. if input_.ndim == 3:
  43. input_ = input_[:, np.newaxis]
  44. input_ = input_.astype(dtype=np.float32, copy=False)
  45. outputs = self._predictor.predict([input_])
  46. pred = batch_input
  47. pred[0][K.PROB_MAP] = outputs
  48. return pred
  49. def _get_pre_transforms_from_config(self):
  50. """get preprocess transforms"""
  51. if self.model_name in ['PP-OCRv4_server_seal_det', 'PP-OCRv4_mobile_seal_det']:
  52. limit_side_len = 736
  53. else:
  54. limit_side_len = 960
  55. return [
  56. image_common.ReadImage(),
  57. T.DetResizeForTest(limit_side_len=limit_side_len, limit_type="max"),
  58. T.NormalizeImage(
  59. mean=[0.485, 0.456, 0.406],
  60. std=[0.229, 0.224, 0.225],
  61. scale=1.0 / 255,
  62. order="hwc",
  63. ),
  64. image_common.ToCHWImage(),
  65. ]
  66. def _get_post_transforms_from_config(self):
  67. """get postprocess transforms"""
  68. if self.model_name in ['PP-OCRv4_server_seal_det', 'PP-OCRv4_mobile_seal_det']:
  69. task = 'poly'
  70. post_transforms = [
  71. T.DBPostProcess(
  72. thresh=0.2,
  73. box_thresh=0.6,
  74. max_candidates=1000,
  75. unclip_ratio=1.5,
  76. use_dilation=False,
  77. score_mode="fast",
  78. box_type="poly",
  79. )
  80. ]
  81. else:
  82. task = 'quad'
  83. post_transforms = [
  84. T.DBPostProcess(
  85. thresh=0.3,
  86. box_thresh=0.6,
  87. max_candidates=1000,
  88. unclip_ratio=1.5,
  89. use_dilation=False,
  90. score_mode="fast",
  91. box_type="quad",
  92. )
  93. ]
  94. if not self.disable_print:
  95. post_transforms.append(T.PrintResult())
  96. if not self.disable_save:
  97. post_transforms.append(
  98. T.SaveTextDetResults(self.output, task),
  99. )
  100. return post_transforms