mirror of
https://github.com/RapidAI/RapidOCR.git
synced 2026-11-11 05:20:49 +08:00
* feat(inference_engine): add TensorRT engine support - Add TensorRT inference session with dynamic shape support - Implement automatic engine building and caching per GPU architecture - Add pre-allocated buffers with pinned memory optimization - Support FP16/FP32/INT8 precision modes - Add optimization profiles for det/rec/cls models - Include comprehensive error handling and resource cleanup - Add unit tests for TensorRT engine (skipped when unavailable) - Add TensorRT configuration section in config.yaml * fix(tensorrt): dynamically allocate output buffers to prevent overflow * Update test_engine.py with TensorRT support final version * feat(tensorrt): add model_type to engine cache key - Include model_type in TensorRT engine cache key generation - Ensures proper cache differentiation based on model_type * fix(tensorrt): update maximum shape configurations for input and output tensors - Increased max_shape values in config.yaml and engine_builder.py to 2048 for both detection and recognition tasks. - Updated default maximum shapes in memory_utils.py to reflect the new limits for input and output buffers. * feat(tensorrt): implement square input handling for MULTI models - Added functionality to check if the model requires square input based on configuration. - Implemented padding of input to square shape and cropping of output back to original dimensions. - Introduced methods to determine maximum profile size and handle square input requirements for TensorRT inference. --------- Co-authored-by: Cursor <cursoragent@cursor.com>
131 lines
3.3 KiB
Python
131 lines
3.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
|
|
|
|
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,
|
|
EngineType.TENSORRT,
|
|
],
|
|
)
|
|
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,
|
|
EngineType.TENSORRT,
|
|
],
|
|
)
|
|
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] == "正品促销"
|
|
|
|
|
|
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
|