如何將人臉變漂亮(二)

利用 mediapipe 進行處理
規劃
1.先把人臉辨識,然後取出框框
2.把框框內的人臉,進行美容
-高反差保留
(1)曝光度調整
(2)綠色與藍色,疊加
(3)YUCIHighPassSkinSmoothingMaskBoost
-調整圖像亮度
-混合
3.把人臉的嘴巴,進行塗紅
4.把人臉的眼睛塗黑

把圖片內的人臉變漂亮

import numpy as np
import cv2
from scipy.interpolate import CubicSpline

from PIL import Image
import matplotlib.pyplot as plt
import time
from PIL import ImageFilter

def np2pil(numpy_image):
    return Image.fromarray(np.uint8(numpy_image*255.0)).convert('RGB')

img = cv2.imread('./images/person2.jpg')

scale_percent = 50 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)   # resize image
img = cv2.resize(img, dim, interpolation = cv2.INTER_NEAREST)

timea = time.time()

input_img = np.array(img[...,::-1]/255.0,dtype=np.float32) # to rgb  變成0-1之間  [x,y,bgr] (2320, 3088, 3)
ea_img = input_img * pow(2,-1.0)  # 把畫面變黑 少一半

# YUCIGreenBlueChannelOverlayBlend  进行绿色和蓝色通道的混合叠加  ba_img
base = ea_img[...,1]   #
overlay = ea_img[...,2]  #
ba = 2.0*overlay*base   #
ba_img = np.zeros((ba.shape[0],ba.shape[1],3),dtype=np.float32)
ba_img[...,0] = ba
ba_img[...,1] = ba
ba_img[...,2] = ba

# YUCIHighPass  高通滤波YUCIHighPass的环节,非常简单,先高斯模糊一下子,然后跟原图做个混合  blur_img
# 先进行高斯模糊    
radius = int(np.ceil(7.0*input_img.shape[0]/750.0))
pil_img = np2pil(ba_img)
pil_blur = pil_img.filter(ImageFilter.GaussianBlur(radius))
blur_img = np.asarray(pil_blur,np.float32)/255.0

# 再进行YUCIHighPass    hp_img = ba_img - blur_img + 0.5
hp_img = ba_img - blur_img + 0.5

#紀錄時間  0.6
timeb = time.time()
print("hpss_img for blue green", timeb-timea)

# YUCIHighPassSkinSmoothingMaskBoost
hardLightColor = hp_img[...,2]
[x1,y1] = np.where(hardLightColor<0.5)
[x2,y2] = np.where(hardLightColor>=0.5)
for i in range(3):
    hardLightColor[x1,y1] = hardLightColor[x1,y1]*hardLightColor[x1,y1]*2.0
    hardLightColor[x2,y2] = 1.0 - (1.0 - hardLightColor[x2,y2]) * (1.0 - hardLightColor[x2,y2]) * 2.0
k = 255.0/(164.0-75.0);
hardLightColor = (hardLightColor - 75.0/255.0) * k
hpss_img = np.zeros((hardLightColor.shape[0],hardLightColor.shape[1],3))
hpss_img[...,0] = hardLightColor
hpss_img[...,1] = hardLightColor
hpss_img[...,2] = hardLightColor

hpss_img = np.clip(hpss_img,0,1)  #將所有數據在 0-1 之間

#紀錄時間 0.6
timeb = time.time()
print("hpss_img", timeb-timea)

# 1111調整亮度 tc_img
# RGB tone curve 
# 先利用控制点生成cubic spline曲线
# 参照https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.CubicSpline.html#scipy.interpolate.CubicSpline
x = [0,120.0/255.0,1]
y = [0,146.0/255.0,1]#146
cs = CubicSpline(x,y)
tc_img = cs(input_img) 

#紀錄時間
timeb = time.time()
print("tc_img", timeb-timea)

####  重要  ####
blend_img = input_img * hpss_img  + tc_img *(1-hpss_img)

#紀錄時間
timeb = time.time()
print("blend_img", timeb-timea)

# sharpen
from PIL import ImageEnhance
enhancer = ImageEnhance.Sharpness(np2pil(blend_img))
img_sharp = enhancer.enhance(2)
result = np.array(img_sharp,np.float32)/255.0

#紀錄時間
timeb = time.time()
print("result", timeb-timea)

plt.figure(figsize=(16,16))
plt.subplot(121)
plt.imshow(input_img)
plt.axis('off')
plt.subplot(122)
plt.imshow(result)
plt.axis('off')

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值