經過預訓練模型進行圖像的自動摳圖及整合。網絡
代碼已經共享在AIStudio上,連接:app
https://aistudio.baidu.com/aistudio/projectdetail/242887ide
模型概述 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 存放待分割圖片的存放路徑spa
API def segmentation(data) 用於人像分割命令行
參數code
data:dict類型,key爲image,str類型;value爲待分割的圖片路徑,list類型。 output_dir:生成圖片的保存路徑,默認爲 humanseg_outputorm
返回blog
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
[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'}
'./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
[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'}
'./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
[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'}
'./humanseg_output/body2.png'