AI换脸:FaceFusion的安装和使用

FaceFusion是一个AI换脸的项目,安装和使用都非常简单。

首先,需要克隆facefusion工程到本地:

$ git clone https://github.com/facefusion/facefusion.git

创建一个虚拟环境用于facefusion的依赖库安装,这里以anaconda为例,创建一个facefusion环境并激活,安装pip:

$ conda create -n facefusion
$ conda activate facefusion
$ conda install pip

我之前设置了清华的pip源,安装facefusion依赖有些问题,去掉了镜像源:

$ pip config list
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
$ pip config unset global.index-url

镜像源有时会与官方仓库存在不同步,产生一些库的版本不满足依赖或是缺库的情况,不过大部分情况下都是OK的。去掉镜像源后,会直接从pypi仓库下载python库,众所周知pythonhosted的连接不是很顺畅,需要耐心或者魔法。

安装facefusion需要的依赖

$ pip install -r requirements.txt

这一步会安装很多依赖库,需要耐心等待,一般来说网络没有问题的话都能成功,我这里最终安装了这些包:

Successfully installed PySocks-1.7.1 PyWavelets-1.4.1 absl-py-2.0.0 addict-2.4.0 aiofiles-23.2.1 albumentations-1.3.1 altair-5.1.1 anyio-3.7.1 astunparse-1.6.3 attrs-23.1.0 basicsr-1.4.2 beautifulsoup4-4.12.2 cachetools-5.3.1 certifi-2023.7.22 charset-normalizer-3.2.0 click-8.1.7 coloredlogs-15.0.1 contourpy-1.1.1 cycler-0.12.0 cython-3.0.2 easydict-1.10 facexlib-0.3.0 fastapi-0.103.2 ffmpy-0.3.1 filelock-3.12.4 filterpy-1.4.5 flatbuffers-23.5.26 fonttools-4.42.1 fsspec-2023.9.2 future-0.18.3 gast-0.4.0 gdown-4.7.1 gfpgan-1.3.8 google-auth-2.23.2 google-auth-oauthlib-1.0.0 google-pasta-0.2.0 gradio-3.44.3 gradio-client-0.5.0 grpcio-1.58.0 h11-0.14.0 h5py-3.9.0 httpcore-0.18.0 httpx-0.25.0 huggingface-hub-0.17.3 humanfriendly-10.0 idna-3.4 imageio-2.31.4 importlib-metadata-6.8.0 importlib-resources-6.1.0 insightface-0.7.3 jinja2-3.1.2 joblib-1.3.2 jsonschema-4.19.1 jsonschema-specifications-2023.7.1 keras-2.13.1 kiwisolver-1.4.5 lazy_loader-0.3 libclang-16.0.6 llvmlite-0.41.0 lmdb-1.4.1 markdown-3.4.4 markupsafe-2.1.3 matplotlib-3.8.0 mpmath-1.3.0 networkx-3.1 numba-0.58.0 numpy-1.24.3 oauthlib-3.2.2 onnx-1.14.1 onnxruntime-1.15.1 opencv-python-4.8.0.76 opencv-python-headless-4.8.1.78 opennsfw2-0.10.2 opt-einsum-3.3.0 orjson-3.9.7 packaging-23.1 pandas-2.1.1 pillow-10.0.1 platformdirs-3.10.0 prettytable-3.9.0 protobuf-4.24.2 psutil-5.9.5 pyasn1-0.5.0 pyasn1-modules-0.3.0 pydantic-1.10.13 pydub-0.25.1 pyparsing-3.1.1 python-dateutil-2.8.2 python-multipart-0.0.6 pytz-2023.3.post1 pyyaml-6.0.1 qudida-0.0.4 realesrgan-0.3.0 referencing-0.30.2 requests-2.31.0 requests-oauthlib-1.3.1 rpds-py-0.10.3 rsa-4.9 scikit-image-0.21.0 scikit-learn-1.3.1 scipy-1.11.3 semantic-version-2.10.0 six-1.16.0 sniffio-1.3.0 soupsieve-2.5 starlette-0.27.0 sympy-1.12 tb-nightly-2.14.0a20230808 tensorboard-2.13.0 tensorboard-data-server-0.7.1 tensorflow-2.13.0 tensorflow-estimator-2.13.0 tensorflow-io-gcs-filesystem-0.34.0 termcolor-2.3.0 threadpoolctl-3.2.0 tifffile-2023.9.26 tomli-2.0.1 toolz-0.12.0 torch-2.0.1 torchvision-0.15.2 tqdm-4.66.1 typing-extensions-4.5.0 tzdata-2023.3 urllib3-2.0.5 uvicorn-0.23.2 wcwidth-0.2.7 websockets-11.0.3 werkzeug-2.3.7 wrapt-1.15.0 yapf-0.40.2 zipp-3.17.0

