model.py 2.5 KB

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