人脸关键点检测及人脸分割

人脸关键点检测,输入opencv格式的bgr通道图,默认在输入图上画关键点,也可以在空白图上画,如果要分割出人脸,可以使用矩形框框住关键点,然后在原图上裁剪,下面是一个成功案例。

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import mediapipe as mp
import numpy as np
# 输入bgr通道图,输出人脸网点图
# mode=1在原图上画,mode=2在空白图上画
def get_face(image,mode=1):
mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1, color=(0, 255, 0))#bgr
with mp_face_mesh.FaceMesh(
max_num_faces=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as face_mesh:
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
results = face_mesh.process(image)

# Draw the face mesh annotations on the image.
# and print landmarks' id, x, y, z
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if mode==1:
mask=image

elif mode==2:
#新建空白图
mask=np.zeros(image.shape,dtype=np.uint8)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
# Draw landmarks on the image.
mp_drawing.draw_landmarks(
image=mask,#在空白图上画
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=drawing_spec,
connection_drawing_spec=drawing_spec)
# print id, x, y, z
# time cost
if mode==2:
xs=[]
ys=[]
zs=[]
for id,lm in enumerate(face_landmarks.landmark):
ih, iw, ic = image.shape
x,y = int(lm.x*iw), int(lm.y*ih)
xs.append(x)
ys.append(y)
zs.append(lm.z)
# print(id, x,y,lm.z)
#用矩形框住这些点
box=cv2.boundingRect(np.array([xs,ys]).T)
#裁剪原图
face=mask[box[1]:box[1]+box[3],box[0]:box[0]+box[2]]
#保持比例缩放到128*128
return face
elif mode==1:
return mask
#没有人脸
if mode==1:
return image
elif mode==2:
return np.zeros(image.shape,dtype=np.uint8)

人脸关键点检测及人脸分割
https://xinhaojin.github.io/2023/03/23/人脸关键点检测及人脸分割/
作者
xinhaojin
发布于
2023年3月23日
许可协议