__init__.py 3.3 KB

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