Skip to content

Commit

Permalink
chore: update files
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed Feb 14, 2025
1 parent 6a2b9e5 commit 8ca4849
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion python/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# engine = engine = RapidOCR(
# params={"Global.with_onnx": True, "EngineConfig.onnxruntime.use_cuda": True}
# )
engine = RapidOCR(params={"Global.lang": "en"})
engine = RapidOCR(params={"Global.lang": "latin", "Global.with_openvino": True})
vis = VisRes()

image_path = "tests/test_files/ch_en_num.jpg"
Expand Down
14 changes: 7 additions & 7 deletions python/rapidocr/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ EngineConfig:
use_cuda: false

Det:
model_path: models/ch_PP-OCRv4_det_infer.onnx
model_dir: models/ch_PP-OCRv4_det_infer
model_path: null
model_dir: null

limit_side_len: 736
limit_type: min
Expand All @@ -54,18 +54,18 @@ Det:
score_mode: fast

Cls:
model_path: models/ch_ppocr_mobile_v2.0_cls_infer.onnx
model_dir: models/ch_ppocr_mobile_v2_cls_infer
model_path: null
model_dir: null

cls_image_shape: [3, 48, 192]
cls_batch_num: 6
cls_thresh: 0.9
label_list: ['0', '180']

Rec:
model_path: models/ch_PP-OCRv4_rec_infer.onnx
model_dir: models/ch_PP-OCRv4_rec_infer
model_path: null
model_dir: null

rec_keys_path: models/ppocr_keys_v1.txt
rec_keys_path: null
rec_img_shape: [3, 48, 320]
rec_batch_num: 6
File renamed without changes.
21 changes: 12 additions & 9 deletions python/rapidocr/inference_engine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

from ..utils.logger import Logger

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


logger = Logger(logger_name=__name__).get_log()
Expand Down Expand Up @@ -76,7 +76,7 @@ def get_engine_name(config: DictConfig) -> str:

class InferSession(abc.ABC):
model_info = OmegaConf.load(MODEL_URL_PATH)
DEFAULT_MODE_PATH = cur_dir.parent / "models"
DEFAULT_MODE_PATH = cur_dir / "models"
logger = Logger(logger_name=__name__).get_log()

@abc.abstractmethod
Expand All @@ -88,26 +88,29 @@ def __call__(self, input_content: np.ndarray) -> np.ndarray:
pass

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

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

if not model_path.is_file():
return False

return True
raise FileExistsError(f"{model_path} is not a file.")

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

@classmethod
def download_file(cls, url: str, save_path: Union[str, Path]):
if Path(save_path).exists():
cls.logger.info("Model already exists in %s", save_path)
return

cls.logger.info("Downloading model from %s to %s", url, save_path)

response = requests.get(url, stream=True, timeout=60)
status_code = response.status_code

Expand Down
8 changes: 4 additions & 4 deletions python/rapidocr/inference_engine/onnxruntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ def __init__(self, config: Dict[str, Any]):
self.logger = Logger(logger_name=__name__).get_log()

model_path = config.get("model_path", None)
if not self._verify_model(model_path):
self.logger.warning(
"Model path is invalid, try to download the default model."
)
if model_path is None:
# 说明用户没有指定自己模型,使用默认模型
default_model_url = self.get_model_url(
config.engine_name, config.task_type, config.lang
)
model_path = self.DEFAULT_MODE_PATH / Path(default_model_url).name
self.download_file(default_model_url, model_path)

self._verify_model(model_path)

self.cfg_use_cuda = config.engine_cfg.get("use_cuda", None)
self.cfg_use_dml = config.engine_cfg.get("use_dml", None)

Expand Down
14 changes: 12 additions & 2 deletions python/rapidocr/inference_engine/openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @Contact: liekkaskono@163.com
import os
import traceback
from pathlib import Path

import numpy as np
from omegaconf import DictConfig
Expand All @@ -17,8 +18,17 @@ def __init__(self, config: DictConfig):

core = Core()

self._verify_model(config["model_path"])
model_onnx = core.read_model(config["model_path"])
model_path = config.get("model_path", None)
if model_path is None:
# 说明用户没有指定自己模型,使用默认模型
default_model_url = self.get_model_url(
config.engine_name, config.task_type, config.lang
)
model_path = self.DEFAULT_MODE_PATH / Path(default_model_url).name
self.download_file(default_model_url, model_path)

self._verify_model(model_path)
model_onnx = core.read_model(model_path)

cpu_nums = os.cpu_count()
infer_num_threads = config.get("inference_num_threads", -1)
Expand Down
4 changes: 2 additions & 2 deletions python/rapidocr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def __init__(
config = ParseParams.load(config_path)
else:
config = ParseParams.load(DEFAULT_CFG_PATH)
config = ParseParams.update_model_path(config)
config = ParseParams.update_dict_path(config)
# config = ParseParams.update_model_path(config)
# config = ParseParams.update_dict_path(config)

if params:
config = ParseParams.update_batch(config, params)
Expand Down

0 comments on commit 8ca4849

Please # to comment.