first commit
Some checks are pending
Build/Publish Develop Docs / deploy (push) Waiting to run

This commit is contained in:
2025-07-02 08:57:16 +03:00
commit 56532cc9a9
1901 changed files with 457695 additions and 0 deletions

67
paddleocr/__init__.py Normal file
View File

@@ -0,0 +1,67 @@
# 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 ._models import (
DocImgOrientationClassification,
DocVLM,
FormulaRecognition,
LayoutDetection,
SealTextDetection,
TableCellsDetection,
TableClassification,
TableStructureRecognition,
TextDetection,
TextImageUnwarping,
TextLineOrientationClassification,
TextRecognition,
)
from ._pipelines import (
DocPreprocessor,
DocUnderstanding,
FormulaRecognitionPipeline,
PaddleOCR,
PPChatOCRv4Doc,
PPDocTranslation,
PPStructureV3,
SealRecognition,
TableRecognitionPipelineV2,
)
from ._utils.logging import logger
from ._version import version as __version__
__all__ = [
"DocImgOrientationClassification",
"DocVLM",
"FormulaRecognition",
"SealTextDetection",
"LayoutDetection",
"TableCellsDetection",
"TableClassification",
"TableStructureRecognition",
"TextDetection",
"TextImageUnwarping",
"TextLineOrientationClassification",
"TextRecognition",
"DocPreprocessor",
"DocUnderstanding",
"FormulaRecognitionPipeline",
"PaddleOCR",
"PPChatOCRv4Doc",
"PPDocTranslation",
"PPStructureV3",
"SealRecognition",
"TableRecognitionPipelineV2",
"logger",
"__version__",
]

39
paddleocr/__main__.py Normal file
View File

@@ -0,0 +1,39 @@
# 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 os
import sys
from ._cli import main
def console_entry() -> int:
# See https://docs.python.org/3/library/signal.html#note-on-sigpipe
try:
# Flush output here to force SIGPIPE to be triggered while inside this
# try block.
main()
sys.stdout.flush()
sys.stderr.flush()
except BrokenPipeError:
# Python flushes standard streams on exit;
# redirect remaining output to devnull to avoid another BrokenPipeError
# at shutdown.
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1)
if __name__ == "__main__":
console_entry()

25
paddleocr/_abstract.py Normal file
View File

@@ -0,0 +1,25 @@
# 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
class CLISubcommandExecutor(metaclass=abc.ABCMeta):
@abc.abstractmethod
def add_subparser(self, subparsers):
raise NotImplementedError
@abc.abstractmethod
def execute_with_args(self, args):
raise NotImplementedError

126
paddleocr/_cli.py Normal file
View 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 argparse
import logging
import subprocess
import sys
import warnings
from ._models import (
DocImgOrientationClassification,
DocVLM,
FormulaRecognition,
LayoutDetection,
SealTextDetection,
TableCellsDetection,
TableClassification,
TableStructureRecognition,
TextDetection,
TextImageUnwarping,
TextLineOrientationClassification,
TextRecognition,
)
from ._pipelines import (
DocPreprocessor,
DocUnderstanding,
FormulaRecognitionPipeline,
PaddleOCR,
PPChatOCRv4Doc,
PPDocTranslation,
PPStructureV3,
SealRecognition,
TableRecognitionPipelineV2,
)
from ._version import version
from ._utils.deprecation import CLIDeprecationWarning
from ._utils.logging import logger
def _register_pipelines(subparsers):
for cls in [
DocPreprocessor,
DocUnderstanding,
FormulaRecognitionPipeline,
PaddleOCR,
PPChatOCRv4Doc,
PPDocTranslation,
PPStructureV3,
SealRecognition,
TableRecognitionPipelineV2,
]:
subcommand_executor = cls.get_cli_subcommand_executor()
subparser = subcommand_executor.add_subparser(subparsers)
subparser.set_defaults(executor=subcommand_executor.execute_with_args)
def _register_models(subparsers):
for cls in [
DocImgOrientationClassification,
DocVLM,
FormulaRecognition,
LayoutDetection,
SealTextDetection,
TableCellsDetection,
TableClassification,
TableStructureRecognition,
TextDetection,
TextImageUnwarping,
TextLineOrientationClassification,
TextRecognition,
]:
subcommand_executor = cls.get_cli_subcommand_executor()
subparser = subcommand_executor.add_subparser(subparsers)
subparser.set_defaults(executor=subcommand_executor.execute_with_args)
def _register_install_hpi_deps_command(subparsers):
def _install_hpi_deps(args):
hpip = f"hpi-{args.variant}"
try:
subprocess.check_call(["paddlex", "--install", hpip])
subprocess.check_call(["paddlex", "--install", "paddle2onnx"])
except subprocess.CalledProcessError:
sys.exit("Failed to install dependencies")
subparser = subparsers.add_parser("install_hpi_deps")
subparser.add_argument("variant", type=str, choices=["cpu", "gpu", "npu"])
subparser.set_defaults(executor=_install_hpi_deps)
def _get_parser():
parser = argparse.ArgumentParser(prog="paddleocr")
parser.add_argument(
"-v", "--version", action="version", version=f"%(prog)s {version}"
)
subparsers = parser.add_subparsers(dest="subcommand")
_register_pipelines(subparsers)
_register_models(subparsers)
_register_install_hpi_deps_command(subparsers)
return parser
def _execute(args):
args.executor(args)
def main():
logger.setLevel(logging.INFO)
warnings.filterwarnings("default", category=CLIDeprecationWarning)
parser = _get_parser()
args = parser.parse_args()
if args.subcommand is None:
parser.print_usage(sys.stderr)
sys.exit(2)
_execute(args)

