PaddleHub创意项目 | 将霉霉P到埃菲尔铁塔前
生活随笔
收集整理的這篇文章主要介紹了
PaddleHub创意项目 | 将霉霉P到埃菲尔铁塔前
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
此項目是使用PaddleHub簡單地對圖片進行摳圖和合成,將霉霉摳出來P到埃菲爾鐵塔前面。
和常見的PS摳圖相比,使用PaddleHub摳圖可以節省時間,并能夠智能地對圖片中顏色較為接近的邊緣進行摳圖,準確率較高。 PaddleHub便捷地獲取PaddlePaddle生態下的預訓練模型,完成模型的管理和一鍵預測。配合使用Fine-tune API,可以基于大規模預訓練模型快速完成遷移學習,讓預訓練模型能更好地服務于用戶特定場景的應用。
PaddleHub官網地址:paddlepaddle.org.cn/hub
更多PaddleHub項目地址:https://github.com/PaddlePaddle/PaddleHub
首先需要安裝PaddleHub標準庫以及其中DeepLabv3+模型一鍵摳圖。
!pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple !hub install deeplabv3p_xception65_humanseg==1.0.0 #導入所用標準庫 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/'# 待預測圖片 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]) # 展示待預測圖片 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) # 預測結果展示 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()摳出來的圖片還會有少許沒有被摳掉的地方。
# 定義合成函數 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] / 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()總結:
此項目使用paddlehub進行了摳圖和合成,可以實現PS軟件的功能,但少量的代碼減小了內存占用,這讓我們看到了paddlehub的精巧之處。但還有很多不足之處,比如精確度不是特別高,后面的人影會影響摳圖效果,在后面的模型中需要改進。
這是使用paddlehub進行摳圖和合成圖片的小嘗試,這次嘗試讓我了解了paddlehub工具的使用,后續我還會做一些更多的嘗試,比如視頻摳圖,讓圖片活起來。
總結
以上是生活随笔為你收集整理的PaddleHub创意项目 | 将霉霉P到埃菲尔铁塔前的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频质量评价 VMAF,为何让人又喜又忧
- 下一篇: 【原创】微信最新表情js代码