PaddleHub人像分割模型:AI人像摳圖及圖像合成

~~本項目根據DeepLabv3+模型一鍵摳圖示例,主要採用PaddleHub DeepLabv3+模型(deeplabv3p_xception65_humanseg)和python圖像處理庫opencv、PIL等完成。在最新做中,做者經過encoder-decoder進行多尺度信息的融合,同時保留了原來的空洞卷積和ASSP層, 其骨幹網絡使用了Xception模型,提升了語義分割的健壯性和運行速率,在 PASCAL VOC 2012 dataset取得新的state-of-art performance,該PaddleHub Module使用百度自建數據集進行訓練,可用於人像分割,支持任意大小的圖片輸入。在完成一鍵摳圖以後,經過圖像合成,實現扣圖比賽任務。html

PaddleHub 是基於 PaddlePaddle 開發的預訓練模型管理工具,能夠藉助預訓練模型更便捷地開展遷移學習工做,目前的預訓練模型涵蓋了圖像分類、目標檢測、詞法分析、語義模型、情感分析、視頻分類、圖像生成、圖像分割、文本審覈、關鍵點檢測等主流模型。html5

PaddleHub官網:PaddleHub官網python

PaddleHub項目地址:PaddleHub githubgit

更多PaddleHub預訓練模型應用可見:教程合集課程 github

NOTE: 若是您在本地運行該項目示例,須要首先安裝PaddleHub。若是您在線運行,須要首先fork該項目示例。以後按照該示例操做便可。網絡

1、安裝環境

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

2、開始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()

output_5_0.png

2. 加載預訓練模型

經過加載PaddleHub DeepLabv3+模型(deeplabv3p_xception65_humanseg)實現一鍵摳圖app

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'}

output_7_2.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] / 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(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_10_0.png

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()

output_11_0.png

# 完整流程來一張
test_img_path = ["xcd.jpg"]
test_img_path = [test_path + img for img in test_img_path]
img = mpimg.imread(test_img_path[0]) 

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)

output_path_img = output_path + 'blend_res_img2.jpg'
blend_images('humanseg_output/xcd.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()
[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

output_12_1.png

3、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))
    # 保存爲 gif
    imageio.mimsave(gif_name, frames, 'GIF', duration=duration)
    return

def 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 notebook
    
    Args:
        image_array(numpy.ndarray): image_array.shape equal to (num_images, height, width, num_channels)
    '''
    dpi = 72.0
    xpixels, 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()

output_17_0.png

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()

output_19_0.png

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' % i
    blend_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)

結果上傳不了,可見原項目連接。ide

4、視頻合成

有時間再寫……函數


原項目連接: PaddleHub創意賽:AI人像摳圖及圖像合成
相關文章
相關標籤/搜索