151
paddleocr/_common_args.py Normal file
View File

@@ -0,0 +1,151 @@
# 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.inference import PaddlePredictorOption
from paddlex.utils.device import get_default_device, parse_device
from ._constants import (
DEFAULT_CPU_THREADS,
DEFAULT_DEVICE,
DEFAULT_ENABLE_MKLDNN,
DEFAULT_MKLDNN_CACHE_CAPACITY,
DEFAULT_PRECISION,
DEFAULT_USE_TENSORRT,
SUPPORTED_PRECISION_LIST,
)
from ._utils.cli import str2bool
def parse_common_args(kwargs, *, default_enable_hpi):
default_vals = {
"device": DEFAULT_DEVICE,
"enable_hpi": default_enable_hpi,
"use_tensorrt": DEFAULT_USE_TENSORRT,
"precision": DEFAULT_PRECISION,
"enable_mkldnn": DEFAULT_ENABLE_MKLDNN,
"mkldnn_cache_capacity": DEFAULT_MKLDNN_CACHE_CAPACITY,
"cpu_threads": DEFAULT_CPU_THREADS,
}
unknown_names = kwargs.keys() - default_vals.keys()
for name in unknown_names:
raise ValueError(f"Unknown argument: {name}")
kwargs = {**default_vals, **kwargs}
if kwargs["precision"] not in SUPPORTED_PRECISION_LIST:
raise ValueError(
f"Invalid precision: {kwargs['precision']}. Supported values are: {SUPPORTED_PRECISION_LIST}."
)
kwargs["use_pptrt"] = kwargs.pop("use_tensorrt")
kwargs["pptrt_precision"] = kwargs.pop("precision")
return kwargs
def prepare_common_init_args(model_name, common_args):
device = common_args["device"]
if device is None:
device = get_default_device()
device_type, device_ids = parse_device(device)
if device_ids is not None:
device_id = device_ids[0]
else:
device_id = None
init_kwargs = {}
init_kwargs["use_hpip"] = common_args["enable_hpi"]
init_kwargs["hpi_config"] = {
"device_type": device_type,
"device_id": device_id,
}
pp_option = PaddlePredictorOption(
model_name, device_type=device_type, device_id=device_id
)
if device_type == "gpu":
if common_args["use_pptrt"]:
if common_args["pptrt_precision"] == "fp32":
pp_option.run_mode = "trt_fp32"
else:
assert common_args["pptrt_precision"] == "fp16", common_args[
"pptrt_precision"
]
pp_option.run_mode = "trt_fp16"
else:
pp_option.run_mode = "paddle"
elif device_type == "cpu":
enable_mkldnn = common_args["enable_mkldnn"]
if enable_mkldnn:
pp_option.run_mode = "mkldnn"
pp_option.mkldnn_cache_capacity = common_args["mkldnn_cache_capacity"]
else:
pp_option.run_mode = "paddle"
pp_option.cpu_threads = common_args["cpu_threads"]
else:
pp_option.run_mode = "paddle"
init_kwargs["pp_option"] = pp_option
return init_kwargs
def add_common_cli_opts(parser, *, default_enable_hpi, allow_multiple_devices):
if allow_multiple_devices:
help_ = "Device(s) to use for inference, e.g., `cpu`, `gpu`, `npu`, `gpu:0`, `gpu:0,1`. If multiple devices are specified, inference will be performed in parallel. Note that parallel inference is not always supported. By default, GPU 0 will be used if available; otherwise, the CPU will be used."
else:
help_ = "Device to use for inference, e.g., `cpu`, `gpu`, `npu`, `gpu:0`. By default, GPU 0 will be used if available; otherwise, the CPU will be used."
parser.add_argument(
"--device",
type=str,
default=DEFAULT_DEVICE,
help=help_,
)
parser.add_argument(
"--enable_hpi",
type=str2bool,
default=default_enable_hpi,
help="Enable the high performance inference.",
)
parser.add_argument(
"--use_tensorrt",
type=str2bool,
default=DEFAULT_USE_TENSORRT,
help="Whether to use the Paddle Inference TensorRT subgraph engine. If the model does not support TensorRT acceleration, even if this flag is set, acceleration will not be used.",
)
parser.add_argument(
"--precision",
type=str,
default=DEFAULT_PRECISION,
choices=SUPPORTED_PRECISION_LIST,
help="Precision for TensorRT when using the Paddle Inference TensorRT subgraph engine.",
)
parser.add_argument(
"--enable_mkldnn",
type=str2bool,
default=DEFAULT_ENABLE_MKLDNN,
help="Enable MKL-DNN acceleration for inference. If MKL-DNN is unavailable or the model does not support it, acceleration will not be used even if this flag is set.",
)
parser.add_argument(
"--mkldnn_cache_capacity",
type=int,
default=DEFAULT_MKLDNN_CACHE_CAPACITY,
help="MKL-DNN cache capacity.",
)
parser.add_argument(
"--cpu_threads",
type=int,
default=DEFAULT_CPU_THREADS,
help="Number of threads to use for inference on CPUs.",
)

