Files
rapidocr/python/tests/test_engine.py
T
JinmingMei 9fe06b676c feat(python): support PP-OCRv6 (onnxruntime + openvino, det + rec) (#692)
* feat(python): support PP-OCRv6 for onnxruntime and openvino

Add PP-OCRv6 rec and det models (tiny/small/medium) for onnxruntime and openvino engines. v6 ships no cls model; reuse PP-OCRv5 cls.

- typings.py: add OCRVersion.PPOCRV6 and ModelType.TINY/SMALL/MEDIUM

- default_models.yaml: add PP-OCRv6 blocks under onnxruntime/openvino with PaddlePaddle official ModelScope URLs, verified SHA256, and dict_url (PaddleOCR dict files)

Verified end-to-end on ch_en_num.jpg with v6 small det+rec + v5 cls (onnxruntime): 18 text lines recognized correctly.

* fix(python): use uniquely-named PP-OCRv6 model URLs to avoid cache collision

PP-OCRv6 official ONNX exports are all named inference.onnx, causing det and rec models to overwrite each other in RapidOCR's cache (cache_dir/Path(url).name). Switch model_dir/dict_url to a mirror with unique filenames (ch_PP-OCRv6_<task>_<size>.onnx) under modelscope.cn/models/jaminmei/PP-OCRv6-onnx@v1.0. SHA256 unchanged (byte-identical files, renamed only).

Verified onnxruntime + openvino: no more redownload-on-collision; second run is pure cache hit.

* test(python): add PP-OCRv6 det/rec unit tests for onnxruntime and openvino

Mirror the test_ppocrv5_det/rec_mobile pattern. v6 small det on ch_en_num.jpg -> 18 boxes; v6 small rec on text_rec.jpg -> '韩国小馆'. Parametrized over the two engines v6 supports (ONNXRUNTIME, OPENVINO). Existing 100 tests unaffected (verified via collect-only).

Verified: 4 new tests pass (onnxruntime + openvino x det/rec).

* chore(python): switch PP-OCRv6 model URLs to official RapidAI/RapidOCR repo

- det/rec x tiny/small/medium now point to modelscope.cn/RapidAI/RapidOCR (official SWHL conversion, not contributor mirror)

- update SHA256 to the official files' hashes

- drop rec dict_url: official ONNX embeds the character list in metadata (session.have_key() path), matching v4/v5 onnx convention

- verified: 4 v6 unit tests pass; det benchmark Hmean identical to prior mirror-based run (0.8263/0.8492/0.8421 for tiny/small/medium)
2026-06-18 21:18:58 +08:00

168 lines
4.3 KiB
Python

# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
import sys
from pathlib import Path
from pytest import mark
root_dir = Path(__file__).resolve().parent.parent
sys.path.append(str(root_dir))
from rapidocr import EngineType, ModelType, OCRVersion, RapidOCR
from rapidocr.utils.utils import import_package
tests_dir = root_dir / "tests" / "test_files"
img_path = tests_dir / "ch_en_num.jpg"
@mark.parametrize(
"engine_type",
[
EngineType.ONNXRUNTIME,
EngineType.PADDLE,
EngineType.OPENVINO,
EngineType.TORCH,
EngineType.MNN,
],
)
def test_ppocrv5_rec_mobile(engine_type):
engine = RapidOCR(
params={
"Rec.ocr_version": OCRVersion.PPOCRV5,
"Rec.model_type": ModelType.MOBILE,
"Rec.engine_type": engine_type,
}
)
img_path = tests_dir / "text_rec.jpg"
result = engine(img_path, use_det=False, use_cls=False, use_rec=True)
assert result.txts is not None
assert result.txts[0] == "韩国小馆"
@mark.parametrize(
"engine_type",
[
EngineType.ONNXRUNTIME,
EngineType.PADDLE,
EngineType.OPENVINO,
EngineType.TORCH,
EngineType.MNN,
],
)
def test_ppocrv5_det_mobile(engine_type):
engine = RapidOCR(
params={
"Det.ocr_version": OCRVersion.PPOCRV5,
"Det.model_type": ModelType.MOBILE,
"Det.engine_type": engine_type,
}
)
img_path = tests_dir / "ch_en_num.jpg"
result = engine(img_path, use_det=True, use_cls=False, use_rec=False)
assert result.boxes is not None
assert len(result.boxes) == 17
@mark.skipif(sys.platform.startswith("darwin"), reason="does not run on macOS")
def test_engine_openvino():
engine = RapidOCR(
params={
"Det.engine_type": EngineType.OPENVINO,
"Cls.engine_type": EngineType.OPENVINO,
"Rec.engine_type": EngineType.OPENVINO,
}
)
result = engine(img_path)
assert result.txts is not None
assert result.txts[0] == "正品促销"
@mark.parametrize(
"ocr_version,gt",
[(OCRVersion.PPOCRV4, "正品促销"), (OCRVersion.PPOCRV5, "大桶装更划算")],
)
def test_engine_paddle(ocr_version, gt):
engine = RapidOCR(
params={
"Det.engine_type": EngineType.PADDLE,
"Det.ocr_version": ocr_version,
"Cls.engine_type": EngineType.PADDLE,
"Rec.engine_type": EngineType.PADDLE,
}
)
result = engine(img_path)
assert result.txts is not None
assert result.txts[0] == gt
def test_engine_torch():
engine = RapidOCR(
params={
"Det.engine_type": EngineType.TORCH,
"Cls.engine_type": EngineType.TORCH,
"Rec.engine_type": EngineType.TORCH,
}
)
result = engine(img_path)
assert result.txts is not None
assert result.txts[0] == "正品促销"
@mark.skipif(not import_package("tensorrt"), reason="tensorrt is not installed")
def test_engine_tensorrt():
"""Test TensorRT engine with full OCR pipeline."""
engine = RapidOCR(
params={
"Det.engine_type": EngineType.TENSORRT,
"Cls.engine_type": EngineType.TENSORRT,
"Rec.engine_type": EngineType.TENSORRT,
}
)
result = engine(img_path)
assert result.txts is not None
assert len(result.txts) > 0
@mark.parametrize(
"engine_type",
[EngineType.ONNXRUNTIME, EngineType.OPENVINO],
)
def test_ppocrv6_det_small(engine_type):
engine = RapidOCR(
params={
"Det.ocr_version": OCRVersion.PPOCRV6,
"Det.model_type": ModelType.SMALL,
"Det.engine_type": engine_type,
}
)
result = engine(img_path, use_det=True, use_cls=False, use_rec=False)
assert result.boxes is not None
assert len(result.boxes) == 18
@mark.parametrize(
"engine_type",
[EngineType.ONNXRUNTIME, EngineType.OPENVINO],
)
def test_ppocrv6_rec_small(engine_type):
engine = RapidOCR(
params={
"Rec.ocr_version": OCRVersion.PPOCRV6,
"Rec.model_type": ModelType.SMALL,
"Rec.engine_type": engine_type,
}
)
rec_img_path = tests_dir / "text_rec.jpg"
result = engine(rec_img_path, use_det=False, use_cls=False, use_rec=True)
assert result.txts is not None
assert result.txts[0] == "韩国小馆"