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

相关推荐
yongche_shi几秒前
ragas官方文档中文版(十六)
python·ai·智能体·ragas·使用工具
三块可乐两块冰4 分钟前
rag学习5
linux·前端·python
DXM05218 分钟前
第11期| 遥感图像分类模型:ResNet_DenseNet原理+实战训练
人工智能·python·深度学习·机器学习·分类·数据挖掘·ageo
SilentSamsara11 分钟前
模型部署实战:FastAPI + ONNX + Docker 的推理服务化
人工智能·pytorch·python·深度学习·机器学习·fastapi
聆风吟º15 分钟前
Python基础数据类型(一):数字类型
开发语言·python·float·int·bool·数字类型
Tisfy16 分钟前
LeetCode 3838.带权单词映射:求和、取模、拼接(附python一行版)
python·算法·leetcode·字符串·题解·模拟·取模
NaclarbCSDN22 分钟前
我写了一个命令行书签管理器,然后抛弃了浏览器书签栏
linux·git·python·github
沪漂阿龙1 小时前
LangChain 系列之Tools:让大模型真正连接业务系统
人工智能·python·langchain
程序猿编码1 小时前
子域猎手:一款高性能DNS枚举工具的设计与实现
linux·c++·python·c·dns
copyer_xyf2 小时前
Agent MCP
后端·python·agent