paddleHub实现抠图、合成、美化一体:刚刚接触PaddleHub,试着用DeepLabv3+模型实现抠图、合成、美化于一体。首先使用deeplabv3p_xception65_humanseg模型进行对图像分割,并完成抠图;使用Blend进行背景替换;接下来用face_landmark_localization模型进行人脸关键点检测,再根据关键点位置对其上色,完成人物的美化。本项目本想实现多人物的检测点及美化,待实现。
import os
import zipfile
import matplotlib.pyplot as plt
import matplotlib.image as image
#显示matplotlib生成的图形
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import math
def unzip_data(src_path,target_path):
'''
解压原始数据集,将src_path路径下的zip包解压至target_path目录下
'''
if(not os.path.isdir(target_path + "test")):
z = zipfile.ZipFile(src_path, 'r')
z.extractall(path=target_path)
z.close()
def show_image(image_path):
'''
展示图片
'''
img = image.imread(image_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
#解压数据集
src_path = "/home/aistudio/work/test.zip"
target_path = "/home/aistudio/work/"
unzip_data(src_path,target_path)
In [281]
!hub install deeplabv3p_xception65_humanseg==1.1.0
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Downloading deeplabv3p_xception65_humanseg
[== ] 4.47%
In [282]
import cv2
import paddlehub as hub
# 加载deeplabv3p_xception65_humanseg模型
module = hub.Module(name="deeplabv3p_xception65_humanseg")
# 待分割图片展示展示
test_img_path = "work/test/test5.jpg"
show_image(test_img_path)
results = module.segmentation(images=[cv2.imread(test_img_path)],
visualization=True)
for result in results:
result_path = result['save_path']
show_image(result_path)
In [283]
from PIL import Image
import numpy as np
import matplotlib.image as mpimg
def blend_images(fore_image, base_image):
"""
将抠出的人物图像换背景
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] / 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("blend_res_img6.jpg")
In [284]
blend_images('humanseg_output/ndarray_time=1597128617554267.png', 'timg.jfif')
# 展示合成图片
plt.figure(figsize=(10,10))
img = mpimg.imread("blend_res_img6.jpg")
plt.imshow(img)
plt.axis('off')
plt.show()
In [285]
src_img = cv2.imread('blend_res_img6.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.putText(img, str(index), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_COMPLEX, 3, (0,0,255), -1)
cv2.circle(tmp_img, (int(point[0]), int(point[1])), 2, (0, 0, 255), -1)
res_img_path = 'face_landmark.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()
In [286]
src_img = cv2.imread('blend_res_img6.jpg')
def rouge(image, face_landmark, ruby=True):
"""
自动涂口红
image: 人像图片
face_landmark: 人脸关键点
ruby:是否需要深色口红
"""
image_cp = image.copy()
if ruby:
rouge_color = (0,0,255)
else:
rouge_color = (0,0,200)
points=face_landmark[48:68]
hull = cv2.convexHull(points)
cv2.drawContours(image, [hull], -1, rouge_color, -1)
cv2.addWeighted(image, 0.2, image_cp, 0.9, 0, image_cp)
return image_cp
In [287]
#src_img = rouge(src_img,face_landmark.jpg)
cv2.imwrite(res_img_path, src_img)
img = mpimg.imread(res_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
可以公开个项目
赞
+1
强啊
对,公开一下哈哈哈
好玩的项目~~
是不是弄成项目里面放到Aistudio好一点
好像已经有了