PaddleHub创意项目 | 将霉霉P到埃菲尔铁塔前
此項(xiàng)目是使用PaddleHub簡單地對圖片進(jìn)行摳圖和合成,將霉霉摳出來P到埃菲爾鐵塔前面。
和常見的PS摳圖相比,使用PaddleHub摳圖可以節(jié)省時間,并能夠智能地對圖片中顏色較為接近的邊緣進(jìn)行摳圖,準(zhǔn)確率較高。 PaddleHub便捷地獲取PaddlePaddle生態(tài)下的預(yù)訓(xùn)練模型,完成模型的管理和一鍵預(yù)測。配合使用Fine-tune API,可以基于大規(guī)模預(yù)訓(xùn)練模型快速完成遷移學(xué)習(xí),讓預(yù)訓(xùn)練模型能更好地服務(wù)于用戶特定場景的應(yīng)用。
PaddleHub官網(wǎng)地址:paddlepaddle.org.cn/hub
更多PaddleHub項(xiàng)目地址:https://github.com/PaddlePaddle/PaddleHub
首先需要安裝PaddleHub標(biāo)準(zhǔn)庫以及其中DeepLabv3+模型一鍵摳圖。
!pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple !hub install deeplabv3p_xception65_humanseg==1.0.0 #導(dǎo)入所用標(biāo)準(zhǔn)庫 import os import numpy as np import paddlehub as hub import matplotlib.pyplot as plt import matplotlib.image as mpimg from PIL import Image In [30] # 測試圖片路徑和輸出路徑 test_path = 'data/test/' output_path = 'data/out/'# 待預(yù)測圖片 test_img_path = ["taylor swift.jpg"] test_img_path = [test_path + img for img in test_img_path] img = mpimg.imread(test_img_path[0]) # 展示待預(yù)測圖片 plt.figure(figsize=(10,10)) plt.imshow(img) plt.axis('off') plt.show() 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) # 預(yù)測結(jié)果展示 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()摳出來的圖片還會有少許沒有被摳掉的地方。
# 定義合成函數(shù) 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)# 圖片加權(quán)合成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)return res_image output_path_img = output_path + 'blend_res_img.jpg' blend_images('humanseg_output/taylor swift.png', 'data/test/eifel.jpg', output_path_img)# 展示合成圖片 plt.figure(figsize=(10,10)) img = mpimg.imread(output_path_img) plt.imshow(img) plt.axis('off') plt.show()總結(jié):
此項(xiàng)目使用paddlehub進(jìn)行了摳圖和合成,可以實(shí)現(xiàn)PS軟件的功能,但少量的代碼減小了內(nèi)存占用,這讓我們看到了paddlehub的精巧之處。但還有很多不足之處,比如精確度不是特別高,后面的人影會影響摳圖效果,在后面的模型中需要改進(jìn)。
這是使用paddlehub進(jìn)行摳圖和合成圖片的小嘗試,這次嘗試讓我了解了paddlehub工具的使用,后續(xù)我還會做一些更多的嘗試,比如視頻摳圖,讓圖片活起來。
總結(jié)
以上是生活随笔為你收集整理的PaddleHub创意项目 | 将霉霉P到埃菲尔铁塔前的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频质量评价 VMAF,为何让人又喜又忧
- 下一篇: 【原创】微信最新表情js代码