Face-mesh(人脸关键点检测及网格绘制)
import mediapipe as mp
import numpy as np
import cv2
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, # False处理视频,True处理单张图片
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) # 静态图片不用设置
# 构建绘图对象
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
# 开启摄像头
cap = cv2.VideoCapture(0)
while True:
# 读取一帧图像
ret, img = cap.read()
height, width, channels = np.shape(img)
img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 特征点提取
results = face_mesh.process(img_RGB)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
# 绘制人脸网格
mp_drawing.draw_landmarks(image=img,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style())
# 绘制人脸轮廓
mp_drawing.draw_landmarks(image=img,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_contours_style())
# 绘制瞳孔轮廓
mp_drawing.draw_landmarks(image=img,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_IRISES,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_iris_connections_style())
# 绘制人脸关键点
# if face_landmarks:
# for i in range(478):
# pos_x = int(face_landmarks.landmark[i].x * width)
# pos_y = int(face_landmarks.landmark[i].y * height)
# cv2.circle(img, (pos_x, pos_y), 3, (0, 255, 0), -1)
cv2.imshow('faces', img)
key = cv2.waitKey(1)
if key == ord('q'):
break
cap.release()
运行结果:
嘴唇换色demo
def empty(a):
pass
def change_color_lip(img, list_lms, index_lip_up, index_lip_down, color):
mask = np.zeros_like(img)
points_lip_up = list_lms[index_lip_up,:]
mask = cv2.fillPoly(mask, [points_lip_up], (255, 255, 255))
points_lip_down = list_lms[index_lip_down,:]
mask = cv2.fillPoly(mask, [points_lip_down], (255, 255, 255))
img_color_lip = np.zeros_like(img)
img_color_lip[:] = color
img_color_lip = cv2.bitwise_and(mask, img_color_lip)
img_color_lip = cv2.GaussianBlur(img_color_lip, (7, 7), 10)
img_color_lip = cv2.addWeighted(img, 1, img_color_lip, 0.8, 0)
return img_color_lip
import mediapipe as mp
import numpy as np
import cv2
from utils import *
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, # False处理视频,True处理单张图片
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) # 静态图片不用设置
# 构建绘图对象
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
cv2.namedWindow("BGR")
cv2.resizeWindow("BGR", 640, 240)
cv2.createTrackbar("Blue", "BGR", 0, 255, empty)
cv2.createTrackbar("Green", "BGR", 0, 255, empty)
cv2.createTrackbar("Red", "BGR", 0, 255, empty)
# 开启摄像头
cap = cv2.VideoCapture(0)
while True:
# 读取一帧图像
ret, img = cap.read()
height, width, channels = np.shape(img)
img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 特征点提取
results = face_mesh.process(img_RGB)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
# # 绘制人脸网格
# mp_drawing.draw_landmarks(image=img,
# landmark_list=face_landmarks,
# connections=mp_face_mesh.FACEMESH_TESSELATION,
# landmark_drawing_spec=None,
# connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style())
# # 绘制人脸轮廓
# mp_drawing.draw_landmarks(image=img,
# landmark_list=face_landmarks,
# connections=mp_face_mesh.FACEMESH_CONTOURS,
# landmark_drawing_spec=None,
# connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_contours_style())
# # 绘制瞳孔轮廓
# mp_drawing.draw_landmarks(image=img,
# landmark_list=face_landmarks,
# connections=mp_face_mesh.FACEMESH_IRISES,
# landmark_drawing_spec=None,
# connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_iris_connections_style())
# 绘制人脸关键点
face_landmarks = results.multi_face_landmarks[0]
list_lms = []
if face_landmarks:
for i in range(478):
pos_x = int(face_landmarks.landmark[i].x * width)
pos_y = int(face_landmarks.landmark[i].y * height)
list_lms.append([pos_x, pos_y])
#cv2.circle(img, (pos_x, pos_y), 3, (0, 255, 0), -1)
list_lms = np.array(list_lms, dtype=np.int32)
index_lip_up = [61, 185, 40, 39, 37, 0, 267, 269, 270, 409, 291, 308, 415, 310, 311, 312, 13, 82, 80, 191, 78, 61]
index_lip_down = [78, 95, 88, 178, 87, 14, 317, 402, 318, 324, 308, 291, 375, 321, 405, 314, 17, 84, 181, 91, 146, 61, 78]
b = cv2.getTrackbarPos("Blue", "BGR")
g = cv2.getTrackbarPos("Green", "BGR")
r = cv2.getTrackbarPos("Red", "BGR")
color = (b, g, r)
# img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)
img = change_color_lip(img, list_lms, index_lip_up, index_lip_down, color)
cv2.imshow("BGR", img)
key = cv2.waitKey(1)
if key == ord('q'):
break
cap.release()
运行结果: