Paddlehub创意赛---给人像增加背景
Paddlehub创意赛
这次创意赛我是实现了一个简单得抠图加背景的操作,将人像抠出来之后并为其添加一个背景。原本还想加上美颜之类的效果,因为时间关系所以放弃了。
自己试了试如果选择的背景合适的话还是很不错的。
首先配置环境
!pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
!hub install deeplabv3p_xception65_humanseg==1.0.0
导入相关的库
import paddlehub as hub
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
from PIL import Image
import numpy as np
展示原图
test_img_path = "1.jpg"
img = mpimg.imread(test_img_path)
# 展示待预测图片
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
使用Paddlehub进行抠图操作
# 显示处理之后的图片
humanseg = hub.Module(name="deeplabv3p_xception65_humanseg")
path = ["1.jpg"]
results = humanseg.segmentation(data={"image":path})
# 预测结果展示
test_img_path = results[0]["processed"]
img = mpimg.imread(test_img_path)
# 展示预测结果图片
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
关键点检测
接下来我是用了一下关键点检测,使原本想进行瘦脸之类的美颜操作。因为各种原因没有时间实现了。
####进行关键点检测#####
src_img = cv2.imread("1.jpg")
module = hub.Module(name="face_landmark_localization")
result = module.keypoint_detection(images=[src_img])
tmp_img = src_img.copy()
for index, point in enumerate(result[0]['data'][0]):
cv2.circle(tmp_img, (int(point[0]), int(point[1])), 2, (0, 0, 255), -1)
res_img_path = 'face_1.jpg'
cv2.imwrite(res_img_path, tmp_img)
img = mpimg.imread(res_img_path)
# 展示预测68个关键点结果
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
合并背景
base_image = Image.open(f'2.jpg').convert('RGB')
fore_image = Image.open(f'humanseg_output/1.png').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(f"humanseg_output/1.png")
print('照片合成完毕')
plt.figure(figsize=(10,10))
plt.imshow(res_image)
plt.axis('off')
plt.show()
Paddlehub创意赛总结
通过这次学习,充分的了解到了Paddlehub的强大,当时也看到了班班小姐姐让我们看的以前的学员做的那些Paddlehub的创意,通过脸来控制飞机、京剧变脸等等好多有趣的创意,以前就想学习paddlepaddle,在去年的时候参加了一期,没有认真学,现在在空余时间都会看一些机器学习,paddlepaddle的一些视频,最近刚看完一些百度人工智能的介绍,以及平台的使用等等。
主要是自己对这个是真的感兴趣,也想通过学习来找一份这样的工作。
十分感谢这次课程的老师,助教,班班姐,文姐姐等所有讲课的老师、运营人员和助教,也想通过更多的这种课程学习到更多的知识。