mirror of
https://github.com/RapidAI/RapidOCR.git
synced 2026-11-11 05:20:49 +08:00
Update unit testing
This commit is contained in:
1 parent
5bc515d73e
commit
ee70d8a6af
5 files changed
+139
-68
No files matched your search
@@ -1,27 +0,0 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
|
||||
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 data
|
||||
@@ -0,0 +1,30 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
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 data
|
||||
+37
-13
@@ -1,23 +1,47 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
import base
|
||||
from base_module import BaseModule
|
||||
import 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'])
|
||||
|
||||
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'
|
||||
|
||||
|
||||
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'
|
||||
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'
|
||||
+37
-15
@@ -1,27 +1,49 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
import base
|
||||
import 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(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)
|
||||
|
||||
|
||||
def test_det():
|
||||
img_path = 'test_files/text_det.jpg'
|
||||
class TestOpenVINO():
|
||||
module_name = 'ch_ppocr_v3_det'
|
||||
class_name = 'TextDetector'
|
||||
|
||||
img = cv2.imread(img_path)
|
||||
base = BaseModule(package_name='rapidocr_openvino')
|
||||
TextDetector = base.init_module(module_name, class_name)
|
||||
|
||||
dt_boxes, elapse = text_det(img)
|
||||
assert dt_boxes.shape == (18, 4, 2)
|
||||
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)
|
||||
+35
-13
@@ -1,23 +1,45 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
import base
|
||||
import 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(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] == '韩国小馆'
|
||||
|
||||
|
||||
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] == '韩国小馆'
|
||||
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] == '韩国小馆'
|
||||
Reference in new issue
Block a user