Skip to content

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

Add ocrweb api deploy

SWHL96efec9a0a7e
diff --git a/.gitignore b/.gitignore
index 5aefa29..80bb95f 100755
--- a/.gitignore
+++ b/.gitignore
@@ -151,4 +151,6 @@ long1.jpg
*.pdiparams
*.pdiparams.info
-*.pdmodel
\ No newline at end of file
+*.pdmodel
+
+.DS_Store
\ No newline at end of file
diff --git a/README.md b/README.md
index f2671ec..0b4d198 100755
--- a/README.md
+++ b/README.md
@@ -25,6 +25,7 @@
- [RapidOCR (捷智OCR)](#rapidocr-捷智ocr)
- [简介](#简介)
- [近期更新(more)](#近期更新more)
+ - [🦣2022-08-14 update:](#2022-08-14-update)
- [🎧2022-07-10 update:](#2022-07-10-update)
- [✨2022-07-07 update:](#2022-07-07-update)
- [相关项目(more)](#相关项目more)
@@ -62,6 +63,10 @@
- 基于百度的开源PaddleOCR 模型及训练,任何人可以使用本推理库,也可以根据自己的需求使用百度的paddlepaddle框架进行模型优化。
## 近期更新([more](./docs/change_log.md))
+#### 🦣2022-08-14 update:
+- ocrweb部分增加以API方式部署调用的功能,可以通过发送POST请求,来获得OCR识别结果。
+- 详情参见:[API方式调用](./ocrweb/README.md#以api方式运行和调用)
+
#### 🎧2022-07-10 update:
- 添加onnxruntime-gpu的测试情况→[link](./python/README.md#onnxruntime-gpu版推理配置)
- 添加benchamark测试集 → [link](./images/README.md)
diff --git a/docs/README_en.md b/docs/README_en.md
index 41d5c9f..f177e7f 100644
--- a/docs/README_en.md
+++ b/docs/README_en.md
@@ -24,6 +24,7 @@
- [RapidOCR](#rapidocr)
- [Introduction](#introduction)
- [Recently updates(more)](#recently-updatesmore)
+ - [🦣2022-08-14 update:](#2022-08-14-update)
- [🎧2022-07-10 update:](#2022-07-10-update)
- [✨2022-07-07 update:](#2022-07-07-update)
- [Related Projects(more)](#related-projectsmore)
@@ -61,6 +62,10 @@
- Based on Baidu's open source PaddleOCR model and training, anyone can use this inference library, or use Baidu's paddlepaddle framework for model optimization according to their own needs.
## Recently updates([more](./change_log_en.md))
+#### 🦣2022-08-14 update:
+- The ocrweb part adds the function of deploying calls by API, and you can send POST requests to get OCR recognition results.
+- For details, see: [API deploy](./ocrweb/README.md#以api方式运行和调用)
+
#### 🎧2022-07-10 update:
- Add test case for onnxruntime-gpu → [link](../python/README.md#onnxruntime-gpu version inference configuration)
- Add benchamark test set → [link](../images/README.md)
diff --git a/docs/change_log.md b/docs/change_log.md
index 94d005b..e727e4a 100644
--- a/docs/change_log.md
+++ b/docs/change_log.md
@@ -1,3 +1,7 @@
+#### 🦣2022-08-14 update:
+- ocrweb部分增加以API方式部署调用的功能,可以通过发送POST请求,来获得OCR识别结果。
+- 详情参见:[API方式调用](../ocrweb/README.md#以api方式运行和调用)
+
#### ✨2022-07-07 update:
- 修复python版中v3 rec推理bug,并将v3 rec与v2 rec合并为同一套推理代码,更加简洁和方便
- 添加python模块下的单元测试
diff --git a/docs/change_log_en.md b/docs/change_log_en.md
index 19ac040..c301a52 100644
--- a/docs/change_log_en.md
+++ b/docs/change_log_en.md
@@ -1,3 +1,7 @@
+#### 🦣2022-08-14 update:
+- The ocrweb part adds the function of deploying calls by API, and you can send POST requests to get OCR recognition results.
+- For details, see: [API deploy](../ocrweb/README.md#以api方式运行和调用)
+
#### 🎧2022-07-10 update:
- Add test case for onnxruntime-gpu → [link](../python/README.md#onnxruntime-gpu version inference configuration)
- Add benchamark test set → [link](../images/README.md)
diff --git a/ocrweb/README.md b/ocrweb/README.md
index bbdedfd..31c3e5f 100755
--- a/ocrweb/README.md
+++ b/ocrweb/README.md
@@ -10,7 +10,7 @@
<img src="../assets/ocrweb_time.jpg" width="80%" height="80%">
</div>
-### 运行
+### Web方式运行
1. 安装`requirements.txt`下相关包
```shell
pip install -r requirements.txt -i https://pypi.douban.com/simple/
@@ -50,3 +50,58 @@
python main.py
```
4. 打开`http://0.0.0.0:9003/`即可, enjoy it!
+
+### 以API方式运行和调用
+1. 同**Web方式运行**中步骤1
+2. 同**Web方式运行**中步骤2
+3. 运行`api.py`
+ ```python
+ python api.py
+ ```
+
+4. 发送post请求,调用
+ ```python
+ import ast
+ import base64
+ import json
+
+ import requests
+
+
+ def get_byte(img_path):
+ with open(img_path, 'rb') as f:
+ img_byte = base64.b64encode(f.read())
+ img_str = img_byte.decode('ascii')
+ return img_str
+
+
+ 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'
+
+ 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)
+
+ if response.status_code == 200:
+ rec_res = ast.literal_eval(response.text)
+ print(rec_res)
+ else:
+ print(response.status_code)
+
+ ```
+
+5. 输出以下结果,即为正确。
+ ```text
+ [['0', '香港深圳抽血', '0.93583983'], ['1', '专业查性别', '0.89865875'], ['2', '专业鉴定B超单', '0.9955703'], ['3', 'b超仪器查性别', '0.99489486'], ['4', '加微信eee', '0.99073666'], ['5', '可邮寄', '0.99923944']]
+ ```
\ No newline at end of file
diff --git a/ocrweb/api.py b/ocrweb/api.py
new file mode 100644
index 0000000..21dd6b6
--- /dev/null
+++ b/ocrweb/api.py
@@ -0,0 +1,44 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+import base64
+import json
+from wsgiref.simple_server import make_server
+
+import cv2
+import numpy as np
+from flask import Flask, render_template, request
+
+from task import detect_recognize
+
+app = Flask(__name__)
+app.config['MAX_CONTENT_LENGTH'] = 3 * 1024 * 1024
+
+
+def index():
+ return render_template('index.html')
+
+
[email protected]('/ocr', methods=['POST'])
+def ocr():
+ if request.method == 'POST':
+ url_get = request.get_json()
+ img_str = url_get.get('file')
+
+ image = base64.b64decode(img_str)
+ nparr = np.frombuffer(image, np.uint8)
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
+ if image.ndim == 2:
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
+
+ img, elapse, elapse_part, rec_res_data = detect_recognize(image)
+ return rec_res_data
+
+
+if __name__ == '__main__':
+ ip = '0.0.0.0'
+ ip_port = 9003
+
+ server = make_server(ip, ip_port, app)
+ server.serve_forever()
diff --git a/ocrweb/main.py b/ocrweb/main.py
index 928208b..380592f 100644
--- a/ocrweb/main.py
+++ b/ocrweb/main.py
@@ -2,16 +2,16 @@
# @Author: SWHL
# @Contact: [email protected]
import base64
+import json
+from wsgiref.simple_server import make_server
import cv2
import numpy as np
from flask import Flask, render_template, request
-from wsgiref.simple_server import make_server
from task import detect_recognize
app = Flask(__name__)
-
app.config['MAX_CONTENT_LENGTH'] = 3 * 1024 * 1024
@@ -24,14 +24,19 @@ def index():
def ocr():
if request.method == 'POST':
url_get = request.get_json()
- url_get = str(url_get).split(',')[1]
+ img_str = str(url_get).split(',')[1]
- image = base64.b64decode(url_get)
+ image = base64.b64decode(img_str)
nparr = np.frombuffer(image, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if image.ndim == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
- return detect_recognize(image)
+
+ img, elapse, elapse_part, rec_res_data = detect_recognize(image)
+ return json.dumps({'image': img,
+ 'total_elapse': f'{elapse:.4f}',
+ 'elapse_part': elapse_part,
+ 'rec_res': rec_res_data})
if __name__ == '__main__':
diff --git a/ocrweb/task.py b/ocrweb/task.py
index 0d813e5..6b3c989 100644
--- a/ocrweb/task.py
+++ b/ocrweb/task.py
@@ -50,10 +50,7 @@ def detect_recognize(image_path):
elapse = reduce(lambda x, y: float(x)+float(y), elapse_part)
elapse_part = ','.join([str(x) for x in elapse_part])
- return json.dumps({'image': img,
- 'total_elapse': f'{elapse:.4f}',
- 'elapse_part': elapse_part,
- 'rec_res': rec_res_data})
+ return img, elapse, elapse_part, rec_res_data
def check_and_read_gif(img_path):