21
paddleocr/_constants.py Normal file
View File

@@ -0,0 +1,21 @@
# 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.
DEFAULT_DEVICE = None
DEFAULT_USE_TENSORRT = False
DEFAULT_PRECISION = "fp32"
DEFAULT_ENABLE_MKLDNN = True
DEFAULT_MKLDNN_CACHE_CAPACITY = 10
DEFAULT_CPU_THREADS = 10
SUPPORTED_PRECISION_LIST = ["fp32", "fp16"]

19
paddleocr/_env.py Normal file
View File

@@ -0,0 +1,19 @@
# 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 os
DISABLE_AUTO_LOGGING_CONFIG = (
os.getenv("PADDLEOCR_DISABLE_AUTO_LOGGING_CONFIG", "0") == "1"
)

View File

@@ -0,0 +1,41 @@
# 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_img_orientation_classification import DocImgOrientationClassification
from .doc_vlm import DocVLM
from .formula_recognition import FormulaRecognition
from .layout_detection import LayoutDetection
from .seal_text_detection import SealTextDetection
from .table_cells_detection import TableCellsDetection
from .table_classification import TableClassification
from .table_structure_recognition import TableStructureRecognition
from .text_detection import TextDetection
from .text_image_unwarping import TextImageUnwarping
from .textline_orientation_classification import TextLineOrientationClassification
from .text_recognition import TextRecognition
__all__ = [
"DocImgOrientationClassification",
"DocVLM",
"FormulaRecognition",
"LayoutDetection",
"SealTextDetection",
"TableCellsDetection",
"TableClassification",
"TableStructureRecognition",
"TextDetection",
"TextImageUnwarping",
"TextLineOrientationClassification",
"TextRecognition",
]

View File

