一、基于PaddleHub一键部署ernie_vilg文图生成等Web应用服务
1.基本介绍
之前写过基于PaddleHub的Web项目,然而晃晃悠悠2年过去了,许多hub升级了,接口改了,经常被问到报错了怎么办,这下和ernie_vilg部署一同作了。基本上只要会html基础、js基础、python基础就可以动手搭建了,属于有手就能干的入门项目。
2.基本功能
- 抠图
- 换背景证件照
- 美颜
- OCR
- ernie_vilg文图生成
3.用到的几个包
- paddlepaddle、paddlehub包
- flask_bootstrap,提供bootstrap支持
- blueprint,通过Blueprint来组织URL以及处理请求
二、hub serving服务启动
1.json文件编写
主要有以下部分:
- 模型名称
- 模型版本
- 模型参数(常见的batch size等,但不一定都是)
- 端口
- 多进程
hub serving start --config config.json
{ "modules_info": { "face_landmark_localization": { "init_args": { "version": "1.0.3" }, "predict_args": { "batch_size": 1 } }, "deeplabv3p_xception65_humanseg": { "init_args": { "version": "1.1.2" }, "predict_args": { "batch_size": 1 } }, "ernie_vilg": { "init_args": { "version": "1.0.0" }, "predict_args": { } } }, "port": 8866, "use_multiprocess": false, "workers": 2 }
2.启动服务
hub serving start --config config.json
(p2) PS C:\Users\livingbody\Desktop\ernie_vilg\flaskautocut> hub serving start --config config.json [2022-08-31 09:22:38,624] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object [2022-08-31 09:22:38,683] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object [2022-08-31 09:22:38,894] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object * Serving Flask app 'paddlehub.serving.app_compat' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off INFO 2022-08-31 09:23:02,049 _internal.py:224] * Running on all addresses (0.0.0.0) WARNING: This is a development server. Do not use it in a production deployment. * Running on http://127.0.0.1:8866 * Running on http://192.168.8.8:8866 (Press CTRL+C to quit)
如上图,在8866端口启动成功
3.其他
目前,hub会自动判断并选择显卡,所以无需特别设置。
三、页面编写
1.提交页面
主要完成2项输入
- 图片描述
- 风格选择
<label class="col-sm-2 control-label">输入描述</label> <div class="col-sm-3"> <input type="text" class="form-control" name="describe" id="describe" placeholder="Text" value="悠然见南山"> </div> <label class="col-sm-2 control-label">背景色选择</label> <div class="col-sm-2"> <select class="form-control" name="selected_color"> <option value="油画">油画</option> <option value="水彩">水彩</option> <option value="粉笔画">粉笔画</option> <option value="卡通">卡通</option> <option value="儿童画">儿童画</option> <option value="蜡笔画">蜡笔画</option> </select>
2.展示页面
主要完成生成展示,具体是对生成的图片列表进行遍历展示。
{% if file_list %} {% for filename in file_list %} <a href='{{ filename }}' download="mypic.jpg"> <img class="col-md-6 img-responsive img-thumbnail" src="{{ filename }}" alt="你的图片被外星人劫持了~~" width="50%"/> </a> {% endfor %} {% else %} <h1>file_list is none</h1> {% endif %}
四、后端调用
1.接收页面form提交
- 收取页面提交的图片描述、风格选择参数
- 调用ernie_vilg_gen函数,生成图片
- 将文件列表绑定到展示页面
# 接收页面传递参数 @index_ernie_vilg.route('/ernie_vilg', methods=['POST', 'GET']) # 添加路由 def ernie_vilg(): if request.method == 'POST': try: ernie_vilg_text = request.form.get('describe') selected_style = request.form.get('selected_style') # print(ernie_vilg_text) file_list = ernie_vilg_gen(ernie_vilg_text, selected_style) # file_list = [f'static/images/target/{fff}' for fff in file_list] for filename in file_list: print(filename) return render_template('ernie_vilg_ok.html', file_list=file_list) except Exception as e: print(e) # return render_template('404.html') return render_template('ernie_vilg.html')
2.serving调用
调用serving的api,生成图片
# 生成 def ernie_vilg_gen(ernie_vilg_text, selected_style): # 发送HTTP请求 data = {'text_prompts': ernie_vilg_text, 'style': "油画"} headers = {"Content-type": "application/json"} url = "http://127.0.0.1:8866/predict/ernie_vilg" r = requests.post(url=url, headers=headers, data=json.dumps(data)) # print(r.json()) # 获取返回结果 file_list = [] for i, result in enumerate(r.json()["results"]): image = Image.open(BytesIO(base64.b64decode(result))) image.save('static/images/target/result_{}.png'.format(i)) file_list.append('static/images/target/result_{}.png'.format(i)) print('result_{}.png'.format(i)) return file_list
五、启动项目
- flask bootstrap支持
pip install flask_bootstrap
- flask blueprint
pip install flask-blueprint
- run下
set CUDA_VISIBLE_DEVICES=0 hub serving start --config config.json python app.py
- 代码 代码上传至项目根目录了【ernievilg_web.zip】,可以自行下载,有问题可以浏览一起解决