本文主要是介绍飞桨课程创意项目:基于Paddlehub实现人像背景虚化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
利用deeplabv3p_xception65_humanseg与PIL.ImageFilter模块实现人像背景虚化
感兴趣的同志可以直接点AI Studio项目地址,可直接运行 :[基于paddlehub实现人像背景虚化]
设计思路
# 首先安装 paddlehub
!pip install paddlehub --upgrade
!pip install paddlepaddle --upgrade
# 导入包
import cv2
import paddlehub as hub
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import math
from PIL import Image, ImageFilter
第一步—获取人像
module = hub.Module(name="deeplabv3p_xception65_humanseg")
picture = 'Taylor2.jpg'
results = module.segmentation(images=[cv2.imread(picture)], visualization=True, output_dir = './humanseg_output')
# 抠图路径 留作后用
fore_image = results[0]['save_path']
plt.figure(figsize=(10,10))
# 原图
f = plt.subplot(121)
show_picture = mpimg.imread(picture)
plt.imshow(show_picture)
plt.axis('off')
# 抠图
show_fore_image = mpimg.imread(results[0]['save_path'])
f = plt.subplot(122)
plt.imshow(show_fore_image)
plt.axis('off')
plt.show
有关paddlehub中的deeplabv3p_xception65_humanseg请点这里
第二步—虚化原始图片
# picture = 'Taylor2.jpg'
back_image = 'blured'+picture # 虚化图片名字
img = Image.open(picture)
blured_img = img.filter(ImageFilter.BLUR) # 使用标准模糊
blured_img.save(back_image, quality=100)
show_blured_img = mpimg.imread(back_image)
plt.figure(figsize=(10,10))
# 原图
f = plt.subplot(121)
show_picture = mpimg.imread(picture)
plt.imshow(show_picture)
plt.axis('off')
# 虚化后的图
f = plt.subplot(122)
plt.imshow(show_blured_img)
plt.axis('off')
plt.show
第三步—加权合成两张图并展示结果
# 定义加权合成函数
def images_fusion(fore_image, back_image, save_name):"""将抠出的人物图像换背景fore_image: 前景图片,抠出的人物图片back_image: 背景图片"""# 读入图片back_image = Image.open(back_image).convert('RGB')# fore_image = Image.open(fore_image).resize(back_image.size) 统一尺寸, 本项目不需要resizefore_image = Image.open(fore_image)# 图片加权合成scope_map = np.array(fore_image)[:, :, -1] / 255scope_map = scope_map[:, :, np.newaxis]scope_map = np.repeat(scope_map, repeats=3, axis=2)res_image = np.multiply(scope_map, np.array(fore_image)[:, :, :3]) + np.multiply((1 - scope_map),np.array(back_image))# 保存图片res_image = Image.fromarray(np.uint8(res_image))res_image.save(save_name)
# 展示结果
images_fusion(fore_image=fore_image, back_image=back_image, save_name='result.jpg')
img = mpimg.imread("result.jpg")# 原图
plt.figure(figsize=(10,10))
f = plt.subplot(121)
show_picture = mpimg.imread(picture)
plt.imshow(show_picture)
plt.axis('off')
f = plt.subplot(122)
# 合成后的背景虚化图
plt.imshow(img)
plt.axis('off')
plt.show()
封装成函数
# 获取分割的人像图片
def get_fore_image(picture):module = hub.Module(name="deeplabv3p_xception65_humanseg")results = module.segmentation(images=[cv2.imread(picture)], visualization=True, output_dir = './humanseg_output')fore_image = results[0]['save_path']return fore_image# 获取虚化图片
def get_back_image(picture):back_image = 'blured'+picture # 虚化图片名字img = Image.open(picture)blured_img = img.filter(ImageFilter.BLUR) # 使用标准模糊blured_img.save(back_image, quality=100)return back_image# 融合虚化图片和人像图片
def images_fusion_final(fore_image, back_image, picture):"""将抠出的人物图像换背景fore_image: 前景图片,抠出的人物图片back_image: 背景图片"""# 读入图片back_image = Image.open(back_image).convert('RGB')# fore_image = Image.open(fore_image).resize(back_image.size) 统一尺寸, 本项目不需要resizefore_image = Image.open(fore_image)# 图片加权合成scope_map = np.array(fore_image)[:, :, -1] / 255scope_map = scope_map[:, :, np.newaxis]scope_map = np.repeat(scope_map, repeats=3, axis=2)res_image = np.multiply(scope_map, np.array(fore_image)[:, :, :3]) + np.multiply((1 - scope_map),np.array(back_image))# 保存图片res_image = Image.fromarray(np.uint8(res_image))picture_result = 'result_'+picture # 保存的名字res_image.save(picture_result)return picture_resultdef show_result(picture):fore_image = get_fore_image(picture)back_image = get_back_image(picture)picture_result = images_fusion_final(fore_image,back_image,picture)img = mpimg.imread(picture_result)# 原图plt.figure(figsize=(10,10))f = plt.subplot(121)show_picture = mpimg.imread(picture)plt.imshow(show_picture)plt.axis('off')# 合成后的背景虚化图f = plt.subplot(122)plt.imshow(img)plt.axis('off')plt.show()
效果展示
# 可以分别执行观察效果
show_result(picture='test1.jpg')
# show_result(picture='test2.jpg')
# show_result(picture='Taylor2.jpg')
AI Stuido项目地址 :基于paddlehub实现人像背景虚化
总结
- 本项目采用虚化图+人像图合成的方式完成背景虚化,因此人像分割是否精确直接决定最终效果
- 对于前景后景区分度明显且人物较近的图片,实现效果最佳
- 现在在网上找一张没有虚化过背景的图片来做测验实在太难了,只能找人家的私房照了…
- 缺点:由于是两张图的加权和,所以合成图像中的任务可能也很会有些模糊的效果。
参考资料(项目):基于PaddleHub实现美颜及背景更换
这篇关于飞桨课程创意项目:基于Paddlehub实现人像背景虚化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!