mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-06 00:57:33 +08:00
[Serving] Add a simple Python serving (#962)
* init simple serving * simple serving is working * ppyoloe demo * Update README_CN.md * update readme * complete vision result to json
This commit is contained in:
@@ -0,0 +1 @@
|
||||
README_CN.md
|
@@ -0,0 +1,43 @@
|
||||
简体中文 | [English](README_EN.md)
|
||||
|
||||
# PaddleDetection Python轻量服务化部署示例
|
||||
|
||||
在部署前,需确认以下两个步骤
|
||||
|
||||
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
- 2. FastDeploy Python whl包安装,参考[FastDeploy Python安装](../../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
|
||||
|
||||
服务端:
|
||||
```bash
|
||||
# 下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/detection/paddledetection/python/serving
|
||||
|
||||
# 下载PPYOLOE模型文件(如果不下载,代码里会自动从hub下载)
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
|
||||
tar xvf ppyoloe_crn_l_300e_coco.tgz
|
||||
|
||||
# 安装uvicorn
|
||||
pip install uvicorn
|
||||
|
||||
# 启动服务,可选择是否使用GPU和TensorRT,可根据uvicorn --help配置IP、端口号等
|
||||
# CPU
|
||||
MODEL_DIR=ppyoloe_crn_l_300e_coco DEVICE=cpu uvicorn server:app
|
||||
# GPU
|
||||
MODEL_DIR=ppyoloe_crn_l_300e_coco DEVICE=gpu uvicorn server:app
|
||||
# GPU上使用TensorRT (注意:TensorRT推理第一次运行,有序列化模型的操作,有一定耗时,需要耐心等待)
|
||||
MODEL_DIR=ppyoloe_crn_l_300e_coco DEVICE=gpu USE_TRT=true uvicorn server:app
|
||||
```
|
||||
|
||||
客户端:
|
||||
```bash
|
||||
# 下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/detection/paddledetection/python/serving
|
||||
|
||||
# 下载测试图片
|
||||
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
|
||||
|
||||
# 请求服务,获取推理结果(如有必要,请修改脚本中的IP和端口号)
|
||||
python client.py
|
||||
```
|
@@ -0,0 +1,44 @@
|
||||
English | [简体中文](README_CN.md)
|
||||
|
||||
# PaddleDetection Python Simple Serving Demo
|
||||
|
||||
|
||||
## Environment
|
||||
|
||||
- 1. Prepare environment and install FastDeploy Python whl, refer to [download_prebuilt_libraries](../../../../../../docs/en/build_and_install/download_prebuilt_libraries.md)
|
||||
|
||||
Server:
|
||||
```bash
|
||||
# Download demo code
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/detection/paddledetection/python/serving
|
||||
|
||||
# Download PPYOLOE model
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
|
||||
tar xvf ppyoloe_crn_l_300e_coco.tgz
|
||||
|
||||
# Install uvicorn
|
||||
pip install uvicorn
|
||||
|
||||
# Launch server, it's configurable to use GPU and TensorRT,
|
||||
# and run 'uvicorn --help' to check how to specify IP and port, etc.
|
||||
# CPU
|
||||
MODEL_DIR=ppyoloe_crn_l_300e_coco DEVICE=cpu uvicorn server:app
|
||||
# GPU
|
||||
MODEL_DIR=ppyoloe_crn_l_300e_coco DEVICE=gpu uvicorn server:app
|
||||
# GPU and TensorRT
|
||||
MODEL_DIR=ppyoloe_crn_l_300e_coco DEVICE=gpu USE_TRT=true uvicorn server:app
|
||||
```
|
||||
|
||||
Client:
|
||||
```bash
|
||||
# Download demo code
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/detection/paddledetection/python/serving
|
||||
|
||||
# Download test image
|
||||
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
|
||||
|
||||
# Send request and get inference result (Please adapt the IP and port if necessary)
|
||||
python client.py
|
||||
```
|
@@ -0,0 +1,28 @@
|
||||
import requests
|
||||
import json
|
||||
import cv2
|
||||
import base64
|
||||
import fastdeploy as fd
|
||||
|
||||
if __name__ == '__main__':
|
||||
url = "http://127.0.0.1:8000/fd/ppyoloe"
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
im = cv2.imread("000000014439.jpg")
|
||||
data = {
|
||||
"data": {
|
||||
"image": fd.serving.utils.cv2_to_base64(im)
|
||||
},
|
||||
"parameters": {}
|
||||
}
|
||||
|
||||
resp = requests.post(url=url, headers=headers, data=json.dumps(data))
|
||||
if resp.status_code == 200:
|
||||
r_json = json.loads(resp.json()["result"])
|
||||
det_result = fd.vision.utils.json_to_detection(r_json)
|
||||
vis_im = fd.vision.vis_detection(im, det_result, score_threshold=0.5)
|
||||
cv2.imwrite("visualized_result.jpg", vis_im)
|
||||
print("Visualized result save in ./visualized_result.jpg")
|
||||
else:
|
||||
print("Error code:", resp.status_code)
|
||||
print(resp.text)
|
@@ -0,0 +1,40 @@
|
||||
import fastdeploy as fd
|
||||
import os
|
||||
import logging
|
||||
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
|
||||
# Get arguments from envrionment variables
|
||||
model_dir = os.environ.get('MODEL_DIR')
|
||||
device = os.environ.get('DEVICE', 'cpu')
|
||||
use_trt = os.environ.get('USE_TRT', False)
|
||||
|
||||
# Prepare model, download from hub or use local dir
|
||||
if model_dir is None:
|
||||
model_dir = fd.download_model(name='ppyoloe_crn_l_300e_coco')
|
||||
|
||||
model_file = os.path.join(model_dir, "model.pdmodel")
|
||||
params_file = os.path.join(model_dir, "model.pdiparams")
|
||||
config_file = os.path.join(model_dir, "infer_cfg.yml")
|
||||
|
||||
# Setup runtime option to select hardware, backend, etc.
|
||||
option = fd.RuntimeOption()
|
||||
if device.lower() == 'gpu':
|
||||
option.use_gpu()
|
||||
if use_trt:
|
||||
option.use_trt_backend()
|
||||
option.set_trt_cache_file('ppyoloe.trt')
|
||||
|
||||
# Create model instance
|
||||
model_instance = fd.vision.detection.PPYOLOE(
|
||||
model_file=model_file,
|
||||
params_file=params_file,
|
||||
config_file=config_file,
|
||||
runtime_option=option)
|
||||
|
||||
# Create server, setup REST API
|
||||
app = fd.serving.SimpleServer()
|
||||
app.register(
|
||||
task_name="fd/ppyoloe",
|
||||
model_handler=fd.serving.handler.VisionModelHandler,
|
||||
predictor=model_instance)
|
Reference in New Issue
Block a user