Skip to content

📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

Optim unit tests

SWHLed9da1329285
diff --git a/python/README.md b/python/README.md
index ebe5a99..c0ab6d4 100755
--- a/python/README.md
+++ b/python/README.md
@@ -61,7 +61,7 @@
rapid_ocr = RapidOCR()
- img = cv2.imread('test_images/ch_en_num.jpg')
+ img = cv2.imread('tests/test_files/ch_en_num.jpg')
result = rapid_ocr(img)
print(result)
@@ -111,12 +111,10 @@
│    ├── ch_ppocr_mobile_v2.0_cls_infer.onnx
│    └── ch_PP-OCRv3_rec_infer.onnx
├── requirements.txt
- ├── resources
- │ └── fonts
- │    └── FZYTK.TTF
- └── test_images
- ├── ch_en_num.jpg
- └── single_line_text.jpg
+ └── resources
+ └── fonts
+    └── FZYTK.TTF
+
```
3. 安装运行环境
@@ -178,7 +176,7 @@
- `min_height`是用来过滤只有一行文本的图像(如下图),这类图像不会进入文本检测模块,直接进入后续过程。
- ![](./test_images/single_line_text.jpg)
+ ![](https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/single_line_text.jpg)
- `Det`部分
| 参数名称 | 取值范围 | 默认值 | 作用 |
diff --git a/python/demo.py b/python/demo.py
index 4830577..035b037 100644
--- a/python/demo.py
+++ b/python/demo.py
@@ -90,8 +90,9 @@ def visualize(image_path, result, font_path="resources/fonts/FZYTK.TTF"):
if __name__ == '__main__':
rapid_ocr = RapidOCR()
- image_path = 'test_images/ch_en_num.jpg'
+ image_path = 'tests/test_files/ch_en_num.jpg'
img = cv2.imread(image_path)
+ img = np.zeros([640, 640, 3])
result, elapse_list = rapid_ocr(img)
print(result)
print(elapse_list)
diff --git a/python/test_images/single_line_text.jpg b/python/test_images/single_line_text.jpg
deleted file mode 100644
index f98cc39..0000000
Binary files a/python/test_images/single_line_text.jpg and /dev/null differ
diff --git a/python/tests/base_module.py b/python/tests/base_module.py
index b6c7e18..13bd0bd 100644
--- a/python/tests/base_module.py
+++ b/python/tests/base_module.py
@@ -13,6 +13,7 @@ class BaseModule():
self.package_name = package_name
self.root_dir = Path(__file__).resolve().parent.parent
self.package_dir = self.root_dir / self.package_name
+ self.tests_dir = self.root_dir / 'tests'
sys.path.append(str(self.root_dir))
sys.path.append(str(self.package_dir))
diff --git a/python/tests/test_all_ort.py b/python/tests/test_all_ort.py
new file mode 100644
index 0000000..1a77900
--- /dev/null
+++ b/python/tests/test_all_ort.py
@@ -0,0 +1,40 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+import sys
+from pathlib import Path
+
+import numpy as np
+import cv2
+import pytest
+
+
+root_dir = Path(__file__).resolve().parent.parent
+sys.path.append(str(root_dir))
+
+from rapidocr_onnxruntime import RapidOCR
+
+rapid_ocr = RapidOCR()
+tests_dir = root_dir / 'test_files'
+
+
+def test_normal():
+ image_path = tests_dir / 'ch_en_num.jpg'
+ img = cv2.imread(str(image_path))
+ result, _ = rapid_ocr(img)
+ assert result[0][1] == '正品促销'
+ assert len(result) == 17
+
+
+def test_empty():
+ img = None
+ with pytest.raises(AttributeError) as exc_info:
+ rapid_ocr(img)
+ raise AttributeError
+ assert exc_info.type is AttributeError
+
+
+def test_zeros():
+ img = np.zeros([640, 640, 3])
+ result, _ = rapid_ocr(img)
+ assert result is None
diff --git a/python/tests/test_all_vino.py b/python/tests/test_all_vino.py
new file mode 100644
index 0000000..fe1cca0
--- /dev/null
+++ b/python/tests/test_all_vino.py
@@ -0,0 +1,41 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+import sys
+from pathlib import Path
+
+import numpy as np
+import cv2
+import pytest
+
+
+root_dir = Path(__file__).resolve().parent.parent
+sys.path.append(str(root_dir))
+
+from rapidocr_openvino import RapidOCR
+
+rapid_ocr = RapidOCR()
+
+tests_dir = root_dir / 'test_files'
+
+
+def test_normal():
+ image_path = tests_dir / 'ch_en_num.jpg'
+ img = cv2.imread(str(image_path))
+ result, _ = rapid_ocr(img)
+ assert result[0][1] == '正品促销'
+ assert len(result) == 17
+
+
+def test_empty():
+ img = None
+ with pytest.raises(AttributeError) as exc_info:
+ rapid_ocr(img)
+ raise AttributeError
+ assert exc_info.type is AttributeError
+
+
+def test_zeros():
+ img = np.zeros([640, 640, 3])
+ result, _ = rapid_ocr(img)
+ assert result is None
diff --git a/python/tests/test_cls.py b/python/tests/test_cls.py
index ed4cc6c..c9ec655 100644
--- a/python/tests/test_cls.py
+++ b/python/tests/test_cls.py
@@ -1,36 +1,20 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
-from base_module import BaseModule
import cv2
+import pytest
+from base_module import BaseModule
-class TestONNXRuntime():
- module_name = 'ch_ppocr_v2_cls'
- class_name = 'TextClassifier'
-
- base = BaseModule(package_name='rapidocr_onnxruntime')
-
- TextClassifier = base.init_module(module_name, class_name)
-
- yaml_path = base.package_dir / module_name / 'config.yaml'
- config = base.read_yaml(str(yaml_path))
- config['model_path'] = str(base.package_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'
-
-
-class TestOpenVINO():
+ 'package_name',
+ ['rapidocr_onnxruntime', 'rapidocr_openvino']
+)
+def test_cls(package_name: str):
module_name = 'ch_ppocr_v2_cls'
class_name = 'TextClassifier'
- base = BaseModule(package_name='rapidocr_openvino')
+ base = BaseModule(package_name=package_name)
TextClassifier = base.init_module(module_name, class_name)
@@ -40,8 +24,7 @@ class TestOpenVINO():
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'
+ img_path = base.tests_dir / 'test_files' / 'text_cls.jpg'
+ img = cv2.imread(str(img_path))
+ result = text_cls([img])
+ assert result[1][0][0] == '180'
diff --git a/python/tests/test_det.py b/python/tests/test_det.py
index 1e03a06..c8e9ec5 100644
--- a/python/tests/test_det.py
+++ b/python/tests/test_det.py
@@ -2,14 +2,19 @@
# @Author: SWHL
# @Contact: [email protected]
import cv2
+import pytest
from base_module import BaseModule
-class TestONNXRuntime():
+ 'package_name',
+ ['rapidocr_onnxruntime', 'rapidocr_openvino']
+)
+def test_det(package_name):
module_name = 'ch_ppocr_v3_det'
class_name = 'TextDetector'
- base = BaseModule(package_name='rapidocr_onnxruntime')
+ base = BaseModule(package_name)
TextDetector = base.init_module(module_name, class_name)
yaml_path = base.package_dir / module_name / 'config.yaml'
@@ -17,33 +22,7 @@ class TestONNXRuntime():
config['model_path'] = str(base.package_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)
-
-
-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.package_dir / module_name / 'config.yaml'
- config = base.read_yaml(str(yaml_path))
- config['model_path'] = str(base.package_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)
+ img_path = base.tests_dir / 'test_files' / 'text_det.jpg'
+ img = cv2.imread(str(img_path))
+ dt_boxes, elapse = text_det(img)
+ assert dt_boxes.shape == (18, 4, 2)
diff --git a/python/test_images/ch_en_num.jpg b/python/tests/test_files/ch_en_num.jpg
similarity index 100%
rename from python/test_images/ch_en_num.jpg
rename to python/tests/test_files/ch_en_num.jpg
diff --git a/python/tests/test_rec.py b/python/tests/test_rec.py
index 34b4041..98b79d0 100644
--- a/python/tests/test_rec.py
+++ b/python/tests/test_rec.py
@@ -2,14 +2,19 @@
# @Author: SWHL
# @Contact: [email protected]
import cv2
+import pytest
from base_module import BaseModule
-class TestONNXRuntime():
+ 'package_name',
+ ['rapidocr_onnxruntime', 'rapidocr_openvino']
+)
+def test_det(package_name):
module_name = 'ch_ppocr_v3_rec'
class_name = 'TextRecognizer'
- base = BaseModule(package_name='rapidocr_onnxruntime')
+ base = BaseModule(package_name)
TextRecognizer = base.init_module(module_name, class_name)
yaml_path = base.package_dir / module_name / 'config.yaml'
@@ -18,28 +23,7 @@ class TestONNXRuntime():
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] == '韩国小馆'
-
-
-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.package_dir / module_name / 'config.yaml'
- config = base.read_yaml(str(yaml_path))
- config['model_path'] = str(base.package_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] == '韩国小馆'
+ img_path = base.tests_dir / 'test_files' / 'text_rec.jpg'
+ img = cv2.imread(str(img_path))
+ rec_res, elapse = text_rec([img])
+ assert rec_res[0][0] == '韩国小馆'