__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 typing import Any, Dict, Optional
  16. from ...utils.config import parse_config
  17. from ..utils.get_pipeline_path import get_pipeline_path
  18. from .base import BasePipeline
  19. from .single_model_pipeline import (
  20. _SingleModelPipeline,
  21. ImageClassification,
  22. ObjectDetection,
  23. InstanceSegmentation,
  24. SemanticSegmentation,
  25. TSFc,
  26. TSAd,
  27. TSCls,
  28. MultiLableImageClas,
  29. SmallObjDet,
  30. AnomalyDetection,
  31. )
  32. from .ocr import OCRPipeline
  33. from .table_recognition import TableRecPipeline
  34. from .ppchatocrv3 import PPChatOCRPipeline
  35. def load_pipeline_config(pipeline: str) -> Dict[str, Any]:
  36. if not Path(pipeline).exists():
  37. pipeline_path = get_pipeline_path(pipeline)
  38. if pipeline_path is None:
  39. raise Exception(
  40. f"The pipeline ({pipeline}) does not exist! Please use a pipeline name or a config file path!"
  41. )
  42. else:
  43. pipeline_path = pipeline
  44. config = parse_config(pipeline_path)
  45. return config
  46. def create_pipeline_from_config(
  47. config: Dict[str, Any],
  48. device=None,
  49. pp_option=None,
  50. use_hpip: bool = False,
  51. hpi_params: Optional[Dict[str, Any]] = None,
  52. *args,
  53. **kwargs,
  54. ) -> BasePipeline:
  55. pipeline_name = config["Global"]["pipeline_name"]
  56. pipeline_setting = config["Pipeline"]
  57. predictor_kwargs = {"use_hpip": use_hpip}
  58. if "use_hpip" in pipeline_setting:
  59. predictor_kwargs["use_hpip"] = use_hpip
  60. if hpi_params is not None:
  61. predictor_kwargs["hpi_params"] = hpi_params
  62. pipeline_setting.pop("hpi_params")
  63. elif "hpi_params" in pipeline_setting:
  64. predictor_kwargs["hpi_params"] = pipeline_setting.pop("hpi_params")
  65. if device is not None:
  66. predictor_kwargs["device"] = device
  67. pipeline_setting.pop("device")
  68. elif "device" in pipeline_setting:
  69. predictor_kwargs["device"] = pipeline_setting.pop("device")
  70. if pp_option is not None:
  71. predictor_kwargs["pp_option"] = pp_option
  72. pipeline_setting.pop("pp_option")
  73. elif "pp_option" in pipeline_setting:
  74. predictor_kwargs["pp_option"] = pipeline_setting.pop("pp_option")
  75. pipeline_setting.update(kwargs)
  76. pipeline = BasePipeline.get(pipeline_name)(
  77. predictor_kwargs=predictor_kwargs, *args, **pipeline_setting
  78. )
  79. return pipeline
  80. def create_pipeline(
  81. pipeline: str,
  82. device=None,
  83. pp_option=None,
  84. use_hpip: bool = False,
  85. hpi_params: Optional[Dict[str, Any]] = None,
  86. *args,
  87. **kwargs,
  88. ) -> BasePipeline:
  89. """build model evaluater
  90. Args:
  91. pipeline (str): the pipeline name, that is name of pipeline class
  92. Returns:
  93. BasePipeline: the pipeline, which is subclass of BasePipeline.
  94. """
  95. config = load_pipeline_config(pipeline)
  96. return create_pipeline_from_config(
  97. config,
  98. device=device,
  99. pp_option=pp_option,
  100. use_hpip=use_hpip,
  101. hpi_params=hpi_params,
  102. *args,
  103. **kwargs,
  104. )