π Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
Update unit testing
SWHLee70d8a6aff3
diff --git a/python/tests/base.py b/python/tests/base.pydeleted file mode 100644index 274c72a..0000000--- a/python/tests/base.py+++ /dev/null@@ -1,27 +0,0 @@-# -*- encoding: utf-8 -*--# @Author: SWHL-# @Contact: [email protected]--import importlib-from pathlib import Path-import sys--import yaml--package_name = 'rapidocr_onnxruntime'-# package_name = 'rapidocr_openvino'--root_dir = Path(__file__).resolve().parent.parent-sys.path.append(str(root_dir))-sys.path.append(str(root_dir / package_name))---def init_module(module_name, class_name):- module_part = importlib.import_module(module_name)- return getattr(module_part, class_name)---def read_yaml(yaml_path):- with open(yaml_path, 'rb') as f:- data = yaml.load(f, Loader=yaml.Loader)- return datadiff --git a/python/tests/base_module.py b/python/tests/base_module.pynew file mode 100644index 0000000..eccf51b--- /dev/null+++ b/python/tests/base_module.py@@ -0,0 +1,30 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+import importlib+import sys+from pathlib import Path++import yaml+++class BaseModule(object):+ def __init__(self, package_name: str = 'rapidocr_onnxruntime'):+ self.package_name = package_name+ self.root_dir = Path(__file__).resolve().parent.parent++ sys.path.append(str(self.root_dir))+ sys.path.append(str(self.root_dir / self.package_name))++ def init_module(self, module_name: str, class_name: str = None):+ if class_name is None:+ module_part = importlib.import_module(f'{self.package_name}')+ return module_part+ module_part = importlib.import_module(f'{self.package_name}.{module_name}')+ return getattr(module_part, class_name)++ @staticmethod+ def read_yaml(yaml_path: str):+ with open(yaml_path, 'rb') as f:+ data = yaml.load(f, Loader=yaml.Loader)+ return datadiff --git a/python/tests/test_cls.py b/python/tests/test_cls.pyindex d8c6dc5..84fb82a 100644--- a/python/tests/test_cls.py+++ b/python/tests/test_cls.py@@ -1,23 +1,47 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]-import base+from base_module import BaseModuleimport cv2-module_name = 'ch_ppocr_v2_cls'-class_name = 'TextClassifier'-TextClassifier = base.init_module(module_name, class_name)+class TestONNXRuntime():+ module_name = 'ch_ppocr_v2_cls'+ class_name = 'TextClassifier'-yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'-config = base.read_yaml(str(yaml_path))-config['model_path'] = str(base.root_dir / config['model_path'])+ base = BaseModule(package_name='rapidocr_onnxruntime')-text_cls = TextClassifier(config)+ TextClassifier = base.init_module(module_name, class_name)+ yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'+ config = base.read_yaml(str(yaml_path))+ config['model_path'] = str(base.root_dir / config['model_path'])-def test_cls():- img_path = 'test_files/text_cls.jpg'- img = cv2.imread(img_path)- result = text_cls([img])- assert result[1][0][0] == '180'+ text_cls = TextClassifier(config)++ def test_cls(self):+ img_path = 'test_files/text_cls.jpg'+ img = cv2.imread(img_path)+ result = self.text_cls([img])+ assert result[1][0][0] == '180'+++class TestOpenVINO():+ module_name = 'ch_ppocr_v2_cls'+ class_name = 'TextClassifier'++ base = BaseModule(package_name='rapidocr_openvino')++ TextClassifier = base.init_module(module_name, class_name)++ yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'+ config = base.read_yaml(str(yaml_path))+ config['model_path'] = str(base.root_dir / config['model_path'])++ text_cls = TextClassifier(config)++ def test_cls(self):+ img_path = 'test_files/text_cls.jpg'+ img = cv2.imread(img_path)+ result = self.text_cls([img])+ assert result[1][0][0] == '180'diff --git a/python/tests/test_det.py b/python/tests/test_det.pyindex e13a332..5033474 100644--- a/python/tests/test_det.py+++ b/python/tests/test_det.py@@ -1,27 +1,49 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]-import baseimport cv2+from base_module import BaseModule-module_name = 'ch_ppocr_v3_det'-class_name = 'TextDetector'-TextDetector = base.init_module(module_name, class_name)+class TestONNXRuntime():+ module_name = 'ch_ppocr_v3_det'+ class_name = 'TextDetector'-yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'-print(yaml_path)-config = base.read_yaml(str(yaml_path))-config['model_path'] = str(base.root_dir / config['model_path'])+ base = BaseModule(package_name='rapidocr_onnxruntime')+ TextDetector = base.init_module(module_name, class_name)-print(config)-text_det = TextDetector(config)+ yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'+ config = base.read_yaml(str(yaml_path))+ config['model_path'] = str(base.root_dir / config['model_path'])+ text_det = TextDetector(config)-def test_det():- img_path = 'test_files/text_det.jpg'+ def test_det(self):+ img_path = 'test_files/text_det.jpg'- img = cv2.imread(img_path)+ img = cv2.imread(img_path)- dt_boxes, elapse = text_det(img)- assert dt_boxes.shape == (18, 4, 2)+ dt_boxes, elapse = self.text_det(img)+ assert dt_boxes.shape == (18, 4, 2)+++class TestOpenVINO():+ module_name = 'ch_ppocr_v3_det'+ class_name = 'TextDetector'++ base = BaseModule(package_name='rapidocr_openvino')+ TextDetector = base.init_module(module_name, class_name)++ yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'+ config = base.read_yaml(str(yaml_path))+ config['model_path'] = str(base.root_dir / config['model_path'])++ text_det = TextDetector(config)++ def test_det(self):+ img_path = 'test_files/text_det.jpg'++ img = cv2.imread(img_path)++ dt_boxes, elapse = self.text_det(img)+ assert dt_boxes.shape == (18, 4, 2)diff --git a/python/tests/test_rec.py b/python/tests/test_rec.pyindex 790162d..71a10bc 100644--- a/python/tests/test_rec.py+++ b/python/tests/test_rec.py@@ -1,23 +1,45 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]-import baseimport cv2+from base_module import BaseModule-module_name = 'ch_ppocr_v3_rec'-class_name = 'TextRecognizer'-TextRecognizer = base.init_module(module_name, class_name)+class TestONNXRuntime():+ module_name = 'ch_ppocr_v3_rec'+ class_name = 'TextRecognizer'-yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'-config = base.read_yaml(str(yaml_path))-config['model_path'] = str(base.root_dir / config['model_path'])+ base = BaseModule(package_name='rapidocr_onnxruntime')+ TextRecognizer = base.init_module(module_name, class_name)-text_rec = TextRecognizer(config)+ yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'+ config = base.read_yaml(str(yaml_path))+ config['model_path'] = str(base.root_dir / config['model_path'])+ text_rec = TextRecognizer(config)-def test_rec():- img_path = 'test_files/text_rec.jpg'- img = cv2.imread(img_path)- rec_res, elapse = text_rec([img])- assert rec_res[0][0] == 'ι©ε½ε°ι¦'+ def test_rec(self):+ img_path = 'test_files/text_rec.jpg'+ img = cv2.imread(img_path)+ rec_res, elapse = self.text_rec([img])+ assert rec_res[0][0] == 'ι©ε½ε°ι¦'+++class TestOpenVINO():+ module_name = 'ch_ppocr_v3_rec'+ class_name = 'TextRecognizer'++ base = BaseModule(package_name='rapidocr_openvino')+ TextRecognizer = base.init_module(module_name, class_name)++ yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'+ config = base.read_yaml(str(yaml_path))+ config['model_path'] = str(base.root_dir / config['model_path'])++ text_rec = TextRecognizer(config)++ def test_rec(self):+ img_path = 'test_files/text_rec.jpg'+ img = cv2.imread(img_path)+ rec_res, elapse = self.text_rec([img])+ assert rec_res[0][0] == 'ι©ε½ε°ι¦'