import cv2
import mediapipe as mp
import numpy as np
import time

mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh

# 加载图片
image = cv2.imread('wlzc.jpg')  # 请将 'wlzc.jpg' 替换为实际的图片路径
image_height, image_width, _ = image.shape

# 初始化面部网格模型
with mp_face_mesh.FaceMesh(
        static_image_mode=False,
        max_num_faces=1,
        min_detection_confidence=0.5,
        min_tracking_confidence=0.5) as face_mesh:

    # 将图像转换为RGB格式
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # 处理图像
    results = face_mesh.process(image_rgb)

    if results.multi_face_landmarks:
        for face_landmarks in results.multi_face_landmarks:
            # 定义嘴部区域的关键点索引
            mouth_indices = [78, 191, 80, 81, 82, 13, 312, 311, 310, 415, 308, 324, 318, 402, 317, 14, 87, 178, 88, 95]

            # 提取嘴部关键点坐标
            mouth_landmarks = np.array([[int(landmark.x * image_width), int(landmark.y * image_height)]
                                        for idx, landmark in enumerate(face_landmarks.landmark) if idx in mouth_indices])

            # 模拟嘴开合逻辑
            open_interval = 3  # 张嘴间隔时间(秒)
            open_duration = 0.5  # 张嘴持续时间(秒)
            last_open_time = time.time()
            is_opening = False
            open_start_time = 0

            while True:
                current_time = time.time()
                # 绘制嘴部关键点
                cv2.polylines(image, [mouth_landmarks], isClosed=True, color=(0, 255, 0), thickness=2)

                # 模拟嘴开合
                if current_time - last_open_time > open_interval and not is_opening:
                    is_opening = True
                    open_start_time = current_time
                elif is_opening and current_time - open_start_time > open_duration:
                    is_opening = False
                    last_open_time = current_time
                if is_opening:
                    # 这里简单地填充嘴部区域来模拟张嘴效果
                    cv2.fillPoly(image, [mouth_landmarks], (0, 0, 0))

                cv2.imshow('Moving Mouth', image)
                if cv2.waitKey(1) & 0xFF == 27:  # 按下Esc键退出
                    break

cv2.destroyAllWindows()

 

往期源码

眼镜眨巴眨巴-一步几个脚印从头设计数字生命2——仙盟创梦IDE-CSDN博客 

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