点击上方“AI搞事情”关注我们


本项目根据DeepLabv3+模型一键抠图示例,主要采用PaddleHub DeepLabv3+模型(deeplabv3p_xception65_humanseg)和python图像处理库opencv、PIL等完成。在最新作中,作者通过encoder-decoder进行多尺度信息的融合,同时保留了原来的空洞卷积和ASSP层, 其骨干网络使用了Xception模型,提高了语义分割的健壮性和运行速率,在 PASCAL VOC 2012 dataset取得新的state-of-art performance,该PaddleHub Module使用百度自建数据集进行训练,可用于人像分割,支持任意大小的图片输入。在完成一键抠图之后,通过图像合成,实现扣图比赛任务。

PaddleHub 是基于 PaddlePaddle 开发的预训练模型管理工具,可以借助预训练模型更便捷地开展迁移学习工作,目前的预训练模型涵盖了图像分类、目标检测、词法分析、语义模型、情感分析、视频分类、图像生成、图像分割、文本审核、关键点检测等主流模型。

NOTE: 如果您在本地运行该项目示例,需要首先安装PaddleHub。如果您在线运行,需要首先fork该项目示例。之后按照该示例操作即可。

一、安装环境

!pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
!hub install deeplabv3p_xception65_humanseg==1.0.0

二、开始P图

1. 引入包

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import animation
import cv2
import paddlehub as hub
from PIL import Image, ImageSequence
from IPython.display import display, HTML
import numpy as np
import imageio
import os
# 测试图片路径和输出路径
test_path = 'image/test/'
output_path = 'image/blend_out/'# 待预测图片
test_img_path = ["test.jpg"]
test_img_path = [test_path + img for img in test_img_path]
img = mpimg.imread(test_img_path[0])# 展示待预测图片
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

2. 加载预训练模型

通过加载PaddleHub DeepLabv3+模型(deeplabv3p_xception65_humanseg)实现一键抠图

