image_unwarping.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 ...modules.image_unwarping.model_list import MODELS
  15. from ..components import *
  16. from ..results import DocTrResult
  17. from ..utils.process_hook import batchable_method
  18. from .base import BasicPredictor
  19. class WarpPredictor(BasicPredictor):
  20. entities = MODELS
  21. def _check_args(self, kwargs):
  22. assert set(kwargs.keys()).issubset(set(["batch_size"]))
  23. return kwargs
  24. def _build_components(self):
  25. ops = {}
  26. ops["ReadImage"] = ReadImage(
  27. format="RGB", batch_size=self.kwargs.get("batch_size", 1)
  28. )
  29. ops["Normalize"] = Normalize(mean=0.0, std=1.0, scale=1.0 / 255)
  30. ops["ToCHWImage"] = ToCHWImage()
  31. predictor = ImagePredictor(
  32. model_dir=self.model_dir,
  33. model_prefix=self.MODEL_FILE_PREFIX,
  34. option=self.pp_option,
  35. )
  36. ops["predictor"] = predictor
  37. ops["postprocess"] = DocTrPostProcess()
  38. return ops
  39. @batchable_method
  40. def _pack_res(self, single):
  41. keys = ["img_path", "doctr_img"]
  42. return DocTrResult({key: single[key] for key in keys})