@@ -0,0 +1,58 @@
# 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
from .._utils.cli import (
add_simple_inference_args,
get_subcommand_args,
perform_simple_inference,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
class ImageClassification(PaddleXPredictorWrapper):
def __init__(
self,
*,
topk=None,
**kwargs,
):
self._extra_init_args = {
"topk": topk,
}
super().__init__(**kwargs)
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class ImageClassificationSubcommandExecutor(PredictorCLISubcommandExecutor):
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
subparser.add_argument(
"--topk",
type=int,
help="Top-k value for prediction results.",
)
@property
@abc.abstractmethod
def wrapper_cls(self):
raise NotImplementedError
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(self.wrapper_cls, params)

View File

@@ -0,0 +1,87 @@
# 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
from .._utils.cli import (
add_simple_inference_args,
get_subcommand_args,
perform_simple_inference,
str2bool,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
class ObjectDetection(PaddleXPredictorWrapper):
def __init__(
self,
*,
img_size=None,
threshold=None,
layout_nms=None,
layout_unclip_ratio=None,
layout_merge_bboxes_mode=None,
**kwargs,
):
self._extra_init_args = {
"img_size": img_size,
"threshold": threshold,
"layout_nms": layout_nms,
"layout_unclip_ratio": layout_unclip_ratio,
"layout_merge_bboxes_mode": layout_merge_bboxes_mode,
}
super().__init__(**kwargs)
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class ObjectDetectionSubcommandExecutor(PredictorCLISubcommandExecutor):
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
subparser.add_argument(
"--img_size",
type=int,
help="Input image size (w, h).",
)
subparser.add_argument(
"--threshold",
type=float,
help="Threshold for filtering out low-confidence predictions.",
)
subparser.add_argument(
"--layout_nms",
type=str2bool,
help="Whether to use layout-aware NMS.",
)
subparser.add_argument(
"--layout_unclip_ratio",
type=float,
help="Ratio of unclipping the bounding box.",
)
subparser.add_argument(
"--layout_merge_bboxes_mode",
type=str,
help="Mode for merging bounding boxes.",
)
@property
@abc.abstractmethod
def wrapper_cls(self):
raise NotImplementedError
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(self.wrapper_cls, params)

View File

@@ -0,0 +1,75 @@
# 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.
class TextDetectionMixin:
def __init__(
self,
*,
limit_side_len=None,
limit_type=None,
thresh=None,
box_thresh=None,
unclip_ratio=None,
input_shape=None,
**kwargs,
):
self._extra_init_args = {
"limit_side_len": limit_side_len,
"limit_type": limit_type,
"thresh": thresh,
"box_thresh": box_thresh,
"unclip_ratio": unclip_ratio,
"input_shape": input_shape,
}
super().__init__(**kwargs)
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class TextDetectionSubcommandExecutorMixin:
def _add_text_detection_args(self, subparser):
subparser.add_argument(
"--limit_side_len",
type=int,
help="This sets a limit on the side length of the input image for the model.",
)
subparser.add_argument(
"--limit_type",
type=str,
help="This determines how the side length limit is applied to the input image before feeding it into the model.",
)
subparser.add_argument(
"--thresh",
type=float,
help="Detection pixel threshold for the model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.",
)
subparser.add_argument(
"--box_thresh",
type=float,
help="Detection box threshold for the 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(
"--unclip_ratio",
type=float,
help="Expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.",
)
subparser.add_argument(
"--input_shape",
nargs=3,
type=int,
metavar=("C", "H", "W"),
help="Input shape of the model.",
)

98
paddleocr/_models/base.py Normal file
View File

@@ -0,0 +1,98 @@
# 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
from paddlex import create_predictor
from .._abstract import CLISubcommandExecutor
from .._common_args import (
add_common_cli_opts,
parse_common_args,
prepare_common_init_args,
)
_DEFAULT_ENABLE_HPI = False
class PaddleXPredictorWrapper(metaclass=abc.ABCMeta):
def __init__(
self,
*,
model_name=None,
model_dir=None,
**common_args,
):
super().__init__()
self._model_name = (
model_name if model_name is not None else self.default_model_name
)
self._model_dir = model_dir
self._common_args = parse_common_args(
common_args, default_enable_hpi=_DEFAULT_ENABLE_HPI
)
self.paddlex_predictor = self._create_paddlex_predictor()
@property
@abc.abstractmethod
def default_model_name(self):
raise NotImplementedError
def predict_iter(self, *args, **kwargs):
return self.paddlex_predictor.predict(*args, **kwargs)
def predict(self, *args, **kwargs):
result = list(self.predict_iter(*args, **kwargs))
return result
@classmethod
@abc.abstractmethod
def get_cli_subcommand_executor(cls):
raise NotImplementedError
def _get_extra_paddlex_predictor_init_args(self):
return {}
def _create_paddlex_predictor(self):
kwargs = prepare_common_init_args(self._model_name, self._common_args)
kwargs = {**self._get_extra_paddlex_predictor_init_args(), **kwargs}
# Should we check model names?
return create_predictor(
model_name=self._model_name, model_dir=self._model_dir, **kwargs
)
class PredictorCLISubcommandExecutor(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)
subparser.add_argument("--model_name", type=str, help="Name of the model.")
subparser.add_argument(
"--model_dir", type=str, help="Directory where the model is stored."
)
add_common_cli_opts(
subparser,
default_enable_hpi=_DEFAULT_ENABLE_HPI,
allow_multiple_devices=False,
)
return subparser
@abc.abstractmethod
def _update_subparser(self, subparser):
raise NotImplementedError

View File

@@ -0,0 +1,40 @@
# 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 ._image_classification import (
ImageClassification,
ImageClassificationSubcommandExecutor,
)
class DocImgOrientationClassification(ImageClassification):
@property
def default_model_name(self):
return "PP-LCNet_x1_0_doc_ori"
@classmethod
def get_cli_subcommand_executor(cls):
return DocImgOrientationClassificationSubcommandExecutor()
class DocImgOrientationClassificationSubcommandExecutor(
ImageClassificationSubcommandExecutor
):
@property
def subparser_name(self):
return "doc_img_orientation_classification"
@property
def wrapper_cls(self):
return DocImgOrientationClassification

View File

@@ -0,0 +1,63 @@
# 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 PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
from paddlex.utils.pipeline_arguments import custom_type
class DocVLM(PaddleXPredictorWrapper):
def __init__(
self,
*args,
**kwargs,
):
self._extra_init_args = {}
super().__init__(*args, **kwargs)
@property
def default_model_name(self):
return "PP-DocBee2-3B"
@classmethod
def get_cli_subcommand_executor(cls):
return DocVLMSubcommandExecutor()
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class DocVLMSubcommandExecutor(PredictorCLISubcommandExecutor):
input_validator = staticmethod(custom_type(dict))
@property
def subparser_name(self):
return "doc_vlm"
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"}`.',
)
def execute_with_args(self, args):
params = get_subcommand_args(args)
params["input"] = self.input_validator(params["input"])
perform_simple_inference(DocVLM, params)

View File

@@ -0,0 +1,54 @@
# 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,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
class FormulaRecognition(PaddleXPredictorWrapper):
def __init__(
self,
*args,
**kwargs,
):
self._extra_init_args = {}
super().__init__(*args, **kwargs)
@property
def default_model_name(self):
return "PP-FormulaNet_plus-M"
@classmethod
def get_cli_subcommand_executor(cls):
return FormulaRecognitionSubcommandExecutor()
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class FormulaRecognitionSubcommandExecutor(PredictorCLISubcommandExecutor):
@property
def subparser_name(self):
return "formula_recognition"
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(FormulaRecognition, params)

View File

@@ -0,0 +1,38 @@
# 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 ._object_detection import (
ObjectDetection,
ObjectDetectionSubcommandExecutor,
)
class LayoutDetection(ObjectDetection):
@property
def default_model_name(self):
return "PP-DocLayout_plus-L"
@classmethod
def get_cli_subcommand_executor(cls):
return LayoutDetectionSubcommandExecutor()
class LayoutDetectionSubcommandExecutor(ObjectDetectionSubcommandExecutor):
@property
def subparser_name(self):
return "layout_detection"
@property
def wrapper_cls(self):
return LayoutDetection

View File

@@ -0,0 +1,47 @@
# 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,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
from ._text_detection import TextDetectionMixin, TextDetectionSubcommandExecutorMixin
class SealTextDetection(TextDetectionMixin, PaddleXPredictorWrapper):
@property
def default_model_name(self):
return "PP-OCRv4_mobile_seal_det"
@classmethod
def get_cli_subcommand_executor(cls):
return SealTextDetectionSubcommandExecutor()
class SealTextDetectionSubcommandExecutor(
TextDetectionSubcommandExecutorMixin, PredictorCLISubcommandExecutor
):
@property
def subparser_name(self):
return "seal_text_detection"
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
self._add_text_detection_args(subparser)
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(SealTextDetection, params)

