diff --git a/deploy/pdserving/det_web_client.py b/deploy/pdserving/det_web_client.py new file mode 100644 index 0000000000000000000000000000000000000000..4324406bc5d17124b208e6064f53562850799980 --- /dev/null +++ b/deploy/pdserving/det_web_client.py @@ -0,0 +1,40 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# -*- coding: utf-8 -*- + +import requests +import json +import cv2 +import base64 +import os, sys +import time + + +def cv2_to_base64(image): + #data = cv2.imencode('.jpg', image)[1] + return base64.b64encode(image).decode( + 'utf8') #data.tostring()).decode('utf8') + + +headers = {"Content-type": "application/json"} +url = "http://127.0.0.1:9292/ocr/prediction" +test_img_dir = "../../doc/imgs/" +for img_file in os.listdir(test_img_dir): + with open(os.path.join(test_img_dir, img_file), 'rb') as file: + image_data1 = file.read() + image = cv2_to_base64(image_data1) + data = {"feed": [{"image": image}], "fetch": ["res"]} + r = requests.post(url=url, headers=headers, data=json.dumps(data)) + rjson = r.json() + print(rjson) diff --git a/deploy/pdserving/readme.md b/deploy/pdserving/readme.md deleted file mode 100644 index c3b7be454c4446bd9fda661b545b8ad167165459..0000000000000000000000000000000000000000 --- a/deploy/pdserving/readme.md +++ /dev/null @@ -1,120 +0,0 @@ -[English](readme_en.md) | 简体中文 - -PaddleOCR提供2种服务部署方式: -- 基于PaddleHub Serving的部署:代码路径为"`./deploy/hubserving`",使用方法参考[文档](../hubserving/readme.md)。 -- 基于PaddleServing的部署:代码路径为"`./deploy/pdserving`",按照本教程使用。 - -# Paddle Serving 服务部署 -本教程将介绍基于[Paddle Serving](https://github.com/PaddlePaddle/Serving)部署PaddleOCR在线预测服务的详细步骤。 - -## 快速启动服务 - -### 1. 准备环境 -我们先安装Paddle Serving相关组件 -我们推荐用户使用GPU来做Paddle Serving的OCR服务部署 - -**CUDA版本:9.0** - -**CUDNN版本:7.0** - -**操作系统版本:CentOS 6以上** - -**Python版本: 2.7/3.6/3.7** - -**Python操作指南:** -``` -#CPU/GPU版本选择一个 -#GPU版本服务端 -python -m pip install paddle_serving_server_gpu -#CPU版本服务端 -python -m pip install paddle_serving_server -#客户端和App包使用以下链接(CPU,GPU通用) -python -m pip install paddle_serving_app paddle_serving_client -``` - - -### 2. 模型转换 -可以使用`paddle_serving_app`提供的模型,执行下列命令 -``` -python -m paddle_serving_app.package --get_model ocr_rec -tar -xzvf ocr_rec.tar.gz -python -m paddle_serving_app.package --get_model ocr_det -tar -xzvf ocr_det.tar.gz -``` -执行上述命令会下载`db_crnn_mobile`的模型,如果想要下载规模更大的`db_crnn_server`模型,可以在下载预测模型并解压之后。参考[如何从Paddle保存的预测模型转为Paddle Serving格式可部署的模型](https://github.com/PaddlePaddle/Serving/blob/develop/doc/INFERENCE_TO_SERVING_CN.md)。 - -我们以`ch_rec_r34_vd_crnn`模型作为例子,下载链接在: - -``` -wget --no-check-certificate https://paddleocr.bj.bcebos.com/ch_models/ch_rec_r34_vd_crnn_infer.tar -tar xf ch_rec_r34_vd_crnn_infer.tar -``` -因此我们按照Serving模型转换教程,运行下列python文件。 -``` -from paddle_serving_client.io import inference_model_to_serving -inference_model_dir = "ch_rec_r34_vd_crnn" -serving_client_dir = "serving_client_dir" -serving_server_dir = "serving_server_dir" -feed_var_names, fetch_var_names = inference_model_to_serving( - inference_model_dir, serving_client_dir, serving_server_dir, model_filename="model", params_filename="params") -``` -最终会在`serving_client_dir`和`serving_server_dir`生成客户端和服务端的模型配置。 - -### 3. 启动服务 -启动服务可以根据实际需求选择启动`标准版`或者`快速版`,两种方式的对比如下表: - -|版本|特点|适用场景| -|-|-|-| -|标准版|稳定性高,分布式部署|适用于吞吐量大,需要跨机房部署的情况| -|快速版|部署方便,预测速度快|适用于对预测速度要求高,迭代速度快的场景| - -#### 方式1. 启动标准版服务 - -``` -# cpu,gpu启动二选一,以下是cpu启动 -python -m paddle_serving_server.serve --model ocr_det_model --port 9293 -python ocr_web_server.py cpu -# gpu启动 -python -m paddle_serving_server_gpu.serve --model ocr_det_model --port 9293 --gpu_id 0 -python ocr_web_server.py gpu -``` - -#### 方式2. 启动快速版服务 - -``` -# cpu,gpu启动二选一,以下是cpu启动 -python ocr_local_server.py cpu -# gpu启动 -python ocr_local_server.py gpu -``` - -## 发送预测请求 - -``` -python ocr_web_client.py -``` - -## 返回结果格式说明 - -返回结果是json格式 -``` -{u'result': {u'res': [u'\u571f\u5730\u6574\u6cbb\u4e0e\u571f\u58e4\u4fee\u590d\u7814\u7a76\u4e2d\u5fc3', u'\u534e\u5357\u519c\u4e1a\u5927\u5b661\u7d20\u56fe']}} -``` -我们也可以打印结果json串中`res`字段的每一句话 -``` -土地整治与土壤修复研究中心 -华南农业大学1素图 -``` - -## 自定义修改服务逻辑 - -在`ocr_web_server.py`或是`ocr_local_server.py`当中的`preprocess`函数里面做了检测服务和识别服务的前处理,`postprocess`函数里面做了识别的后处理服务,可以在相应的函数中做修改。调用了`paddle_serving_app`库提供的常见CV模型的前处理/后处理库。 - -如果想要单独启动Paddle Serving的检测服务和识别服务,参见下列表格, 执行对应的脚本即可,并且在命令行参数注明用的CPU或是GPU来提供服务。 - -| 模型 | 标准版 | 快速版 | -| ---- | ----------------- | ------------------- | -| 检测 | det_web_server.py | det_local_server.py | -| 识别 | rec_web_server.py | rec_local_server.py | - -更多信息参见[Paddle Serving](https://github.com/PaddlePaddle/Serving) diff --git a/deploy/pdserving/readme_en.md b/deploy/pdserving/readme_en.md deleted file mode 100644 index bfd88e63eb6a7be01145efd9aebe821bc441a901..0000000000000000000000000000000000000000 --- a/deploy/pdserving/readme_en.md +++ /dev/null @@ -1,123 +0,0 @@ -English | [简体中文](readme.md) - -PaddleOCR provides 2 service deployment methods: -- Based on **PaddleHub Serving**: Code path is "`./deploy/hubserving`". Please refer to the [tutorial](../hubserving/readme_en.md) for usage. -- Based on **PaddleServing**: Code path is "`./deploy/pdserving`". Please follow this tutorial. - -# Service deployment based on Paddle Serving - -This tutorial will introduce the detail steps of deploying PaddleOCR online prediction service based on [Paddle Serving](https://github.com/PaddlePaddle/Serving). - -## Quick start service - -### 1. Prepare the environment -Let's first install the relevant components of Paddle Serving. GPU is recommended for service deployment with Paddle Serving. - -**Requirements:** -- **CUDA version: 9.0** -- **CUDNN version: 7.0** -- **Operating system version: >= CentOS 6** -- **Python version: 2.7/3.6/3.7** - -**Installation:** -``` -# install GPU server -python -m pip install paddle_serving_server_gpu - -# or, install CPU server -python -m pip install paddle_serving_server - -# install client and App package (CPU/GPU) -python -m pip install paddle_serving_app paddle_serving_client -``` - -### 2. Model transformation -You can directly use converted model provided by `paddle_serving_app` for convenience. Execute the following command to obtain: -``` -python -m paddle_serving_app.package --get_model ocr_rec -tar -xzvf ocr_rec.tar.gz -python -m paddle_serving_app.package --get_model ocr_det -tar -xzvf ocr_det.tar.gz -``` -Executing the above command will download the `db_crnn_mobile` model, which is in different format with inference model. If you want to use other models for deployment, you can refer to the [tutorial](https://github.com/PaddlePaddle/Serving/blob/develop/doc/INFERENCE_TO_SERVING_CN.md) to convert your inference model to a model which is deployable for Paddle Serving. - -We take `ch_rec_r34_vd_crnn` model as example. Download the inference model by executing the following command: -``` -wget --no-check-certificate https://paddleocr.bj.bcebos.com/ch_models/ch_rec_r34_vd_crnn_infer.tar -tar xf ch_rec_r34_vd_crnn_infer.tar -``` - -Convert the downloaded model by executing the following python script: -``` -from paddle_serving_client.io import inference_model_to_serving -inference_model_dir = "ch_rec_r34_vd_crnn" -serving_client_dir = "serving_client_dir" -serving_server_dir = "serving_server_dir" -feed_var_names, fetch_var_names = inference_model_to_serving( - inference_model_dir, serving_client_dir, serving_server_dir, model_filename="model", params_filename="params") -``` - -Finally, model configuration of client and server will be generated in `serving_client_dir` and `serving_server_dir`. - -### 3. Start service -Start the standard version or the fast version service according to your actual needs. The comparison of the two versions is shown in the table below: - -|version|characteristics|recommended scenarios| -|-|-|-| -|standard version|High stability, suitable for distributed deployment|Large throughput and cross regional deployment| -|fast version|Easy to deploy and fast to predict|Suitable for scenarios which requires high prediction speed and fast iteration speed| - -#### Mode 1. Start the standard mode service - -``` -# start with CPU -python -m paddle_serving_server.serve --model ocr_det_model --port 9293 -python ocr_web_server.py cpu - -# or, with GPU -python -m paddle_serving_server_gpu.serve --model ocr_det_model --port 9293 --gpu_id 0 -python ocr_web_server.py gpu -``` - -#### Mode 2. Start the fast mode service - -``` -# start with CPU -python ocr_local_server.py cpu - -# or, with GPU -python ocr_local_server.py gpu -``` - -## Send prediction requests - -``` -python ocr_web_client.py -``` - -## Returned result format - -The returned result is a JSON string, eg. -``` -{u'result': {u'res': [u'\u571f\u5730\u6574\u6cbb\u4e0e\u571f\u58e4\u4fee\u590d\u7814\u7a76\u4e2d\u5fc3', u'\u534e\u5357\u519c\u4e1a\u5927\u5b661\u7d20\u56fe']}} -``` - -You can also print the readable result in `res`: -``` -土地整治与土壤修复研究中心 -华南农业大学1素图 -``` - -## User defined service module modification - -The pre-processing and post-processing process, can be found in the `preprocess` and `postprocess` function in `ocr_web_server.py` or `ocr_local_server.py`. The pre-processing/post-processing library for common CV models provided by `paddle_serving_app` is called. -You can modify the corresponding code as actual needs. - -If you only want to start the detection service or the recognition service, execute the corresponding script reffering to the following table. Indicate the CPU or GPU is used in the start command parameters. - -| task | standard | fast | -| ---- | ----------------- | ------------------- | -| detection | det_web_server.py | det_local_server.py | -| recognition | rec_web_server.py | rec_local_server.py | - -More info can be found in [Paddle Serving](https://github.com/PaddlePaddle/Serving).