执行python run.py,因为我的环境没有安装ffmpeg,facefusion给了贴心的提示:

$ python run.py       
[FACEFUSION.CORE] FFMpeg is not installed

不同平台的安装方法可以参照官方精炼的安装文档:https://docs.facefusion.io/installation/platform

再次运行,首次运行facefusion会从外部下载一些模型数据,这一步比较慢,也可能需要魔法

$ python run.py      
Downloading: 100%|███████████████████████████████████████████████████████████████████████████| 529M/529M [1:15:34<00:00, 122kB/s]
download_path: /home/.insightface/models/buffalo_l
Downloading /home/.insightface/models/buffalo_l.zip from https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip...
100%|██████████████████████████████████████████████████████████████████████████████████| 281857/281857 [00:28<00:00, 9791.59KB/s]
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /home/.insightface/models/buffalo_l/1k3d68.onnx landmark_3d_68 ['None', 3, 192, 192] 0.0 1.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /home/.insightface/models/buffalo_l/2d106det.onnx landmark_2d_106 ['None', 3, 192, 192] 0.0 1.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /home/.insightface/models/buffalo_l/det_10g.onnx detection [1, 3, '?', '?'] 127.5 128.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /home/.insightface/models/buffalo_l/genderage.onnx genderage ['None', 3, 96, 96] 0.0 1.0
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
find model: /home/.insightface/models/buffalo_l/w600k_r50.onnx recognition ['None', 3, 112, 112] 127.5 127.5
set det-size: (640, 640)
Running on local URL:  http://127.0.0.1:7860

facefusion启动了一个Web服务器,打开本地的URL就可以访问了,体验上与stable-difussion-webui差不多,不过比SD简单了很多。

facefusion每次启动都会自动连接外网,可能是检查模型更新等等,如果不希望每次都连外网,可以指定--skip-download参数运行:

$ python run.py --skip-download

下面来看看基本用法,用浏览器打开http://127.0.0.1:7860

Source是指待应用的一张包含人脸的照片,Target可以是图片,也可以是视频,两者都设置后,点击Start启动替换,将Source中的人脸替换到Target上。

下面就来试一试这个组合 :D

Reference Face是指facefusion从Target中找出的脸,如果图片或视频有多张人脸,这里会出现多个,可以选择换哪个。

Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
1/1 [==============================] - 1s 1s/step
[FACEFUSION.FRAME_PROCESSOR.FACE_SWAPPER] Select an image for source path!
1/1 [==============================] - 0s 88ms/step
Applied providers: ['CPUExecutionProvider'], with options: {'CPUExecutionProvider': {}}
inswapper-shape: [1, 3, 128, 128]
100%|███████████████████████████████████████████████████████████████████████| 168/168 [00:01<00:00, 114.02it/s]
[FACEFUSION.CORE] Creating temporary resources
[FACEFUSION.CORE] Extracting frames with 25.0 FPS
[FACEFUSION.FRAME_PROCESSOR.FACE_SWAPPER] Processing
Processing: 100%|█| 175/175 [05:26<00:00,  1.86s/frame, memory_usage=01.63GB, execution_providers=['CPUExecutio
[FACEFUSION.CORE] Merging video with 25.0 FPS
[FACEFUSION.CORE] Restoring audio
[FACEFUSION.CORE] Clearing temporary resources
[FACEFUSION.CORE] Processing to video succeed

当终端出现Processing to video succeed表明处理完成,可以从Web界面的Output播放或者下载处理后的视频。

原视频

视频加载中...

处理后视频

视频加载中...

效果还是不错的。

facefusion不支持非人脸,比如动漫类,或是把动物头像换上,这些还不行,会提示找不到源图,应该跟使用的模型有关。

展开阅读全文

页面更新:2024-05-30

标签:都会   组合   仓库   模型   提示   简单   环境   官方   魔法   视频

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top