Real-ESRGAN 图像和视频AI修复工具使用

背景
在我们的日常中,对于一些年代比较久远的图片 or 视频,我们希望可以进行一些修复,但是现在很多软件都比较耗费性能,或者需要付费,我们希望有一个比较轻量级的软件,可以进行一些修复,并且可以免费使用。
然后在 github 偶然发现了这个工具,记录一下使用的过程和遇到的问题。
前置准备
工具 github地址 Real-ESRGAN
克隆代码到本地
git clone https://github.com/xinntao/Real-ESRGAN.git
安装依赖
pip install basicsr
这时候遇到了报错,basicsr 安装失败,错误信息1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
pip install basicsr
Collecting basicsr
Using cached basicsr-1.4.2.tar.gz (172 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [24 lines of output]
Traceback (most recent call last):
File "/Users/topjoy/PycharmProjects/Real-ESRGAN/venv/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
~~~~^^
File "/Users/topjoy/PycharmProjects/Real-ESRGAN/venv/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
~~~~^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/topjoy/PycharmProjects/Real-ESRGAN/venv/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
File "/private/var/folders/h7/b_kcqfhn5t7cs_w3p6ks093w0000gn/T/pip-build-env-gfw___4v/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 334, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=[])
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/private/var/folders/h7/b_kcqfhn5t7cs_w3p6ks093w0000gn/T/pip-build-env-gfw___4v/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 304, in _get_build_requires
self.run_setup()
~~~~~~~~~~~~~~^^
File "/private/var/folders/h7/b_kcqfhn5t7cs_w3p6ks093w0000gn/T/pip-build-env-gfw___4v/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 522, in run_setup
super().run_setup(setup_script=setup_script)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/private/var/folders/h7/b_kcqfhn5t7cs_w3p6ks093w0000gn/T/pip-build-env-gfw___4v/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 320, in run_setup
exec(code, locals())
~~~~^^^^^^^^^^^^^^^^
File "<string>", line 149, in <module>
File "<string>", line 79, in get_version
KeyError: '__version__'
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.解决办法:
- 更新 setuptools 和 pip 版本
pip install --upgrade pip setuptools wheel
- 安装 torch、opencv
pip install torch opencv-python
此时因为我本地的python 版本是 3.13,torch 不支持 3.13,官方文档的支持是 3.9~3.12,所以更换使用 3.12 版本 - 更换版本后,basicsr 安装成功
- 更新 setuptools 和 pip 版本
快速上手
根据官方文档针对于普通图片和动图有两个提供好的模型使用,分别是 RealESRGAN_x4plus 和 RealESRGAN_x4plus_anime_6B
普通图片
使用模型RealESRGAN_x4plus.pth
1 | 下载模型 |
动图
使用模型RealESRGAN_x4plus_anime_6B
1 | 下载模型 |
运行成功后,会在 outputs 目录下生成处理后的图片,可以查看效果。
输出尺寸的自定义
RealESRGAN_x4plus.pth模型默认使用 X4即4倍的超分辨率,但是可以通过自定义命令参数outscale
来修改输出尺寸。
1 | Usage: python inference_realesrgan.py -n RealESRGAN_x4plus -i infile -o outfile [options]... |
懒人版本
官方提供了可执行文件,支持 Windows Linux 和 MacOS,直接下载即可使用。
我体验使用了 mac os。
如果是图片
1 | ./realesrgan-ncnn-vulkan.exe -i 输入图像.jpg -o 输出图像.png -n 模型名字 |
如果是视频
其实原理就是把视频拆分成一帧一帧的图片,先处理一张张图片,然后再合成成视频。
Use ffmpeg to extract frames from a video (Remember to create the folder
tmp_frames
ahead)
创建解压目录tmp_frames,ffmpeg 需要手动安装一下brew install ffmpeg
1
ffmpeg -i onepiece_demo.mp4 -qscale:v 1 -qmin 1 -qmax 1 -vsync 0 tmp_frames/frame%08d.jpg
Inference with Real-ESRGAN executable file (Remember to create the folder
out_frames
ahead)
创建处理输出目录out_frames,使用模型realesr-animevideov3,放大倍数2,输出格式jpg(也可以使用提供的其他模型)1
./realesrgan-ncnn-vulkan -i tmp_frames -o out_frames -n realesr-animevideov3 -s 2 -f jpg
Merge the enhanced frames back into a video
把处理输出的图片再集成成视频1
ffmpeg -i out_frames/frame%08d.jpg -i onepiece_demo.mp4 -map 0:v:0 -map 1:a:0 -c:a copy -c:v libx264 -r 23.98 -pix_fmt yuv420p output_w_audio.mp4
使用效果对比
这个是我源文件的分辨率的预览
这个是使用模型RealESRGAN_x4plus.pth处理后的结果,分辨率接近 8K(源文件太大了,直接粘进来很卡就截了一张对比图,可能效果不明显,可以自己体验一下,对比真的很明显)
- 标题: Real-ESRGAN 图像和视频AI修复工具使用
- 作者: 77
- 创建于 : 2025-01-06 14:53:55
- 更新于 : 2025-01-13 14:06:12
- 链接: https://www.jiaheqi.cloud/2025/01/06/Real-ESRGAN-图像和视频AI修复工具使用/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。