1. 环境搭建
参考:《PaddleOCR加载chinese_ocr_db_crnn_server模型进行中英文混合预测(命令行)实践》
2. 服务端部署
hub serving start -m chinese_ocr_db_crnn_mobile -p 8866
3. 客户端访问
# coding: utf8
import requests
import json
import cv2
import base64
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
# 发送HTTP请求
data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_mobile"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
# 打印预测结果
print(r.json()["results"])
4. 将图像识别服务部署到Flask上
先参考《Flask开发实践》或《Flask入门》
在此基础上进行了以下修改:
文件routes.py增加的内容
import base64
import json
import os
import cv2
import requests
from werkzeug.utils import secure_filename
# OCR测试
@webapp.route('/ocr', methods=['POST', 'GET'])
def ocr():
basepath = os.path.dirname(__file__) # 当前文件所在路径
print(request.method)
if request.method == 'POST':
f = request.files['file']
upload_path = os.path.join(basepath, 'static/uploads', secure_filename(f.filename)) # 注意:没有的文件夹要先创建
f.save(upload_path)
return redirect(url_for('ocr_result', pic_name=f.filename))
else:
return render_template('ocr.html')
# OCR测试结果
@webapp.route('/ocr/<pic_name>', methods=['POST', 'GET'])
def ocr_result(pic_name):
basepath = os.path.dirname(__file__) # 当前文件所在路径
print(request.method)
if pic_name != "":
upload_path = os.path.join(basepath, 'static/uploads', secure_filename(pic_name))
data = {'images': [cv2_to_base64(cv2.imread(upload_path))]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_mobile"
# 图像识别服务: hub.exe serving start -m chinese_ocr_db_crnn_mobile -p 8866
r = requests.post(url=url, headers=headers, data=json.dumps(data))
# 打印预测结果
results = r.json()["results"][0]["data"]
return render_template('ocr.html', results=results, file=upload_path.replace(basepath, ''))
else:
return render_template('ocr.html')
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
新建模板文件ocr.html
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>图像识别测试页</title>
</head>
<body>
<h1>图像识别测试页</h1>
<form action="/ocr" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
<h1>识别结果</h1>
<img src="{{ file }}">
{% for result in results %}
<table>
<tr valign="top">
<td>[置信度] {{ (result.confidence*100) | round(2) }}%, [文字内容] {{ result.text }}</td>
</tr>
</table>
{% endfor %}
{{ result }}
</body>
</html>
5. 运行效果
先上传图片
提交后显示图片内容和识别结果
【参考文档】
《PaddleHub一键OCR中文识别》