目录
1.环境搭建
1.1 创建conda环境
conda create --name paddlehub python=3.8
conda activate paddlehub
1.2 安装paddlepaddle和paddlehub
pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install paddlehub -i https://pypi.tuna.tsinghua.edu.cn/simple
1.3 安装依赖
pip install shapely pyclipper
2. 移动端模型部署
2.1 安装移动端模型
hub install chinese_ocr_db_crnn_mobile
报错1:
增加download函数
vim /home/yinsuan/miniconda3/envs/paddlehub/lib/python3.8/site-packages/aistudio_sdk/hub.py
def download(url, target_path):
os.makedirs(os.path.dirname(target_path), exist_ok=True)
response = requests.get(url, stream=True)
with open(target_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print(f"Downloaded {url} to {target_path}")
报错2:
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
安装成功
2.2 测试
将其中的np.int 修改为int即可
vim /home/yinsuan/.paddlehub/modules/chinese_ocr_db_crnn_mobile/module.py
hub run chinese_ocr_db_crnn_mobile --input_path "/PATH/TO/IMAGE"
测试成功
3. 服务部署
3.1 启动PaddleHub Serving
hub serving start -m chinese_ocr_db_crnn_mobile
3.2 发送预测请求
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"])