AI实现移除视频复杂背景-Background-Matting(2)

Background Matting V2

引用来源
@article{BGMv2,
title={Real-Time High-Resolution Background Matting},
author={Lin, Shanchuan and Ryabtsev, Andrey and Sengupta, Soumyadip and Curless, Brian and Seitz, Steve and Kemelmacher-Shlizerman, Ira},
journal={arXiv},
pages={arXiv–2012},
year={2020}
}

华盛顿大学Background Matting前一版提供了背景移除的测试程序test_background-matting_image.py。此测试版最大的问题是,移除后的图像会导致清晰度降低。
因为,此版本模型能处理的图像分辨率最大是512x512,从模型获得预测的前景F和遮罩alpha分辨率也是512x512,组合图像由公式:
I′=alphaF+(1−alpha)B′ 确定,输出图像则是由512x512上变换得到,因此导致合成的前景图像损失。
V2版Background MattingV2 对此进行了改进,其细化网络对遮罩alpha和前景预测图像在原始分辨率下产生,输出图像计算公式仍是
I′=alpha
F+(1−alpha)B′
但这在原始分辨率下执行运算,由此保留原图像细节。

经本人验证效果不错,现将结果记录如下:

1 准备工作

将github目录克隆到本地,产生目录BackgroundMattingV2

git clone https://github.com/PeterL1n/BackgroundMattingV2

安装所需的运行环境

pip install -r requirements.txt

其中,因虚拟环境安装tensorflow-gpu 1.14.0,tensorflow v1版和v2版的问题,无法安装其中的tensorboard 2.3.0。这倒是不影响程序运行,可忽略。
将PyTorch模型的权重pytorch_resnet50.pth复制到本地
下载地址:在这里
选择PyTorch, 模型pytorch_resnet50.pth,放入BackgroundMattingV2/model目录。另外也可以下载pytorch_resnet101.pth,抠像效果优于resnet50,运算速度略慢些。

在BackgroundMattingV2目录下,建立输入视频或图像目录:
my_video
my_video/output
将输入视频和输入视频的背景图像,img68.mp4, img68.png,放到my_video目录。
需要混合的背景视频,即用来替换原输入视频中背景的视频文件,img68_bgr.mp4,放到my_video目录。
输出目录:my_video/output 存放抠像程序产生的输出文件。

2 运行python程序

所克隆的目录提供了三个演示程序:
inference_images.py: 执行图像背景移除
inference_video.py: 执行视频背景移除
inference_webcam.py: 使用webcam的交互式背景移除

以视频背景移除 inference_video.py 为例,执行如下

python inference_video.py \
--model-type mattingrefine \
--model-backbone resnet50 \
--model-backbone-scale 0.25 \
--model-refine-mode sampling \
--model-refine-sample-pixels 80000 \
--model-checkpoint model/pytorch_resnet50.pth \
--video-src my_video/img68.mp4 \
--video-bgr my_video/img68.png \
--video-resize 1920 1080 \
--output-dir my_video/output \
--output-type {com,pha,fgr,err,ref} \
--video-target-bgr my_video/img68_bgr.mp4

将命令行 python inference_video.py --model-type mattingrefine …
复制,粘贴到运行终端中,运行。即可获得抠像结果。

其中 --video-src:输入视频
–video-bgr: 输入视频的背景图片
–output-dir:输出视频目录
–video-target-bgr:需合成的背景视频
–output-type:输出视频类型,com-合成的视频, pha-alpha遮罩, fgr-前景视频, err-误差视频, ref-参考遮罩
经本人验证,output-type的输入参数需用{ }包围,参数间用逗号分开,与程序中给出的示例不同。这可能是不同的参数解析器:parser.parse_args()不同所致。

注:作者出新版github,去掉了旧版所需的背景图片,只需输入源视频就可以完成抠图!!!请大家移步观赏。
Robust High-Resolution Video Matting with Temporal Guidance
新版Robust Video Matting不需要背景图片,看上去不错。对需抠像的视频video_demo.mp4,可如下执行inference.py:

python inference.py \
--variant resnet50  \
--checkpoint  rvm_resnet50.pth \
--device  cuda  \
--input-source  input/video_demo.mp4 \
--output-type  video \
--output-composition output/composition.mp4 \
--output-alpha output/alpha.mp4  \
--output-foreground output/foreground.mp4 \
--output-video-mbps  4 \
--seq-chunk  1
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 44
    评论
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值