Skip to content

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

Update ocrweb api deploy

SWHL60a94c42ab35
diff --git a/ocrweb/README.md b/ocrweb/README.md
index ac2dead..c34a801 100755
--- a/ocrweb/README.md
+++ b/ocrweb/README.md
@@ -63,31 +63,21 @@
import requests
- def get_byte(img_path):
+ def get_json_format(img_path):
with open(img_path, 'rb') as f:
img_byte = base64.b64encode(f.read())
- img_str = img_byte.decode('ascii')
- return img_str
+ img_json = json.dumps({'file': img_byte.decode('ascii')})
+ return img_json
if __name__ == '__main__':
- header = {
- "cookie": "token=code_space;",
- "Accept": "*/*",
- "Accept-Encoding": "gzip, deflate, br",
- "Accept-Language": "zh-CN,zh;q=0.9",
- "Connection": "keep-alive",
- "Content-Type": "application/json; charset=UTF-8",
- }
-
url = 'http://localhost:9003/ocr'
+ header = {'Content-Type': 'application/json; charset=UTF-8'}
img_path = '../images/1.jpg'
- img = get_byte(img_path)
- post_json = json.dumps({'file': img})
-
- response = requests.post(url, data=post_json, headers=header)
+ img_json = get_json_format(img_path)
+ response = requests.post(url, data=img_json, headers=header)
if response.status_code == 200:
rec_res = ast.literal_eval(response.text)
print(rec_res)
@@ -100,6 +90,8 @@
```text
[['0', '香港深圳抽血', '0.93583983'], ['1', '专业查性别', '0.89865875'], ['2', '专业鉴定B超单', '0.9955703'], ['3', 'b超仪器查性别', '0.99489486'], ['4', '加微信eee', '0.99073666'], ['5', '可邮寄', '0.99923944']]
```
+ - 如果图像中存在文字,则会输出`list`类型,如上。
+ - 如果没有检测到文字,则会输出空列表(`[]`)。
### 相关问题
1. 各个阶段使用的模型以及配置参数有哪些?
diff --git a/ocrweb/api.py b/ocrweb/api.py
index 21dd6b6..e9a9009 100644
--- a/ocrweb/api.py
+++ b/ocrweb/api.py
@@ -2,7 +2,6 @@
# @Author: SWHL
# @Contact: [email protected]
import base64
-import json
from wsgiref.simple_server import make_server
import cv2
@@ -32,7 +31,7 @@ def ocr():
if image.ndim == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
- img, elapse, elapse_part, rec_res_data = detect_recognize(image)
+ rec_res_data = detect_recognize(image, is_api=True)
return rec_res_data
diff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py
index a0dbf8d..c24d907 100644
--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py
+++ b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py
@@ -112,5 +112,5 @@ class CTCLabelDecode(object):
else:
conf_list.append(1)
text = ''.join(char_list)
- result_list.append((text, np.mean(conf_list)))
+ result_list.append((text, np.mean(conf_list + [1e-20])))
return result_list
diff --git a/ocrweb/task.py b/ocrweb/task.py
index 6b3c989..f1abf2d 100644
--- a/ocrweb/task.py
+++ b/ocrweb/task.py
@@ -15,7 +15,7 @@ from rapidocr_onnxruntime import TextSystem
text_sys = TextSystem('config.yaml')
-def detect_recognize(image_path):
+def detect_recognize(image_path, is_api=False):
if isinstance(image_path, str):
image = cv2.imread(image_path)
elif isinstance(image_path, np.ndarray):
@@ -26,31 +26,34 @@ def detect_recognize(image_path):
dt_boxes, rec_res, img, elapse_part = text_sys(image)
if dt_boxes is None or rec_res is None:
- rec_res_data = json.dumps([],
- indent=2,
- ensure_ascii=False)
- elapse = 0
- elapse_part = ''
- image = cv2.imencode('.jpg', img)[1]
- img = str(base64.b64encode(image))[2:-1]
+ elapse, elapse_part = 0, ''
+ img_str = img_to_base64(img)
+ rec_res_data = json.dumps([], indent=2, ensure_ascii=False)
else:
temp_rec_res = []
for i, value in enumerate(rec_res):
temp_rec_res.append([i, value[0], value[1]])
temp_rec_res = np.array(temp_rec_res)
-
rec_res_data = json.dumps(temp_rec_res.tolist(),
indent=2,
ensure_ascii=False)
det_im = draw_text_det_res(dt_boxes, img)
- image = cv2.imencode('.jpg', det_im)[1]
- img = str(base64.b64encode(image))[2:-1]
+ img_str = img_to_base64(det_im)
elapse = reduce(lambda x, y: float(x)+float(y), elapse_part)
elapse_part = ','.join([str(x) for x in elapse_part])
- return img, elapse, elapse_part, rec_res_data
+ if is_api:
+ return rec_res_data
+ else:
+ return img_str, elapse, elapse_part, rec_res_data
+
+
+def img_to_base64(img):
+ img = cv2.imencode('.jpg', img)[1]
+ img_str = str(base64.b64encode(img))[2:-1]
+ return img_str
def check_and_read_gif(img_path):