通过预训练模型进行图像的自动抠图及整合。

代码已经共享在AIStudio上,链接:

https://aistudio.baidu.com/aistudio/projectdetail/242887

模型概述 DeepLabv3+ 是Google DeepLab语义分割系列网络的最新作,其前作有 DeepLabv1,DeepLabv2, DeepLabv3。在最新作中,作者通过encoder-decoder进行多尺度信息的融合,同时保留了原来的空洞卷积和ASSP层, 其骨干网络使用了Xception模型,提高了语义分割的健壮性和运行速率,在 PASCAL VOC 2012 dataset取得新的state-of-art performance。该PaddleHub Module使用百度自建数据集进行训练,可用于人像分割,支持任意大小的图片输入。

命令行预测示例 $ hub run deeplabv3p_xception65_humanseg --input_path "/PATH/TO/IMAGE" $ hub run deeplabv3p_xception65_humanseg --input_file test.txt test.txt 存放待分割图片的存放路径

API def segmentation(data) 用于人像分割

参数

data:dict类型,key为image,str类型;value为待分割的图片路径,list类型。 output_dir:生成图片的保存路径,默认为 humanseg_output

返回

result:list类型,每个元素为对应输入图片的预测结果。预测结果为dict类型,有以下字段:

origin 原输入图片路径 processed 分割图片的路径。

In[1]

cd work

/home/aistudio/work

先定义抠图的函数,通过调用图像分割模型抠图

In[7]

def body_seg_fore(imgname):

    module = hub.Module(name="deeplabv3p_xception65_humanseg")

    test_img_path = "./"+imgname+".jpg"

    # 预测结果展示

    img = mpimg.imread(test_img_path)

    plt.imshow(img)

    plt.axis('off')

    plt.show()

    # set input dict

    input_dict = {"image": [test_img_path]}


    # execute predict and print the result

    results = module.segmentation(data=input_dict)

    for result in results:

        print(result)

    test_img_path = "./humanseg_output/"+imgname+".png"

    img = mpimg.imread(test_img_path)

    plt.imshow(img)

    plt.axis('off')

    plt.show()

    return test_img_path

In[8]

body_seg_fore('body2')

[2020-01-10 06:39:53,705] [    INFO] - Installing deeplabv3p_xception65_humanseg module

2020-01-10 06:39:53,705-INFO: Installing deeplabv3p_xception65_humanseg module

[2020-01-10 06:39:53,753] [    INFO] - Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg

2020-01-10 06:39:53,753-INFO: Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg


1240

[2020-01-10 06:39:54,539] [    INFO] - 0 pretrained paramaters loaded by PaddleHub

2020-01-10 06:39:54,539-INFO: 0 pretrained paramaters loaded by PaddleHub

{'origin': './body2.jpg', 'processed': 'humanseg_output/body2.png'}


1240

'./humanseg_output/body2.png'

定义图片合成函数。

In[12]

#图片整合

#foreimage:前景照片,baseimage:景区照片,outputimage:数据结果,rate:前景照片缩放比例

def combine_image(foreimage,baseimage,outputimage,rate):

    from PIL import Image

    base_img = Image.open(baseimage)

    BL, BH = base_img.size

    #读取要粘贴的图片 RGBA模式   

    #当需要将一张有透明部分的图片粘贴到一张底片上时,如果用Python处理,可能会用到PIL,

    #但是PIL中 有说明,在粘贴RGBA模式的图片是,alpha通道不会被帖上,也就是不会有透明的效果,

    #当然也给出了解决方法,就是粘贴的时候,将RGBA的的alpha通道提取出来做为mask传入。

    fore_image = Image.open(foreimage)

    L, H = fore_image.size

    #缩放

    fore_image = fore_image.resize((int(L * rate), int(H * rate)))

    L, H = fore_image.size

    #分离通道   

    r,g,b,a = fore_image.split()    #粘贴


    box=(int(BL/2-L/2), BH-H, int(BL/2+L/2) ,BH)


    base_img.paste(fore_image,box,mask = a)

    base_img.save(outputimage)  # 保存图片


#输出程序

def show_image(originimage,baseimage,outputimage,rate):

    segname=body_seg_fore(originimage)

    combine_image(segname,baseimage,outputimage,rate)


    img = mpimg.imread(outputimage)

    plt.imshow(img)

    plt.axis('off')

    plt.show()

    return test_img_path

结果展示:

In[14]

show_image('body2','./desert.jpg','body2_desert.jpg',2)

[2020-01-10 06:42:43,724] [    INFO] - Installing deeplabv3p_xception65_humanseg module

2020-01-10 06:42:43,724-INFO: Installing deeplabv3p_xception65_humanseg module

[2020-01-10 06:42:43,746] [    INFO] - Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg

2020-01-10 06:42:43,746-INFO: Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg


1240

[2020-01-10 06:42:44,629] [    INFO] - 0 pretrained paramaters loaded by PaddleHub

2020-01-10 06:42:44,629-INFO: 0 pretrained paramaters loaded by PaddleHub

{'origin': './body2.jpg', 'processed': 'humanseg_output/body2.png'}


1240


1240

'./humanseg_output/body2.png'

In[18]

show_image('body1','./desert.jpg','body2_desert.jpg',0.3)

[2020-01-10 06:44:26,397] [    INFO] - Installing deeplabv3p_xception65_humanseg module

2020-01-10 06:44:26,397-INFO: Installing deeplabv3p_xception65_humanseg module

[2020-01-10 06:44:26,423] [    INFO] - Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg

2020-01-10 06:44:26,423-INFO: Module deeplabv3p_xception65_humanseg already installed in /home/aistudio/.paddlehub/modules/deeplabv3p_xception65_humanseg


1240

[2020-01-10 06:44:27,592] [    INFO] - 0 pretrained paramaters loaded by PaddleHub

2020-01-10 06:44:27,592-INFO: 0 pretrained paramaters loaded by PaddleHub

{'origin': './body1.jpg', 'processed': 'humanseg_output/body1.png'}


1240


1240

'./humanseg_output/body2.png'