From 2a420e013e8efe33f1b1997b2848afdbd155d15c Mon Sep 17 00:00:00 2001 From: SWHL Date: Sun, 9 Mar 2025 10:06:04 +0800 Subject: [PATCH] fix(rapidocr): complete parsing parameters --- .../workflows/gen_whl_to_pypi_rapidocr.yml | 84 ++++++++++++++ .../gen_whl_to_pypi_rapidocr_ort.yml | 15 ++- python/demo.py | 16 +-- python/rapidocr/main.py | 4 +- python/rapidocr/utils/parse_parameters.py | 107 +----------------- python/requirements.txt | 3 +- python/tests/test_main.py | 4 +- 7 files changed, 102 insertions(+), 131 deletions(-) create mode 100644 .github/workflows/gen_whl_to_pypi_rapidocr.yml diff --git a/.github/workflows/gen_whl_to_pypi_rapidocr.yml b/.github/workflows/gen_whl_to_pypi_rapidocr.yml new file mode 100644 index 0000000..0c54c1c --- /dev/null +++ b/.github/workflows/gen_whl_to_pypi_rapidocr.yml @@ -0,0 +1,84 @@ +name: Push rapidocr to pypi + +on: + push: + # branches: [ main ] + # paths: + # - 'python/rapidocr_onnxruntime/**' + # - 'docs/doc_whl_rapidocr_ort.md' + # - 'python/setup_onnxruntime.py' + # - '.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml' + tags: + - v* + +env: + RESOURCES_URL: https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.3.0.zip + +jobs: + UnitTesting: + runs-on: ubuntu-latest + steps: + - name: Pull latest code + uses: actions/checkout@v4 + + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: '3.10' + architecture: 'x64' + + - name: Display Python version + run: python -c "import sys; print(sys.version)" + + - name: Unit testings + run: | + cd python + pip install -r requirements.txt + pip install pytest wheel get_pypi_latest_version openvino>=2022.2.0,<=2024.0.0 + pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu + python -m pip install paddlepaddle==3.0.0rc1 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/ + + pytest tests/test_main.py + + GenerateWHL_PushPyPi: + needs: UnitTesting + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: '3.10' + architecture: 'x64' + + - name: Download models + run: | + cd python + wget $RESOURCES_URL + ZIP_NAME=${RESOURCES_URL##*/} + DIR_NAME=${ZIP_NAME%.*} + unzip $ZIP_NAME + cp $DIR_NAME/resources/models/*.onnx rapidocr/models + + - name: Run setup.py + run: | + cd python + pip install setuptools get_pypi_latest_version wheel + mkdir rapidocr_t + mv rapidocr rapidocr_t + mv rapidocr_t rapidocr + cd rapidocr + echo "from .rapidocr.main import RapidOCR, VisRes" > __init__.py + + cd .. + python -m pip install --upgrade pip + python setup.py bdist_wheel ${{ github.ref_name }} + mv dist ../ + + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@v1.5.0 + with: + password: ${{ secrets.PYPI_API_TOKEN }} + packages_dir: dist/ diff --git a/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml b/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml index 56f908e..3065632 100644 --- a/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml +++ b/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml @@ -2,14 +2,13 @@ name: Push rapidocr_onnxruntime to pypi on: push: - # branches: [ main ] - # paths: - # - 'python/rapidocr_onnxruntime/**' - # - 'docs/doc_whl_rapidocr_ort.md' - # - 'python/setup_onnxruntime.py' - # - '.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml' - tags: - - v* + branches: [ main ] + paths: + - 'python/rapidocr_onnxruntime/**' + - 'docs/doc_whl_rapidocr_ort.md' + - 'python/setup_onnxruntime.py' + # - '.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml' + env: RESOURCES_URL: https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.3.0.zip diff --git a/python/demo.py b/python/demo.py index aa92dd8..c2ee3e9 100644 --- a/python/demo.py +++ b/python/demo.py @@ -5,27 +5,13 @@ import cv2 from rapidocr import RapidOCR, VisRes -# from rapidocr_onnxruntime import RapidOCR, VisRes -# from rapidocr_torch import RapidOCR, VisRes - -# from rapidocr_paddle import RapidOCR, VisRes -# from rapidocr_openvino import RapidOCR, VisRes - -# yaml_path = "tests/test_files/config.yaml" -# engine = RapidOCR(config_path=yaml_path) - -# engine = engine = RapidOCR( -# params={"Global.with_onnx": True, "EngineConfig.onnxruntime.use_cuda": True} -# ) -# engine = RapidOCR(params={"Global.with_torch": True, "Global.lang": "ch"}) -engine = RapidOCR(params={"Global.with_openvino": True, "Global.lang_rec": "ch_server"}) +engine = RapidOCR(params={"Global.with_openvino": True}) vis = VisRes() image_path = "tests/test_files/ch_en_num.jpg" with open(image_path, "rb") as f: img = f.read() -# result, elapse_list = engine(img, use_det=True, use_cls=False, use_rec=False) result = engine(img, return_word_box=True) print(result) print(result.elapse) diff --git a/python/rapidocr/main.py b/python/rapidocr/main.py index acc0157..83a728c 100644 --- a/python/rapidocr/main.py +++ b/python/rapidocr/main.py @@ -351,12 +351,12 @@ def main(arg_list: Optional[List[str]] = None): vis_img = vis(args.img_path, words_boxes, words, words_scores) save_path = cur_dir / f"{Path(args.img_path).stem}_vis_single.png" cv2.imwrite(str(save_path), vis_img) - print("The vis single result has saved in %s", save_path) + print(f"The vis single result has saved in {save_path}") else: save_path = cur_dir / f"{Path(args.img_path).stem}_vis.png" vis_img = vis(args.img_path, result.boxes, result.txts, result.scores) cv2.imwrite(str(save_path), vis_img) - print("The vis result has saved in %s", save_path) + print(f"The vis result has saved in {save_path}") if __name__ == "__main__": diff --git a/python/rapidocr/utils/parse_parameters.py b/python/rapidocr/utils/parse_parameters.py index ba9cdb3..d1ec299 100644 --- a/python/rapidocr/utils/parse_parameters.py +++ b/python/rapidocr/utils/parse_parameters.py @@ -122,119 +122,20 @@ class ParseLang: return "devanagari" if lang in ["ch", "en"]: - return + pass raise ValueError(f"lang: {lang} is not in the supported list.") -def parse_lang(lang): - latin_lang = [ - "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_lang = ["ar", "fa", "ug", "ur"] - cyrillic_lang = [ - "ru", - "rs_cyrillic", - "be", - "bg", - "uk", - "mn", - "abq", - "ady", - "kbd", - "ava", - "dar", - "inh", - "che", - "lbe", - "lez", - "tab", - ] - devanagari_lang = [ - "hi", - "mr", - "ne", - "bh", - "mai", - "ang", - "bho", - "mah", - "sck", - "new", - "gom", - "sa", - "bgc", - ] - if lang in latin_lang: - lang = "latin" - elif lang in arabic_lang: - lang = "arabic" - elif lang in cyrillic_lang: - lang = "cyrillic" - elif lang in devanagari_lang: - lang = "devanagari" - elif lang in ["ch", "en"]: - pass - else: - raise ValueError(f"lang: {lang} is not in the supported list.") - - if lang == "ch": - det_lang = "ch" - elif lang in ["en", "latin"]: - det_lang = "en" - else: - det_lang = "Multilingual" - - return det_lang, lang - - class ParseParams(OmegaConf): def __init__(self): pass @classmethod def update_batch(cls, cfg: DictConfig, params: Dict[str, str]) -> DictConfig: + global_keys = list(OmegaConf.to_container(cfg.Global).keys()) for k, v in params.items(): + if k.startswith("Global") and k.split(".")[1] not in global_keys: + raise ValueError(f"{k} is not a valid key.") cls.update(cfg, k, v) return cfg diff --git a/python/requirements.txt b/python/requirements.txt index 11139a3..2e7ef61 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -6,4 +6,5 @@ Shapely>=1.7.1,!=2.0.4 # python3.12 2.0.4 bug PyYAML Pillow onnxruntime>=1.7.0 -tqdm \ No newline at end of file +tqdm +omegaconf \ No newline at end of file diff --git a/python/tests/test_main.py b/python/tests/test_main.py index 6565c77..1d11859 100644 --- a/python/tests/test_main.py +++ b/python/tests/test_main.py @@ -79,11 +79,11 @@ def test_error_lang(): def test_lang(): - engine = get_engine(params={"Global.lang": "en"}) + engine = get_engine(params={"Global.lang_rec": "en_mobile"}) img_path = tests_dir / "en.jpg" result = engine(img_path) assert result.txts is not None - assert result.txts[0] == "MovieShots Dataset" + assert result.txts[0] == "3" def test_engine_openvino():