__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 pathlib import Path
  15. from typing import Any, Dict, Optional
  16. from ..utils.official_models import official_models
  17. from .base import BasePredictor, BasicPredictor
  18. from .image_classification import ClasPredictor
  19. from .text_detection import TextDetPredictor
  20. from .text_recognition import TextRecPredictor
  21. from .table_recognition import TablePredictor
  22. from .object_detection import DetPredictor
  23. from .instance_segmentation import InstanceSegPredictor
  24. from .semantic_segmentation import SegPredictor
  25. from .general_recognition import ShiTuRecPredictor
  26. from .ts_fc import TSFcPredictor
  27. from .ts_ad import TSAdPredictor
  28. from .ts_cls import TSClsPredictor
  29. from .image_unwarping import WarpPredictor
  30. from .multilabel_classification import MLClasPredictor
  31. from .anomaly_detection import UadPredictor
  32. from .formula_recognition import LaTeXOCRPredictor
  33. def _create_hp_predictor(
  34. model_name, model_dir, device, config, hpi_params, *args, **kwargs
  35. ):
  36. try:
  37. from paddlex_hpi.predictors import HPPredictor
  38. except ModuleNotFoundError as e:
  39. raise RuntimeError(
  40. "The PaddleX HPI plugin is not properly installed, and the high-performance model inference features are not available."
  41. )
  42. if hpi_params is None:
  43. raise ValueError("No HPI params given")
  44. if "serial_number" not in hpi_params:
  45. raise ValueError("The serial number is required but was not provided.")
  46. serial_number = hpi_params["serial_number"]
  47. update_license = hpi_params.get("update_license", False)
  48. return HPPredictor.get(model_name)(
  49. model_dir=model_dir,
  50. config=config,
  51. device=device,
  52. serial_number=serial_number,
  53. update_license=update_license,
  54. *args,
  55. **kwargs,
  56. )
  57. def create_predictor(
  58. model: str,
  59. device=None,
  60. pp_option=None,
  61. use_hpip: bool = False,
  62. hpi_params: Optional[Dict[str, Any]] = None,
  63. *args,
  64. **kwargs,
  65. ) -> BasePredictor:
  66. model_dir = check_model(model)
  67. config = BasePredictor.load_config(model_dir)
  68. model_name = config["Global"]["model_name"]
  69. if use_hpip:
  70. return _create_hp_predictor(
  71. model_name=model_name,
  72. model_dir=model_dir,
  73. config=config,
  74. hpi_params=hpi_params,
  75. device=device,
  76. *args,
  77. **kwargs,
  78. )
  79. else:
  80. return BasicPredictor.get(model_name)(
  81. model_dir=model_dir,
  82. config=config,
  83. device=device,
  84. pp_option=pp_option,
  85. *args,
  86. **kwargs,
  87. )
  88. def check_model(model):
  89. if Path(model).exists():
  90. return Path(model)
  91. elif model in official_models:
  92. return official_models[model]
  93. else:
  94. raise Exception(
  95. f"The model ({model}) is no exists! Please using directory of local model files or model name supported by PaddleX!"
  96. )