Skip to content

Commit 017b8fd

Browse files
authored
feat: add en/el/th ppocr_v5_rec_mobile paddle/onnx/openvino (#546)
* feat: add en/el/th ppocr_v5_rec_mobile paddle/onnx/openvino * test: change to a more secure code
1 parent a40454c commit 017b8fd

File tree

15 files changed

+924
-675
lines changed

15 files changed

+924
-675
lines changed

python/demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
engine = RapidOCR()
77

88
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
9-
result = engine(img_url, return_word_box=True, return_single_char_box=True)
9+
result = engine(img_url)
1010
print(result)
1111

1212
result.vis("vis_result.jpg")

python/rapidocr/default_models.yaml

Lines changed: 214 additions & 169 deletions
Large diffs are not rendered by default.

python/rapidocr/utils/typings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class LangRec(Enum):
2929
TA = "ta"
3030
TE = "te"
3131
ESLAV = "eslav"
32+
TH = "th"
33+
EL = "el"
3234

3335

3436
class OCRVersion(Enum):

python/tests/test_cli.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: liekkaskono@163.com
4+
import shlex
5+
import sys
6+
from pathlib import Path
7+
8+
import pytest
9+
from pytest import mark
10+
11+
root_dir = Path(__file__).resolve().parent.parent
12+
sys.path.append(str(root_dir))
13+
14+
from rapidocr import RapidOCR
15+
from rapidocr.main import main
16+
17+
tests_dir = root_dir / "tests" / "test_files"
18+
img_path = tests_dir / "ch_en_num.jpg"
19+
20+
21+
@pytest.fixture()
22+
def engine():
23+
engine = RapidOCR()
24+
return engine
25+
26+
27+
@mark.parametrize("cmd,gt", [(f"--img_path {img_path}", "正品促销")])
28+
def test_cli(capsys, cmd, gt):
29+
main(shlex.split(cmd))
30+
output = capsys.readouterr().out.strip()
31+
assert gt in output
32+
33+
34+
@mark.parametrize("cmd", [f"config --save_cfg_file {tests_dir}/config.yaml"])
35+
def test_cli_config(capsys, cmd):
36+
main(shlex.split(cmd))
37+
output = capsys.readouterr().out.strip()
38+
39+
assert "The config file has saved in" in output
40+
41+
cfg_yaml_path = tests_dir / "config.yaml"
42+
assert cfg_yaml_path.exists()
43+
cfg_yaml_path.unlink()
44+
45+
46+
@mark.parametrize("cmd", ["check"])
47+
def test_cli_check(capsys, cmd):
48+
main(shlex.split(cmd))
49+
output = capsys.readouterr().out.strip()
50+
51+
assert "Success! rapidocr is installed correctly!" in output
52+
53+
54+
@mark.parametrize(
55+
"cmd,img_name",
56+
[
57+
(
58+
f"--img_path {img_path} -vis --vis_save_dir {tests_dir}",
59+
f"{img_path.stem}_vis.png",
60+
),
61+
(
62+
f"--img_path {img_path} -vis --vis_save_dir {tests_dir} -word",
63+
f"{img_path.stem}_vis_single.png",
64+
),
65+
],
66+
)
67+
def test_cli_vis(cmd, img_name):
68+
main(shlex.split(cmd))
69+
vis_path = tests_dir / img_name
70+
assert vis_path.exists()
71+
vis_path.unlink()
72+
73+
74+
def test_cli_lang_type():
75+
img_path = tests_dir / "japan.jpg"
76+
cmd = f"--img_path {img_path} --lang_type japan -vis --vis_save_dir {tests_dir}"
77+
vis_path = tests_dir / f"{img_path.stem}_vis.png"
78+
79+
main(shlex.split(cmd))
80+
assert vis_path.exists()
81+
vis_path.unlink()

python/tests/test_det_cls_rec.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: liekkaskono@163.com
4+
import sys
5+
from pathlib import Path
6+
7+
import cv2
8+
import pytest
9+
10+
root_dir = Path(__file__).resolve().parent.parent
11+
sys.path.append(str(root_dir))
12+
13+
from rapidocr import RapidOCR
14+
15+
tests_dir = root_dir / "tests" / "test_files"
16+
img_path = tests_dir / "ch_en_num.jpg"
17+
18+
19+
@pytest.fixture()
20+
def engine():
21+
engine = RapidOCR()
22+
return engine
23+
24+
25+
def test_only_det(engine):
26+
result = engine(img_path, use_det=True, use_cls=False, use_rec=False)
27+
assert len(result) == 18
28+
29+
30+
def test_only_cls(engine):
31+
img_path = tests_dir / "text_cls.jpg"
32+
result = engine(img_path, use_det=False, use_cls=True, use_rec=False)
33+
assert len(result) == 1
34+
assert result.cls_res[0][0] == "0"
35+
36+
37+
def test_only_rec(engine):
38+
img_path = tests_dir / "text_rec.jpg"
39+
result = engine(img_path, use_det=False, use_cls=False, use_rec=True)
40+
assert len(result) == 1
41+
assert result.txts[0] == "韩国小馆"
42+
43+
44+
def test_det_rec(engine):
45+
result = engine(img_path, use_det=True, use_cls=False, use_rec=True)
46+
assert len(result) == 18
47+
assert result.txts[0] == "正品促销"
48+
49+
50+
def test_cls_rec(engine):
51+
img_path = tests_dir / "text_cls.jpg"
52+
result = engine(img_path, use_det=False, use_cls=True, use_rec=True)
53+
54+
assert result is not None
55+
assert len(result) == 1
56+
assert result.txts[0] == "韩国小馆"
57+
58+
59+
def test_det_cls_rec(engine):
60+
img = cv2.imread(str(img_path))
61+
62+
result = engine(img)
63+
assert result is not None
64+
assert len(result) == 18
65+
assert result.txts[0] == "正品促销"

python/tests/test_engine.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: liekkaskono@163.com
4+
import sys
5+
from pathlib import Path
6+
7+
from pytest import mark
8+
9+
root_dir = Path(__file__).resolve().parent.parent
10+
sys.path.append(str(root_dir))
11+
12+
from rapidocr import EngineType, ModelType, OCRVersion, RapidOCR
13+
14+
tests_dir = root_dir / "tests" / "test_files"
15+
img_path = tests_dir / "ch_en_num.jpg"
16+
17+
18+
@mark.parametrize(
19+
"engine_type",
20+
[EngineType.ONNXRUNTIME, EngineType.PADDLE, EngineType.OPENVINO, EngineType.TORCH],
21+
)
22+
def test_ppocrv5_rec_mobile(engine_type):
23+
engine = RapidOCR(
24+
params={
25+
"Rec.ocr_version": OCRVersion.PPOCRV5,
26+
"Rec.model_type": ModelType.MOBILE,
27+
"Rec.engine_type": engine_type,
28+
}
29+
)
30+
img_path = tests_dir / "text_rec.jpg"
31+
result = engine(img_path, use_det=False, use_cls=False, use_rec=True)
32+
33+
assert result.txts is not None
34+
assert result.txts[0] == "韩国小馆"
35+
36+
37+
@mark.parametrize(
38+
"engine_type",
39+
[EngineType.ONNXRUNTIME, EngineType.PADDLE, EngineType.OPENVINO, EngineType.TORCH],
40+
)
41+
def test_ppocrv5_det_mobile(engine_type):
42+
engine = RapidOCR(
43+
params={
44+
"Det.ocr_version": OCRVersion.PPOCRV5,
45+
"Det.model_type": ModelType.MOBILE,
46+
"Det.engine_type": engine_type,
47+
}
48+
)
49+
img_path = tests_dir / "ch_en_num.jpg"
50+
result = engine(img_path, use_det=True, use_cls=False, use_rec=False)
51+
52+
assert result.boxes is not None
53+
assert len(result.boxes) == 17
54+
55+
56+
@mark.skipif(sys.platform.startswith("darwin"), reason="does not run on macOS")
57+
def test_engine_openvino():
58+
engine = RapidOCR(
59+
params={
60+
"Det.engine_type": EngineType.OPENVINO,
61+
"Cls.engine_type": EngineType.OPENVINO,
62+
"Rec.engine_type": EngineType.OPENVINO,
63+
}
64+
)
65+
66+
result = engine(img_path)
67+
assert result.txts is not None
68+
assert result.txts[0] == "正品促销"
69+
70+
71+
def test_engine_paddle():
72+
engine = RapidOCR(
73+
params={
74+
"Det.engine_type": EngineType.PADDLE,
75+
"Cls.engine_type": EngineType.PADDLE,
76+
"Rec.engine_type": EngineType.PADDLE,
77+
}
78+
)
79+
80+
result = engine(img_path)
81+
assert result.txts is not None
82+
assert result.txts[0] == "正品促销"
83+
84+
85+
def test_engine_torch():
86+
engine = RapidOCR(
87+
params={
88+
"Det.engine_type": EngineType.TORCH,
89+
"Cls.engine_type": EngineType.TORCH,
90+
"Rec.engine_type": EngineType.TORCH,
91+
}
92+
)
93+
94+
result = engine(img_path)
95+
assert result.txts is not None
96+
assert result.txts[0] == "正品促销"

python/tests/test_files/el_rec.jpg

9.11 KB
Loading

python/tests/test_files/en_rec.jpg

19.8 KB
Loading

python/tests/test_files/th_rec.jpg

30.9 KB
Loading

0 commit comments

Comments
 (0)