window10安装insightface、onnxruntime-gpu、视频换脸

1. https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements

2.下载cuda_12.2.0_536.25_windows

3.下载cudnn cudnn-windows-x86_64-8.5.0.96_cuda11-archive

4.conda create -n onnx_test python=3.10

5.根据官方文档说明cuda12.x的版本安装onnx采用如下方式 。

pip install onnxruntime-gpu --extra-index-url  https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-12/pypi/simple/

6.提示错误:Could not locate zlibwapi.dll. Please make sure it is in your library path!

https://www.winimage.com/zLibDll/ 下载

附:

Package                Version
---------------------- -----------
albumentations         1.4.4
annotated-types        0.6.0
certifi                2024.2.2
charset-normalizer     3.3.2
cmake                  3.29.2
colorama               0.4.6
coloredlogs            15.0.1
contourpy              1.2.1
cycler                 0.12.1
Cython                 3.0.10
easydict               1.13
flatbuffers            24.3.25
fonttools              4.51.0
humanfriendly          10.0
idna                   3.7
imageio                2.34.1
insightface            0.7.3
joblib                 1.4.0
kiwisolver             1.4.5
lazy_loader            0.4
matplotlib             3.8.4
mpmath                 1.3.0
networkx               3.3
numpy                  1.26.4
onnx                   1.16.0
onnxruntime-gpu        1.17.1
opencv-python          4.9.0.80
opencv-python-headless 4.9.0.80
packaging              24.0
pillow                 10.3.0
pip                    24.0
prettytable            3.10.0
protobuf               5.26.1
pydantic               2.7.0
pydantic_core          2.18.1
pyparsing              3.1.2
pyreadline3            3.4.1
python-dateutil        2.9.0.post0
PyYAML                 6.0.1
requests               2.31.0
scikit-image           0.23.2
scikit-learn           1.4.2
scipy                  1.13.0
setuptools             69.5.1
six                    1.16.0
sympy                  1.12
threadpoolctl          3.4.0
tifffile               2024.4.18
tqdm                   4.66.2
typing_extensions      4.11.0
urllib3                2.2.1
wcwidth                0.2.13
wheel                  0.43.0

附代码:

import os.path as osp
import os
import cv2
import insightface
from insightface.app import FaceAnalysis
import time
import argparse
import numpy as np

assert insightface.__version__ >= '0.7'


def count_files_in_directory(directory):
    file_count = 0
    for file in os.listdir(directory):
        file_path = os.path.join(directory, file)
        if os.path.isfile(file_path):
            file_count += 1
    return file_count


def load_fit_image(img, resize=False):
    if resize == True:
        w, h, _ = img.shape
        if w > h:
            rate = 512 / w
        else:
            rate = 512 / h

        w = int(w * rate)
        h = int(h * rate)

        img = cv2.resize(img, (h, w), interpolation=cv2.INTER_CUBIC)

    return img


def get_my_image(image_file, resize=True):
    if osp.exists(image_file):
        image_file = image_file
    else:
        assert image_file is not None, '%s not found' % image_file
    img = cv2.imread(image_file)
    if resize is True:
        return load_fit_image(img)
    return img


if __name__ == '__main__':
    start = time.time()

    # os.environ['CUDA_VISIBLE_DEVICES'] = '2, 3'
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '--swap_to',
        type=str,
        default='',
        help="要被替换的图片路径"
    )

    parser.add_argument(
        '--swap_face',
        type=str,
        default='',
        help="脸的路径"
    )

    parser.add_argument(
        '--swap_fps',
        type=str,
        default='25',
        help="帧率"
    )

    parser.add_argument(
        '--swap_check',
        type=str,
        default='',
        help="将此图片的脸替换掉"
    )

    args = parser.parse_args()

    if args.swap_to == "":
        raise "--swap_to参数不能为空"

    src_dir = args.swap_to + os.sep + 'src' + os.sep
    dst_dir = args.swap_to + os.sep + 'dst' + os.sep

    if not osp.exists(src_dir):
        os.system("mkdir " + src_dir)
    if not osp.exists(dst_dir):
        os.system("mkdir " + dst_dir)

    src_mp4 = args.swap_to + os.sep + "src.mp4"
    src_mp3 = args.swap_to + os.sep + "audio.mp3"
    output_mp4 = args.swap_to + os.sep + "output.mp4"
    merge_mp4 =  args.swap_to + os.sep + "merge.mp4"
    if not os.path.exists(src_mp4):
        src_mp4 = args.swap_to + os.sep + "src.mp4"

    dst_mp4 = args.swap_to + os.sep + "dst.mp4"

    if args.swap_check == "":
        check_image = args.swap_to + os.sep + "check.jpg"
        if not os.path.exists(check_image):
            check_image = args.swap_to + os.sep + "check.png"
        if not os.path.exists(check_image):
            check_image = args.swap_to + os.sep + "check.jpeg"
        #不存在参考图片,默认目标所有视频中的人脸都换成一个人
        if not os.path.exists(check_image):
           check_image = ""
    else:
        check_image = args.swap_check

    if not osp.exists(src_mp4):
        print(src_mp4)
        raise src_mp4 + " not found"

    if count_files_in_directory(src_dir) < 5:
        command_cut = "ffmpeg -i " + src_mp4 + " -r " + args.swap_fps + " " + src_dir + "%08d.png"
        print(command_cut)
        os.system(command_cut)

    app = FaceAnalysis(name='buffalo_l',root="./")
    app.prepare(ctx_id=0, det_size=(640, 640))

    swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)

    for root, dirs, files in os.walk(src_dir):
        for file in files:
            file_path = os.path.join(root, file)
            file_name, file_extension = os.path.splitext(file)
            if file_extension.lower() in ['.jpeg', '.jpg', '.png']:
                # print(src_dir + file)
                print(dst_dir + file)

                if not osp.exists(dst_dir + file):
                    #old face
                    img_src = get_my_image(src_dir + file)
                    face_src = app.get(img_src)

                    #new face
                    img_dst = get_my_image(args.swap_face)
                    face_dst = app.get(img_dst)

                    res = img_src.copy()
                    faces = []

                    if check_image != "":
                        img_check = get_my_image(check_image)
                        faces_check = app.get(img_check)

                        for face in face_src:
                            feat1 = face.embedding
                            feat2 = faces_check[0].embedding
                            sim = np.linalg.norm(feat1 - feat2)
                            faces.append(sim)

                        if len(faces) > 0:
                            min_index = faces.index(min(faces))
                            if face_src[min_index].sex == 'F':
                                res = swapper.get(res, face_src[min_index], face_dst[0], paste_back=True)

                    else:
                        for face in face_src:
                            res = swapper.get(res, face, face_dst[0], paste_back=True)

                    cv2.imwrite(dst_dir + file, res)


    if count_files_in_directory(dst_dir) > 100:
        command_merge = "ffmpeg -r " + args.swap_fps + " -i " + dst_dir + "%08d.png " + dst_mp4 + " -y"
        print(command_merge)
        os.system(command_merge)

        command_audio = "ffmpeg -i "+ src_mp4 +" -vn -f mp3 -ar 16000 -ac 1 " + src_mp3 + " -y"
        print(command_audio)
        os.system(command_audio)

        command_output = "ffmpeg -i " + dst_mp4 + " -i " + src_mp3 + " -c:v copy -c:a copy " + output_mp4 + " -y"
        print(command_output)
        os.system(command_output)

    end = time.time()
    print('CPU执行时间: ', end - start)
posted @   从雍和宫走到电影学院  阅读(846)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示