Skip to content

Commit 2d81290

Browse files
authored
feat: adapt rapidocr v2
feat: adapt rapidocr v2
2 parents 021ae47 + 083e035 commit 2d81290

File tree

3 files changed

+44
-66
lines changed

3 files changed

+44
-66
lines changed

demo.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,41 @@
1010
img_path = "tests/test_files/ch_en_num.jpg"
1111

1212
# 方式一:使用base64编码传
13-
stime = time.time()
13+
stime = time.perf_counter()
1414
with open(img_path, "rb") as fa:
1515
img_str = base64.b64encode(fa.read())
1616

1717
payload = {"image_data": img_str}
18-
response = requests.post(url, data=payload) # , timeout=60
18+
response = requests.post(url, data=payload, timeout=180)
1919

2020
print(response.json())
21-
etime = time.time() - stime
22-
print(f"用时:{etime:.3f}秒")
21+
elapse = time.perf_counter() - stime
22+
print(f"用时:{elapse:.3f}秒")
2323

2424
print("-" * 40)
2525

26-
# # 方式二:使用文件上传方式
27-
# stime = time.time()
28-
# with open(img_path, "rb") as f:
29-
# file_dict = {"image_file": (img_path, f, "image/png")}
30-
# response = requests.post(url, files=file_dict) # , timeout=60
31-
# print(response.json())
32-
33-
# etime = time.time() - stime
34-
# print(f"用时:{etime:.3f}秒")
35-
# print("-" * 40)
36-
37-
# # 方式三:控制是否使用检测、方向分类和识别这三部分的模型; 不使用检测模型:use_det=False
38-
# stime = time.time()
39-
# img_path = "../python/tests/test_files/test_without_det.jpg"
40-
41-
# with open(img_path, "rb") as f:
42-
# file_dict = {"image_file": (img_path, f, "image/png")}
43-
# # 添加控制参数
44-
# data = {"use_det": False, "use_cls": True, "use_rec": True}
45-
# response = requests.post(url, files=file_dict, data=data) # , timeout=60
46-
# print(response.json())
47-
48-
# etime = time.time() - stime
49-
# print(f"用时:{etime:.3f}秒")
50-
# print("-" * 40)
26+
# 方式二:使用文件上传方式
27+
stime = time.perf_counter()
28+
with open(img_path, "rb") as f:
29+
file_dict = {"image_file": (img_path, f, "image/png")}
30+
response = requests.post(url, files=file_dict, timeout=180)
31+
print(response.json())
32+
33+
elapse = time.perf_counter() - stime
34+
print(f"用时:{elapse:.3f}秒")
35+
print("-" * 40)
36+
37+
# 方式三:控制是否使用检测、方向分类和识别这三部分的模型; 不使用检测模型:use_det=False
38+
stime = time.perf_counter()
39+
img_path = "tests/test_files/ch_en_num.jpg"
40+
41+
with open(img_path, "rb") as f:
42+
file_dict = {"image_file": (img_path, f, "image/png")}
43+
# 添加控制参数
44+
data = {"use_det": False, "use_cls": True, "use_rec": True}
45+
response = requests.post(url, files=file_dict, data=data, timeout=180)
46+
print(response.json())
47+
48+
elapse = time.perf_counter() - stime
49+
print(f"用时:{elapse:.3f}秒")
50+
print("-" * 40)

rapidocr_api/main.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# @Contact: [email protected]
44
import argparse
55
import base64
6-
import importlib
76
import io
87
import os
98
import sys
@@ -14,17 +13,7 @@
1413
import uvicorn
1514
from fastapi import FastAPI, Form, UploadFile
1615
from PIL import Image
17-
18-
if importlib.util.find_spec("rapidocr_onnxruntime"):
19-
from rapidocr_onnxruntime import RapidOCR
20-
elif importlib.util.find_spec("rapidocr_paddle"):
21-
from rapidocr_paddle import RapidOCR
22-
elif importlib.util.find_spec("rapidocr_openvino"):
23-
from rapidocr_openvino import RapidOCR
24-
else:
25-
raise ImportError(
26-
"Please install one of [rapidocr_onnxruntime,rapidocr-paddle,rapidocr-openvino]"
27-
)
16+
from rapidocr import RapidOCR
2817

2918
sys.path.append(str(Path(__file__).resolve().parent.parent))
3019

@@ -39,36 +28,29 @@ def __init__(self) -> None:
3928
self.ocr = RapidOCR()
4029
else:
4130
self.ocr = RapidOCR(
42-
det_model_path=det_model_path,
43-
cls_model_path=cls_model_path,
44-
rec_model_path=rec_model_path,
31+
params={
32+
"Det.model_path": det_model_path,
33+
"Cls.model_path": cls_model_path,
34+
"Rec.model_path": rec_model_path,
35+
}
4536
)
4637

4738
def __call__(
48-
self, img: Image.Image, use_det=None, use_cls=None, use_rec=None, **kwargs
39+
self, ori_img: Image.Image, use_det=None, use_cls=None, use_rec=None, **kwargs
4940
) -> Dict:
50-
img = np.array(img)
51-
ocr_res, _ = self.ocr(
41+
img = np.array(ori_img)
42+
ocr_res = self.ocr(
5243
img, use_det=use_det, use_cls=use_cls, use_rec=use_rec, **kwargs
5344
)
5445

55-
if not ocr_res:
46+
if ocr_res.boxes is None or ocr_res.txts is None or ocr_res.scores is None:
5647
return {}
5748

5849
out_dict = {}
59-
for i, dats in enumerate(ocr_res):
60-
values = {}
61-
for dat in dats:
62-
if isinstance(dat, str):
63-
values["rec_txt"] = dat
64-
65-
if isinstance(dat, np.float32):
66-
values["score"] = f"{dat:.4f}"
67-
68-
if isinstance(dat, list):
69-
values["dt_boxes"] = dat
70-
out_dict[str(i)] = values
71-
50+
for i, (boxes, txt, score) in enumerate(
51+
zip(ocr_res.boxes, ocr_res.txts, ocr_res.scores)
52+
):
53+
out_dict[i] = {"rec_txt": txt, "dt_boxes": boxes.tolist(), "score": score}
7254
return out_dict
7355

7456

@@ -100,6 +82,7 @@ def ocr(
10082
"When sending a post request, data or files must have a value."
10183
)
10284
ocr_res = processor(img, use_det=use_det, use_cls=use_cls, use_rec=use_rec)
85+
10386
return ocr_res
10487

10588

setup.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,4 @@ def get_readme():
7777
f"{MODULE_NAME}={MODULE_NAME}.main:main",
7878
],
7979
},
80-
extras_require={
81-
"onnx": ["rapidocr-onnxruntime"],
82-
"paddle": ["rapidocr-paddle"],
83-
"openvino": ["rapidocr-openvino"],
84-
},
8580
)

0 commit comments

Comments
 (0)