predictor.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 typing import Any, Dict, List, Tuple, Union
  15. import numpy as np
  16. from ....modules.image_unwarping.model_list import MODELS
  17. from ...common.batch_sampler import ImageBatchSampler
  18. from ...common.reader import ReadImage
  19. from ..base import BasePredictor
  20. from ..common import Normalize, ToBatch, ToCHWImage
  21. from .processors import DocTrPostProcess
  22. from .result import DocTrResult
  23. class WarpPredictor(BasePredictor):
  24. """WarpPredictor that inherits from BasePredictor."""
  25. entities = MODELS
  26. def __init__(self, *args: List, **kwargs: Dict) -> None:
  27. """Initializes WarpPredictor.
  28. Args:
  29. *args: Arbitrary positional arguments passed to the superclass.
  30. **kwargs: Arbitrary keyword arguments passed to the superclass.
  31. """
  32. super().__init__(*args, **kwargs)
  33. self.preprocessors, self.infer, self.postprocessors = self._build()
  34. def _build_batch_sampler(self) -> ImageBatchSampler:
  35. """Builds and returns an ImageBatchSampler instance.
  36. Returns:
  37. ImageBatchSampler: An instance of ImageBatchSampler.
  38. """
  39. return ImageBatchSampler()
  40. def _get_result_class(self) -> type:
  41. """Returns the warpping result, DocTrResult.
  42. Returns:
  43. type: The DocTrResult.
  44. """
  45. return DocTrResult
  46. def _build(self) -> Tuple:
  47. """Build the preprocessors, inference engine, and postprocessors based on the configuration.
  48. Returns:
  49. tuple: A tuple containing the preprocessors, inference engine, and postprocessors.
  50. """
  51. preprocessors = {"Read": ReadImage(format="BGR")}
  52. preprocessors["Normalize"] = Normalize(mean=0.0, std=1.0, scale=1.0 / 255)
  53. preprocessors["ToCHW"] = ToCHWImage()
  54. preprocessors["ToBatch"] = ToBatch()
  55. infer = self.create_static_infer()
  56. postprocessors = {"DocTrPostProcess": DocTrPostProcess()}
  57. return preprocessors, infer, postprocessors
  58. def process(self, batch_data: List[Union[str, np.ndarray]]) -> Dict[str, Any]:
  59. """
  60. Process a batch of data through the preprocessing, inference, and postprocessing.
  61. Args:
  62. batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).
  63. Returns:
  64. dict: A dictionary containing the input path, raw image, class IDs, scores, and label names for every instance of the batch. Keys include 'input_path', 'input_img', 'class_ids', 'scores', and 'label_names'.
  65. """
  66. batch_raw_imgs = self.preprocessors["Read"](imgs=batch_data.instances)
  67. batch_imgs = self.preprocessors["Normalize"](imgs=batch_raw_imgs)
  68. batch_imgs = self.preprocessors["ToCHW"](imgs=batch_imgs)
  69. x = self.preprocessors["ToBatch"](imgs=batch_imgs)
  70. batch_preds = self.infer(x=x)
  71. batch_warp_preds = self.postprocessors["DocTrPostProcess"](batch_preds)
  72. return {
  73. "input_path": batch_data.input_paths,
  74. "page_index": batch_data.page_indexes,
  75. "input_img": batch_raw_imgs,
  76. "doctr_img": batch_warp_preds,
  77. }