Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleX
提交
41183ea7
P
PaddleX
项目概览
PaddlePaddle
/
PaddleX
通知
138
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
43
列表
看板
标记
里程碑
合并请求
5
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleX
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
43
Issue
43
列表
看板
标记
里程碑
合并请求
5
合并请求
5
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
41183ea7
编写于
6月 23, 2020
作者:
F
FlyingQianMM
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix prediction in bg_replace
上级
cd8e9ced
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
64 addition
and
40 deletion
+64
-40
examples/human_segmentation/bg_replace.py
examples/human_segmentation/bg_replace.py
+61
-37
examples/human_segmentation/video_infer.py
examples/human_segmentation/video_infer.py
+3
-3
未找到文件。
examples/human_segmentation/bg_replace.py
浏览文件 @
41183ea7
...
...
@@ -19,7 +19,7 @@ import os.path as osp
import
cv2
import
numpy
as
np
from
utils.humanseg_
postprocess
import
postprocess
,
threshold_mask
from
postprocess
import
postprocess
,
threshold_mask
import
paddlex
as
pdx
import
paddlex.utils.logging
as
logging
from
paddlex.seg
import
transforms
...
...
@@ -74,44 +74,29 @@ def parse_args():
return
parser
.
parse_args
()
def
predict
(
img
,
model
,
test_transforms
):
model
.
arrange_transforms
(
transforms
=
test_transforms
,
mode
=
'test'
)
img
,
im_info
=
test_transforms
(
img
.
astype
(
'float32'
))
img
=
np
.
expand_dims
(
img
,
axis
=
0
)
result
=
model
.
exe
.
run
(
model
.
test_prog
,
feed
=
{
'image'
:
img
},
fetch_list
=
list
(
model
.
test_outputs
.
values
()))
score_map
=
result
[
1
]
score_map
=
np
.
squeeze
(
score_map
,
axis
=
0
)
score_map
=
np
.
transpose
(
score_map
,
(
1
,
2
,
0
))
return
score_map
,
im_info
def
bg_replace
(
label_map
,
img
,
bg
):
h
,
w
,
_
=
img
.
shape
bg
=
cv2
.
resize
(
bg
,
(
w
,
h
))
label_map
=
np
.
repeat
(
label_map
[:,
:,
np
.
newaxis
],
3
,
axis
=
2
)
comb
=
(
label_map
*
img
+
(
1
-
label_map
)
*
bg
).
astype
(
np
.
uint8
)
return
comb
def
recover
(
img
,
im_info
):
for
info
in
im_info
[::
-
1
]:
if
info
[
0
]
==
'resize'
:
w
,
h
=
info
[
1
][
1
],
info
[
1
][
0
]
img
=
cv2
.
resize
(
img
,
(
w
,
h
),
cv2
.
INTER_LINEAR
)
elif
info
[
0
]
==
'padding'
:
w
,
h
=
info
[
1
][
0
],
info
[
1
][
0
]
img
=
img
[
0
:
h
,
0
:
w
,
:]
if
im_info
[
0
]
==
'resize'
:
w
,
h
=
im_info
[
1
][
1
],
im_info
[
1
][
0
]
img
=
cv2
.
resize
(
img
,
(
w
,
h
),
cv2
.
INTER_LINEAR
)
elif
im_info
[
0
]
==
'padding'
:
w
,
h
=
im_info
[
1
][
0
],
im_info
[
1
][
0
]
img
=
img
[
0
:
h
,
0
:
w
,
:]
return
img
def
bg_replace
(
score_map
,
img
,
bg
):
h
,
w
,
_
=
img
.
shape
bg
=
cv2
.
resize
(
bg
,
(
w
,
h
))
score_map
=
np
.
repeat
(
score_map
[:,
:,
np
.
newaxis
],
3
,
axis
=
2
)
comb
=
(
score_map
*
img
+
(
1
-
score_map
)
*
bg
).
astype
(
np
.
uint8
)
return
comb
def
infer
(
args
):
resize_h
=
args
.
image_shape
[
1
]
resize_w
=
args
.
image_shape
[
0
]
test_transforms
=
transforms
.
Compose
(
[
transforms
.
Resize
((
resize_w
,
resize_h
)),
transforms
.
Normalize
()])
test_transforms
=
transforms
.
Compose
([
transforms
.
Normalize
()])
model
=
pdx
.
load_model
(
args
.
model_dir
)
if
not
osp
.
exists
(
args
.
save_dir
):
...
...
@@ -130,14 +115,27 @@ def infer(args):
raise
Exception
(
'The --background_image_path is not existed: {}'
.
format
(
args
.
background_image_path
))
img
=
cv2
.
imread
(
args
.
image_path
)
score_map
,
im_info
=
predict
(
img
,
model
,
test_transforms
)
score_map
=
score_map
[:,
:,
1
]
score_map
=
recover
(
score_map
,
im_info
)
im_shape
=
img
.
shape
im_scale_x
=
float
(
resize_w
)
/
float
(
im_shape
[
1
])
im_scale_y
=
float
(
resize_h
)
/
float
(
im_shape
[
0
])
im
=
cv2
.
resize
(
img
,
None
,
None
,
fx
=
im_scale_x
,
fy
=
im_scale_y
,
interpolation
=
cv2
.
INTER_LINEAR
)
image
=
im
.
astype
(
'float32'
)
im_info
=
(
'resize'
,
im_shape
[
0
:
2
])
pred
=
model
.
predict
(
image
,
test_transforms
)
label_map
=
pred
[
'label_map'
]
label_map
=
recover
(
label_map
,
im_info
)
bg
=
cv2
.
imread
(
args
.
background_image_path
)
save_name
=
osp
.
basename
(
args
.
image_path
)
save_path
=
osp
.
join
(
args
.
save_dir
,
save_name
)
result
=
bg_replace
(
score
_map
,
img
,
bg
)
result
=
bg_replace
(
label
_map
,
img
,
bg
)
cv2
.
imwrite
(
save_path
,
result
)
# 视频背景替换,如果提供背景视频则以背景视频作为背景,否则采用提供的背景图片
...
...
@@ -192,8 +190,21 @@ def infer(args):
while
cap_video
.
isOpened
():
ret
,
frame
=
cap_video
.
read
()
if
ret
:
score_map
,
im_info
=
predict
(
frame
,
model
,
test_transforms
)
cur_gray
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
im_shape
=
frame
.
shape
im_scale_x
=
float
(
resize_w
)
/
float
(
im_shape
[
1
])
im_scale_y
=
float
(
resize_h
)
/
float
(
im_shape
[
0
])
im
=
cv2
.
resize
(
frame
,
None
,
None
,
fx
=
im_scale_x
,
fy
=
im_scale_y
,
interpolation
=
cv2
.
INTER_LINEAR
)
image
=
im
.
astype
(
'float32'
)
im_info
=
(
'resize'
,
im_shape
[
0
:
2
])
pred
=
model
.
predict
(
image
,
test_transforms
)
score_map
=
pred
[
'score_map'
]
cur_gray
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
cur_gray
=
cv2
.
resize
(
cur_gray
,
(
resize_w
,
resize_h
))
score_map
=
255
*
score_map
[:,
:,
1
]
optflow_map
=
postprocess
(
cur_gray
,
score_map
,
prev_gray
,
prev_cfd
,
\
...
...
@@ -248,8 +259,21 @@ def infer(args):
while
cap_video
.
isOpened
():
ret
,
frame
=
cap_video
.
read
()
if
ret
:
score_map
,
im_info
=
predict
(
frame
,
model
,
test_transforms
)
cur_gray
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
im_shape
=
frame
.
shape
im_scale_x
=
float
(
resize_w
)
/
float
(
im_shape
[
1
])
im_scale_y
=
float
(
resize_h
)
/
float
(
im_shape
[
0
])
im
=
cv2
.
resize
(
frame
,
None
,
None
,
fx
=
im_scale_x
,
fy
=
im_scale_y
,
interpolation
=
cv2
.
INTER_LINEAR
)
image
=
im
.
astype
(
'float32'
)
im_info
=
(
'resize'
,
im_shape
[
0
:
2
])
pred
=
model
.
predict
(
image
,
test_transforms
)
score_map
=
pred
[
'score_map'
]
cur_gray
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
cur_gray
=
cv2
.
resize
(
cur_gray
,
(
resize_w
,
resize_h
))
score_map
=
255
*
score_map
[:,
:,
1
]
optflow_map
=
postprocess
(
cur_gray
,
score_map
,
prev_gray
,
prev_cfd
,
\
...
...
examples/human_segmentation/video_infer.py
浏览文件 @
41183ea7
...
...
@@ -70,8 +70,8 @@ def video_infer(args):
resize_h
=
args
.
image_shape
[
1
]
resize_w
=
args
.
image_shape
[
0
]
test_transforms
=
transforms
.
Compose
([
transforms
.
Normalize
()])
model
=
pdx
.
load_model
(
args
.
model_dir
)
test_transforms
=
transforms
.
Compose
([
transforms
.
Normalize
()])
if
not
args
.
video_path
:
cap
=
cv2
.
VideoCapture
(
0
)
else
:
...
...
@@ -115,7 +115,7 @@ def video_infer(args):
interpolation
=
cv2
.
INTER_LINEAR
)
image
=
im
.
astype
(
'float32'
)
im_info
=
(
'resize'
,
im_shape
[
0
:
2
])
pred
=
model
.
predict
(
image
)
pred
=
model
.
predict
(
image
,
test_transforms
)
score_map
=
pred
[
'score_map'
]
cur_gray
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
score_map
=
255
*
score_map
[:,
:,
1
]
...
...
@@ -155,7 +155,7 @@ def video_infer(args):
interpolation
=
cv2
.
INTER_LINEAR
)
image
=
im
.
astype
(
'float32'
)
im_info
=
(
'resize'
,
im_shape
[
0
:
2
])
pred
=
model
.
predict
(
image
)
pred
=
model
.
predict
(
image
,
test_transforms
)
score_map
=
pred
[
'score_map'
]
cur_gray
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
cur_gray
=
cv2
.
resize
(
cur_gray
,
(
resize_w
,
resize_h
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录