__init__.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 importlib import import_module
  15. from pathlib import Path
  16. from typing import Any, Dict, Optional, Union
  17. from ...utils import errors
  18. from ..utils.hpi import HPIConfig
  19. from ..utils.official_models import official_models
  20. # from .table_recognition import TablePredictor
  21. # from .general_recognition import ShiTuRecPredictor
  22. from .anomaly_detection import UadPredictor
  23. from .base import BasePredictor
  24. from .doc_vlm import DocVLMPredictor
  25. from .face_feature import FaceFeaturePredictor
  26. from .formula_recognition import FormulaRecPredictor
  27. from .image_classification import ClasPredictor
  28. from .image_feature import ImageFeaturePredictor
  29. from .image_multilabel_classification import MLClasPredictor
  30. from .image_unwarping import WarpPredictor
  31. from .instance_segmentation import InstanceSegPredictor
  32. from .keypoint_detection import KptPredictor
  33. from .m_3d_bev_detection import BEVDet3DPredictor
  34. # from .face_recognition import FaceRecPredictor
  35. from .multilingual_speech_recognition import WhisperPredictor
  36. from .object_detection import DetPredictor
  37. from .open_vocabulary_detection import OVDetPredictor
  38. from .open_vocabulary_segmentation import OVSegPredictor
  39. from .semantic_segmentation import SegPredictor
  40. from .table_structure_recognition import TablePredictor
  41. from .text_detection import TextDetPredictor
  42. from .text_recognition import TextRecPredictor
  43. from .ts_anomaly_detection import TSAdPredictor
  44. from .ts_classification import TSClsPredictor
  45. from .ts_forecasting import TSFcPredictor
  46. from .video_classification import VideoClasPredictor
  47. from .video_detection import VideoDetPredictor
  48. def create_predictor(
  49. model_name: str,
  50. model_dir: Optional[str] = None,
  51. device=None,
  52. pp_option=None,
  53. use_hpip: bool = False,
  54. hpi_config: Optional[Union[Dict[str, Any], HPIConfig]] = None,
  55. *args,
  56. **kwargs,
  57. ) -> BasePredictor:
  58. if model_dir is None:
  59. model_dir = check_model(model_name)
  60. else:
  61. assert Path(model_dir).exists(), f"{model_dir} is not exists!"
  62. model_dir = Path(model_dir)
  63. config = BasePredictor.load_config(model_dir)
  64. assert (
  65. model_name == config["Global"]["model_name"]
  66. ), f"Model name mismatch,please input the correct model dir."
  67. return BasePredictor.get(model_name)(
  68. model_dir=model_dir,
  69. config=config,
  70. device=device,
  71. pp_option=pp_option,
  72. use_hpip=use_hpip,
  73. hpi_config=hpi_config,
  74. *args,
  75. **kwargs,
  76. )
  77. def check_model(model):
  78. if Path(model).exists():
  79. return Path(model)
  80. elif model in official_models:
  81. return official_models[model]
  82. else:
  83. raise Exception(
  84. f"The model ({model}) is no exists! Please using directory of local model files or model name supported by PaddleX!"
  85. )