This commit is contained in:
35
paddleocr/_pipelines/__init__.py
Normal file
35
paddleocr/_pipelines/__init__.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .doc_preprocessor import DocPreprocessor
|
||||
from .doc_understanding import DocUnderstanding
|
||||
from .formula_recognition import FormulaRecognitionPipeline
|
||||
from .ocr import PaddleOCR
|
||||
from .pp_chatocrv4_doc import PPChatOCRv4Doc
|
||||
from .pp_doctranslation import PPDocTranslation
|
||||
from .pp_structurev3 import PPStructureV3
|
||||
from .seal_recognition import SealRecognition
|
||||
from .table_recognition_v2 import TableRecognitionPipelineV2
|
||||
|
||||
__all__ = [
|
||||
"DocPreprocessor",
|
||||
"DocUnderstanding",
|
||||
"FormulaRecognitionPipeline",
|
||||
"PaddleOCR",
|
||||
"PPChatOCRv4Doc",
|
||||
"PPDocTranslation",
|
||||
"PPStructureV3",
|
||||
"SealRecognition",
|
||||
"TableRecognitionPipelineV2",
|
||||
]
|
||||
126
paddleocr/_pipelines/base.py
Normal file
126
paddleocr/_pipelines/base.py
Normal file
@@ -0,0 +1,126 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import abc
|
||||
|
||||
import yaml
|
||||
from paddlex import create_pipeline
|
||||
from paddlex.inference import load_pipeline_config
|
||||
from paddlex.utils.config import AttrDict
|
||||
|
||||
from .._abstract import CLISubcommandExecutor
|
||||
from .._common_args import (
|
||||
add_common_cli_opts,
|
||||
parse_common_args,
|
||||
prepare_common_init_args,
|
||||
)
|
||||
|
||||
_DEFAULT_ENABLE_HPI = None
|
||||
|
||||
|
||||
def _merge_dicts(d1, d2):
|
||||
res = d1.copy()
|
||||
for k, v in d2.items():
|
||||
if k in res and isinstance(res[k], dict) and isinstance(v, dict):
|
||||
res[k] = _merge_dicts(res[k], v)
|
||||
else:
|
||||
res[k] = v
|
||||
return res
|
||||
|
||||
|
||||
def _to_builtin(obj):
|
||||
if isinstance(obj, AttrDict):
|
||||
return {k: _to_builtin(v) for k, v in obj.items()}
|
||||
elif isinstance(obj, dict):
|
||||
return {k: _to_builtin(v) for k, v in obj.items()}
|
||||
elif isinstance(obj, list):
|
||||
return [_to_builtin(item) for item in obj]
|
||||
else:
|
||||
return obj
|
||||
|
||||
|
||||
class PaddleXPipelineWrapper(metaclass=abc.ABCMeta):
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
paddlex_config=None,
|
||||
**common_args,
|
||||
):
|
||||
super().__init__()
|
||||
self._paddlex_config = paddlex_config
|
||||
self._common_args = parse_common_args(
|
||||
common_args, default_enable_hpi=_DEFAULT_ENABLE_HPI
|
||||
)
|
||||
self._merged_paddlex_config = self._get_merged_paddlex_config()
|
||||
self.paddlex_pipeline = self._create_paddlex_pipeline()
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def _paddlex_pipeline_name(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def export_paddlex_config_to_yaml(self, yaml_path):
|
||||
with open(yaml_path, "w", encoding="utf-8") as f:
|
||||
config = _to_builtin(self._merged_paddlex_config)
|
||||
yaml.safe_dump(config, f)
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
raise NotImplementedError
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
return {}
|
||||
|
||||
def _get_merged_paddlex_config(self):
|
||||
if self._paddlex_config is None:
|
||||
config = load_pipeline_config(self._paddlex_pipeline_name)
|
||||
elif isinstance(self._paddlex_config, str):
|
||||
config = load_pipeline_config(self._paddlex_config)
|
||||
else:
|
||||
config = self._paddlex_config
|
||||
|
||||
overrides = self._get_paddlex_config_overrides()
|
||||
|
||||
return _merge_dicts(config, overrides)
|
||||
|
||||
def _create_paddlex_pipeline(self):
|
||||
kwargs = prepare_common_init_args(None, self._common_args)
|
||||
return create_pipeline(config=self._merged_paddlex_config, **kwargs)
|
||||
|
||||
|
||||
class PipelineCLISubcommandExecutor(CLISubcommandExecutor):
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def subparser_name(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def add_subparser(self, subparsers):
|
||||
subparser = subparsers.add_parser(name=self.subparser_name)
|
||||
self._update_subparser(subparser)
|
||||
add_common_cli_opts(
|
||||
subparser,
|
||||
default_enable_hpi=_DEFAULT_ENABLE_HPI,
|
||||
allow_multiple_devices=True,
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--paddlex_config",
|
||||
type=str,
|
||||
help="Path to PaddleX pipeline configuration file.",
|
||||
)
|
||||
return subparser
|
||||
|
||||
@abc.abstractmethod
|
||||
def _update_subparser(self, subparser):
|
||||
raise NotImplementedError
|
||||
147
paddleocr/_pipelines/doc_preprocessor.py
Normal file
147
paddleocr/_pipelines/doc_preprocessor.py
Normal file
@@ -0,0 +1,147 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .._utils.cli import (
|
||||
add_simple_inference_args,
|
||||
get_subcommand_args,
|
||||
perform_simple_inference,
|
||||
str2bool,
|
||||
)
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class DocPreprocessor(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
**kwargs,
|
||||
):
|
||||
|
||||
self._params = {
|
||||
"doc_orientation_classify_model_name": doc_orientation_classify_model_name,
|
||||
"doc_orientation_classify_model_dir": doc_orientation_classify_model_dir,
|
||||
"doc_unwarping_model_name": doc_unwarping_model_name,
|
||||
"doc_unwarping_model_dir": doc_unwarping_model_dir,
|
||||
"use_doc_orientation_classify": use_doc_orientation_classify,
|
||||
"use_doc_unwarping": use_doc_unwarping,
|
||||
}
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "doc_preprocessor"
|
||||
|
||||
def predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
):
|
||||
return self.paddlex_pipeline.predict(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
)
|
||||
|
||||
def predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
):
|
||||
return list(
|
||||
self.predict_iter(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return DocPreprocessorCLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"use_doc_unwarping": self._params["use_doc_unwarping"],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class DocPreprocessorCLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "doc_preprocessor"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
add_simple_inference_args(subparser)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the document image unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image unwarping model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
help="Whether to use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
help="Whether to use text image unwarping.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
|
||||
perform_simple_inference(DocPreprocessor, params)
|
||||
107
paddleocr/_pipelines/doc_understanding.py
Normal file
107
paddleocr/_pipelines/doc_understanding.py
Normal file
@@ -0,0 +1,107 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from paddlex.utils.pipeline_arguments import custom_type
|
||||
|
||||
from .._utils.cli import (
|
||||
add_simple_inference_args,
|
||||
get_subcommand_args,
|
||||
perform_simple_inference,
|
||||
)
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class DocUnderstanding(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
doc_understanding_model_name=None,
|
||||
doc_understanding_model_dir=None,
|
||||
doc_understanding_batch_size=None,
|
||||
**kwargs,
|
||||
):
|
||||
|
||||
self._params = {
|
||||
"doc_understanding_model_name": doc_understanding_model_name,
|
||||
"doc_understanding_model_dir": doc_understanding_model_dir,
|
||||
"doc_understanding_batch_size": doc_understanding_batch_size,
|
||||
}
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "doc_understanding"
|
||||
|
||||
def predict_iter(self, input, **kwargs):
|
||||
return self.paddlex_pipeline.predict(input, **kwargs)
|
||||
|
||||
def predict(
|
||||
self,
|
||||
input,
|
||||
**kwargs,
|
||||
):
|
||||
return list(self.predict_iter(input, **kwargs))
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return DocUnderstandingCLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"SubModules.DocUnderstanding.model_name": self._params[
|
||||
"doc_understanding_model_name"
|
||||
],
|
||||
"SubModules.DocUnderstanding.model_dir": self._params[
|
||||
"doc_understanding_model_dir"
|
||||
],
|
||||
"SubModules.DocUnderstanding.batch_size": self._params[
|
||||
"doc_understanding_batch_size"
|
||||
],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class DocUnderstandingCLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
input_validator = staticmethod(custom_type(dict))
|
||||
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "doc_understanding"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
add_simple_inference_args(
|
||||
subparser,
|
||||
input_help='Input dict, e.g. `{"image": "https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/medal_table.png", "query": "Recognize this table"}`.',
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_understanding_model_name",
|
||||
type=str,
|
||||
help="Name of the document understanding model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_understanding_model_dir",
|
||||
type=str,
|
||||
help="Path to the document understanding model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_understanding_batch_size",
|
||||
type=str,
|
||||
help="Batch size for the document understanding model.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
params["input"] = self.input_validator(params["input"])
|
||||
perform_simple_inference(DocUnderstanding, params)
|
||||
283
paddleocr/_pipelines/formula_recognition.py
Normal file
283
paddleocr/_pipelines/formula_recognition.py
Normal file
@@ -0,0 +1,283 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .._utils.cli import (
|
||||
add_simple_inference_args,
|
||||
get_subcommand_args,
|
||||
perform_simple_inference,
|
||||
str2bool,
|
||||
)
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class FormulaRecognitionPipeline(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_orientation_classify_batch_size=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
doc_unwarping_batch_size=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
layout_detection_model_name=None,
|
||||
layout_detection_model_dir=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
layout_detection_batch_size=None,
|
||||
use_layout_detection=None,
|
||||
formula_recognition_model_name=None,
|
||||
formula_recognition_model_dir=None,
|
||||
formula_recognition_batch_size=None,
|
||||
**kwargs,
|
||||
):
|
||||
params = locals().copy()
|
||||
params.pop("self")
|
||||
params.pop("kwargs")
|
||||
self._params = params
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "formula_recognition"
|
||||
|
||||
def predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_layout_detection=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
layout_det_res=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
**kwargs,
|
||||
):
|
||||
return self.paddlex_pipeline.predict(
|
||||
input,
|
||||
use_layout_detection=use_layout_detection,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
layout_det_res=layout_det_res,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_layout_detection=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
layout_det_res=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
**kwargs,
|
||||
):
|
||||
return list(
|
||||
self.predict_iter(
|
||||
input,
|
||||
use_layout_detection=use_layout_detection,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
layout_det_res=layout_det_res,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
**kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return FormulaRecognitionPipelineCLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"use_layout_detection": self._params["use_layout_detection"],
|
||||
"SubModules.LayoutDetection.model_name": self._params[
|
||||
"layout_detection_model_name"
|
||||
],
|
||||
"SubModules.LayoutDetection.model_dir": self._params[
|
||||
"layout_detection_model_dir"
|
||||
],
|
||||
"SubModules.LayoutDetection.threshold": self._params["layout_threshold"],
|
||||
"SubModules.LayoutDetection.layout_nms": self._params["layout_nms"],
|
||||
"SubModules.LayoutDetection.layout_unclip_ratio": self._params[
|
||||
"layout_unclip_ratio"
|
||||
],
|
||||
"SubModules.LayoutDetection.layout_merge_bboxes_mode": self._params[
|
||||
"layout_merge_bboxes_mode"
|
||||
],
|
||||
"SubModules.LayoutDetection.batch_size": self._params[
|
||||
"layout_detection_batch_size"
|
||||
],
|
||||
"SubModules.FormulaRecognition.model_name": self._params[
|
||||
"formula_recognition_model_name"
|
||||
],
|
||||
"SubModules.FormulaRecognition.model_dir": self._params[
|
||||
"formula_recognition_model_dir"
|
||||
],
|
||||
"SubModules.FormulaRecognition.batch_size": self._params[
|
||||
"formula_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_unwarping": self._params[
|
||||
"use_doc_unwarping"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.batch_size": self._params[
|
||||
"doc_orientation_classify_batch_size"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.batch_size": self._params[
|
||||
"doc_unwarping_batch_size"
|
||||
],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class FormulaRecognitionPipelineCLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "formula_recognition_pipeline"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
add_simple_inference_args(subparser)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Directory of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_batch_size",
|
||||
type=int,
|
||||
help="Batch size for document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the document unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Directory of the document unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_batch_size",
|
||||
type=int,
|
||||
help="Batch size for document unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
help="Use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
help="Use document unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_dir",
|
||||
type=str,
|
||||
help="Directory of the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_threshold",
|
||||
type=float,
|
||||
help="Threshold for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_nms",
|
||||
type=str2bool,
|
||||
help="Non-maximum suppression for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_unclip_ratio",
|
||||
type=float,
|
||||
help="Unclip ratio for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_merge_bboxes_mode",
|
||||
type=str,
|
||||
help="Mode for merging bounding boxes in layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_batch_size",
|
||||
type=int,
|
||||
help="Batch size for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_layout_detection",
|
||||
type=str2bool,
|
||||
help="Use layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the formula recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_model_dir",
|
||||
type=str,
|
||||
help="Directory of the formula recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for formula recognition.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
perform_simple_inference(FormulaRecognitionPipeline, params)
|
||||
630
paddleocr/_pipelines/ocr.py
Normal file
630
paddleocr/_pipelines/ocr.py
Normal file
@@ -0,0 +1,630 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# TODO: Should we use a third-party CLI library to auto-generate command-line
|
||||
# arguments from the pipeline class, to reduce boilerplate and improve
|
||||
# maintainability?
|
||||
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
from .._utils.cli import (
|
||||
add_simple_inference_args,
|
||||
get_subcommand_args,
|
||||
perform_simple_inference,
|
||||
str2bool,
|
||||
)
|
||||
from .._utils.deprecation import (
|
||||
DeprecatedOptionAction,
|
||||
deprecated,
|
||||
warn_deprecated_param,
|
||||
)
|
||||
from .._utils.logging import logger
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
_DEPRECATED_PARAM_NAME_MAPPING = {
|
||||
"det_model_dir": "text_detection_model_dir",
|
||||
"det_limit_side_len": "text_det_limit_side_len",
|
||||
"det_limit_type": "text_det_limit_type",
|
||||
"det_db_thresh": "text_det_thresh",
|
||||
"det_db_box_thresh": "text_det_box_thresh",
|
||||
"det_db_unclip_ratio": "text_det_unclip_ratio",
|
||||
"rec_model_dir": "text_recognition_model_dir",
|
||||
"rec_batch_num": "text_recognition_batch_size",
|
||||
"use_angle_cls": "use_textline_orientation",
|
||||
"cls_model_dir": "textline_orientation_model_dir",
|
||||
"cls_batch_num": "textline_orientation_batch_size",
|
||||
}
|
||||
|
||||
_SUPPORTED_OCR_VERSIONS = ["PP-OCRv3", "PP-OCRv4", "PP-OCRv5"]
|
||||
|
||||
|
||||
# Be comptable with PaddleOCR 2.x interfaces
|
||||
class PaddleOCR(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
text_detection_model_name=None,
|
||||
text_detection_model_dir=None,
|
||||
textline_orientation_model_name=None,
|
||||
textline_orientation_model_dir=None,
|
||||
textline_orientation_batch_size=None,
|
||||
text_recognition_model_name=None,
|
||||
text_recognition_model_dir=None,
|
||||
text_recognition_batch_size=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_det_input_shape=None,
|
||||
text_rec_score_thresh=None,
|
||||
text_rec_input_shape=None,
|
||||
lang=None,
|
||||
ocr_version=None,
|
||||
**kwargs,
|
||||
):
|
||||
if ocr_version is not None and ocr_version not in _SUPPORTED_OCR_VERSIONS:
|
||||
raise ValueError(
|
||||
f"Invalid OCR version: {ocr_version}. Supported values are {_SUPPORTED_OCR_VERSIONS}."
|
||||
)
|
||||
|
||||
if all(
|
||||
map(
|
||||
lambda p: p is None,
|
||||
(
|
||||
text_detection_model_name,
|
||||
text_detection_model_dir,
|
||||
text_recognition_model_name,
|
||||
text_recognition_model_dir,
|
||||
),
|
||||
)
|
||||
):
|
||||
if lang is not None or ocr_version is not None:
|
||||
det_model_name, rec_model_name = self._get_ocr_model_names(
|
||||
lang, ocr_version
|
||||
)
|
||||
if det_model_name is None or rec_model_name is None:
|
||||
raise ValueError(
|
||||
f"No models are available for the language {repr(lang)} and OCR version {repr(ocr_version)}."
|
||||
)
|
||||
text_detection_model_name = det_model_name
|
||||
text_recognition_model_name = rec_model_name
|
||||
else:
|
||||
if lang is not None or ocr_version is not None:
|
||||
warnings.warn(
|
||||
"`lang` and `ocr_version` will be ignored when model names or model directories are not `None`.",
|
||||
stacklevel=2,
|
||||
)
|
||||
|
||||
params = {
|
||||
"doc_orientation_classify_model_name": doc_orientation_classify_model_name,
|
||||
"doc_orientation_classify_model_dir": doc_orientation_classify_model_dir,
|
||||
"doc_unwarping_model_name": doc_unwarping_model_name,
|
||||
"doc_unwarping_model_dir": doc_unwarping_model_dir,
|
||||
"text_detection_model_name": text_detection_model_name,
|
||||
"text_detection_model_dir": text_detection_model_dir,
|
||||
"textline_orientation_model_name": textline_orientation_model_name,
|
||||
"textline_orientation_model_dir": textline_orientation_model_dir,
|
||||
"textline_orientation_batch_size": textline_orientation_batch_size,
|
||||
"text_recognition_model_name": text_recognition_model_name,
|
||||
"text_recognition_model_dir": text_recognition_model_dir,
|
||||
"text_recognition_batch_size": text_recognition_batch_size,
|
||||
"use_doc_orientation_classify": use_doc_orientation_classify,
|
||||
"use_doc_unwarping": use_doc_unwarping,
|
||||
"use_textline_orientation": use_textline_orientation,
|
||||
"text_det_limit_side_len": text_det_limit_side_len,
|
||||
"text_det_limit_type": text_det_limit_type,
|
||||
"text_det_thresh": text_det_thresh,
|
||||
"text_det_box_thresh": text_det_box_thresh,
|
||||
"text_det_unclip_ratio": text_det_unclip_ratio,
|
||||
"text_det_input_shape": text_det_input_shape,
|
||||
"text_rec_score_thresh": text_rec_score_thresh,
|
||||
"text_rec_input_shape": text_rec_input_shape,
|
||||
}
|
||||
base_params = {}
|
||||
for name, val in kwargs.items():
|
||||
if name in _DEPRECATED_PARAM_NAME_MAPPING:
|
||||
new_name = _DEPRECATED_PARAM_NAME_MAPPING[name]
|
||||
warn_deprecated_param(name, new_name)
|
||||
assert (
|
||||
new_name in params
|
||||
), f"{repr(new_name)} is not a valid parameter name."
|
||||
if params[new_name] is not None:
|
||||
raise ValueError(
|
||||
f"`{name}` and `{new_name}` are mutually exclusive."
|
||||
)
|
||||
params[new_name] = val
|
||||
else:
|
||||
base_params[name] = val
|
||||
|
||||
self._params = params
|
||||
|
||||
super().__init__(**base_params)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "OCR"
|
||||
|
||||
def predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
):
|
||||
return self.paddlex_pipeline.predict(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
)
|
||||
|
||||
def predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
):
|
||||
return list(
|
||||
self.predict_iter(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
)
|
||||
)
|
||||
|
||||
@deprecated("Please use `predict` instead.")
|
||||
def ocr(self, img, **kwargs):
|
||||
return self.predict(img, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return PaddleOCRCLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"SubModules.TextDetection.model_name": self._params[
|
||||
"text_detection_model_name"
|
||||
],
|
||||
"SubModules.TextDetection.model_dir": self._params[
|
||||
"text_detection_model_dir"
|
||||
],
|
||||
"SubModules.TextLineOrientation.model_name": self._params[
|
||||
"textline_orientation_model_name"
|
||||
],
|
||||
"SubModules.TextLineOrientation.model_dir": self._params[
|
||||
"textline_orientation_model_dir"
|
||||
],
|
||||
"SubModules.TextLineOrientation.batch_size": self._params[
|
||||
"textline_orientation_batch_size"
|
||||
],
|
||||
"SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_unwarping": self._params[
|
||||
"use_doc_unwarping"
|
||||
],
|
||||
"use_textline_orientation": self._params["use_textline_orientation"],
|
||||
"SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubModules.TextDetection.limit_type": self._params["text_det_limit_type"],
|
||||
"SubModules.TextDetection.thresh": self._params["text_det_thresh"],
|
||||
"SubModules.TextDetection.box_thresh": self._params["text_det_box_thresh"],
|
||||
"SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"text_det_unclip_ratio"
|
||||
],
|
||||
"SubModules.TextDetection.input_shape": self._params[
|
||||
"text_det_input_shape"
|
||||
],
|
||||
"SubModules.TextRecognition.score_thresh": self._params[
|
||||
"text_rec_score_thresh"
|
||||
],
|
||||
"SubModules.TextRecognition.input_shape": self._params[
|
||||
"text_rec_input_shape"
|
||||
],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
def _get_ocr_model_names(self, lang, ppocr_version):
|
||||
LATIN_LANGS = [
|
||||
"af",
|
||||
"az",
|
||||
"bs",
|
||||
"cs",
|
||||
"cy",
|
||||
"da",
|
||||
"de",
|
||||
"es",
|
||||
"et",
|
||||
"fr",
|
||||
"ga",
|
||||
"hr",
|
||||
"hu",
|
||||
"id",
|
||||
"is",
|
||||
"it",
|
||||
"ku",
|
||||
"la",
|
||||
"lt",
|
||||
"lv",
|
||||
"mi",
|
||||
"ms",
|
||||
"mt",
|
||||
"nl",
|
||||
"no",
|
||||
"oc",
|
||||
"pi",
|
||||
"pl",
|
||||
"pt",
|
||||
"ro",
|
||||
"rs_latin",
|
||||
"sk",
|
||||
"sl",
|
||||
"sq",
|
||||
"sv",
|
||||
"sw",
|
||||
"tl",
|
||||
"tr",
|
||||
"uz",
|
||||
"vi",
|
||||
"french",
|
||||
"german",
|
||||
]
|
||||
ARABIC_LANGS = ["ar", "fa", "ug", "ur"]
|
||||
ESLAV_LANGS = ["ru", "be", "uk"]
|
||||
CYRILLIC_LANGS = [
|
||||
"ru",
|
||||
"rs_cyrillic",
|
||||
"be",
|
||||
"bg",
|
||||
"uk",
|
||||
"mn",
|
||||
"abq",
|
||||
"ady",
|
||||
"kbd",
|
||||
"ava",
|
||||
"dar",
|
||||
"inh",
|
||||
"che",
|
||||
"lbe",
|
||||
"lez",
|
||||
"tab",
|
||||
]
|
||||
DEVANAGARI_LANGS = [
|
||||
"hi",
|
||||
"mr",
|
||||
"ne",
|
||||
"bh",
|
||||
"mai",
|
||||
"ang",
|
||||
"bho",
|
||||
"mah",
|
||||
"sck",
|
||||
"new",
|
||||
"gom",
|
||||
"sa",
|
||||
"bgc",
|
||||
]
|
||||
SPECIFIC_LANGS = [
|
||||
"ch",
|
||||
"en",
|
||||
"korean",
|
||||
"japan",
|
||||
"chinese_cht",
|
||||
"te",
|
||||
"ka",
|
||||
"ta",
|
||||
]
|
||||
|
||||
if lang is None:
|
||||
lang = "ch"
|
||||
|
||||
if ppocr_version is None:
|
||||
if (
|
||||
lang
|
||||
in ["ch", "chinese_cht", "en", "japan", "korean"]
|
||||
+ LATIN_LANGS
|
||||
+ ESLAV_LANGS
|
||||
):
|
||||
ppocr_version = "PP-OCRv5"
|
||||
elif lang in (
|
||||
LATIN_LANGS
|
||||
+ ARABIC_LANGS
|
||||
+ CYRILLIC_LANGS
|
||||
+ DEVANAGARI_LANGS
|
||||
+ SPECIFIC_LANGS
|
||||
):
|
||||
ppocr_version = "PP-OCRv3"
|
||||
else:
|
||||
# Unknown language specified
|
||||
return None, None
|
||||
|
||||
if ppocr_version == "PP-OCRv5":
|
||||
rec_lang, rec_model_name = None, None
|
||||
if lang in ("ch", "chinese_cht", "en", "japan"):
|
||||
rec_model_name = "PP-OCRv5_server_rec"
|
||||
elif lang in LATIN_LANGS:
|
||||
rec_lang = "latin"
|
||||
elif lang in ESLAV_LANGS:
|
||||
rec_lang = "eslav"
|
||||
elif lang == "korean":
|
||||
rec_lang = "korean"
|
||||
|
||||
if rec_lang is not None:
|
||||
rec_model_name = f"{rec_lang}_PP-OCRv5_mobile_rec"
|
||||
return "PP-OCRv5_server_det", rec_model_name
|
||||
|
||||
elif ppocr_version == "PP-OCRv4":
|
||||
if lang == "ch":
|
||||
return "PP-OCRv4_mobile_det", "PP-OCRv4_mobile_rec"
|
||||
elif lang == "en":
|
||||
return "PP-OCRv4_mobile_det", "en_PP-OCRv4_mobile_rec"
|
||||
else:
|
||||
return None, None
|
||||
else:
|
||||
# PP-OCRv3
|
||||
rec_lang = None
|
||||
if lang in LATIN_LANGS:
|
||||
rec_lang = "latin"
|
||||
elif lang in ARABIC_LANGS:
|
||||
rec_lang = "arabic"
|
||||
elif lang in CYRILLIC_LANGS:
|
||||
rec_lang = "cyrillic"
|
||||
elif lang in DEVANAGARI_LANGS:
|
||||
rec_lang = "devanagari"
|
||||
else:
|
||||
if lang in SPECIFIC_LANGS:
|
||||
rec_lang = lang
|
||||
|
||||
rec_model_name = None
|
||||
if rec_lang == "ch":
|
||||
rec_model_name = "PP-OCRv3_mobile_rec"
|
||||
elif rec_lang is not None:
|
||||
rec_model_name = f"{rec_lang}_PP-OCRv3_mobile_rec"
|
||||
return "PP-OCRv3_mobile_det", rec_model_name
|
||||
|
||||
|
||||
class PaddleOCRCLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "ocr"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
add_simple_inference_args(subparser)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the text image unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Path to the image unwarping model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_name",
|
||||
type=str,
|
||||
help="Name of the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_dir",
|
||||
type=str,
|
||||
help="Path to the text line orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
help="Whether to use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
help="Whether to use text image unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_textline_orientation",
|
||||
type=str2bool,
|
||||
help="Whether to use text line orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_input_shape",
|
||||
nargs=3,
|
||||
type=int,
|
||||
metavar=("C", "H", "W"),
|
||||
help="Input shape of the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_rec_score_thresh",
|
||||
type=float,
|
||||
help="Text recognition threshold. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_rec_input_shape",
|
||||
nargs=3,
|
||||
type=int,
|
||||
metavar=("C", "H", "W"),
|
||||
help="Input shape of the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--lang", type=str, help="Language in the input image for OCR processing."
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--ocr_version",
|
||||
type=str,
|
||||
choices=_SUPPORTED_OCR_VERSIONS,
|
||||
help="PP-OCR version to use.",
|
||||
)
|
||||
|
||||
deprecated_arg_types = {
|
||||
"det_model_dir": str,
|
||||
"det_limit_side_len": int,
|
||||
"det_limit_type": str,
|
||||
"det_db_thresh": float,
|
||||
"det_db_box_thresh": float,
|
||||
"det_db_unclip_ratio": float,
|
||||
"rec_model_dir": str,
|
||||
"rec_batch_num": int,
|
||||
"use_angle_cls": str2bool,
|
||||
"cls_model_dir": str,
|
||||
"cls_batch_num": int,
|
||||
}
|
||||
|
||||
for name, new_name in _DEPRECATED_PARAM_NAME_MAPPING.items():
|
||||
assert name in deprecated_arg_types, name
|
||||
subparser.add_argument(
|
||||
"--" + name,
|
||||
action=DeprecatedOptionAction,
|
||||
type=str,
|
||||
help=f"[Deprecated] Please use `--{new_name}` instead.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
for name, new_name in _DEPRECATED_PARAM_NAME_MAPPING.items():
|
||||
assert name in params
|
||||
val = params[name]
|
||||
new_val = params[new_name]
|
||||
if val is not None and new_val is not None:
|
||||
logger.error(
|
||||
"`--%s` and `--%s` are mutually exclusive.", name, new_name
|
||||
)
|
||||
sys.exit(2)
|
||||
if val is None:
|
||||
params.pop(name)
|
||||
|
||||
perform_simple_inference(PaddleOCR, params)
|
||||
721
paddleocr/_pipelines/pp_chatocrv4_doc.py
Normal file
721
paddleocr/_pipelines/pp_chatocrv4_doc.py
Normal file
@@ -0,0 +1,721 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .._utils.cli import (
|
||||
get_subcommand_args,
|
||||
str2bool,
|
||||
)
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class PPChatOCRv4Doc(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
layout_detection_model_name=None,
|
||||
layout_detection_model_dir=None,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
text_detection_model_name=None,
|
||||
text_detection_model_dir=None,
|
||||
textline_orientation_model_name=None,
|
||||
textline_orientation_model_dir=None,
|
||||
textline_orientation_batch_size=None,
|
||||
text_recognition_model_name=None,
|
||||
text_recognition_model_dir=None,
|
||||
text_recognition_batch_size=None,
|
||||
table_structure_recognition_model_name=None,
|
||||
table_structure_recognition_model_dir=None,
|
||||
seal_text_detection_model_name=None,
|
||||
seal_text_detection_model_dir=None,
|
||||
seal_text_recognition_model_name=None,
|
||||
seal_text_recognition_model_dir=None,
|
||||
seal_text_recognition_batch_size=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
retriever_config=None,
|
||||
mllm_chat_bot_config=None,
|
||||
chat_bot_config=None,
|
||||
**kwargs,
|
||||
):
|
||||
params = locals().copy()
|
||||
params.pop("self")
|
||||
params.pop("kwargs")
|
||||
self._params = params
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "PP-ChatOCRv4-doc"
|
||||
|
||||
def visual_predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
**kwargs,
|
||||
):
|
||||
return self.paddlex_pipeline.visual_predict(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
use_seal_recognition=use_seal_recognition,
|
||||
use_table_recognition=use_table_recognition,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def visual_predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
**kwargs,
|
||||
):
|
||||
return list(
|
||||
self.visual_predict_iter(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
use_seal_recognition=use_seal_recognition,
|
||||
use_table_recognition=use_table_recognition,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
**kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
def build_vector(
|
||||
self,
|
||||
visual_info,
|
||||
*,
|
||||
min_characters=3500,
|
||||
block_size=300,
|
||||
flag_save_bytes_vector=False,
|
||||
retriever_config=None,
|
||||
):
|
||||
return self.paddlex_pipeline.build_vector(
|
||||
visual_info,
|
||||
min_characters=min_characters,
|
||||
block_size=block_size,
|
||||
flag_save_bytes_vector=flag_save_bytes_vector,
|
||||
retriever_config=retriever_config,
|
||||
)
|
||||
|
||||
def mllm_pred(self, input, key_list, *, mllm_chat_bot_config=None):
|
||||
return self.paddlex_pipeline.mllm_pred(
|
||||
input,
|
||||
key_list,
|
||||
mllm_chat_bot_config=mllm_chat_bot_config,
|
||||
)
|
||||
|
||||
def chat(
|
||||
self,
|
||||
key_list,
|
||||
visual_info,
|
||||
*,
|
||||
use_vector_retrieval=True,
|
||||
vector_info=None,
|
||||
min_characters=3500,
|
||||
text_task_description=None,
|
||||
text_output_format=None,
|
||||
text_rules_str=None,
|
||||
text_few_shot_demo_text_content=None,
|
||||
text_few_shot_demo_key_value_list=None,
|
||||
table_task_description=None,
|
||||
table_output_format=None,
|
||||
table_rules_str=None,
|
||||
table_few_shot_demo_text_content=None,
|
||||
table_few_shot_demo_key_value_list=None,
|
||||
mllm_predict_info=None,
|
||||
mllm_integration_strategy="integration",
|
||||
chat_bot_config=None,
|
||||
retriever_config=None,
|
||||
):
|
||||
return self.paddlex_pipeline.chat(
|
||||
key_list,
|
||||
visual_info,
|
||||
use_vector_retrieval=use_vector_retrieval,
|
||||
vector_info=vector_info,
|
||||
min_characters=min_characters,
|
||||
text_task_description=text_task_description,
|
||||
text_output_format=text_output_format,
|
||||
text_rules_str=text_rules_str,
|
||||
text_few_shot_demo_text_content=text_few_shot_demo_text_content,
|
||||
text_few_shot_demo_key_value_list=text_few_shot_demo_key_value_list,
|
||||
table_task_description=table_task_description,
|
||||
table_output_format=table_output_format,
|
||||
table_rules_str=table_rules_str,
|
||||
table_few_shot_demo_text_content=table_few_shot_demo_text_content,
|
||||
table_few_shot_demo_key_value_list=table_few_shot_demo_key_value_list,
|
||||
mllm_predict_info=mllm_predict_info,
|
||||
mllm_integration_strategy=mllm_integration_strategy,
|
||||
chat_bot_config=chat_bot_config,
|
||||
retriever_config=retriever_config,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return PPChatOCRv4DocCLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.model_name": self._params[
|
||||
"layout_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.model_dir": self._params[
|
||||
"layout_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_name": self._params[
|
||||
"textline_orientation_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_dir": self._params[
|
||||
"textline_orientation_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.batch_size": self._params[
|
||||
"textline_orientation_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.TableStructureRecognition.model_name": self._params[
|
||||
"table_structure_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.TableStructureRecognition.model_dir": self._params[
|
||||
"table_structure_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"seal_text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"seal_text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"seal_text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"seal_text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"seal_text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.use_doc_unwarping": self._params[
|
||||
"use_doc_unwarping"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.use_textline_orientation": self._params[
|
||||
"use_textline_orientation"
|
||||
],
|
||||
"SubPipelines.LayoutParser.use_seal_recognition": self._params[
|
||||
"use_seal_recognition"
|
||||
],
|
||||
"SubPipelines.LayoutParser.use_table_recognition": self._params[
|
||||
"use_table_recognition"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.threshold": self._params[
|
||||
"layout_threshold"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.nms": self._params[
|
||||
"layout_nms"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.unclip_ratio": self._params[
|
||||
"layout_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.merge_bboxes_mode": self._params[
|
||||
"layout_merge_bboxes_mode"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"text_det_limit_type"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"text_det_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"text_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"text_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"text_rec_score_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"seal_det_limit_type"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"seal_det_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"seal_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"seal_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"seal_rec_score_thresh"
|
||||
],
|
||||
"SubModules.LLM_Retriever": self._params["retriever_config"],
|
||||
"SubModules.MLLM_Chat": self._params["mllm_chat_bot_config"],
|
||||
"SubModules.LLM_Chat": self._params["chat_bot_config"],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class PPChatOCRv4DocCLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "pp_chatocrv4_doc"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
subparser.add_argument(
|
||||
"-i",
|
||||
"--input",
|
||||
type=str,
|
||||
required=True,
|
||||
help="Input path or URL.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"-k",
|
||||
"--keys",
|
||||
type=str,
|
||||
nargs="+",
|
||||
required=True,
|
||||
metavar="KEY",
|
||||
help="Keys use for information extraction.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--save_path",
|
||||
type=str,
|
||||
help="Path to the output directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--invoke_mllm",
|
||||
type=str2bool,
|
||||
default=False,
|
||||
help="Whether to invoke the multimodal large language model.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the layout detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the text image unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Path to the image unwarping model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_name",
|
||||
type=str,
|
||||
help="Name of the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_dir",
|
||||
type=str,
|
||||
help="Path to the text line orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--table_structure_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the table structure recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--table_structure_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the table structure recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the seal text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the seal text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the seal text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the seal text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
help="Whether to use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
help="Whether to use text image unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_textline_orientation",
|
||||
type=str2bool,
|
||||
help="Whether to use text line orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_seal_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use seal recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_table_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use table recognition.",
|
||||
)
|
||||
# TODO: Support dict and list types
|
||||
subparser.add_argument(
|
||||
"--layout_threshold",
|
||||
type=float,
|
||||
help="Score threshold for the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_nms",
|
||||
type=str2bool,
|
||||
help="Whether to use NMS in layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_unclip_ratio",
|
||||
type=float,
|
||||
help="Expansion coefficient for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_merge_bboxes_mode",
|
||||
type=str,
|
||||
help="Overlapping box filtering method.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_rec_score_thresh",
|
||||
type=float,
|
||||
help="Text recognition threshold used in general OCR. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the seal text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the seal text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the seal text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Seal text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_rec_score_thresh",
|
||||
type=float,
|
||||
help="Seal text recognition threshold. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
|
||||
# FIXME: Passing API key through CLI is not secure; consider using
|
||||
# environment variables.
|
||||
subparser.add_argument(
|
||||
"--qianfan_api_key",
|
||||
type=str,
|
||||
help="Configuration for the embedding model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--pp_docbee_base_url",
|
||||
type=str,
|
||||
help="Configuration for the multimodal large language model.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
input = params.pop("input")
|
||||
keys = params.pop("keys")
|
||||
save_path = params.pop("save_path")
|
||||
invoke_mllm = params.pop("invoke_mllm")
|
||||
qianfan_api_key = params.pop("qianfan_api_key")
|
||||
if qianfan_api_key is not None:
|
||||
params["retriever_config"] = {
|
||||
"module_name": "retriever",
|
||||
"model_name": "embedding-v1",
|
||||
"base_url": "https://qianfan.baidubce.com/v2",
|
||||
"api_type": "qianfan",
|
||||
"api_key": qianfan_api_key,
|
||||
}
|
||||
params["chat_bot_config"] = {
|
||||
"module_name": "chat_bot",
|
||||
"model_name": "ernie-3.5-8k",
|
||||
"base_url": "https://qianfan.baidubce.com/v2",
|
||||
"api_type": "openai",
|
||||
"api_key": qianfan_api_key,
|
||||
}
|
||||
pp_docbee_base_url = params.pop("pp_docbee_base_url")
|
||||
if pp_docbee_base_url is not None:
|
||||
params["mllm_chat_bot_config"] = {
|
||||
"module_name": "chat_bot",
|
||||
"model_name": "PP-DocBee",
|
||||
# PaddleX requires endpoints such as ".../chat/completions",
|
||||
# which, as the parameter name suggests, are not base URLs.
|
||||
"base_url": pp_docbee_base_url,
|
||||
"api_type": "openai",
|
||||
"api_key": "fake_key",
|
||||
}
|
||||
|
||||
chatocr = PPChatOCRv4Doc(**params)
|
||||
|
||||
result_visual = chatocr.visual_predict_iter(input)
|
||||
|
||||
visual_info_list = []
|
||||
for res in result_visual:
|
||||
visual_info_list.append(res["visual_info"])
|
||||
if save_path:
|
||||
res["layout_parsing_result"].save_all(save_path)
|
||||
|
||||
vector_info = chatocr.build_vector(visual_info_list)
|
||||
|
||||
if invoke_mllm:
|
||||
result_mllm = chatocr.mllm_pred(input, keys)
|
||||
mllm_predict_info = result_mllm["mllm_res"]
|
||||
else:
|
||||
mllm_predict_info = None
|
||||
|
||||
result_chat = chatocr.chat(
|
||||
keys,
|
||||
visual_info_list,
|
||||
vector_info=vector_info,
|
||||
mllm_predict_info=mllm_predict_info,
|
||||
)
|
||||
|
||||
# Print the result to stdout
|
||||
for k, v in result_chat["chat_res"].items():
|
||||
print(f"{k} {v}")
|
||||
936
paddleocr/_pipelines/pp_doctranslation.py
Normal file
936
paddleocr/_pipelines/pp_doctranslation.py
Normal file
@@ -0,0 +1,936 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .._utils.cli import (
|
||||
get_subcommand_args,
|
||||
str2bool,
|
||||
)
|
||||
from .._utils.logging import logger
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class PPDocTranslation(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
layout_detection_model_name=None,
|
||||
layout_detection_model_dir=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
chart_recognition_model_name=None,
|
||||
chart_recognition_model_dir=None,
|
||||
chart_recognition_batch_size=None,
|
||||
region_detection_model_name=None,
|
||||
region_detection_model_dir=None,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
text_detection_model_name=None,
|
||||
text_detection_model_dir=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
textline_orientation_model_name=None,
|
||||
textline_orientation_model_dir=None,
|
||||
textline_orientation_batch_size=None,
|
||||
text_recognition_model_name=None,
|
||||
text_recognition_model_dir=None,
|
||||
text_recognition_batch_size=None,
|
||||
text_rec_score_thresh=None,
|
||||
table_classification_model_name=None,
|
||||
table_classification_model_dir=None,
|
||||
wired_table_structure_recognition_model_name=None,
|
||||
wired_table_structure_recognition_model_dir=None,
|
||||
wireless_table_structure_recognition_model_name=None,
|
||||
wireless_table_structure_recognition_model_dir=None,
|
||||
wired_table_cells_detection_model_name=None,
|
||||
wired_table_cells_detection_model_dir=None,
|
||||
wireless_table_cells_detection_model_name=None,
|
||||
wireless_table_cells_detection_model_dir=None,
|
||||
table_orientation_classify_model_name=None,
|
||||
table_orientation_classify_model_dir=None,
|
||||
seal_text_detection_model_name=None,
|
||||
seal_text_detection_model_dir=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_text_recognition_model_name=None,
|
||||
seal_text_recognition_model_dir=None,
|
||||
seal_text_recognition_batch_size=None,
|
||||
seal_rec_score_thresh=None,
|
||||
formula_recognition_model_name=None,
|
||||
formula_recognition_model_dir=None,
|
||||
formula_recognition_batch_size=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
use_formula_recognition=None,
|
||||
use_chart_recognition=None,
|
||||
use_region_detection=None,
|
||||
chat_bot_config=None,
|
||||
**kwargs,
|
||||
):
|
||||
params = locals().copy()
|
||||
params.pop("self")
|
||||
params.pop("kwargs")
|
||||
self._params = params
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "PP-DocTranslation"
|
||||
|
||||
def visual_predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
use_formula_recognition=None,
|
||||
use_chart_recognition=False,
|
||||
use_region_detection=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
use_wired_table_cells_trans_to_html=False,
|
||||
use_wireless_table_cells_trans_to_html=False,
|
||||
use_table_orientation_classify=True,
|
||||
use_ocr_results_with_table_cells=True,
|
||||
use_e2e_wired_table_rec_model=False,
|
||||
use_e2e_wireless_table_rec_model=True,
|
||||
**kwargs,
|
||||
):
|
||||
return self.paddlex_pipeline.visual_predict(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
use_seal_recognition=use_seal_recognition,
|
||||
use_table_recognition=use_table_recognition,
|
||||
use_formula_recognition=use_formula_recognition,
|
||||
use_chart_recognition=use_chart_recognition,
|
||||
use_region_detection=use_region_detection,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
use_wired_table_cells_trans_to_html=use_wired_table_cells_trans_to_html,
|
||||
use_wireless_table_cells_trans_to_html=use_wireless_table_cells_trans_to_html,
|
||||
use_table_orientation_classify=use_table_orientation_classify,
|
||||
use_ocr_results_with_table_cells=use_ocr_results_with_table_cells,
|
||||
use_e2e_wired_table_rec_model=use_e2e_wired_table_rec_model,
|
||||
use_e2e_wireless_table_rec_model=use_e2e_wireless_table_rec_model,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def visual_predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
use_formula_recognition=None,
|
||||
use_chart_recognition=False,
|
||||
use_region_detection=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
use_wired_table_cells_trans_to_html=False,
|
||||
use_wireless_table_cells_trans_to_html=False,
|
||||
use_table_orientation_classify=True,
|
||||
use_ocr_results_with_table_cells=True,
|
||||
use_e2e_wired_table_rec_model=False,
|
||||
use_e2e_wireless_table_rec_model=True,
|
||||
**kwargs,
|
||||
):
|
||||
return list(
|
||||
self.visual_predict_iter(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
use_seal_recognition=use_seal_recognition,
|
||||
use_table_recognition=use_table_recognition,
|
||||
use_formula_recognition=use_formula_recognition,
|
||||
use_chart_recognition=use_chart_recognition,
|
||||
use_region_detection=use_region_detection,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
use_wired_table_cells_trans_to_html=use_wired_table_cells_trans_to_html,
|
||||
use_wireless_table_cells_trans_to_html=use_wireless_table_cells_trans_to_html,
|
||||
use_table_orientation_classify=use_table_orientation_classify,
|
||||
use_ocr_results_with_table_cells=use_ocr_results_with_table_cells,
|
||||
use_e2e_wired_table_rec_model=use_e2e_wired_table_rec_model,
|
||||
use_e2e_wireless_table_rec_model=use_e2e_wireless_table_rec_model,
|
||||
**kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
def translate_iter(
|
||||
self,
|
||||
ori_md_info_list,
|
||||
*,
|
||||
target_language="zh",
|
||||
chunk_size=5000,
|
||||
task_description=None,
|
||||
output_format=None,
|
||||
rules_str=None,
|
||||
few_shot_demo_text_content=None,
|
||||
few_shot_demo_key_value_list=None,
|
||||
chat_bot_config=None,
|
||||
**kwargs,
|
||||
):
|
||||
return self.paddlex_pipeline.translate(
|
||||
ori_md_info_list,
|
||||
target_language=target_language,
|
||||
chunk_size=chunk_size,
|
||||
task_description=task_description,
|
||||
output_format=output_format,
|
||||
rules_str=rules_str,
|
||||
few_shot_demo_text_content=few_shot_demo_text_content,
|
||||
few_shot_demo_key_value_list=few_shot_demo_key_value_list,
|
||||
chat_bot_config=chat_bot_config,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def translate(
|
||||
self,
|
||||
ori_md_info_list,
|
||||
*,
|
||||
target_language="zh",
|
||||
chunk_size=5000,
|
||||
task_description=None,
|
||||
output_format=None,
|
||||
rules_str=None,
|
||||
few_shot_demo_text_content=None,
|
||||
few_shot_demo_key_value_list=None,
|
||||
chat_bot_config=None,
|
||||
**kwargs,
|
||||
):
|
||||
return list(
|
||||
self.translate_iter(
|
||||
ori_md_info_list,
|
||||
target_language=target_language,
|
||||
chunk_size=chunk_size,
|
||||
task_description=task_description,
|
||||
output_format=output_format,
|
||||
rules_str=rules_str,
|
||||
few_shot_demo_text_content=few_shot_demo_text_content,
|
||||
few_shot_demo_key_value_list=few_shot_demo_key_value_list,
|
||||
chat_bot_config=chat_bot_config,
|
||||
**kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
def load_from_markdown(self, input):
|
||||
return self.paddlex_pipeline.load_from_markdown(input)
|
||||
|
||||
def concatenate_markdown_pages(self, markdown_list):
|
||||
return self.paddlex_pipeline.concatenate_markdown_pages(markdown_list)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return PPDocTranslationCLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
# HACK: We should consider reducing duplication.
|
||||
STRUCTURE = {
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.use_doc_unwarping": self._params[
|
||||
"use_doc_unwarping"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.use_textline_orientation": self._params[
|
||||
"use_textline_orientation"
|
||||
],
|
||||
"SubPipelines.LayoutParser.use_seal_recognition": self._params[
|
||||
"use_seal_recognition"
|
||||
],
|
||||
"SubPipelines.LayoutParser.use_table_recognition": self._params[
|
||||
"use_table_recognition"
|
||||
],
|
||||
"SubPipelines.LayoutParser.use_formula_recognition": self._params[
|
||||
"use_formula_recognition"
|
||||
],
|
||||
"SubPipelines.LayoutParser.use_chart_recognition": self._params[
|
||||
"use_chart_recognition"
|
||||
],
|
||||
"SubPipelines.LayoutParser.use_region_detection": self._params[
|
||||
"use_region_detection"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.model_name": self._params[
|
||||
"layout_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.model_dir": self._params[
|
||||
"layout_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.threshold": self._params[
|
||||
"layout_threshold"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.layout_nms": self._params[
|
||||
"layout_nms"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.layout_unclip_ratio": self._params[
|
||||
"layout_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.LayoutDetection.layout_merge_bboxes_mode": self._params[
|
||||
"layout_merge_bboxes_mode"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.ChartRecognition.model_name": self._params[
|
||||
"chart_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.ChartRecognition.model_dir": self._params[
|
||||
"chart_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.ChartRecognition.batch_size": self._params[
|
||||
"chart_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.RegionDetection.model_name": self._params[
|
||||
"region_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubModules.RegionDetection.model_dir": self._params[
|
||||
"region_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"text_det_limit_type"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"text_det_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"text_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"text_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_name": self._params[
|
||||
"textline_orientation_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_dir": self._params[
|
||||
"textline_orientation_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.batch_size": self._params[
|
||||
"textline_orientation_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.GeneralOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"text_rec_score_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.TableClassification.model_name": self._params[
|
||||
"table_classification_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.TableClassification.model_dir": self._params[
|
||||
"table_classification_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WiredTableStructureRecognition.model_name": self._params[
|
||||
"wired_table_structure_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WiredTableStructureRecognition.model_dir": self._params[
|
||||
"wired_table_structure_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WirelessTableStructureRecognition.model_name": self._params[
|
||||
"wireless_table_structure_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WirelessTableStructureRecognition.model_dir": self._params[
|
||||
"wireless_table_structure_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WiredTableCellsDetection.model_name": self._params[
|
||||
"wired_table_cells_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WiredTableCellsDetection.model_dir": self._params[
|
||||
"wired_table_cells_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WirelessTableCellsDetection.model_name": self._params[
|
||||
"wireless_table_cells_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.WirelessTableCellsDetection.model_dir": self._params[
|
||||
"wireless_table_cells_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.TableOrientationClassify.model_name": self._params[
|
||||
"table_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubModules.TableOrientationClassify.model_dir": self._params[
|
||||
"table_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"text_det_limit_type"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"text_det_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"text_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"text_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_name": self._params[
|
||||
"textline_orientation_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_dir": self._params[
|
||||
"textline_orientation_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.batch_size": self._params[
|
||||
"textline_orientation_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"text_rec_score_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"seal_text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"seal_text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"seal_det_limit_type"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"seal_det_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"seal_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"seal_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"seal_text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"seal_text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"seal_text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.FormulaRecognition.SubModules.FormulaRecognition.model_name": self._params[
|
||||
"formula_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.FormulaRecognition.SubModules.FormulaRecognition.model_dir": self._params[
|
||||
"formula_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.LayoutParser.SubPipelines.FormulaRecognition.SubModules.FormulaRecognition.batch_size": self._params[
|
||||
"formula_recognition_batch_size"
|
||||
],
|
||||
"SubModules.LLM_Chat": self._params["chat_bot_config"],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class PPDocTranslationCLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "pp_doctranslation"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
subparser.add_argument(
|
||||
"-i",
|
||||
"--input",
|
||||
type=str,
|
||||
required=True,
|
||||
help="Input path or URL.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--save_path",
|
||||
type=str,
|
||||
help="Path to the output directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--target_language",
|
||||
type=str,
|
||||
default="zh",
|
||||
help="Target language.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the layout detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_threshold",
|
||||
type=float,
|
||||
help="Score threshold for the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_nms",
|
||||
type=str2bool,
|
||||
help="Whether to use NMS in layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_unclip_ratio",
|
||||
type=float,
|
||||
help="Expansion coefficient for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_merge_bboxes_mode",
|
||||
type=str,
|
||||
help="Overlapping box filtering method.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--chart_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the chart recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--chart_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the chart recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--chart_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the chart recognition model.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--region_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the region detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--region_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the region detection model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the text image unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Path to the image unwarping model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_name",
|
||||
type=str,
|
||||
help="Name of the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_dir",
|
||||
type=str,
|
||||
help="Path to the text line orientation classification directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_rec_score_thresh",
|
||||
type=float,
|
||||
help="Text recognition threshold used in general OCR. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--table_classification_model_name",
|
||||
type=str,
|
||||
help="Name of the table classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--table_classification_model_dir",
|
||||
type=str,
|
||||
help="Path to the table classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_structure_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the wired table structure recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_structure_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table structure recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_structure_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the wireless table structure recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_structure_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table structure recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_cells_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the wired table cells detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_cells_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table cells detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_cells_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the wireless table cells detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_cells_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the wireless table cells detection model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the seal text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the seal text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the seal text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the seal text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Seal text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the seal text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the seal text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the seal text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_rec_score_thresh",
|
||||
type=float,
|
||||
help="Seal text recognition threshold. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the formula recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the formula recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the formula recognition model.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
default=False,
|
||||
help="Whether to use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
default=False,
|
||||
help="Whether to use text image unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_textline_orientation",
|
||||
type=str2bool,
|
||||
help="Whether to use text line orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_seal_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use seal recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_table_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use table recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_formula_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use formula recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_chart_recognition",
|
||||
type=str2bool,
|
||||
default=False,
|
||||
help="Whether to use chart recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_region_detection",
|
||||
type=str2bool,
|
||||
help="Whether to use region detection.",
|
||||
)
|
||||
|
||||
# FIXME: Passing API key through CLI is not secure; consider using
|
||||
# environment variables.
|
||||
subparser.add_argument(
|
||||
"--qianfan_api_key",
|
||||
type=str,
|
||||
help="Configuration for the embedding model.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
input = params.pop("input")
|
||||
target_language = params.pop("target_language")
|
||||
save_path = params.pop("save_path")
|
||||
qianfan_api_key = params.pop("qianfan_api_key")
|
||||
if qianfan_api_key is not None:
|
||||
params["chat_bot_config"] = {
|
||||
"module_name": "chat_bot",
|
||||
"model_name": "ernie-3.5-8k",
|
||||
"base_url": "https://qianfan.baidubce.com/v2",
|
||||
"api_type": "openai",
|
||||
"api_key": qianfan_api_key,
|
||||
}
|
||||
|
||||
chatocr = PPDocTranslation(**params)
|
||||
|
||||
logger.info("Start analyzing images")
|
||||
result_visual = chatocr.visual_predict_iter(input)
|
||||
|
||||
ori_md_info_list = []
|
||||
for res in result_visual:
|
||||
ori_md_info_list.append(res["layout_parsing_result"].markdown)
|
||||
if save_path:
|
||||
res["layout_parsing_result"].save_all(save_path)
|
||||
|
||||
logger.info("Start translation")
|
||||
result_translate = chatocr.translate_iter(
|
||||
ori_md_info_list,
|
||||
target_language=target_language,
|
||||
)
|
||||
|
||||
for res in result_translate:
|
||||
res.print()
|
||||
if save_path:
|
||||
res.save_to_markdown(save_path)
|
||||
810
paddleocr/_pipelines/pp_structurev3.py
Normal file
810
paddleocr/_pipelines/pp_structurev3.py
Normal file
@@ -0,0 +1,810 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .._utils.cli import (
|
||||
add_simple_inference_args,
|
||||
get_subcommand_args,
|
||||
perform_simple_inference,
|
||||
str2bool,
|
||||
)
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class PPStructureV3(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
layout_detection_model_name=None,
|
||||
layout_detection_model_dir=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
chart_recognition_model_name=None,
|
||||
chart_recognition_model_dir=None,
|
||||
chart_recognition_batch_size=None,
|
||||
region_detection_model_name=None,
|
||||
region_detection_model_dir=None,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
text_detection_model_name=None,
|
||||
text_detection_model_dir=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
textline_orientation_model_name=None,
|
||||
textline_orientation_model_dir=None,
|
||||
textline_orientation_batch_size=None,
|
||||
text_recognition_model_name=None,
|
||||
text_recognition_model_dir=None,
|
||||
text_recognition_batch_size=None,
|
||||
text_rec_score_thresh=None,
|
||||
table_classification_model_name=None,
|
||||
table_classification_model_dir=None,
|
||||
wired_table_structure_recognition_model_name=None,
|
||||
wired_table_structure_recognition_model_dir=None,
|
||||
wireless_table_structure_recognition_model_name=None,
|
||||
wireless_table_structure_recognition_model_dir=None,
|
||||
wired_table_cells_detection_model_name=None,
|
||||
wired_table_cells_detection_model_dir=None,
|
||||
wireless_table_cells_detection_model_name=None,
|
||||
wireless_table_cells_detection_model_dir=None,
|
||||
table_orientation_classify_model_name=None,
|
||||
table_orientation_classify_model_dir=None,
|
||||
seal_text_detection_model_name=None,
|
||||
seal_text_detection_model_dir=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_text_recognition_model_name=None,
|
||||
seal_text_recognition_model_dir=None,
|
||||
seal_text_recognition_batch_size=None,
|
||||
seal_rec_score_thresh=None,
|
||||
formula_recognition_model_name=None,
|
||||
formula_recognition_model_dir=None,
|
||||
formula_recognition_batch_size=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
use_formula_recognition=None,
|
||||
use_chart_recognition=None,
|
||||
use_region_detection=None,
|
||||
**kwargs,
|
||||
):
|
||||
params = locals().copy()
|
||||
params.pop("self")
|
||||
params.pop("kwargs")
|
||||
self._params = params
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "PP-StructureV3"
|
||||
|
||||
def predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
use_formula_recognition=None,
|
||||
use_chart_recognition=False,
|
||||
use_region_detection=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
use_wired_table_cells_trans_to_html=False,
|
||||
use_wireless_table_cells_trans_to_html=False,
|
||||
use_table_orientation_classify=True,
|
||||
use_ocr_results_with_table_cells=True,
|
||||
use_e2e_wired_table_rec_model=False,
|
||||
use_e2e_wireless_table_rec_model=True,
|
||||
**kwargs,
|
||||
):
|
||||
return self.paddlex_pipeline.predict(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
use_seal_recognition=use_seal_recognition,
|
||||
use_table_recognition=use_table_recognition,
|
||||
use_formula_recognition=use_formula_recognition,
|
||||
use_chart_recognition=use_chart_recognition,
|
||||
use_region_detection=use_region_detection,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
use_wired_table_cells_trans_to_html=use_wired_table_cells_trans_to_html,
|
||||
use_wireless_table_cells_trans_to_html=use_wireless_table_cells_trans_to_html,
|
||||
use_table_orientation_classify=use_table_orientation_classify,
|
||||
use_ocr_results_with_table_cells=use_ocr_results_with_table_cells,
|
||||
use_e2e_wired_table_rec_model=use_e2e_wired_table_rec_model,
|
||||
use_e2e_wireless_table_rec_model=use_e2e_wireless_table_rec_model,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=None,
|
||||
use_seal_recognition=None,
|
||||
use_table_recognition=None,
|
||||
use_formula_recognition=None,
|
||||
use_chart_recognition=False,
|
||||
use_region_detection=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
use_wired_table_cells_trans_to_html=False,
|
||||
use_wireless_table_cells_trans_to_html=False,
|
||||
use_table_orientation_classify=True,
|
||||
use_ocr_results_with_table_cells=True,
|
||||
use_e2e_wired_table_rec_model=False,
|
||||
use_e2e_wireless_table_rec_model=True,
|
||||
**kwargs,
|
||||
):
|
||||
return list(
|
||||
self.predict_iter(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_textline_orientation=use_textline_orientation,
|
||||
use_seal_recognition=use_seal_recognition,
|
||||
use_table_recognition=use_table_recognition,
|
||||
use_formula_recognition=use_formula_recognition,
|
||||
use_chart_recognition=use_chart_recognition,
|
||||
use_region_detection=use_region_detection,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
use_wired_table_cells_trans_to_html=use_wired_table_cells_trans_to_html,
|
||||
use_wireless_table_cells_trans_to_html=use_wireless_table_cells_trans_to_html,
|
||||
use_table_orientation_classify=use_table_orientation_classify,
|
||||
use_ocr_results_with_table_cells=use_ocr_results_with_table_cells,
|
||||
use_e2e_wired_table_rec_model=use_e2e_wired_table_rec_model,
|
||||
use_e2e_wireless_table_rec_model=use_e2e_wireless_table_rec_model,
|
||||
**kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
def concatenate_markdown_pages(self, markdown_list):
|
||||
return self.paddlex_pipeline.concatenate_markdown_pages(markdown_list)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return PPStructureV3CLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"SubPipelines.DocPreprocessor.use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_unwarping": self._params[
|
||||
"use_doc_unwarping"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.use_textline_orientation": self._params[
|
||||
"use_textline_orientation"
|
||||
],
|
||||
"use_seal_recognition": self._params["use_seal_recognition"],
|
||||
"use_table_recognition": self._params["use_table_recognition"],
|
||||
"use_formula_recognition": self._params["use_formula_recognition"],
|
||||
"use_chart_recognition": self._params["use_chart_recognition"],
|
||||
"use_region_detection": self._params["use_region_detection"],
|
||||
"SubModules.LayoutDetection.model_name": self._params[
|
||||
"layout_detection_model_name"
|
||||
],
|
||||
"SubModules.LayoutDetection.model_dir": self._params[
|
||||
"layout_detection_model_dir"
|
||||
],
|
||||
"SubModules.LayoutDetection.threshold": self._params["layout_threshold"],
|
||||
"SubModules.LayoutDetection.layout_nms": self._params["layout_nms"],
|
||||
"SubModules.LayoutDetection.layout_unclip_ratio": self._params[
|
||||
"layout_unclip_ratio"
|
||||
],
|
||||
"SubModules.LayoutDetection.layout_merge_bboxes_mode": self._params[
|
||||
"layout_merge_bboxes_mode"
|
||||
],
|
||||
"SubModules.ChartRecognition.model_name": self._params[
|
||||
"chart_recognition_model_name"
|
||||
],
|
||||
"SubModules.ChartRecognition.model_dir": self._params[
|
||||
"chart_recognition_model_dir"
|
||||
],
|
||||
"SubModules.ChartRecognition.batch_size": self._params[
|
||||
"chart_recognition_batch_size"
|
||||
],
|
||||
"SubModules.RegionDetection.model_name": self._params[
|
||||
"region_detection_model_name"
|
||||
],
|
||||
"SubModules.RegionDetection.model_dir": self._params[
|
||||
"region_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"text_det_limit_type"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"text_det_thresh"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"text_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"text_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_name": self._params[
|
||||
"textline_orientation_model_name"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_dir": self._params[
|
||||
"textline_orientation_model_dir"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextLineOrientation.batch_size": self._params[
|
||||
"textline_orientation_batch_size"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"text_rec_score_thresh"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.TableClassification.model_name": self._params[
|
||||
"table_classification_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.TableClassification.model_dir": self._params[
|
||||
"table_classification_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WiredTableStructureRecognition.model_name": self._params[
|
||||
"wired_table_structure_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WiredTableStructureRecognition.model_dir": self._params[
|
||||
"wired_table_structure_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WirelessTableStructureRecognition.model_name": self._params[
|
||||
"wireless_table_structure_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WirelessTableStructureRecognition.model_dir": self._params[
|
||||
"wireless_table_structure_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WiredTableCellsDetection.model_name": self._params[
|
||||
"wired_table_cells_detection_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WiredTableCellsDetection.model_dir": self._params[
|
||||
"wired_table_cells_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WirelessTableCellsDetection.model_name": self._params[
|
||||
"wireless_table_cells_detection_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.WirelessTableCellsDetection.model_dir": self._params[
|
||||
"wireless_table_cells_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.TableOrientationClassify.model_name": self._params[
|
||||
"table_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubModules.TableOrientationClassify.model_dir": self._params[
|
||||
"table_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"text_det_limit_type"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"text_det_thresh"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"text_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"text_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_name": self._params[
|
||||
"textline_orientation_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_dir": self._params[
|
||||
"textline_orientation_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.batch_size": self._params[
|
||||
"textline_orientation_batch_size"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"text_rec_score_thresh"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"seal_text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"seal_text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"seal_det_limit_type"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"seal_det_thresh"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"seal_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"seal_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"seal_text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"seal_text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"seal_text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.FormulaRecognition.SubModules.FormulaRecognition.model_name": self._params[
|
||||
"formula_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.FormulaRecognition.SubModules.FormulaRecognition.model_dir": self._params[
|
||||
"formula_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.FormulaRecognition.SubModules.FormulaRecognition.batch_size": self._params[
|
||||
"formula_recognition_batch_size"
|
||||
],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class PPStructureV3CLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "pp_structurev3"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
add_simple_inference_args(subparser)
|
||||
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the layout detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_threshold",
|
||||
type=float,
|
||||
help="Score threshold for the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_nms",
|
||||
type=str2bool,
|
||||
help="Whether to use NMS in layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_unclip_ratio",
|
||||
type=float,
|
||||
help="Expansion coefficient for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_merge_bboxes_mode",
|
||||
type=str,
|
||||
help="Overlapping box filtering method.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--chart_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the chart recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--chart_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the chart recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--chart_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the chart recognition model.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--region_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the region detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--region_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the region detection model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the text image unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Path to the image unwarping model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_name",
|
||||
type=str,
|
||||
help="Name of the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_model_dir",
|
||||
type=str,
|
||||
help="Path to the text line orientation classification directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--textline_orientation_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text line orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_rec_score_thresh",
|
||||
type=float,
|
||||
help="Text recognition threshold used in general OCR. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--table_classification_model_name",
|
||||
type=str,
|
||||
help="Name of the table classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--table_classification_model_dir",
|
||||
type=str,
|
||||
help="Path to the table classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_structure_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the wired table structure recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_structure_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table structure recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_structure_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the wireless table structure recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_structure_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table structure recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_cells_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the wired table cells detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_cells_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table cells detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_cells_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the wireless table cells detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_cells_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the wireless table cells detection model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the seal text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the seal text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the seal text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the seal text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Seal text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the seal text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the seal text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the seal text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_rec_score_thresh",
|
||||
type=float,
|
||||
help="Seal text recognition threshold. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the formula recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the formula recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--formula_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the formula recognition model.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
default=False,
|
||||
help="Whether to use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
default=False,
|
||||
help="Whether to use text image unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_textline_orientation",
|
||||
type=str2bool,
|
||||
help="Whether to use text line orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_seal_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use seal recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_table_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use table recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_formula_recognition",
|
||||
type=str2bool,
|
||||
help="Whether to use formula recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_chart_recognition",
|
||||
type=str2bool,
|
||||
default=False,
|
||||
help="Whether to use chart recognition.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_region_detection",
|
||||
type=str2bool,
|
||||
help="Whether to use region detection.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
perform_simple_inference(
|
||||
PPStructureV3,
|
||||
params,
|
||||
predict_param_names={
|
||||
"use_doc_orientation_classify",
|
||||
"use_doc_unwarping",
|
||||
"use_chart_recognition",
|
||||
},
|
||||
)
|
||||
375
paddleocr/_pipelines/seal_recognition.py
Normal file
375
paddleocr/_pipelines/seal_recognition.py
Normal file
@@ -0,0 +1,375 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .._utils.cli import (
|
||||
add_simple_inference_args,
|
||||
get_subcommand_args,
|
||||
perform_simple_inference,
|
||||
str2bool,
|
||||
)
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class SealRecognition(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
layout_detection_model_name=None,
|
||||
layout_detection_model_dir=None,
|
||||
seal_text_detection_model_name=None,
|
||||
seal_text_detection_model_dir=None,
|
||||
text_recognition_model_name=None,
|
||||
text_recognition_model_dir=None,
|
||||
text_recognition_batch_size=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_layout_detection=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
**kwargs,
|
||||
):
|
||||
|
||||
self._params = {
|
||||
"doc_orientation_classify_model_name": doc_orientation_classify_model_name,
|
||||
"doc_orientation_classify_model_dir": doc_orientation_classify_model_dir,
|
||||
"doc_unwarping_model_name": doc_unwarping_model_name,
|
||||
"doc_unwarping_model_dir": doc_unwarping_model_dir,
|
||||
"layout_detection_model_name": layout_detection_model_name,
|
||||
"layout_detection_model_dir": layout_detection_model_dir,
|
||||
"seal_text_detection_model_name": seal_text_detection_model_name,
|
||||
"seal_text_detection_model_dir": seal_text_detection_model_dir,
|
||||
"text_recognition_model_name": text_recognition_model_name,
|
||||
"text_recognition_model_dir": text_recognition_model_dir,
|
||||
"text_recognition_batch_size": text_recognition_batch_size,
|
||||
"use_doc_orientation_classify": use_doc_orientation_classify,
|
||||
"use_doc_unwarping": use_doc_unwarping,
|
||||
"use_layout_detection": use_layout_detection,
|
||||
"layout_threshold": layout_threshold,
|
||||
"layout_nms": layout_nms,
|
||||
"layout_unclip_ratio": layout_unclip_ratio,
|
||||
"layout_merge_bboxes_mode": layout_merge_bboxes_mode,
|
||||
"seal_det_limit_side_len": seal_det_limit_side_len,
|
||||
"seal_det_limit_type": seal_det_limit_type,
|
||||
"seal_det_thresh": seal_det_thresh,
|
||||
"seal_det_box_thresh": seal_det_box_thresh,
|
||||
"seal_det_unclip_ratio": seal_det_unclip_ratio,
|
||||
"seal_rec_score_thresh": seal_rec_score_thresh,
|
||||
}
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "seal_recognition"
|
||||
|
||||
def predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_layout_detection=None,
|
||||
layout_det_res=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
**kwargs,
|
||||
):
|
||||
return self.paddlex_pipeline.predict(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_layout_detection=use_layout_detection,
|
||||
layout_det_res=layout_det_res,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_layout_detection=None,
|
||||
layout_det_res=None,
|
||||
layout_threshold=None,
|
||||
layout_nms=None,
|
||||
layout_unclip_ratio=None,
|
||||
layout_merge_bboxes_mode=None,
|
||||
seal_det_limit_side_len=None,
|
||||
seal_det_limit_type=None,
|
||||
seal_det_thresh=None,
|
||||
seal_det_box_thresh=None,
|
||||
seal_det_unclip_ratio=None,
|
||||
seal_rec_score_thresh=None,
|
||||
**kwargs,
|
||||
):
|
||||
return list(
|
||||
self.predict_iter(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_layout_detection=use_layout_detection,
|
||||
layout_det_res=layout_det_res,
|
||||
layout_threshold=layout_threshold,
|
||||
layout_nms=layout_nms,
|
||||
layout_unclip_ratio=layout_unclip_ratio,
|
||||
layout_merge_bboxes_mode=layout_merge_bboxes_mode,
|
||||
seal_det_limit_side_len=seal_det_limit_side_len,
|
||||
seal_det_limit_type=seal_det_limit_type,
|
||||
seal_det_thresh=seal_det_thresh,
|
||||
seal_det_box_thresh=seal_det_box_thresh,
|
||||
seal_det_unclip_ratio=seal_det_unclip_ratio,
|
||||
seal_rec_score_thresh=seal_rec_score_thresh,
|
||||
**kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return SealRecognitionCLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"SubModules.LayoutDetection.model_name": self._params[
|
||||
"layout_detection_model_name"
|
||||
],
|
||||
"SubModules.LayoutDetection.model_dir": self._params[
|
||||
"layout_detection_model_dir"
|
||||
],
|
||||
"SubModules.LayoutDetection.threshold": self._params["layout_threshold"],
|
||||
"SubModules.LayoutDetection.layout_nms": self._params["layout_nms"],
|
||||
"SubModules.LayoutDetection.layout_unclip_ratio": self._params[
|
||||
"layout_unclip_ratio"
|
||||
],
|
||||
"SubModules.LayoutDetection.layout_merge_bboxes_mode": self._params[
|
||||
"layout_merge_bboxes_mode"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_unwarping": self._params[
|
||||
"use_doc_unwarping"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"seal_text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"seal_text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"seal_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"seal_det_limit_type"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"seal_det_thresh"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"seal_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"seal_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.SealOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"seal_rec_score_thresh"
|
||||
],
|
||||
"use_layout_detection": self._params["use_layout_detection"],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class SealRecognitionCLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "seal_recognition"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
add_simple_inference_args(subparser)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the document image unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image unwarping model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the layout detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the seal text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
help="Whether to use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
help="Whether to use document image unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_layout_detection",
|
||||
type=str2bool,
|
||||
help="Whether to use layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_threshold",
|
||||
type=float,
|
||||
help="Threshold for layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_nms",
|
||||
type=str2bool,
|
||||
help="Non-Maximum Suppression threshold for layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_unclip_ratio",
|
||||
type=float,
|
||||
help="Layout detection expansion coefficient.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_merge_bboxes_mode",
|
||||
type=str,
|
||||
help="Mode for merging bounding boxes in layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the seal text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the seal text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the seal text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Seal text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--seal_rec_score_thresh",
|
||||
type=float,
|
||||
help="Text recognition threshold. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
|
||||
perform_simple_inference(SealRecognition, params)
|
||||
436
paddleocr/_pipelines/table_recognition_v2.py
Normal file
436
paddleocr/_pipelines/table_recognition_v2.py
Normal file
@@ -0,0 +1,436 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .._utils.cli import (
|
||||
add_simple_inference_args,
|
||||
get_subcommand_args,
|
||||
perform_simple_inference,
|
||||
str2bool,
|
||||
)
|
||||
from .base import PaddleXPipelineWrapper, PipelineCLISubcommandExecutor
|
||||
from .utils import create_config_from_structure
|
||||
|
||||
|
||||
class TableRecognitionPipelineV2(PaddleXPipelineWrapper):
|
||||
def __init__(
|
||||
self,
|
||||
layout_detection_model_name=None,
|
||||
layout_detection_model_dir=None,
|
||||
table_classification_model_name=None,
|
||||
table_classification_model_dir=None,
|
||||
wired_table_structure_recognition_model_name=None,
|
||||
wired_table_structure_recognition_model_dir=None,
|
||||
wireless_table_structure_recognition_model_name=None,
|
||||
wireless_table_structure_recognition_model_dir=None,
|
||||
wired_table_cells_detection_model_name=None,
|
||||
wired_table_cells_detection_model_dir=None,
|
||||
wireless_table_cells_detection_model_name=None,
|
||||
wireless_table_cells_detection_model_dir=None,
|
||||
doc_orientation_classify_model_name=None,
|
||||
doc_orientation_classify_model_dir=None,
|
||||
doc_unwarping_model_name=None,
|
||||
doc_unwarping_model_dir=None,
|
||||
text_detection_model_name=None,
|
||||
text_detection_model_dir=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_recognition_model_name=None,
|
||||
text_recognition_model_dir=None,
|
||||
text_recognition_batch_size=None,
|
||||
text_rec_score_thresh=None,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_layout_detection=None,
|
||||
use_ocr_model=None,
|
||||
**kwargs,
|
||||
):
|
||||
params = locals().copy()
|
||||
params.pop("self")
|
||||
params.pop("kwargs")
|
||||
self._params = params
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def _paddlex_pipeline_name(self):
|
||||
return "table_recognition_v2"
|
||||
|
||||
def predict_iter(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_layout_detection=None,
|
||||
use_ocr_model=None,
|
||||
overall_ocr_res=None,
|
||||
layout_det_res=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
use_e2e_wired_table_rec_model=False,
|
||||
use_e2e_wireless_table_rec_model=False,
|
||||
use_wired_table_cells_trans_to_html=False,
|
||||
use_wireless_table_cells_trans_to_html=False,
|
||||
use_table_orientation_classify=True,
|
||||
use_ocr_results_with_table_cells=True,
|
||||
**kwargs,
|
||||
):
|
||||
return self.paddlex_pipeline.predict(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_layout_detection=use_layout_detection,
|
||||
use_ocr_model=use_ocr_model,
|
||||
overall_ocr_res=overall_ocr_res,
|
||||
layout_det_res=layout_det_res,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
use_e2e_wired_table_rec_model=use_e2e_wired_table_rec_model,
|
||||
use_e2e_wireless_table_rec_model=use_e2e_wireless_table_rec_model,
|
||||
use_wired_table_cells_trans_to_html=use_wired_table_cells_trans_to_html,
|
||||
use_wireless_table_cells_trans_to_html=use_wireless_table_cells_trans_to_html,
|
||||
use_table_orientation_classify=use_table_orientation_classify,
|
||||
use_ocr_results_with_table_cells=use_ocr_results_with_table_cells,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def predict(
|
||||
self,
|
||||
input,
|
||||
*,
|
||||
use_doc_orientation_classify=None,
|
||||
use_doc_unwarping=None,
|
||||
use_layout_detection=None,
|
||||
use_ocr_model=None,
|
||||
overall_ocr_res=None,
|
||||
layout_det_res=None,
|
||||
text_det_limit_side_len=None,
|
||||
text_det_limit_type=None,
|
||||
text_det_thresh=None,
|
||||
text_det_box_thresh=None,
|
||||
text_det_unclip_ratio=None,
|
||||
text_rec_score_thresh=None,
|
||||
use_e2e_wired_table_rec_model=False,
|
||||
use_e2e_wireless_table_rec_model=False,
|
||||
use_wired_table_cells_trans_to_html=False,
|
||||
use_wireless_table_cells_trans_to_html=False,
|
||||
use_table_orientation_classify=True,
|
||||
use_ocr_results_with_table_cells=True,
|
||||
**kwargs,
|
||||
):
|
||||
return list(
|
||||
self.predict_iter(
|
||||
input,
|
||||
use_doc_orientation_classify=use_doc_orientation_classify,
|
||||
use_doc_unwarping=use_doc_unwarping,
|
||||
use_layout_detection=use_layout_detection,
|
||||
use_ocr_model=use_ocr_model,
|
||||
overall_ocr_res=overall_ocr_res,
|
||||
layout_det_res=layout_det_res,
|
||||
text_det_limit_side_len=text_det_limit_side_len,
|
||||
text_det_limit_type=text_det_limit_type,
|
||||
text_det_thresh=text_det_thresh,
|
||||
text_det_box_thresh=text_det_box_thresh,
|
||||
text_det_unclip_ratio=text_det_unclip_ratio,
|
||||
text_rec_score_thresh=text_rec_score_thresh,
|
||||
use_e2e_wired_table_rec_model=use_e2e_wired_table_rec_model,
|
||||
use_e2e_wireless_table_rec_model=use_e2e_wireless_table_rec_model,
|
||||
use_wired_table_cells_trans_to_html=use_wired_table_cells_trans_to_html,
|
||||
use_wireless_table_cells_trans_to_html=use_wireless_table_cells_trans_to_html,
|
||||
use_table_orientation_classify=use_table_orientation_classify,
|
||||
use_ocr_results_with_table_cells=use_ocr_results_with_table_cells,
|
||||
**kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_cli_subcommand_executor(cls):
|
||||
return TableRecognitionPipelineV2CLISubcommandExecutor()
|
||||
|
||||
def _get_paddlex_config_overrides(self):
|
||||
STRUCTURE = {
|
||||
"SubPipelines.DocPreprocessor.use_doc_orientation_classify": self._params[
|
||||
"use_doc_orientation_classify"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.use_doc_unwarping": self._params[
|
||||
"use_doc_unwarping"
|
||||
],
|
||||
"use_layout_detection": self._params["use_layout_detection"],
|
||||
"use_ocr_model": self._params["use_ocr_model"],
|
||||
"SubModules.LayoutDetection.model_name": self._params[
|
||||
"layout_detection_model_name"
|
||||
],
|
||||
"SubModules.LayoutDetection.model_dir": self._params[
|
||||
"layout_detection_model_dir"
|
||||
],
|
||||
"SubModules.TableClassification.model_name": self._params[
|
||||
"table_classification_model_name"
|
||||
],
|
||||
"SubModules.TableClassification.model_dir": self._params[
|
||||
"table_classification_model_dir"
|
||||
],
|
||||
"SubModules.WiredTableStructureRecognition.model_name": self._params[
|
||||
"wired_table_structure_recognition_model_name"
|
||||
],
|
||||
"SubModules.WiredTableStructureRecognition.model_dir": self._params[
|
||||
"wired_table_structure_recognition_model_dir"
|
||||
],
|
||||
"SubModules.WirelessTableStructureRecognition.model_name": self._params[
|
||||
"wireless_table_structure_recognition_model_name"
|
||||
],
|
||||
"SubModules.WirelessTableStructureRecognition.model_dir": self._params[
|
||||
"wireless_table_structure_recognition_model_dir"
|
||||
],
|
||||
"SubModules.WiredTableCellsDetection.model_name": self._params[
|
||||
"wired_table_cells_detection_model_name"
|
||||
],
|
||||
"SubModules.WiredTableCellsDetection.model_dir": self._params[
|
||||
"wired_table_cells_detection_model_dir"
|
||||
],
|
||||
"SubModules.WirelessTableCellsDetection.model_name": self._params[
|
||||
"wireless_table_cells_detection_model_name"
|
||||
],
|
||||
"SubModules.WirelessTableCellsDetection.model_dir": self._params[
|
||||
"wireless_table_cells_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_name": self._params[
|
||||
"doc_orientation_classify_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dir": self._params[
|
||||
"doc_orientation_classify_model_dir"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_name": self._params[
|
||||
"doc_unwarping_model_name"
|
||||
],
|
||||
"SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dir": self._params[
|
||||
"doc_unwarping_model_dir"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.model_name": self._params[
|
||||
"text_detection_model_name"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.model_dir": self._params[
|
||||
"text_detection_model_dir"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_len": self._params[
|
||||
"text_det_limit_side_len"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.limit_type": self._params[
|
||||
"text_det_limit_type"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.thresh": self._params[
|
||||
"text_det_thresh"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.box_thresh": self._params[
|
||||
"text_det_box_thresh"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratio": self._params[
|
||||
"text_det_unclip_ratio"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.model_name": self._params[
|
||||
"text_recognition_model_name"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dir": self._params[
|
||||
"text_recognition_model_dir"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_size": self._params[
|
||||
"text_recognition_batch_size"
|
||||
],
|
||||
"SubPipelines.GeneralOCR.SubModules.TextRecognition.score_thresh": self._params[
|
||||
"text_rec_score_thresh"
|
||||
],
|
||||
}
|
||||
return create_config_from_structure(STRUCTURE)
|
||||
|
||||
|
||||
class TableRecognitionPipelineV2CLISubcommandExecutor(PipelineCLISubcommandExecutor):
|
||||
@property
|
||||
def subparser_name(self):
|
||||
return "table_recognition_v2"
|
||||
|
||||
def _update_subparser(self, subparser):
|
||||
add_simple_inference_args(subparser)
|
||||
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the layout detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--layout_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the layout detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--table_classification_model_name",
|
||||
type=str,
|
||||
help="Name of the table classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--table_classification_model_dir",
|
||||
type=str,
|
||||
help="Path to the table classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_structure_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the wired table structure recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_structure_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table structure recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_structure_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the wireless table structure recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_structure_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table structure recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_cells_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the wired table cells detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wired_table_cells_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the wired table cells detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_cells_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the wireless table cells detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--wireless_table_cells_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the wireless table cells detection model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_name",
|
||||
type=str,
|
||||
help="Name of the document image orientation classification model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_orientation_classify_model_dir",
|
||||
type=str,
|
||||
help="Path to the document image orientation classification model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_name",
|
||||
type=str,
|
||||
help="Name of the text image unwarping model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--doc_unwarping_model_dir",
|
||||
type=str,
|
||||
help="Path to the image unwarping model directory.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_name",
|
||||
type=str,
|
||||
help="Name of the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_detection_model_dir",
|
||||
type=str,
|
||||
help="Path to the text detection model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_side_len",
|
||||
type=int,
|
||||
help="This sets a limit on the side length of the input image for the text detection model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_limit_type",
|
||||
type=str,
|
||||
help="This determines how the side length limit is applied to the input image before feeding it into the text deteciton model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_thresh",
|
||||
type=float,
|
||||
help="Detection pixel threshold for the text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_box_thresh",
|
||||
type=float,
|
||||
help="Detection box threshold for the text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_det_unclip_ratio",
|
||||
type=float,
|
||||
help="Text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_name",
|
||||
type=str,
|
||||
help="Name of the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_model_dir",
|
||||
type=str,
|
||||
help="Path to the text recognition model directory.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_recognition_batch_size",
|
||||
type=int,
|
||||
help="Batch size for the text recognition model.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--text_rec_score_thresh",
|
||||
type=float,
|
||||
help="Text recognition threshold used in general OCR. Text results with scores greater than this threshold are retained.",
|
||||
)
|
||||
|
||||
subparser.add_argument(
|
||||
"--use_doc_orientation_classify",
|
||||
type=str2bool,
|
||||
help="Whether to use document image orientation classification.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_doc_unwarping",
|
||||
type=str2bool,
|
||||
help="Whether to use text image unwarping.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_layout_detection",
|
||||
type=str2bool,
|
||||
help="Whether to use layout detection.",
|
||||
)
|
||||
subparser.add_argument(
|
||||
"--use_ocr_model",
|
||||
type=str2bool,
|
||||
help="Whether to use OCR models.",
|
||||
)
|
||||
|
||||
def execute_with_args(self, args):
|
||||
params = get_subcommand_args(args)
|
||||
perform_simple_inference(TableRecognitionPipelineV2, params)
|
||||
30
paddleocr/_pipelines/utils.py
Normal file
30
paddleocr/_pipelines/utils.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
def create_config_from_structure(structure, *, unset=None, config=None):
|
||||
if config is None:
|
||||
config = {}
|
||||
for k, v in structure.items():
|
||||
if v is unset:
|
||||
continue
|
||||
idx = k.find(".")
|
||||
if idx == -1:
|
||||
config[k] = v
|
||||
else:
|
||||
sk = k[:idx]
|
||||
if sk not in config:
|
||||
config[sk] = {}
|
||||
create_config_from_structure({k[idx + 1 :]: v}, config=config[sk])
|
||||
return config
|
||||
Reference in New Issue
Block a user