predictor.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_for_data(self, data):
  50. """ get preprocess transforms """
  51. if K.IMAGE not in data and K.IM_PATH not in data:
  52. raise KeyError(
  53. f"Key {repr(K.IMAGE)} or {repr(K.IM_PATH)} is required, but not found."
  54. )
  55. pre_transforms = []
  56. if K.IMAGE not in data:
  57. pre_transforms.append(image_common.ReadImage())
  58. pre_transforms.append(
  59. T.DetResizeForTest(
  60. limit_side_len=960, limit_type="max"))
  61. pre_transforms.append(
  62. T.NormalizeImage(
  63. mean=[0.485, 0.456, 0.406],
  64. std=[0.229, 0.224, 0.225],
  65. scale=1. / 255,
  66. order='hwc'))
  67. pre_transforms.append(image_common.ToCHWImage())
  68. return pre_transforms
  69. def _get_post_transforms_for_data(self, data):
  70. """ get postprocess transforms """
  71. post_transforms = [
  72. T.DBPostProcess(
  73. thresh=0.3,
  74. box_thresh=0.6,
  75. max_candidates=1000,
  76. unclip_ratio=1.5,
  77. use_dilation=False,
  78. score_mode='fast',
  79. box_type='quad'),
  80. ]
  81. if data.get('cli_flag', False):
  82. output_dir = data.get("output_dir", "./")
  83. post_transforms.append(T.SaveTextDetResults(output_dir))
  84. return post_transforms