__init__.py 1.6 KB

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