使用PaddleHub给头像添加圣诞帽

每到圣诞节的时候大家都会给自己的头像戴上一个圣诞帽,这一般会用到PS,但是如果有的人不会PS怎么办呢,快来用python一件添加吧!(如果python也不会怎么办(+_+)?)
gitee项目地址

1.完整代码

import matplotlib.pyplot as plt 
import matplotlib.image as mpimg

path_person = 'data/person.jpg'
path_hat = 'data/hat.jpg'

# 展示待处理图片
img1 = mpimg.imread(path_person)
plt.axis('off') 
plt.imshow(img1)
plt.show()

# 加载预训练模型
import paddlehub as hub
model = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_320")

input_dict = {'image': [path_person]}

results = model.face_detection(data=input_dict, visualization=True)
for result in results:
    print(result)

# 预测结果展示
img = mpimg.imread("face_detector_320_predict_output/person.jpg")
plt.figure(figsize=(10,10))
plt.imshow(img) 
plt.axis('off') 
plt.show()

# 取出人脸关键点坐标
left, right, top, bottom = int(result['data'][0]['left']), int(result['data'][0]['right']),int(result['data'][0]['top']), int(result['data'][0]['bottom'])

import cv2
hat=cv2.imread(path_hat)
# 用cv2.imread()读到的图像,是BGR三通道图像,可以用cvtColor()函数转换一下
hat_rgb=cv2.cvtColor(hat,cv2.COLOR_BGR2RGB)
plt.imshow(hat_rgb)
plt.show()

# 对帽子进行缩放
hat_h, hat_w, _ = hat.shape
face_w = right - left

factor = 1.5 #用来调整帽子的大小
ratio = face_w/hat_w
resized_hat_h = int(round(hat_h*ratio*factor))
resized_hat_w = int(round(hat_w*ratio*factor))
print(resized_hat_h, resized_hat_w)
resized_hat = cv2.resize(hat,(resized_hat_w,resized_hat_h))

# 读取人物照片
person = cv2.imread(path_person)
person_rgb=cv2.cvtColor(person,cv2.COLOR_BGR2RGB)

# 对帽子进行二值化,确定掩膜
hat2gray = cv2.cvtColor(resized_hat, cv2.COLOR_BGR2GRAY)  # 转换为灰度图像
ret, mask = cv2.threshold(hat2gray, 250, 255, cv2.THRESH_BINARY)  # 设置阈值,大于175的置为255,小于175的置为0
mask_inv = cv2.bitwise_not(mask)

# 确定帽子放置位置
dw = -50
dh = 10
mid_axis = int((right-left)/2)
roi = person[(top-resized_hat_h+dh):(top+dh), left+dw:left+resized_hat_w+dw]
print(roi.shape)
person_bg = cv2.bitwise_and(roi, roi, mask=mask)  #删除了ROI中的logo区域
hat_fg = cv2.bitwise_and(resized_hat, resized_hat, mask=mask_inv) #删除了logo中的空白区域

dst = cv2.add(person_bg, hat_fg)
person[(top-resized_hat_h+dh):(top+dh), left+dw:left+resized_hat_w+dw] = dst
person_rgb=cv2.cvtColor(person,cv2.COLOR_BGR2RGB)
plt.imshow(person_rgb)
plt.show()

结果展示:
运行结果

2.代码详解

这里主要要讲的就是PaddleHub的face_detection函数,它的输出是一个列表,列表里只有一个对象,可以用for循环取出来,这个对象打印下来如下所示:

{'data': [{'left': 313.9062805175781, 'right': 547.9287109375, 'top': 496.75042724609375, 'bottom': 801.8109130859375,
 'confidence': 0.9980539083480835}], 'path': 'data/person.jpg', 'save_path': 'face_detector_320_predict_output/person.jpg'}

里面会有四个参数leftrighttopbottom。如果没有接触过图像处理可能不太清楚它所表示的含义,看下面的图像可以方便大家理解(图片来源):
在这里插入图片描述
在这里插入图片描述
这四个数表示头像检测框的位置,了解PaddleHub的返回值含义了之后就好办了,我们可以对圣诞帽进行处理。

圣诞帽处理的思想是:

  • 以头像检测框的宽为参照对圣诞帽进行等比例缩放,圣诞帽的宽和人脸检测框的宽的比例可以自己定义,这里我定义的是1:1;
  • 将圣诞帽放置在人脸检测框的正上方,这里用到了掩膜操作,关于掩膜操作的详细解释参照我上一篇博客,介绍的非常详细;
  • 由于人脸角度的差异需要对圣诞帽进行细微调整,在确定圣诞帽放置位置时引入了 d w dw dw d h dh dh偏差量;
  • 生成最终头像,完成。

3.总结

  • 在进行掩膜操作的时候我用到的是转化为灰度图的方法,这样做的弊端是圣诞帽有白色的部分,灰度阈值设置不当可能会造成圣诞帽掩膜制作不准确;
  • 博客用到的是alpha通道法,该方法利用透明背景的圣诞帽头像可以很准确的抠出圣诞帽,不用担心阈值的问题,不过关于alpha通道的使用我当时没有看懂,感兴趣可以前往学习
  • 当时由于时间仓促没有考虑到人脸较高,添加圣诞帽后圣诞帽可能会超出图像边界的问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值