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

相关推荐
Ulyanov16 分钟前
Python实现6-DOF刚体仿真器(下)——环境扰动与控制闭环
开发语言·python·算法·系统仿真·雷达电子对抗·导引头
小小的木头人35 分钟前
Python 批量解析 Excel 经纬度,调用高德地图 API 获取中文地址
开发语言·python·excel
金銀銅鐵1 小时前
[Python] 为 Vole 机器语言实现图形化界面
python·程序员
小林ixn1 小时前
Python基础全梳理:从注释到函数,这些细节你都掌握了吗?
python·编程语言
今天AI了吗2 小时前
Hermes Agent 搭建全流程:从本机试跑到可持续运行的个人 AI Agent
java·人工智能·python·学习·embedding
为啥全要学3 小时前
自注意力中随着根号dk的增大,qk点积的方差为什么也会增大
python·深度学习
weipt3 小时前
用Ollama开发聊天程序实战
python·ollama
来两个炸鸡腿4 小时前
【Datawhale2607】llm-algo-leetcode task02 基础算子
人工智能·python·大模型
庵中十三居士4 小时前
【纯AI无人工修改】AI Agent从0到1实战:50行Python手写核心循环,一次看懂所有Agent框架的底层逻辑
开发语言·人工智能·python
有同事要进步4 小时前
python常见问题--2
开发语言·python