__init__.py 1.8 KB

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