model.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. import os
  15. from ...base.utils.arg import CLIArgument
  16. from ...base.utils.subprocess import CompletedProcess
  17. from ....utils.misc import abspath
  18. from ..text_rec.model import TextRecModel
  19. class TextDetModel(TextRecModel):
  20. """ Text Detection Model """
  21. def infer(self,
  22. model_dir: str,
  23. input_path: str,
  24. device: str='gpu',
  25. save_dir: str=None,
  26. **kwargs) -> CompletedProcess:
  27. """predict image using infernece model
  28. Args:
  29. model_dir (str): the directory path of inference model files that would use to predict.
  30. input_path (str): the path of image that would be predict.
  31. device (str, optional): the running device. Defaults to 'gpu'.
  32. save_dir (str, optional): the directory path to save output. Defaults to None.
  33. Returns:
  34. CompletedProcess: the result of infering subprocess execution.
  35. """
  36. config = self.config.copy()
  37. cli_args = []
  38. model_dir = abspath(model_dir)
  39. cli_args.append(CLIArgument('--det_model_dir', model_dir))
  40. input_path = abspath(input_path)
  41. cli_args.append(CLIArgument('--image_dir', input_path))
  42. device_type, _ = self.runner.parse_device(device)
  43. cli_args.append(CLIArgument('--use_gpu', str(device_type == 'gpu')))
  44. if save_dir is not None:
  45. save_dir = abspath(save_dir)
  46. else:
  47. # `save_dir` is None
  48. save_dir = abspath(os.path.join('output', 'infer'))
  49. cli_args.append(CLIArgument('--draw_img_save_dir', save_dir))
  50. model_type = config._get_model_type()
  51. cli_args.append(CLIArgument('--det_algorithm', model_type))
  52. self._assert_empty_kwargs(kwargs)
  53. with self._create_new_config_file() as config_path:
  54. config.dump(config_path)
  55. return self.runner.infer(config_path, cli_args, device)