0
点赞
收藏
分享

微信扫一扫

【深度学习入门案例】三十行代码实现抠图及图片合成


文章目录

  • ​​一义待抠图 图片​​
  • ​​二载预训练模型​​
  • ​​三.片合成​​
  • ​​四.完整源码​​

一义待抠图 图片

图片和py文件统计目录:
【深度学习入门案例】三十行代码实现抠图及图片合成_抠图
代码为:

#待预测图片
test_img_path = ["./test.jpg"]


import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread(test_img_path[0])

# 展示待预测图片
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

返回:
【深度学习入门案例】三十行代码实现抠图及图片合成_背景图片_02

二载预训练模型

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

import paddlehub as hub
module = hub.Module(name="humanseg_lite")
res = module.segment(
paths = ["./test.jpg"],
visualization=True,
output_dir='humanseg_lite')

# 预测结果展示
test_img_path = "./humanseg_lite/test.png"
img = mpimg.imread(test_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

返回:
【深度学习入门案例】三十行代码实现抠图及图片合成_pytorch_03

三.片合成

将抠出的人物图片合成在想要的背景图片当中

#图像合成
from PIL import Image
import numpy as np


def blend_images(fore_image, base_image):
"""
将抠出的人物图像换背景
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] / 255
scope_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("blend_res_img.jpg")
blend_images('./test.jpg', 'img.png')

# 展示合成图片
plt.figure(figsize=(10,10))
img = mpimg.imread("./blend_res_img.jpg")
plt.imshow(img)
plt.axis('off')
plt.show()

返回:
【深度学习入门案例】三十行代码实现抠图及图片合成_pytorch_04
如此便是成功了,yeah!

四.完整源码

相关文件创建和图片可以创建和找,需要我得文件可以主页左侧栏联系我。

# coding=gbk
"""
作者:川川
@时间 : 2021/8/29 5:48
群:970353786
"""
#待预测图片
test_img_path = ["./test.jpg"]

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread(test_img_path[0])
# 展示待预测图片
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()


#加载模型
import paddlehub as hub
module = hub.Module(name="humanseg_lite")
res = module.segment(
paths = ["./test.jpg"],
visualization=True,
output_dir='humanseg_lite')

# 预测结果展示
test_img_path = "./humanseg_lite/test.png"
img = mpimg.imread(test_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

#图像合成
# from PIL import Image
# import numpy as np
#
#
# def blend_images(fore_image, base_image):
# """
# 将抠出的人物图像换背景
# 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] / 255
# scope_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("blend_res_img.jpg")
# blend_images('./humanseg_lite/test.png', 'img.png')
#
# # 展示合成图片
plt.figure(figsize=(10,10))
img = mpimg.imread("./blend_res_img.jpg")
plt.imshow(img)
plt.axis('off')
plt.show()


举报

相关推荐

0 条评论