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

相关推荐
小陈工12 分钟前
2026年3月31日技术资讯洞察:AI智能体安全、异步编程突破与Python运行时演进
开发语言·jvm·数据库·人工智能·python·安全·oracle
老李的勺子1 小时前
Agent 记忆失效的 5 种方式:完整排查复盘
python·llm
Leo655351 小时前
动态透视报表 + 查询接口 + Excel导出
开发语言·windows·python
清水白石0081 小时前
pytest Fixture 设计实战指南:作用域、依赖链、自动清理与测试资源高效复用
python·pytest
tottoramen2 小时前
如何安装龙虾
python
QC·Rex2 小时前
AI Agent 任务规划实战:从 ReAct 到 Plan-and-Solve 的完整指南
人工智能·python·react
kcuwu.3 小时前
Python面向对象:封装、继承、多态
开发语言·python
YuanDaima20483 小时前
LangChain基础配置与对话模型实战
人工智能·python·langchain·大模型·智能体·langgraph
河西石头3 小时前
分享python项目与开源python项目中的效率法宝--requirements文件的使用
开发语言·python·requirements文件·批量安装python依赖·python虚拟环境配置
不懒不懒3 小时前
【卷积神经网络作业实现人脸的关键点定位功能】
开发语言·python