insightface 换脸

复制代码
import datetime
import numpy as np
import os
import os.path as osp
import glob
import cv2
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image


assert insightface.__version__>='0.7'

if __name__ == '__main__':
    初始化
    app = FaceAnalysis(name='buffalo_l')
    app.prepare(ctx_id=0, det_size=(640, 640))
    swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)


    img = ins_get_image('t1')
    faces = app.get(img)
    faces = sorted(faces, key = lambda x : x.bbox[0])
    assert len(faces)==6
    source_face = faces[2]
    res = img.copy()
    for face in faces:
        res = swapper.get(res, face, source_face, paste_back=True)
    cv2.imwrite("./t1_swapped.jpg", res)
    res = []
    for face in faces:
        _img, _ = swapper.get(img, face, source_face, paste_back=False)
        res.append(_img)
    res = np.concatenate(res, axis=1)
    cv2.imwrite("./t1_swapped2.jpg", res)

参考:

https://github.com/deepinsight/insightface/tree/0cb03d391bb9335821448ce6e0519dbc870ec68d/examples/in_swapper

相关推荐
岁月宁静几秒前
四、《从零手撸 Agent》 — 流式输出:接住 AI “一个字一个字” 想出来的过程
python·agent
是吕先森4 分钟前
【python】selenium实现web自动化测试
前端·python·selenium
卷无止境11 分钟前
从脚本到程序:Windows平台上的Python打包全景图
后端·python
my059211 分钟前
市场学习,不止看资讯,数据查阅与思维练习同样重要
python·学习
黑科技工坊13 分钟前
2026年口碑载道:铝面板定制供应商优选指南
大数据·人工智能·python
ai小陈18 分钟前
PyTorch实验可复现实战:随机种子、依赖锁定与配置归档
人工智能·pytorch·python·深度学习·ai·gpu算力
郝学胜-神的一滴19 分钟前
C++11 工程级应用 08:Lambda表达式与Tuple元组
开发语言·jvm·c++·python·程序人生·开源
岁月宁静27 分钟前
三、《从零手撸 Agent》 · system prompt 与核心参数:调好你的旋钮
后端·python·agent
卷无止境30 分钟前
除了写代码,AI智能体还能帮开发者做什么
人工智能·python