Skip to content

πŸ“„ Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

6.1 KB
Raw Blame
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
import abc
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Dict, List, Tuple, Union

import numpy as np
from omegaconf import OmegaConf

from ..utils.log import logger
from ..utils.model_resolver import normalize_lang, route_to_model_key
from ..utils.typings import EngineType, ModelType, OCRVersion, TaskType
from ..utils.utils import import_package

cur_dir = Path(__file__).resolve().parent.parent
MODEL_URL_PATH = cur_dir / "default_models.yaml"


def get_engine(engine_type: EngineType):
    logger.info("Using engine_name: %s", engine_type.value)

    if engine_type == EngineType.ONNXRUNTIME:
        if not import_package(engine_type.value):
            raise ImportError(f"{engine_type.value} is not installed.")

        from .onnxruntime import OrtInferSession

        return OrtInferSession

    if engine_type == EngineType.OPENVINO:
        if not import_package(engine_type.value):
            raise ImportError(f"{engine_type.value} is not installed")

        from .openvino import OpenVINOInferSession

        return OpenVINOInferSession

    if engine_type == EngineType.PADDLE:
        if not import_package(engine_type.value):
            raise ImportError(f"{engine_type.value} is not installed")

        from .paddle import PaddleInferSession

        return PaddleInferSession

    if engine_type == EngineType.TORCH:
        if not import_package(engine_type.value):
            raise ImportError(f"{engine_type.value} is not installed")

        from .pytorch import TorchInferSession

        return TorchInferSession

    if engine_type == EngineType.TENSORRT:
        if not import_package("tensorrt"):
            raise ImportError("tensorrt is not installed")

        from .tensorrt import TRTInferSession

        return TRTInferSession

    if engine_type == EngineType.MNN:
        if not import_package("MNN"):
            raise ImportError("MNN is not installed")

        from .mnn import MNNInferSession

        return MNNInferSession

    raise ValueError(f"Unsupported engine: {engine_type.value}")


@dataclass
class FileInfo:
    engine_type: EngineType
    ocr_version: OCRVersion
    task_type: TaskType
    lang_type: Union[Enum, str]
    model_type: ModelType


class InferSession(abc.ABC):
    model_info = OmegaConf.load(MODEL_URL_PATH)

    @abc.abstractmethod
    def __init__(self, config):
        pass

    @abc.abstractmethod
    def __call__(self, input_content: np.ndarray) -> np.ndarray:
        pass

    def infer_argmax(self, input_content: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        """Return ``(argmax, max)`` along the *last* axis of the model output.

        Used by the text recognizer (CTC decode), whose logits have shape
        ``(batch, time, vocab)``: reducing on-device before the CPU copy avoids a
        large device-to-host transfer. Subclasses running on an accelerator (e.g.
        torch/CUDA) should override this to perform the reduction on-device.

        Raises:
            ValueError: if the model output has fewer than 2 dimensions.
        """
        preds = self(input_content)
        if preds.ndim < 2:
            raise ValueError(
                f"infer_argmax expects an output with at least 2 dimensions, "
                f"got {preds.ndim} (shape={preds.shape}). It is intended for rec "
                "logits of shape (batch, time, vocab)."
            )
        return preds.argmax(axis=-1), preds.max(axis=-1)

    @staticmethod
    def _verify_model(model_path: Union[str, Path, None]):
        if model_path is None:
            raise ValueError("model_path is None!")

        model_path = Path(model_path)
        if not model_path.exists():
            raise FileNotFoundError(f"{model_path} does not exists.")

        if not model_path.is_file():
            raise FileExistsError(f"{model_path} is not a file.")

    @abc.abstractmethod
    def have_key(self, key: str = "character") -> bool:
        pass

    @abc.abstractmethod
    def get_character_list(self, key: str = "character") -> List[str]:
        pass

    @classmethod
    def get_model_url(cls, file_info: FileInfo) -> Dict[str, str]:
        engine_type = file_info.engine_type.value
        ocr_version = file_info.ocr_version.value
        task_type = file_info.task_type.value
        lang_type = normalize_lang(file_info.lang_type)
        model_type = file_info.model_type.value

        model_dict = OmegaConf.select(
            cls.model_info, f"{engine_type}.{ocr_version}.{task_type}"
        )
        if not model_dict:
            raise ValueError(
                f"Unsupported configuration: {engine_type}.{ocr_version}.{task_type}.{model_type}"
            )

        model_key = route_to_model_key(
            file_info.task_type,
            file_info.ocr_version,
            file_info.lang_type,
            file_info.model_type,
        )

        if model_key is not None:
            if model_key in model_dict:
                return model_dict[model_key]

            raise ValueError(
                f"Unsupported configuration: {engine_type}.{ocr_version}.{task_type}.{lang_type}.{model_type}"
            )

        logger.error(
            "Unsupported configuration:\n"
            f"  engine_type   = {engine_type}\n"
            f"  ocr_version   = {ocr_version}\n"
            f"  task_type     = {task_type}\n"
            f"  lang_type     = {lang_type}\n"
            f"  model_type     = {model_type}\n"
            "\n"
            "Please refer to the official model list for supported combinations:\n"
            "https://rapidai.github.io/RapidOCRDocs/main/model_list/\n"
            "\n"
            "Example valid usage:\n"
            "  from rapidocr import LangRec, OCRVersion, RapidOCR\n"
            "  engine = RapidOCR(params={'Rec.ocr_version': OCRVersion.PPOCRV5, 'Rec.lang_type': LangRec.CH, 'Rec.model_type': 'mobile'})",
        )
        raise ValueError("Invalid OCR configuration.")

    @classmethod
    def get_dict_key_url(cls, file_info: FileInfo) -> str:
        model_dict = cls.get_model_url(file_info)
        return model_dict["dict_url"]