嘴巴动起来-一步几个脚印从头设计数字生命3——仙盟创梦IDE

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博客 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值