__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 .formula_recognition import FormulaRecognitionPipeline
  34. from .table_recognition import TableRecPipeline
  35. from .seal_recognition import SealOCRPipeline
  36. from .ppchatocrv3 import PPChatOCRPipeline
  37. def load_pipeline_config(pipeline: str) -> Dict[str, Any]:
  38. if not Path(pipeline).exists():
  39. pipeline_path = get_pipeline_path(pipeline)
  40. if pipeline_path is None:
  41. raise Exception(
  42. f"The pipeline ({pipeline}) does not exist! Please use a pipeline name or a config file path!"
  43. )
  44. else:
  45. pipeline_path = pipeline
  46. config = parse_config(pipeline_path)
  47. return config
  48. def create_pipeline_from_config(
  49. config: Dict[str, Any],
  50. device=None,
  51. pp_option=None,
  52. use_hpip: bool = False,
  53. hpi_params: Optional[Dict[str, Any]] = None,
  54. *args,
  55. **kwargs,
  56. ) -> BasePipeline:
  57. pipeline_name = config["Global"]["pipeline_name"]
  58. pipeline_setting = config["Pipeline"]
  59. predictor_kwargs = {"use_hpip": use_hpip}
  60. if "use_hpip" in pipeline_setting:
  61. predictor_kwargs["use_hpip"] = use_hpip
  62. if hpi_params is not None:
  63. predictor_kwargs["hpi_params"] = hpi_params
  64. pipeline_setting.pop("hpi_params", None)
  65. elif "hpi_params" in pipeline_setting:
  66. predictor_kwargs["hpi_params"] = pipeline_setting.pop("hpi_params")
  67. if device is not None:
  68. predictor_kwargs["device"] = device
  69. pipeline_setting.pop("device", None)
  70. elif "device" in pipeline_setting:
  71. predictor_kwargs["device"] = pipeline_setting.pop("device")
  72. if pp_option is not None:
  73. predictor_kwargs["pp_option"] = pp_option
  74. pipeline_setting.pop("pp_option", None)
  75. elif "pp_option" in pipeline_setting:
  76. predictor_kwargs["pp_option"] = pipeline_setting.pop("pp_option")
  77. pipeline_setting.update(kwargs)
  78. pipeline = BasePipeline.get(pipeline_name)(
  79. predictor_kwargs=predictor_kwargs, *args, **pipeline_setting
  80. )
  81. return pipeline
  82. def create_pipeline(
  83. pipeline: str,
  84. device=None,
  85. pp_option=None,
  86. use_hpip: bool = False,
  87. hpi_params: Optional[Dict[str, Any]] = None,
  88. *args,
  89. **kwargs,
  90. ) -> BasePipeline:
  91. """build model evaluater
  92. Args:
  93. pipeline (str): the pipeline name, that is name of pipeline class
  94. Returns:
  95. BasePipeline: the pipeline, which is subclass of BasePipeline.
  96. """
  97. config = load_pipeline_config(pipeline)
  98. return create_pipeline_from_config(
  99. config,
  100. device=device,
  101. pp_option=pp_option,
  102. use_hpip=use_hpip,
  103. hpi_params=hpi_params,
  104. *args,
  105. **kwargs,
  106. )