基于mediapipe的人脸关键点检测及嘴唇换色demo

 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()

运行结果:

 代码及课件链接

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值