View File

@@ -0,0 +1,38 @@
# 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 ._object_detection import (
ObjectDetection,
ObjectDetectionSubcommandExecutor,
)
class TableCellsDetection(ObjectDetection):
@property
def default_model_name(self):
return "RT-DETR-L_wired_table_cell_det"
@classmethod
def get_cli_subcommand_executor(cls):
return TableCellsDetectionSubcommandExecutor()
class TableCellsDetectionSubcommandExecutor(ObjectDetectionSubcommandExecutor):
@property
def subparser_name(self):
return "table_cells_detection"
@property
def wrapper_cls(self):
return TableCellsDetection

View File

@@ -0,0 +1,38 @@
# 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 ._image_classification import (
ImageClassification,
ImageClassificationSubcommandExecutor,
)
class TableClassification(ImageClassification):
@property
def default_model_name(self):
return "PP-LCNet_x1_0_table_cls"
@classmethod
def get_cli_subcommand_executor(cls):
return TableClassificationSubcommandExecutor()
class TableClassificationSubcommandExecutor(ImageClassificationSubcommandExecutor):
@property
def subparser_name(self):
return "table_classification"
@property
def wrapper_cls(self):
return TableClassification

View File

@@ -0,0 +1,54 @@
# 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,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
class TableStructureRecognition(PaddleXPredictorWrapper):
def __init__(
self,
*args,
**kwargs,
):
self._extra_init_args = {}
super().__init__(*args, **kwargs)
@property
def default_model_name(self):
return "SLANet"
@classmethod
def get_cli_subcommand_executor(cls):
return TableStructureRecognitionSubcommandExecutor()
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class TableStructureRecognitionSubcommandExecutor(PredictorCLISubcommandExecutor):
@property
def subparser_name(self):
return "table_structure_recognition"
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(TableStructureRecognition, params)

View File

@@ -0,0 +1,47 @@
# 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,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
from ._text_detection import TextDetectionMixin, TextDetectionSubcommandExecutorMixin
class TextDetection(TextDetectionMixin, PaddleXPredictorWrapper):
@property
def default_model_name(self):
return "PP-OCRv5_server_det"
@classmethod
def get_cli_subcommand_executor(cls):
return TextDetectionSubcommandExecutor()
class TextDetectionSubcommandExecutor(
TextDetectionSubcommandExecutorMixin, PredictorCLISubcommandExecutor
):
@property
def subparser_name(self):
return "text_detection"
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
self._add_text_detection_args(subparser)
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(TextDetection, params)

View File

@@ -0,0 +1,54 @@
# 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,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
class TextImageUnwarping(PaddleXPredictorWrapper):
def __init__(
self,
*args,
**kwargs,
):
self._extra_init_args = {}
super().__init__(*args, **kwargs)
@property
def default_model_name(self):
return "UVDoc"
@classmethod
def get_cli_subcommand_executor(cls):
return TextImageUnwarpingSubcommandExecutor()
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class TextImageUnwarpingSubcommandExecutor(PredictorCLISubcommandExecutor):
@property
def subparser_name(self):
return "text_image_unwarping"
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(TextImageUnwarping, params)

View File

@@ -0,0 +1,64 @@
# 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,
)
from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
class TextRecognition(PaddleXPredictorWrapper):
def __init__(
self,
*,
input_shape=None,
**kwargs,
):
self._extra_init_args = {
"input_shape": input_shape,
}
super().__init__(**kwargs)
@property
def default_model_name(self):
return "PP-OCRv5_server_rec"
@classmethod
def get_cli_subcommand_executor(cls):
return TextRecognitionSubcommandExecutor()
def _get_extra_paddlex_predictor_init_args(self):
return self._extra_init_args
class TextRecognitionSubcommandExecutor(PredictorCLISubcommandExecutor):
@property
def subparser_name(self):
return "text_recognition"
def _update_subparser(self, subparser):
add_simple_inference_args(subparser)
subparser.add_argument(
"--input_shape",
nargs=3,
type=int,
metavar=("C", "H", "W"),
help="Input shape of the model.",
)
def execute_with_args(self, args):
params = get_subcommand_args(args)
perform_simple_inference(TextRecognition, params)

