|
|
@@ -0,0 +1,104 @@
|
|
|
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
|
|
|
+#
|
|
|
+# 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 typing import Any, Union, Dict, List, Tuple
|
|
|
+import numpy as np
|
|
|
+
|
|
|
+from ....modules.image_unwarping.model_list import MODELS
|
|
|
+from ...common.batch_sampler import ImageBatchSampler
|
|
|
+from ...common.reader import ReadImage
|
|
|
+from ..common import (
|
|
|
+ Normalize,
|
|
|
+ ToCHWImage,
|
|
|
+ ToBatch,
|
|
|
+ StaticInfer,
|
|
|
+)
|
|
|
+from ..base import BasicPredictor
|
|
|
+from .processors import DocTrPostProcess
|
|
|
+from .result import DocTrResult
|
|
|
+
|
|
|
+
|
|
|
+class WarpPredictor(BasicPredictor):
|
|
|
+ """WarpPredictor that inherits from BasicPredictor."""
|
|
|
+
|
|
|
+ entities = MODELS
|
|
|
+
|
|
|
+ def __init__(self, *args: List, **kwargs: Dict) -> None:
|
|
|
+ """Initializes WarpPredictor.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ *args: Arbitrary positional arguments passed to the superclass.
|
|
|
+ **kwargs: Arbitrary keyword arguments passed to the superclass.
|
|
|
+ """
|
|
|
+ super().__init__(*args, **kwargs)
|
|
|
+ self.preprocessors, self.infer, self.postprocessors = self._build()
|
|
|
+
|
|
|
+ def _build_batch_sampler(self) -> ImageBatchSampler:
|
|
|
+ """Builds and returns an ImageBatchSampler instance.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ ImageBatchSampler: An instance of ImageBatchSampler.
|
|
|
+ """
|
|
|
+ return ImageBatchSampler()
|
|
|
+
|
|
|
+ def _get_result_class(self) -> type:
|
|
|
+ """Returns the warpping result, DocTrResult.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ type: The DocTrResult.
|
|
|
+ """
|
|
|
+ return DocTrResult
|
|
|
+
|
|
|
+ def _build(self) -> Tuple:
|
|
|
+ """Build the preprocessors, inference engine, and postprocessors based on the configuration.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ tuple: A tuple containing the preprocessors, inference engine, and postprocessors.
|
|
|
+ """
|
|
|
+ preprocessors = {"Read": ReadImage(format="RGB")}
|
|
|
+ preprocessors["Normalize"] = Normalize(mean=0.0, std=1.0, scale=1.0 / 255)
|
|
|
+ preprocessors["ToCHW"] = ToCHWImage()
|
|
|
+ preprocessors["ToBatch"] = ToBatch()
|
|
|
+
|
|
|
+ infer = StaticInfer(
|
|
|
+ model_dir=self.model_dir,
|
|
|
+ model_prefix=self.MODEL_FILE_PREFIX,
|
|
|
+ option=self.pp_option,
|
|
|
+ )
|
|
|
+
|
|
|
+ postprocessors = {"DocTrPostProcess": DocTrPostProcess()}
|
|
|
+ return preprocessors, infer, postprocessors
|
|
|
+
|
|
|
+ def process(self, batch_data: List[Union[str, np.ndarray]]) -> Dict[str, Any]:
|
|
|
+ """
|
|
|
+ Process a batch of data through the preprocessing, inference, and postprocessing.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: A dictionary containing the input path, raw image, class IDs, scores, and label names for every instance of the batch. Keys include 'input_path', 'input_img', 'class_ids', 'scores', and 'label_names'.
|
|
|
+ """
|
|
|
+ batch_raw_imgs = self.preprocessors["Read"](imgs=batch_data)
|
|
|
+ batch_imgs = self.preprocessors["Normalize"](imgs=batch_raw_imgs)
|
|
|
+ batch_imgs = self.preprocessors["ToCHW"](imgs=batch_imgs)
|
|
|
+ x = self.preprocessors["ToBatch"](imgs=batch_imgs)
|
|
|
+ batch_preds = self.infer(x=x)
|
|
|
+ batch_warp_preds = self.postprocessors["DocTrPostProcess"](batch_preds)
|
|
|
+
|
|
|
+ return {
|
|
|
+ "input_path": batch_data,
|
|
|
+ "input_img": batch_raw_imgs,
|
|
|
+ "doctr_img": batch_warp_preds,
|
|
|
+ }
|