predictor.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 ..support_models import SUPPORT_MODELS
  23. class TextDetPredictor(BasePredictor):
  24. """ TextDetPredictor """
  25. support_models = SUPPORT_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(), T.DetResizeForTest(
  53. limit_side_len=960, limit_type="max"), T.NormalizeImage(
  54. mean=[0.485, 0.456, 0.406],
  55. std=[0.229, 0.224, 0.225],
  56. scale=1. / 255,
  57. order='hwc'), image_common.ToCHWImage()
  58. ]
  59. def _get_post_transforms_from_config(self):
  60. """ get postprocess transforms """
  61. post_transforms = [
  62. T.DBPostProcess(
  63. thresh=0.3,
  64. box_thresh=0.6,
  65. max_candidates=1000,
  66. unclip_ratio=1.5,
  67. use_dilation=False,
  68. score_mode='fast',
  69. box_type='quad'), T.SaveTextDetResults(self.output_dir)
  70. ]
  71. return post_transforms