text_recognition.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2025 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 .._utils.cli import (
  15. add_simple_inference_args,
  16. get_subcommand_args,
  17. perform_simple_inference,
  18. )
  19. from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
  20. class TextRecognition(PaddleXPredictorWrapper):
  21. def __init__(
  22. self,
  23. *,
  24. input_shape=None,
  25. **kwargs,
  26. ):
  27. self._extra_init_args = {
  28. "input_shape": input_shape,
  29. }
  30. super().__init__(**kwargs)
  31. @property
  32. def default_model_name(self):
  33. return "PP-OCRv5_server_rec"
  34. @classmethod
  35. def get_cli_subcommand_executor(cls):
  36. return TextRecognitionSubcommandExecutor()
  37. def _get_extra_paddlex_predictor_init_args(self):
  38. return self._extra_init_args
  39. class TextRecognitionSubcommandExecutor(PredictorCLISubcommandExecutor):
  40. @property
  41. def subparser_name(self):
  42. return "text_recognition"
  43. def _update_subparser(self, subparser):
  44. add_simple_inference_args(subparser)
  45. subparser.add_argument(
  46. "--input_shape",
  47. nargs=3,
  48. type=int,
  49. metavar=("C", "H", "W"),
  50. help="Input shape of the model.",
  51. )
  52. def execute_with_args(self, args):
  53. params = get_subcommand_args(args)
  54. perform_simple_inference(TextRecognition, params)