module = hub.Module(name="deeplabv3p_xception65_humanseg")
input_dict = {"image": test_img_path}# execute predict and print the result
results = module.segmentation(data=input_dict)
for result in results:print(result)# 预测结果展示
out_img_path = 'humanseg_output/' + os.path.basename(test_img_path[0]).split('.')[0] + '.png'
img = mpimg.imread(out_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
[32m[2020-04-01 22:40:09,064] [    INFO] - Installing deeplabv3p_xception65_humanseg module[0m
[32m[2020-04-01 22:40:09,100] [    INFO] - Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg[0m
[32m[2020-04-01 22:40:09,814] [    INFO] - 0 pretrained paramaters loaded by PaddleHub[0m
{'origin': 'image/test/test.jpg', 'processed': 'humanseg_output/test.png'}

3. 图像合成

# 合成函数
def blend_images(fore_image, base_image, output_path):"""将抠出的人物图像换背景fore_image: 前景图片,抠出的人物图片base_image: 背景图片"""# 读入图片base_image = Image.open(base_image).convert('RGB')fore_image = Image.open(fore_image).resize(base_image.size)# 图片加权合成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(base_image))#保存图片res_image = Image.fromarray(np.uint8(res_image))res_image.save(output_path)
output_path_img = output_path + 'blend_res_img.jpg'
blend_images('humanseg_output/test.png', 'image/test/bg.jpg', output_path_img)# 展示合成图片
plt.figure(figsize=(10,10))
img = mpimg.imread(output_path_img)
plt.imshow(img)
plt.axis('off')
plt.show()

output_path_img = output_path + 'blend_res_img2.jpg'
blend_images('humanseg_output/test.png', 'image/test/bg1.jpg', output_path_img)# 展示合成图片
plt.figure(figsize=(10,10))
img = mpimg.imread(output_path_img)
plt.imshow(img)
plt.axis('off')
plt.show()

# 完整流程来一张
test_img_path = ["xcd.jpg"]
test_img_path = [test_path + img for img in test_img_path]
img = mpimg.imread(test_img_path[0])input_dict = {"image": test_img_path}# execute predict and print the result
results = module.segmentation(data=input_dict)output_path_img = output_path + 'blend_res_img2.jpg'
img = blend_images('humanseg_output/xcd.png', 'image/test/bg.jpg', output_path_img)ttfont = ImageFont.truetype("image/STXINGKA.TTF",100)
draw = ImageDraw.Draw(img)
draw.text((350,450), u'都市绣春刀', fill=(255 , 25, 0), font=ttfont)
img.save(output_path_img)# 展示合成图片
plt.figure(figsize=(10,10))
img = mpimg.imread(output_path_img)
plt.imshow(img)
plt.axis('off')
plt.show()
[32m[2020-04-01 22:40:28,805] [    INFO] - Installing deeplabv3p_xception65_humanseg module[0m
[32m[2020-04-01 22:40:28,821] [    INFO] - Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg[0m
[32m[2020-04-01 22:40:29,497] [    INFO] - 0 pretrained paramaters loaded by PaddleHub[0m

三、GIF合成

GIF处理函数

def create_gif(gif_name, path, duration=0.3):'''生成gif文件,原始图片仅支持png格式gif_name :字符串,所生成的 gif 文件名,带 .gif 后缀path :      需要合成为 gif 的图片所在路径duration :  gif 图像时间间隔'''frames = []pngFiles = os.listdir(path)image_list = [os.path.join(path, f) for f in pngFiles]for image_name in image_list:frames.append(imageio.imread(image_name))# 保存为 gifimageio.mimsave(gif_name, frames, 'GIF', duration=duration)returndef split_gif(gif_name, output_path, resize=False):'''拆分gif文件,生成png格式,便于生成gif_name :gif 文件路径,带 .gif 后缀path :      拆分图片所在路径'''gif_file = Image.open(gif_name)name = gif_name.split('/')[-1].split('.')[0]if not os.path.exists(output_path):                        # 判断该文件夹是否存在,如果存在再创建则会报错os.mkdir(output_path)for i, frame in enumerate(ImageSequence.Iterator(gif_file), 1):if resize:frame = frame.resize((300, 168), Image.ANTIALIAS)frame.save('%s/%s_%d.png' % (output_path, name, i))                       # 保存在等目录的output文件夹下def plot_sequence_images(image_array):''' Display images sequence as an animation in jupyter notebookArgs:image_array(numpy.ndarray): image_array.shape equal to (num_images, height, width, num_channels)'''dpi = 72.0xpixels, ypixels = image_array[0].shape[:2]fig = plt.figure(figsize=(ypixels/dpi, xpixels/dpi), dpi=dpi)im = plt.figimage(image_array[0])def animate(i):im.set_array(image_array[i])return (im,)anim = animation.FuncAnimation(fig, animate, frames=len(image_array), interval=500, repeat_delay=1, repeat=True)display(HTML(anim.to_html5_video()))

1. 拆分GIF

# 拆GIF文件为png帧
split_gif('image/test_gif/wushu.gif', 'image/test_gif/wushu_frame', True)imgs = []
for i, fname in enumerate(os.listdir('image/test_gif/wushu_frame')):img = cv2.imread('image/test_gif/wushu_frame/' + fname)img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)imgs.append(img_rgb)
plot_sequence_images(imgs)

# 测试图片路径和输出路径
test_path = 'image/test_gif/wushu_frame/'
output_path = 'image/blend_out/'# 待预测图片
test_img_path = os.listdir(test_path)
test_img_path = [test_path + i for i in test_img_path]
img = mpimg.imread(test_img_path[0])# 展示待预测图片
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

2. 预测分割

input_dict = {"image": test_img_path}# execute predict and print the result
results = module.segmentation(data=input_dict)# 预测结果展示
out_img_path = 'humanseg_output/' + os.path.basename(test_img_path[0]).split('.')[0] + '.png'
img = mpimg.imread(out_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

3. 合成结果

# 合成图片
humanseg_wushu = [filename for filename in os.listdir('humanseg_output/') if filename.startswith("wushu")]for i, img in enumerate(humanseg_wushu):img_path = os.path.join('humanseg_output/wushu_%d.png' % (i+1))output_path_img = output_path + 'wushu/%d.png' % iblend_images(img_path, 'image/test/bg1.jpg', output_path_img)
# 合成GIF
create_gif('image/blend_out/blend_res_wushu.gif', 'image/blend_out/wushu/', duration=0.5)imgs = []
for i, fname in enumerate(os.listdir('image/blend_out/wushu/')):img = cv2.imread('image/blend_out/wushu/' + fname)img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)imgs.append(img_rgb)
plot_sequence_images(imgs)

四、视频合成

# 拆视频
cap = cv2.VideoCapture('image/video/input.mp4')imgs = []
num = 0
while(True):ret,frame = cap.read()if ret:cv2.imwrite('image/video/frame/%d.jpg'%num, frame)# img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)# imgs.append(img_rgb)num += 1else:break
cap.release()#关闭相机# plot_sequence_images(imgs)
# 显示一张
out_img_path = 'image/video/frame/1.jpg'
img = mpimg.imread(out_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()


frame_path = 'image/video/frame'
test_img_path = [os.path.join(frame_path, fname) for fname in os.listdir(frame_path)]
input_dict = {"image": test_img_path}# execute predict and print the result
results = module.segmentation(data=input_dict, output_dir='image/video/frame_seg/')
# plot_sequence_images(imgs)
# 显示一张
out_img_path = 'image/video/frame_seg/1.png'
img = mpimg.imread(out_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

# 合并输出视频
humanseg_wushu = [filename for filename in os.listdir('image/video/frame_seg/')]
for i, img in enumerate(humanseg_wushu):if i <= 145 or (i >= 250 and i <= 427) or (i >= 552 and i <= 601) or (i >= 729 and i <= 761):img_path = os.path.join('image/video/frame_seg/%d.png' % (i+1))output_path_img = output_path + 'video/%d.png' % iimg = blend_images(img_path, 'image/test/bg2.jpg', output_path_img)if (i >= 146 and i <= 249) or (i >= 428 and i<= 551) or (i >= 602 and i<= 728):img_path = os.path.join('image/video/frame_seg/%d.png' % (i+1))output_path_img = output_path + 'video/%d.png' % iimg = blend_images(img_path, 'image/test/bg3.jpg', output_path_img)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 25.0, (1280,720))
files = os.listdir('image/blend_out/video')
for i in range(len(files)):img = cv2.imread('image/blend_out/video/%d.png' % i)img = cv2.resize(img, (1280,720))out.write(img)#保存帧
out.release()

项目地址:  阅读原文

视频地址:https://www.bilibili.com/video/BV1ng4y1a77T

长按二维码关注我们

有趣的灵魂在等你

留言请摁

PaddleHub人像分割模型:AI人像抠图及图像合成相关推荐

  1. (记录)PaddleHub创意赛:AI人像抠图及图像合成

    文章目录 PaddleHub创意赛:AI人像抠图及图像合成 一.安装环境(这里面有几个坑) 二.接下来就``开始P图 1. 引入包 2. 加载预训练模型(挺厉害的不得不说) 3. 图像合成 Paddl ...

  2. AI深度学习入门与实战19 语义分割:打造简单高效的人像分割模型

    上一讲,我向你介绍了语义分割的原理.在理解上一课时中 U-Net 语义分割网络的基础上,这一讲,让我们来实际构建一个人像分割模型吧. 语义分割的评估 我们先简单回顾一下语义分割的目的:把一张图中的每一 ...

  3. JAVA代码实现人物照片的人像分割 | 百度AI

    效果展示 原图 处理后 实现方法 第一步 先去百度云上注册账号,创建 ai人像分割应用 人像分割技术_人像分割算法_人像分割-百度AI开放平台

  4. python人像精细分割_PaddleHub人像分割模型:AI人像抠图及图像合成

    本项目根据DeepLabv3+模型一键抠图示例,主要采用PaddleHub DeepLabv3+模型(deeplabv3p_xception65_humanseg)和python图像处理库opencv ...

  5. 百度人像分割API调用——一键抠图

    现在,越来越多的图像分割技术应用到现实生活中来,比如各种辣眼睛的抠图神剧.自带瘦身美体效果的修图神器.这些看得见摸不着的神秘图像特效是如何实现的呢?还有哪些好玩又有用的应用? 今天,就以百度" ...

  6. 荟聚NeurIPS顶会模型、智能标注10倍速神器、人像分割SOTA方案、3D医疗影像分割利器,PaddleSeg重磅升级!

    导读 图像分割是计算机视觉三大任务之一,基于深度学习的图像分割技术也发挥日益重要的作用,广泛应用于智慧医疗.工业质检.自动驾驶.遥感.智能办公等行业. 然而在实际业务中,图像分割依旧面临诸多挑战,比如 ...

  7. 【开源】PaddleHub人像分割实现桌面动态挂件

    开源 项目地址:https://aistudio.baidu.com/aistudio/projectdetail/2322820 一.项目介绍 电脑桌面单单调调,突然想起来(其实看到别人的桌面上的- ...

  8. Google出品Mediapipe人像分割,可任意更换图片与视频背景

    在一些视频聊天软件中,我们可以看到很多人的背景特别的漂亮,甚至我们都怀疑是不是真实地来到了某个地方,本期介绍一下Mediapipe人像分割(RVM人像分割) MediaPipe Selfie Segm ...

  9. 轻量人像分割SINet

    引入 随着算力和算法的不断提升,能够训练的模型也越来越大了,当然精度也越来越高了 不过过于巨大的模型也带来了部署上的不便 今天就介绍两个轻量级的人像分割模型:SINet 和 ExtremeC3Net ...

最新文章

  1. parameter缩略语_缩略语
  2. 硬核!两个博士结婚,接亲时新娘给新郎摆了盘棋局:你赢了再娶我!
  3. 颠覆性技术丨无人驾驶
  4. c语言是一门对标识符大小写敏感的程序设计语言,《C语言程序设计》课程自测题...
  5. 【MM模块】Stock transfersTransfer posting 转储和调拨
  6. 又掌握了一项新技能 - 断点调试 Gradle 插件
  7. 语言里怎么防误输_育儿知识|我们的孩子为什么会怕输?
  8. vspy如何在图形面板显示报文_设备实时状态监控:如何进行工业生产设备数据采集?...
  9. JavaScript实现向OL列表内动态添加LI元素的方法
  10. python源码中的学习笔记_第2章_对象的布尔值与选择结构
  11. android手机鼠标,安卓手机变鼠标以及手机控制电脑图文教程
  12. 外贸常用术语_13个常用的国际贸易术语详解
  13. macbook卡在进度条开不了机_解决MacBook Pro开机卡死在进度条无反应,进不去桌面问题...
  14. 客厅经济回潮,强势崛起的OTT大屏如何赢得广告主选择?
  15. 快速消除PDF复制内容里的换行
  16. 计算机组老师颁奖词,优秀教研团队颁奖词
  17. 最新Discuz手机模板NVBING5手机版源码+可封装APP
  18. java如何获取当前时间 年月日 时分秒
  19. 已知坐标用matlab画图,已知45个点X Y Z的坐标值已知,如何用matlab画出网格图,每......
  20. ArcGIS API for Silverlight 绘制降雨路径动画

热门文章

  1. 在通常的微型计算机的汉字系统中,计算机应用基础win7+office2010试题(三校生公共课3)...
  2. SVN主干与分支的合并
  3. mysql反应慢_MySQL反应慢排查思路
  4. vb.net 机器学习-候选消除法
  5. go语言基础到提高(5)-结构
  6. 【机器学习】使用奇异值分解(SVD)构建推荐系统
  7. 【深度学习】手把手教你实现一个人工智能案例(蓄电池爬碱识别)
  8. pytorch几种损失函数CrossEntropyLoss、NLLLoss、BCELoss、BCEWithLogitsLoss、focal_loss、heatmap_loss
  9. 重磅!就在刚刚,吊打一切的 YOLOv4 开源了!
  10. 从用户反馈的可解释性提升推荐模型