PaddleX部署OCR产线
前情提要: PaddleX主要介绍高稳定性服务器部署过程
✅ 准备工作
PaddleX模型产线部署是根据产线来部署的,所以先要确定你需要的产线,
- 下载对应的SDK,比如本文示例:·通用OCR。
- 下载PaddleX,用于部署的机器上需要安装有 19.03 或更高版本的 Docker Engine。首先,根据需要拉取 Docker 镜像:
-
支持使用 NVIDIA GPU 部署的镜像(机器上需要安装有支持 CUDA 11.8 的 NVIDIA 驱动)
docker pull ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlex/hps:paddlex3.4-gpu
-
CPU-only 镜像:
docker pull ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlex/hps:paddlex3.4-cpu
本文示例下载:docker pull ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlex/hps:paddlex3.4-gpu
**注意:**如果是无网络部署,那么根据SDK下的pipeline_config产线配置文件中把对应的包都提前下载好。例如本示例需要的模型:
| 模块名称 | 配置中的模型名 | 模型作用 | 推理模型下载链接 |
|---|---|---|---|
| DocOrientationClassify(文档方向分类) | PP-LCNet_x1_0_doc_ori |
自动校正文档整体旋转方向 | https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x1_0_doc_ori_infer.tar |
| DocUnwarping(文档图像矫正) | UVDoc |
校正文档透视 / 弯曲变形(你配置中use_doc_unwarping: false,可选下载) |
https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/UVDoc_infer.tar |
| TextDetection(文本检测) | PP-OCRv5_server_det |
定位图像中的文字区域 | https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_det_infer.tar |
| TextLineOrientation(文本行方向分类) | PP-LCNet_x1_0_textline_ori |
校正单行文字的旋转方向 | https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-LCNet_x1_0_textline_ori_infer.tar |
| TextRecognition(文本识别) | (配置中未显式写模型名,OCR 产线默认配套PP-OCRv5_server_rec) |
识别文字区域的内容 | https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-OCRv5_server_rec_infer.tar |
✅开始安装
自定义模型存放路径,将以上模型均放在同个目录下
✅Docker配置
shell
~/ai$ mkidr paddleX3.4-OCR
~/ai$ cd paddleX3.4-OCR
~/ai/paddleX3.4-OCR$ tar -xzvf SDK.tar.gz ./ #将SDK在本目录解压
~/ai/paddleX3.4-OCR$ nano docker-compose.yml # 新增docker-compose.yml
将以下内容复制拷贝进docker-compose.yml文件里:
- 单卡部署
yaml
services:
paddlex-server:
image: ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlex/hps:paddlex3.4-gpu
container_name: paddlex-ocr-hps
# 特权模式,如果遇到权限问题再开启
# privileged: true
environment:
- PADDLEX_HPS_DEVICE_TYPE=gpu # 设备类型:gpu cpu
- NVIDIA_VISIBLE_DEVICES=0 # 指定显卡下标
# - PADDLEX_HPS_USE_HPIP=1 # 可以使用 PaddleX 高性能推理插件加速产线推理过程
- PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK=True # 禁用在线搜索模型
volumes:
# models地址映射
- ./models:/app/models
# 配置地址映射
- ./server:/app
ports:
- "8000:8000"
# 工作目录:对应 -w /app
working_dir: /app
# GPU 配置:对应 --gpus all
runtime: nvidia
shm_size: 8gb #指定共享内存
# 作用:作为 init 系统接收信号,防止僵尸进程
init: true
command: /bin/bash server.sh start
restart: on-failure
- 多卡部署的本质是同一个镜像开4个容器,每个容器指定固定下标的显卡,再去修改产线配置文件。具体参考官方文档 后续有实现多卡部署时再完善
ctrl+o 保存当前文件回车,ctrl+x退出当前编辑状态。
✅ 修改配置
server/pipeline_config.yaml 为产线配置文件,将下载好的模型所在地址对应补充进,如本文示例:
**注意注意注意:**model_dir是要填写容器内的模型映射地址,取决于docker-compose.yml里的volumes模型挂载配置
pipeline_config.yaml
pipeline_name: OCR
text_type: general
use_doc_preprocessor: True
use_textline_orientation: True
SubPipelines:
DocPreprocessor:
pipeline_name: doc_preprocessor
use_doc_orientation_classify: true
use_doc_unwarping: true
SubModules:
DocOrientationClassify:
module_name: doc_text_orientation
model_name: PP-LCNet_x1_0_doc_ori
model_dir: /app/models/PP-LCNet_x1_0_doc_ori_infer
DocUnwarping:
module_name: image_unwarping
model_name: UVDoc
model_dir: /app/models/UVDoc_infer
SubModules:
TextDetection:
module_name: text_detection
model_name: PP-OCRv5_server_det
model_dir: /app/models/PP-OCRv5_server_det_infer
limit_side_len: 64
limit_type: min
max_side_limit: 4000
thresh: 0.3
box_thresh: 0.6
unclip_ratio: 1.5
TextLineOrientation:
module_name: textline_orientation
model_name: PP-LCNet_x1_0_textline_ori
model_dir: /app/models/PP-LCNet_x1_0_textline_ori_infer
batch_size: 6
TextRecognition:
module_name: text_recognition
model_name: PP-OCRv5_server_rec
model_dir: /app/models/PP-OCRv5_server_rec_infer
batch_size: 6
score_thresh: 0.0
✅启动
docker compose 命令启动:
shell
~/ai/paddleX3.4-OCR$ docker compose up -d
✅启动验证
日志输出类似:
I1216 11:37:21.601943 35 grpc_server.cc:4117] Started GRPCInferenceService at 0.0.0.0:8001
I1216 11:37:21.602333 35 http_server.cc:2815] Started HTTPService at 0.0.0.0:8000
I1216 11:37:21.643494 35 http_server.cc:167] Started Metrics Service at 0.0.0.0:8002
✅ HTTP接口调用
python调用可参考官网,一下是http的OCR接口调用:
curl
curl --location --request POST 'http://localhost:8000/v2/models/ocr/infer' \
--header 'Content-Type: application/json' \
--data-raw '{
"inputs": [
{
"name": "input",
"shape": [
1,
1
],
"datatype": "BYTES",
"data": [
"{\"file\":\"https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png\",\"visualize\":false}"
]
}
],
"outputs": [
{
"name": "output"
}
]
}'
输出:
json
{
"model_name": "ocr",
"model_version": "1",
"outputs": [
{
"name": "output",
"datatype": "BYTES",
"shape": [
1,
1
],
"data": [
"{\"logId\":\"260c78c3-2635-4432-bf2b-9761dfcf3c5d\",\"result\":{\"ocrResults\":[{\"prunedResult\":{\"model_settings\":{\"use_doc_preprocessor\":true,\"use_textline_orientation\":true},\"doc_preprocessor_res\":{\"model_settings\":{\"use_doc_orientation_classify\":true,\"use_doc_unwarping\":true},\"angle\":0},\"dt_polys\":[[[0,311],[496,327],[493,420],[0,403]],[[168,409],[392,413],[391,464],[167,461]],[[0,452],[517,465],[515,537],[0,524]],[[54,525],[379,537],[377,582],[53,570]]],\"text_det_params\":{\"limit_side_len\":64,\"limit_type\":\"min\",\"thresh\":0.3,\"max_side_limit\":4000,\"box_thresh\":0.6,\"unclip_ratio\":1.5},\"text_type\":\"general\",\"textline_orientation_angles\":[0,0,0,0],\"text_rec_score_thresh\":0.0,\"return_word_box\":false,\"rec_texts\":[\"上海斯格威铂尔曼大酒店\",\"打浦路15号\",\"绿洲仕格维花园公寓\",\"打浦路252935号\"],\"rec_scores\":[0.9444320201873779,0.9936174750328064,0.9817326068878174,0.9838994741439819],\"rec_polys\":[[[0,311],[496,327],[493,420],[0,403]],[[168,409],[392,413],[391,464],[167,461]],[[0,452],[517,465],[515,537],[0,524]],[[54,525],[379,537],[377,582],[53,570]]],\"rec_boxes\":[[0,311,496,420],[167,409,392,464],[0,452,517,537],[53,525,379,582]]}}],\"dataInfo\":{\"width\":720,\"height\":1150,\"type\":\"image\"}},\"errorCode\":0,\"errorMsg\":\"Success\"}"
]
}
]
}