__init__.py 3.5 KB

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