利用 mediapipe 進行處理
規劃
1.先把人臉辨識,然後取出框框
2.把框框內的人臉,進行美容
-高反差保留
(1)曝光度調整
(2)綠色與藍色,疊加
(3)YUCIHighPassSkinSmoothingMaskBoost
-調整圖像亮度
-混合
3.把人臉的嘴巴,進行塗紅
4.把人臉的眼睛塗黑
把人臉取出來,然後變漂亮,再貼回去,順便看看處理時間,以利後來用video時改進。
import cv2
import mediapipe
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from PIL import ImageFilter
from scipy.interpolate import CubicSpline
import time
img = cv2.imread("./images/person.jpg")
img_o = np.array(img,np.float32)/255.0
start_time = time.time() #紀錄
def landmarksDetection(img, results, draw=False):
img_height, img_width= img.shape[:2]
# list[(x,y), (x,y)....]
mesh_coord = [(int(point.x * img_width), int(point.y * img_height)) for point in results.multi_face_landmarks[0].landmark]
if draw :
[cv2.circle(img, p, 2, utils.GREEN, -1) for p in mesh_coord]
# returning the list of tuples for each landmarks
return mesh_coord
def np2pil(numpy_image):
return Image.fromarray(np.uint8(numpy_image*255.0)).convert('RGB')
mp_face_mesh = mediapipe.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(max_num_faces=1,min_detection_confidence=0.5,min_tracking_confidence=0.5)
results = face_mesh.process(img)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
mesh_coords = landmarksDetection(img, results, False)
print("...:",mesh_coords[10],mesh_coords[152],mesh_coords[234],mesh_coords[454])
# img =utils.fillPolyTrans(img, [mesh_coords[10],mesh_coords[152],mesh_coords[234],mesh_coords[454]], utils.RED, opacity=0.7)
#img=cv2.line(img, mesh_coords[10],mesh_coords[152],(255,0,0),10)
# img=cv2.line(img, mesh_coords[234],mesh_coords[454],(255,0,0),10)
y=mesh_coords[10][1]
h=abs(mesh_coords[10][1]-mesh_coords[152][1])
yy = np.maximum((y - int(h/4)),0) #np.maximun 主要是用在video時,畫面跑出外框,xx,yy值為負
hh = h + int(h/4)
xx = np.maximum(mesh_coords[234][0],0)
ww = abs(mesh_coords[454][0]-mesh_coords[234][0])
#print("xx:",x,w)
#print("yy:",y,h,yy,hh)
cv2.rectangle(img, (xx, yy), (xx + ww, yy + hh), (255, 0, 0), thickness = 10)
img_face=img[yy:yy+hh,xx:xx+ww]
input_img = np.array(img_face/255.0,dtype=np.float32)
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
# 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 之間
# 先利用控制点生成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)
#### 重要 ####
blend_img = input_img * hpss_img + tc_img *(1-hpss_img)
# sharpen
from PIL import ImageEnhance
enhancer = ImageEnhance.Sharpness(np2pil(blend_img))
img_sharp = enhancer.enhance(2)
result1 = np.array(img_sharp,np.float32)/255.0
img_o[yy:yy+hh,xx:xx+ww] = result1
end_time = time.time()-start_time
print("time:",end_time)
plt.figure(figsize=(16,16))
plt.subplot(121)
plt.imshow(img[:,:,::-1])
plt.axis('off')
plt.subplot(122)
plt.imshow(img_o[:,:,::-1])
plt.axis('off')
花了 0.32秒