live2d 抠人脸

Peppa_Pig_Face_Landmark

python 复制代码
import glob

import cv2
import time
import numpy as np
import os
import argparse


from Skps import FaceAna
def video(video_path_or_cam):
    facer = FaceAna()
    vide_capture=cv2.VideoCapture(video_path_or_cam)

    while 1:
        ret, image = vide_capture.read()
        if ret:
            pattern = np.zeros_like(image)
            img_show = image.copy()
            star=time.time()
            result = facer.run(image)
            duration=time.time()-star
            #print('one iamge cost %f s'%(duration))
            fps=1/duration
            cv2.putText(img_show, "X: " + "{:7.2f}".format(fps), (20, 20), cv2.FONT_HERSHEY_SIMPLEX,
                        0.75, (0, 0, 0), thickness=2)
            for face_index in range(len(result)):
                cur_face_kps=result[face_index]['kps']
                cur_face_kps_score=result[face_index]['scores']
                for landmarks_index in range(cur_face_kps.shape[0]):
                    x_y = cur_face_kps[landmarks_index]
                    score=cur_face_kps_score[landmarks_index]
                    # color = (255, 255, 255)
                    if score>0.8:
                        color=(255,255,255)
                    else:
                        color = (0, 0, 255)
                    cv2.circle(img_show, (int(x_y[0]), int(x_y[1])),
                                   color=color, radius=1, thickness=2)
            cv2.namedWindow("capture", 0)
            cv2.imshow("capture", img_show)
            if args.mask:
                cv2.namedWindow("masked", 0)
                cv2.imshow("masked", image*pattern)

            key=cv2.waitKey(1)
            if key==ord('q'):
                return

def save_roi_as_png(mask, landmarks, indices,expand_ratio=1.0):

    pts = landmarks[indices].astype(np.int32)
    x_min = np.min(pts[:, 0])
    x_max = np.max(pts[:, 0])
    y_min = np.min(pts[:, 1])
    y_max = np.max(pts[:, 1])
    
    H, W = mask.shape[:2]
    # 矩形中心
    cx = (x_min + x_max) // 2
    cy = (y_min + y_max) // 2

    # 边长取矩形最大边,扩展 expand_ratio
    side = int(max(x_max - x_min, y_max - y_min) * expand_ratio / 2)

    # 正方形边界
    x1 = max(cx - side, 0)
    x2 = min(cx + side, W - 1)
    y1 = max(cy - side, 0)
    y2 = min(cy + side, H - 1)

    # 填充 mask
    cv2.rectangle(mask, (x1, y1), (x2, y2), 255, -1)
    
    return mask
def images(image_dir):
    facer = FaceAna()
    
    # image_list=glob.glob(image_dir+'/*.jpg')
    image_list=glob.glob(r"B:\360MoveData\Users\Administrator\Desktop\tmp\a123.jpeg")

    for image_name in image_list:

        image=cv2.imread(os.path.join(image_dir,image_name))
        pattern = np.zeros_like(image)
        img_show = image.copy()

        star=time.time()
        result = facer.run(image)
        ###no track
        facer.reset()
        duration=time.time()-star
        print('one iamge cost %f s'%(duration))
        for face_index in range(len(result)):

            cur_face_kps = result[face_index]['kps']
            cur_face_kps_score = result[face_index]['scores']
            
            left_eye_idx = np.arange(60, 68)
            right_eye_idx = np.arange(68, 76)
            mouth_idx = np.arange(76, 96)
            jaw_low_idx = np.arange(8, 11)
            
            
            H, W = img_show.shape[:2]
            
            # 创建黑色 mask
            mask = np.zeros((H, W), dtype=np.uint8)
            # ----- 保存区域 -----
            mask = save_roi_as_png(mask, cur_face_kps, left_eye_idx)
            mask = save_roi_as_png(mask, cur_face_kps, right_eye_idx)
            
        
            pts = np.concatenate([cur_face_kps[mouth_idx], cur_face_kps[jaw_low_idx]]).astype(np.int32)
            x_min, y_min = np.min(pts, axis=0)
            x_max, y_max = np.max(pts, axis=0)
            cv2.rectangle(mask, (x_min, y_min), (x_max, y_max), 255, -1)
            
            cv2.imwrite('mask1.png',mask)
            
            for landmarks_index in range(cur_face_kps.shape[0]):

                x_y = cur_face_kps[landmarks_index]
                score = cur_face_kps_score[landmarks_index]
                # color = (255, 255, 255)
                if score > 0.8:
                    color = (255, 0, 0)
                else:
                    color = (0, 0, 255)
                cv2.circle(img_show, (int(x_y[0]), int(x_y[1])),
                           color=color, radius=1, thickness=1)

        # cv2.namedWindow("capture", 0)
        cv2.imshow("capture", img_show)

        key=cv2.waitKey(0)
        if key==ord('q'):
            return

def build_argparse():
    parser = argparse.ArgumentParser(description='Start train.')
    parser.add_argument('--video', dest='video', type=str, default=None,  help='the camera id (default: 0)')
    parser.add_argument('--cam_id', dest='cam_id', type=int, default=0,help='the camera to use')
    parser.add_argument('--img_dir', dest='img_dir', type=str, default='aa',  help='the images dir to use')

    parser.add_argument('--mask', dest='mask', type=bool, default=False, help='mask the face or not')

    args = parser.parse_args()
    return  args

if __name__=='__main__':

    args=build_argparse()

    if args.img_dir is not None:
        images(args.img_dir)

    elif args.video is not None:
        video(args.video)
    else:
        video(args.cam_id)
相关推荐
墨染天姬2 小时前
【AI】端侧AIBOX可以部署哪些智能体
人工智能
AI成长日志2 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
2501_948114243 小时前
2026年大模型API聚合平台技术评测:企业级接入层的治理演进与星链4SAPI架构观察
大数据·人工智能·gpt·架构·claude
小小工匠3 小时前
LLM - awesome-design-md 从 DESIGN.md 到“可对话的设计系统”:用纯文本驱动 AI 生成一致 UI 的新范式
人工智能·ui
黎阳之光3 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
小超同学你好3 小时前
面向 LLM 的程序设计 6:Tool Calling 的完整生命周期——从定义、决策、执行到观测回注
人工智能·语言模型
智星云算力3 小时前
本地GPU与租用GPU混合部署:混合算力架构搭建指南
人工智能·架构·gpu算力·智星云·gpu租用
jinanwuhuaguo3 小时前
截止到4月8日,OpenClaw 2026年4月更新深度解读剖析:从“能力回归”到“信任内建”的范式跃迁
android·开发语言·人工智能·深度学习·kotlin
xiaozhazha_3 小时前
效率提升80%:2026年AI CRM与ERP深度集成的架构设计与实现
人工智能
枫叶林FYL3 小时前
【自然语言处理 NLP】7.2.2 安全性评估与Constitutional AI
人工智能·自然语言处理