Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
dd00d540
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
282
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
dd00d540
编写于
12月 07, 2021
作者:
C
chenjian
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add face_parse module
上级
b3f19bfc
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
327 addition
and
0 deletion
+327
-0
modules/image/Image_gan/style_transfer/face_parse/README.md
modules/image/Image_gan/style_transfer/face_parse/README.md
+133
-0
modules/image/Image_gan/style_transfer/face_parse/model.py
modules/image/Image_gan/style_transfer/face_parse/model.py
+51
-0
modules/image/Image_gan/style_transfer/face_parse/module.py
modules/image/Image_gan/style_transfer/face_parse/module.py
+131
-0
modules/image/Image_gan/style_transfer/face_parse/requirements.txt
...mage/Image_gan/style_transfer/face_parse/requirements.txt
+2
-0
modules/image/Image_gan/style_transfer/face_parse/util.py
modules/image/Image_gan/style_transfer/face_parse/util.py
+10
-0
未找到文件。
modules/image/Image_gan/style_transfer/face_parse/README.md
0 → 100644
浏览文件 @
dd00d540
# face_parse
|模型名称|face_parse|
| :--- | :---: |
|类别|图像 - 人脸解析|
|网络|BiSeNet|
|数据集|-|
|是否支持Fine-tuning|否|
|模型大小|77MB|
|最新更新日期|2021-12-07|
|数据指标|-|
## 一、模型基本信息
-
### 应用效果展示
-
样例结果示例:
<p
align=
"center"
>
<img
src=
"https://user-images.githubusercontent.com/22424850/144999882-cd880e47-bce1-467e-85d9-52bbcd5bb2ce.jpg"
width =
"40%"
hspace=
'10'
/>
<br
/>
输入图像
<br
/>
<img
src=
"https://user-images.githubusercontent.com/22424850/144999926-63921ef6-4196-445d-8524-5232fd15a325.png"
width =
"40%"
hspace=
'10'
/>
<br
/>
输出图像
<br
/>
</p>
-
### 模型介绍
-
人脸解析是语义图像分割的一种特殊情况,人脸解析是计算人脸图像中不同语义成分(如头发、嘴唇、鼻子、眼睛等)的像素级标签映射。给定一个输入的人脸图像,人脸解析将为每个语义成分分配一个像素级标签。
## 二、安装
-
### 1、环境依赖
-
ppgan
-
dlib
-
### 2、安装
-
```shell
$ hub install face_parse
```
-
如您安装时遇到问题,可参考:
[
零基础windows安装
](
../../../../docs/docs_ch/get_start/windows_quickstart.md
)
|
[
零基础Linux安装
](
../../../../docs/docs_ch/get_start/linux_quickstart.md
)
|
[
零基础MacOS安装
](
../../../../docs/docs_ch/get_start/mac_quickstart.md
)
## 三、模型API预测
-
### 1、命令行预测
-
```shell
# Read from a file
$ hub run face_parse --input_path "/PATH/TO/IMAGE"
```
-
通过命令行方式实现人脸解析模型的调用,更多请见
[
PaddleHub命令行指令
](
../../../../docs/docs_ch/tutorial/cmd_usage.rst
)
-
### 2、预测代码示例
-
```python
import paddlehub as hub
module = hub.Module(name="face_parse")
input_path = ["/PATH/TO/IMAGE"]
# Read from a file
module.style_transfer(paths=input_path, output_dir='./transfer_result/', use_gpu=True)
```
-
### 3、API
-
```python
style_transfer(images=None, paths=None, output_dir='./transfer_result/', use_gpu=False, visualization=True):
```
-
人脸解析转换API。
- **参数**
- images (list\[numpy.ndarray\]): 图片数据,ndarray.shape 为 \[H, W, C\];<br/>
- paths (list\[str\]): 图片的路径;<br/>
- output\_dir (str): 结果保存的路径; <br/>
- use\_gpu (bool): 是否使用 GPU;<br/>
- visualization(bool): 是否保存结果到本地文件夹
## 四、服务部署
-
PaddleHub Serving可以部署一个在线人脸解析转换服务。
-
### 第一步:启动PaddleHub Serving
-
运行启动命令:
-
```shell
$ hub serving start -m face_parse
```
-
这样就完成了一个人脸解析转换的在线服务API的部署,默认端口号为8866。
-
**NOTE:**
如使用GPU预测,则需要在启动服务之前,请设置CUDA
\_
VISIBLE
\_
DEVICES环境变量,否则不用设置。
-
### 第二步:发送预测请求
-
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
-
```python
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/face_parse"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
# 打印预测结果
print(r.json()["results"])
## 五、更新历史
* 1.0.0
初始发布
- ```
shell
$ hub install face_parse==1.0.0
```
modules/image/Image_gan/style_transfer/face_parse/model.py
0 → 100644
浏览文件 @
dd00d540
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# 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.
import
os
import
sys
import
argparse
from
PIL
import
Image
import
numpy
as
np
import
cv2
import
ppgan.faceutils
as
futils
from
ppgan.utils.preprocess
import
*
from
ppgan.utils.visual
import
mask2image
class
FaceParsePredictor
:
def
__init__
(
self
):
self
.
input_size
=
(
512
,
512
)
self
.
up_ratio
=
0.6
/
0.85
self
.
down_ratio
=
0.2
/
0.85
self
.
width_ratio
=
0.2
/
0.85
self
.
face_parser
=
futils
.
mask
.
FaceParser
()
def
run
(
self
,
image
):
image
=
Image
.
fromarray
(
image
)
face
=
futils
.
dlib
.
detect
(
image
)
if
not
face
:
return
face_on_image
=
face
[
0
]
image
,
face
,
crop_face
=
futils
.
dlib
.
crop
(
image
,
face_on_image
,
self
.
up_ratio
,
self
.
down_ratio
,
self
.
width_ratio
)
np_image
=
np
.
array
(
image
)
mask
=
self
.
face_parser
.
parse
(
np
.
float32
(
cv2
.
resize
(
np_image
,
self
.
input_size
)))
mask
=
cv2
.
resize
(
mask
.
numpy
(),
(
256
,
256
))
mask
=
mask
.
astype
(
np
.
uint8
)
mask
=
mask2image
(
mask
)
return
mask
modules/image/Image_gan/style_transfer/face_parse/module.py
0 → 100644
浏览文件 @
dd00d540
# Copyright (c) 2021 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.
import
os
import
argparse
import
copy
import
paddle
import
paddlehub
as
hub
from
paddlehub.module.module
import
moduleinfo
,
runnable
,
serving
import
numpy
as
np
import
cv2
from
skimage.io
import
imread
from
skimage.transform
import
rescale
,
resize
from
.model
import
FaceParsePredictor
from
.util
import
base64_to_cv2
@
moduleinfo
(
name
=
"face_parse"
,
type
=
"CV/style_transfer"
,
author
=
"paddlepaddle"
,
author_email
=
""
,
summary
=
""
,
version
=
"1.0.0"
)
class
Face_parse
:
def
__init__
(
self
):
self
.
pretrained_model
=
os
.
path
.
join
(
self
.
directory
,
"bisenet.pdparams"
)
self
.
network
=
FaceParsePredictor
()
def
style_transfer
(
self
,
images
=
None
,
paths
=
None
,
output_dir
=
'./transfer_result/'
,
use_gpu
=
False
,
visualization
=
True
):
'''
images (list[numpy.ndarray]): data of images, shape of each is [H, W, C], color space must be BGR(read by cv2).
paths (list[str]): paths to images
output_dir: the dir to save the results
use_gpu: if True, use gpu to perform the computation, otherwise cpu.
visualization: if True, save results in output_dir.
'''
results
=
[]
paddle
.
disable_static
()
place
=
'gpu:0'
if
use_gpu
else
'cpu'
place
=
paddle
.
set_device
(
place
)
if
images
==
None
and
paths
==
None
:
print
(
'No image provided. Please input an image or a image path.'
)
return
if
images
!=
None
:
for
image
in
images
:
image
=
image
[:,
:,
::
-
1
]
out
=
self
.
network
.
run
(
image
)
results
.
append
(
out
)
if
paths
!=
None
:
for
path
in
paths
:
image
=
cv2
.
imread
(
path
)[:,
:,
::
-
1
]
out
=
self
.
network
.
run
(
image
)
results
.
append
(
out
)
if
visualization
==
True
:
if
not
os
.
path
.
exists
(
output_dir
):
os
.
makedirs
(
output_dir
,
exist_ok
=
True
)
for
i
,
out
in
enumerate
(
results
):
if
out
is
not
None
:
cv2
.
imwrite
(
os
.
path
.
join
(
output_dir
,
'output_{}.png'
.
format
(
i
)),
out
[:,
:,
::
-
1
])
return
results
@
runnable
def
run_cmd
(
self
,
argvs
:
list
):
"""
Run as a command.
"""
self
.
parser
=
argparse
.
ArgumentParser
(
description
=
"Run the {} module."
.
format
(
self
.
name
),
prog
=
'hub run {}'
.
format
(
self
.
name
),
usage
=
'%(prog)s'
,
add_help
=
True
)
self
.
arg_input_group
=
self
.
parser
.
add_argument_group
(
title
=
"Input options"
,
description
=
"Input data. Required"
)
self
.
arg_config_group
=
self
.
parser
.
add_argument_group
(
title
=
"Config options"
,
description
=
"Run configuration for controlling module behavior, not required."
)
self
.
add_module_config_arg
()
self
.
add_module_input_arg
()
self
.
args
=
self
.
parser
.
parse_args
(
argvs
)
results
=
self
.
style_transfer
(
paths
=
[
self
.
args
.
input_path
],
output_dir
=
self
.
args
.
output_dir
,
use_gpu
=
self
.
args
.
use_gpu
,
visualization
=
self
.
args
.
visualization
)
return
results
@
serving
def
serving_method
(
self
,
images
,
**
kwargs
):
"""
Run as a service.
"""
images_decode
=
[
base64_to_cv2
(
image
)
for
image
in
images
]
results
=
self
.
style_transfer
(
images
=
images_decode
,
**
kwargs
)
tolist
=
[
result
.
tolist
()
for
result
in
results
]
return
tolist
def
add_module_config_arg
(
self
):
"""
Add the command config options.
"""
self
.
arg_config_group
.
add_argument
(
'--use_gpu'
,
action
=
'store_true'
,
help
=
"use GPU or not"
)
self
.
arg_config_group
.
add_argument
(
'--output_dir'
,
type
=
str
,
default
=
'transfer_result'
,
help
=
'output directory for saving result.'
)
self
.
arg_config_group
.
add_argument
(
'--visualization'
,
type
=
bool
,
default
=
False
,
help
=
'save results or not.'
)
def
add_module_input_arg
(
self
):
"""
Add the command input options.
"""
self
.
arg_input_group
.
add_argument
(
'--input_path'
,
type
=
str
,
help
=
"path to input image."
)
modules/image/Image_gan/style_transfer/face_parse/requirements.txt
0 → 100644
浏览文件 @
dd00d540
ppgan
dlib
modules/image/Image_gan/style_transfer/face_parse/util.py
0 → 100644
浏览文件 @
dd00d540
import
base64
import
cv2
import
numpy
as
np
def
base64_to_cv2
(
b64str
):
data
=
base64
.
b64decode
(
b64str
.
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
data
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
return
data
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录