likes
comments
collection
share

旧照片修复-模糊图片变清晰-2023年度最强神器 codeformer

作者站长头像
站长
· 阅读数 8

CodeFormer是一种基于AI技术深度学习的人脸复原模型,由南洋理工大学和商汤科技联合研究中心联合开发。该模型通过结合了VQGAN和Transformer等技术,可以通过提供模糊或马赛克图像来生成清晰的原始图像。

功能:

1、老照片修复

2、黑白照片彩色化

3、照片马赛克修复

4、低码率视频增强,增加细节

参考资料:

github.com/sczhou/Code…

按照教程,安装环境

首先安装miniconda3

新建python3.8 环境

/home/sean.xd/miniconda3/bin/conda create -n codeformer python=3.8

新建本地的venv

/home/sean.xd/miniconda3/envs/codeformer/bin/python -m venv venv
source venv/bin/activate

按照官方教程,安装requirements.txt

pip install -r requirements.txt

按照教程,安装

python basicsr/setup.py develop

但是会遇到报错,没有cython, 那就手工安装一下

pip install cython

然后再执行 python basicsr/setup.py develop, 这次就成功了

安装dlib

/home/sean.xd/miniconda3/bin/conda install -c conda-forge dlib

安装ffmpeg

/home/sean.xd/miniconda3/bin/conda install -c conda-forge ffmpeg

下载训练好的模型

python scripts/download_pretrained_models.py facelib
python scripts/download_pretrained_models.py dlib
python scripts/download_pretrained_models.py CodeFormer

测试一个视频增强功能

提醒缺少ffmpeg, import ffmpeg失败

pip3 install ffmpeg-python ,这样就有了

python inference_codeformer.py --bg_upsampler realesrgan --face_upsample -w 1.0 --input_path inputs/xudong.mp4
python inference_codeformer.py --bg_upsampler realesrgan --face_upsample -w 1.0 --input_path  inputs/xudong.mp4  
inference_codeformer.py:49: RuntimeWarning: Running on CPU now! Make sure your PyTorch version matches your CUDA.The unoptimized RealESRGAN is slow on CPU. If you want to disable it, please remove `--bg_upsampler` and `--face_upsample` in command.
  warnings.warn('Running on CPU now! Make sure your PyTorch version matches your CUDA.'
Face detection model: retinaface_resnet50
Background upsampling: True, Face upsampling: True
[1/276] Processing: xudong_000000
	detect 0 faces
[2/276] Processing: xudong_000001
	detect 0 faces
[3/276] Processing: xudong_000002
	detect 0 faces
[4/276] Processing: xudong_000003
	detect 0 faces
[5/276] Processing: xudong_000004
	detect 0 faces
[6/276] Processing: xudong_000005
	detect 0 faces
[7/276] Processing: xudong_000006
	detect 0 faces
[8/276] Processing: xudong_000007
	detect 0 faces
[9/276] Processing: xudong_000008
	detect 0 faces
[10/276] Processing: xudong_000009
	detect 0 faces

处理的很慢,最后给kill掉了。 不过过程来看,相对顺利。

占用cpu很高

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                            
 29715 sean.xd   20   0 9967.3m   1.8g 104776 R  1237  1.5 116:27.63 python inference_codeformer.py --bg_upsampler realesrgan --face_upsample -w 1.0 --input_path inputs/xudong.mp4 

测试图片增强功能

项目有个目录,专门用于测试的。例如这张

github.com/sczhou/Code…

运行如下命令

$ python inference_codeformer.py -w 0.7 --bg_upsampler realesrgan  --face_upsample  --input_path inputs/whole_imgs/03.jpg
inference_codeformer.py:49: RuntimeWarning: Running on CPU now! Make sure your PyTorch version matches your CUDA.The unoptimized RealESRGAN is slow on CPU. If you want to disable it, please remove `--bg_upsampler` and `--face_upsample` in command.
  warnings.warn('Running on CPU now! Make sure your PyTorch version matches your CUDA.'
Face detection model: retinaface_resnet50
Background upsampling: True, Face upsampling: True
[1/1] Processing: 03.jpg
	detect 1 faces

All results are saved in results/test_img_0.7

旧照片修复-模糊图片变清晰-2023年度最强神器 codeformer

旧照片修复-模糊图片变清晰-2023年度最强神器 codeformer

不得不说,效果是真的牛批!

使用time命令,看看执行总共耗时多少。32核CPU,耗时55秒。如果是单核心cpu,则大概耗时8min多。

real 0m55.259s

user 8m35.757s

sys 1m43.415s

再多测试几张照片

旧照片修复-模糊图片变清晰-2023年度最强神器 codeformer

旧照片修复-模糊图片变清晰-2023年度最强神器 codeformer

旧照片修复-模糊图片变清晰-2023年度最强神器 codeformer

旧照片修复-模糊图片变清晰-2023年度最强神器 codeformer