predictor.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. return [
  52. image_common.ReadImage(),
  53. T.DetResizeForTest(limit_side_len=960, limit_type="max"),
  54. T.NormalizeImage(
  55. mean=[0.485, 0.456, 0.406],
  56. std=[0.229, 0.224, 0.225],
  57. scale=1.0 / 255,
  58. order="hwc",
  59. ),
  60. image_common.ToCHWImage(),
  61. ]
  62. def _get_post_transforms_from_config(self):
  63. """get postprocess transforms"""
  64. post_transforms = [
  65. T.DBPostProcess(
  66. thresh=0.3,
  67. box_thresh=0.6,
  68. max_candidates=1000,
  69. unclip_ratio=1.5,
  70. use_dilation=False,
  71. score_mode="fast",
  72. box_type="quad",
  73. )
  74. ]
  75. if not self.disable_print:
  76. post_transforms.append(T.PrintResult())
  77. if not self.disable_save:
  78. post_transforms.append(
  79. T.SaveTextDetResults(self.output),
  80. )
  81. return post_transforms