利用 mediapipe 進行處理
規劃
1.先把人臉辨識,然後取出框框
2.把框框內的人臉,進行美容
-高反差保留
(1)曝光度調整
(2)綠色與藍色,疊加
(3)YUCIHighPassSkinSmoothingMaskBoost
-調整圖像亮度
-混合
3.把人臉的嘴巴,進行塗紅
4.把人臉的眼睛塗黑
把人臉抓下來處理後,貼回去
如何將人臉變漂亮(一)是針對 mediapipe.solutions.face_detection 劃出框的,因為後面要在畫上 口紅,使人變漂亮,因此 改用 mediapipe.solutions.face_mesh
import cv2
import mediapipe
from numpy import greater
import matplotlib.pyplot as plt
img = cv2.imread("./images/person.jpg")
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 = y - int(h/4)
hh = h + int(h/4)
xx=mesh_coords[234][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, 255, 255), thickness = 5)
plt.figure(figsize=(10,10))
plt.subplot(121)
plt.imshow(img[:,:,::-1])
plt.axis('off')