model.py 2.5 KB

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