__init__.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 .base import BasePredictor, BasicPredictor
  16. from .image_classification import ClasPredictor
  17. from .text_detection import TextDetPredictor
  18. from .text_recognition import TextRecPredictor
  19. from .table_recognition import TablePredictor
  20. from .object_detection import DetPredictor
  21. from .instance_segmentation import InstanceSegPredictor
  22. from .semantic_segmentation import SegPredictor
  23. from .general_recognition import ShiTuRecPredictor
  24. from .official_models import official_models
  25. def create_predictor(model: str, device: str = None, *args, **kwargs) -> BasePredictor:
  26. model_dir = check_model(model)
  27. config = BasePredictor.load_config(model_dir)
  28. model_name = config["Global"]["model_name"]
  29. return BasicPredictor.get(model_name)(
  30. model_dir=model_dir,
  31. config=config,
  32. device=device,
  33. *args,
  34. **kwargs,
  35. )
  36. def check_model(model):
  37. if Path(model).exists():
  38. return Path(model)
  39. elif model in official_models:
  40. return official_models[model]
  41. else:
  42. raise Exception(
  43. f"The model ({model}) is no exists! Please using directory of local model files or model name supported by PaddleX!"
  44. )