1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > PaddleHub创意项目-制作证件照(抠图换底美颜)

PaddleHub创意项目-制作证件照(抠图换底美颜)

时间:2023-04-27 06:56:51

相关推荐

PaddleHub创意项目-制作证件照(抠图换底美颜)

PaddleHub创意项目-制作证件照(抠图换底美颜)

经过三个星期的百度架构师手把手带你零基础实践深度学习,对paddle有了一定了解,现在利用PaddleHub实践一个对图像进行抠图换底加美颜的小项目。

1、安装python 3.6+;

2、安装jupyter notebook;

3、安装paddle 1.8.0;

前三步不清楚的可以百度一下!

4、启动jupyter notebook,添加如下代码并执行;

#下载抠图模型!hub install deeplabv3p_xception65_humanseg==1.1.1

#调用一些相关的包import paddlehub as hubimport matplotlib.pyplot as plt import matplotlib.image as mpimg import cv2from PIL import Imageimport numpy as npimport math

# 展示处理前的图片img = mpimg.imread("data/obm.jpg") #修改为自己要处理的图片名plt.figure(figsize=(8,8))plt.imshow(img) plt.axis('off') plt.show()

# 开始抠图# 指定模型名称、待预测的图片路径、输出结果的路径,执行并输出预测结果module = hub.Module(name="deeplabv3p_xception65_humanseg")res = module.segmentation(paths = ["data/obm.jpg"], visualization=True, output_dir='data/')

# 展示抠图后的图片img = mpimg.imread("data/obm.png") plt.figure(figsize=(8,8))plt.imshow(img) plt.axis('off') plt.show()

使用图像加权合成的方法进行图像换底 ,参照/aistudio/projectdetail/509443

#图像背景和前景融合------换蓝底fore_image = Image.open(f'data/obm.png')#按照前景尺寸来改变背景的尺寸base_image = Image.open(f'data/base/blue.jpg').convert('RGB').resize(fore_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(f'data/obm_blue.jpg')plt.figure(figsize=(8,8))plt.imshow(res_image) plt.axis('off') plt.show()

#图像背景和前景融合------换红底fore_image = Image.open(f'data/obm.png')base_image = Image.open(f'data//base/red.jpg').convert('RGB').resize(fore_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(f'data/obm_red.jpg')plt.figure(figsize=(8,8))plt.imshow(res_image) plt.axis('off') plt.show()

#下载人脸关键点检测模型!hub install face_landmark_localization==1.0.2

#人脸关键点检测face_landmark = hub.Module(name="face_landmark_localization")result_point = face_landmark.keypoint_detection(images=[cv2.imread('data/obm_blue.jpg')])src_img = cv2.imread('data/obm_blue.jpg')tmp_img = src_img.copy()for index, point in enumerate(result_point[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 = 'data/obm_point.jpg'cv2.imwrite(res_img_path, tmp_img)

#关键点检测图像img_point = mpimg.imread(f'data/obm_point.jpg')plt.figure(figsize=(10,10))plt.imshow(img_point) plt.axis('off') plt.show()

实现美颜方法 瘦脸 首先介绍如何利用识别到的68个关键点完成瘦脸功能。 利用其中3号点到5号点距离作为瘦左脸距离,13号点到15号点距离作为瘦右脸距离(有点问题)。 同时利用局部平移算法完成瘦脸,参考:/aistudio/projectdetail/705875

def thin_face(image, face_landmark):"""实现自动人像瘦脸image: 人像图片face_landmark: 人脸关键点"""end_point = face_landmark[30]# 瘦左脸,3号点到5号点的距离作为瘦脸距离dist_left = np.linalg.norm(face_landmark[3] - face_landmark[5])image = local_traslation_warp(image, face_landmark[3], end_point, dist_left)# 瘦右脸,7号点到9号点的距离作为瘦脸距离(搞不懂)dist_right = np.linalg.norm(face_landmark[7] - face_landmark[9])image = local_traslation_warp(image, face_landmark[7], end_point, dist_right)#dist_right = np.linalg.norm(face_landmark[13] - face_landmark[15])#image = local_traslation_warp(image, face_landmark[13], end_point, dist_right)return image

def local_traslation_warp(image, start_point, end_point, radius):"""局部平移算法"""radius_square = math.pow(radius, 2)image_cp = image.copy()dist_se = math.pow(np.linalg.norm(end_point - start_point), 2)height, width, channel = image.shapefor i in range(width):for j in range(height):# 计算该点是否在形变圆的范围之内# 优化,第一步,直接判断是会在(start_point[0], start_point[1])的矩阵框中if math.fabs(i - start_point[0]) > radius and math.fabs(j - start_point[1]) > radius:continuedistance = (i - start_point[0]) * (i - start_point[0]) + (j - start_point[1]) * (j - start_point[1])if (distance < radius_square):# 计算出(i,j)坐标的原坐标# 计算公式中右边平方号里的部分ratio = (radius_square - distance) / (radius_square - distance + dist_se)ratio = ratio * ratio# 映射原位置new_x = i - ratio * (end_point[0] - start_point[0])new_y = j - ratio * (end_point[1] - start_point[1])new_x = new_x if new_x >=0 else 0new_x = new_x if new_x < height-1 else height-2new_y = new_y if new_y >= 0 else 0new_y = new_y if new_y < width-1 else width-2# 根据双线性插值法得到new_x, new_y的值image_cp[j, i] = bilinear_insert(image, new_x, new_y)return image_cpdef bilinear_insert(image, new_x, new_y):"""双线性插值法"""w, h, c = image.shapeif c == 3:x1 = int(new_x)x2 = x1 + 1y1 = int(new_y)y2 = y1 + 1part1 = image[y1, x1].astype(np.float) * (float(x2) - new_x) * (float(y2) - new_y)part2 = image[y1, x2].astype(np.float) * (new_x - float(x1)) * (float(y2) - new_y)part3 = image[y2, x1].astype(np.float) * (float(x2) - new_x) * (new_y - float(y1))part4 = image[y2, x2].astype(np.float) * (new_x - float(x1)) * (new_y - float(y1))insertValue = part1 + part2 + part3 + part4return insertValue.astype(np.int8)

#美颜瘦脸face_landmark = np.array(result_point[0]['data'][0], dtype='int')src_img = thin_face(src_img, face_landmark)res_img_path = 'data/obm_res.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()

瘦脸效果不明显,技术有待提升,新手,勿怪!

总之经过三周的学习,感觉还是收获满满。

具体可见:/aistudio/projectdetail/780408

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。