View File

@@ -0,0 +1,40 @@
# 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 ._image_classification import (
ImageClassification,
ImageClassificationSubcommandExecutor,
)
class TextLineOrientationClassification(ImageClassification):
@property
def default_model_name(self):
return "PP-LCNet_x0_25_textline_ori"
@classmethod
def get_cli_subcommand_executor(cls):
return TextLineOrientationClassificationSubcommandExecutor()
class TextLineOrientationClassificationSubcommandExecutor(
ImageClassificationSubcommandExecutor
):
@property
def subparser_name(self):
return "textline_orientation_classification"
@property
def wrapper_cls(self):
return TextLineOrientationClassification

View 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",
]

View 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

View 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)

View 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)

View 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
View 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)

View 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}")

View 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)

View 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",
},
)

View 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)

View 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)

View 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

View File

@@ -0,0 +1,13 @@
# 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.

72
paddleocr/_utils/cli.py Normal file
View File

@@ -0,0 +1,72 @@
# 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 time
from .logging import logger
def str2bool(v, /):
return v.lower() in ("true", "yes", "t", "y", "1")
def get_subcommand_args(args):
args = vars(args).copy()
args.pop("subcommand")
args.pop("executor")
return args
def add_simple_inference_args(subparser, *, input_help=None):
if input_help is None:
input_help = "Input path or URL."
subparser.add_argument(
"-i",
"--input",
type=str,
required=True,
help=input_help,
)
subparser.add_argument(
"--save_path",
type=str,
help="Path to the output directory.",
)
def perform_simple_inference(wrapper_cls, params, predict_param_names=None):
params = params.copy()
input_ = params.pop("input")
save_path = params.pop("save_path")
if predict_param_names is not None:
predict_params = {}
for name in predict_param_names:
predict_params[name] = params.pop(name)
else:
predict_params = {}
init_params = params
wrapper = wrapper_cls(**init_params)
result = wrapper.predict_iter(input_, **predict_params)
t1 = time.time()
for i, res in enumerate(result):
logger.info(f"Processed item {i} in {(time.time()-t1) * 1000} ms")
t1 = time.time()
res.print()
if save_path:
res.save_all(save_path)

View File

@@ -0,0 +1,42 @@
# 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 argparse
import sys
import warnings
from typing_extensions import deprecated as deprecated
class CLIDeprecationWarning(DeprecationWarning):
pass
class DeprecatedOptionAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
assert option_string
warnings.warn(
f"The option `{option_string}` has been deprecated and will be removed in the future. Please refer to the documentation for more details.",
CLIDeprecationWarning,
)
setattr(namespace, self.dest, values)
def warn_deprecated_param(name, new_name=None):
msg = (
f"The parameter `{name}` has been deprecated and will be removed in the future."
)
if new_name is not None:
msg += f" Please use `{new_name}` instead."
warnings.warn(msg, DeprecationWarning, stacklevel=3)

View File

@@ -0,0 +1,39 @@
# 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 logging
from .._env import DISABLE_AUTO_LOGGING_CONFIG
LOGGER_NAME = "paddleocr"
logger = logging.getLogger(LOGGER_NAME)
def _set_up_logger():
if DISABLE_AUTO_LOGGING_CONFIG:
return
# Basically compatible with PaddleOCR 2.x, except for logging to stderr
formatter = logging.Formatter(
"[%(asctime)s] %(name)s %(levelname)s: %(message)s", datefmt="%Y/%m/%d %H:%M:%S"
)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.setLevel(logging.ERROR)
logger.propagate = False
_set_up_logger()

20
paddleocr/_version.py Normal file
View File

@@ -0,0 +1,20 @@
# 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 importlib.metadata
try:
version = importlib.metadata.version(__package__)
except importlib.metadata.PackageNotFoundError:
version = "0.0.0"