Python开源项目CodeFormer——人脸重建(Face Restoration),模糊清晰、划痕修复及黑白上色的实践

本文主要是介绍Python开源项目CodeFormer——人脸重建(Face Restoration),模糊清晰、划痕修复及黑白上色的实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

无论是自己、家人或是朋友、客户的照片,免不了有些是黑白的、被污损的、模糊的,总想着修复一下。作为一个程序员 或者 程序员的家属,当然都有责任满足他们的需求、实现他们的想法。除了这个,学习了本文的成果,或许你还可以用来赚点小钱。

比如这样!

或是这样! 

是不是很酷?那么。。。

需要什么编程技能?什么知识?

答案:你不需要会编程序!你只需要认识26个字母 和 大约4GB左右的硬盘空间.

1 CODEFORMER概要(可略过,允许你看不懂)

GitHub - sczhou/CodeFormer: [NeurIPS 2022] Towards Robust Blind Face Restoration with Codebook Lookup Transformer

Releases · sczhou/CodeFormer · GitHub

下面是项目网址,可访问,无需下载哦。

https://github.com/sczhou/CodeFormer/
https://github.com/sczhou/CodeFormer/releases

1.1 论文Paper(可略过,允许你看不懂)

Towards Robust Blind Face Restoration with Codebook Lookup Transformer (NeurIPS 2022)

Shangchen Zhou, Kelvin C.K. Chan, Chongyi Li, Chen Change Loy

PAPER: https://arxiv.org/abs/2206.11253

Zhou: Shangchen Zhou

Chan: Kelvin C.K. Chan

Li: Chongyi Li

Loy: Home | Chen Change Loy

of Lab, Nanyang Technological University

(新加坡)南阳科技大学(NTU)

基本原理(可略过)

⭐ If CodeFormer is helpful to your images or projects, please help star this repo. Thanks!

1.2 进化史Update(可略过)

  1. 2023.07.20: Integrated to OpenXLab. Try out online demo! OpenXLab
    Demo: 🐼 OpenXLab
    OpenXLab浦源 - 应用中心
  2. 2023.04.19: Training codes and config files are public available now.
  3. 2023.04.09: Add features of inpainting and colorization for cropped and aligned face images.
  4. 2023.02.10: Include dlib as a new face detector option, it produces more accurate face identity.
  5. 2022.10.05: Support video input --input_path [YOUR_VIDEO.mp4]. Try it to enhance your videos! ?
  6. 2022.09.14: Integrated to Hugging Face. Try out online demo! Hugging Face
  7. 2022.09.09: Integrated to Replicate. Try out online demo! Replicate

1.3 功能 TODO(虽然不可略过,但瞄一眼即可)

  1. Add training code and config files
  2. Add checkpoint and script for face inpainting
  3. Add checkpoint and script for face colorization
  4. Add background image enhancement

1.3.1 老照片修复Try Enhancing Old Photos / Fixing AI-arts

1.3.2 人脸重建(画质修复,画质提升)Face Restoration

1.3.3 黑白上色Face Color Enhancement and Restoration

1.3.4 照片划痕修复Face Inpainting

1.4 依赖项与安装Dependencies and Installation

(可或不可略过,快速浏览即可)

1.4.1 依赖项 Dependencies

  1. Pytorch >= 1.7.1
  2. [Option] CUDA >= 10.1 可选,没有CUDA也可以运行。
  3. Other required packages in requirements.txt

1.4.2 安装 Install(看看就行了,看后面的教程再干活!)

# git clone this repository

git clone https://github.com/sczhou/CodeFormer

cd CodeFormer

# create new anaconda env

conda create -n codeformer python=3.8 -y

conda activate codeformer

# install python dependencies

pip3 install -r requirements.txt

python basicsr/setup.py develop

conda install -c conda-forge dlib (only for face detection or cropping with dlib)

1.5 快速指南Quick Inference

(可略过,后面有更快的指南)

下载预训练模型Download Pre-trained Models:

(下面的网址 与 方法 都很慢,用后面分享的网盘会快一些)

Download the facelib and dlib pretrained models from [Releases | Google Drive | OneDrive] to the weights/facelib folder. You can manually download the pretrained models OR download by running the following command:

Release CodeFormer V0.1.0 Release · sczhou/CodeFormer · GitHub

https://drive.google.com/drive/folders/1b_3qwrzY_kTQh0-SnBoGBgOrJ_PLZSKm?usp=sharing

OneDrive

python scripts/download_pretrained_models.py facelib

python scripts/download_pretrained_models.py dlib (only for dlib face detector)

Download the CodeFormer pretrained models from [Releases | Google Drive | OneDrive] to the weights/CodeFormer folder. You can manually download the pretrained models OR download by running the following command:

Release CodeFormer V0.1.0 Release · sczhou/CodeFormer · GitHub

https://drive.google.com/drive/folders/1CNNByjHDFt0b95q54yMVp6Ifo5iuU6QS?usp=sharing

OneDrive

python scripts/download_pretrained_models.py CodeFormer

准备测试数据Prepare Testing Data: 

(作者已经帮你下载好了)

You can put the testing images in the inputs/TestWhole folder. If you would like to test on cropped and aligned faces, you can put them in the inputs/cropped_faces folder. You can get the cropped and aligned faces by running the following command:

# you may need to install dlib via: conda install -c conda-forge dlib

python scripts/crop_align_face.py -i [input folder] -o [output folder]

1.6 测试Testing:

(可略过,后面有更快的指南,下同!)

[Note] If you want to compare CodeFormer in your paper, please run the following command indicating --has_aligned (for cropped and aligned face), as the command for the whole image will involve a process of face-background fusion that may damage hair texture on the boundary, which leads to unfair comparison.

Fidelity weight w lays in [0, 1]. Generally, smaller w tends to produce a higher-quality result, while larger w yields a higher-fidelity result. The results will be saved in the results folder.

1.6.1 人脸重建Face Restoration (cropped and aligned face)

脸部重建(局部512x512)

# For cropped and aligned faces (512x512)

python inference_codeformer.py -w 0.5 --has_aligned --input_path [image folder]|[image path]

全图

Whole Image Enhancement

# For whole image

# Add '--bg_upsampler realesrgan' to enhance the background regions with Real-ESRGAN

# Add '--face_upsample' to further upsample restorated face with Real-ESRGAN

python inference_codeformer.py -w 0.7 --input_path [image folder]|[image path]

Video Enhancement

# For Windows/Mac users, please install ffmpeg first

conda install -c conda-forge ffmpeg

# For video clips

# Video path should end with '.mp4'|'.mov'|'.avi'

python inference_codeformer.py --bg_upsampler realesrgan --face_upsample -w 1.0 --input_path [video path]

1.6.2 黑白上色Face Colorization (cropped and aligned face)

# For cropped and aligned faces (512x512)

# Colorize black and white or faded photo

python inference_colorization.py --input_path [image folder]|[image path]

1.6.3 人脸修复Face Inpainting (cropped and aligned face)

# For cropped and aligned faces (512x512)

# Inputs could be masked by white brush using an image editing app (e.g., Photoshop)

# (check out the examples in inputs/masked_faces)

python inference_inpainting.py --input_path [image folder]|[image path]

1.7 自训练Training:

(费力不讨好的事!别干了。再说你也没有资源!)

The training commands can be found in the documents: English | 简体中文.

https://github.com/sczhou/CodeFormer/blob/master/docs/train.md

https://github.com/sczhou/CodeFormer/blob/master/docs/train_CN.md

1.8 发行版本Release V0.1.0

(可略过,后面有更快的指南)

下面这些是你需要下载的模型。。。。。。

但是,作者已经帮你下载好了。

CodeFormer V0.1.0 Release Latest

This release is mainly for storing pre-trained models, etc.

Assets 22

codeformer.pth  359 MB  Aug 10, 2022

codeformer_colorization.pth 355 MB  Apr 3

codeformer_inpainting.pth   354 MB  Apr 9

codeformer_stage2.pth   365 MB  Jul 23

detection_mobilenet0.25_Final.pth   1.71 MB Aug 10, 2022

detection_Resnet50_Final.pth    104 MB  Aug 10, 2022

latent_gt_code1024.pth  302 MB  Apr 17

latent_gt_code512.pth   302 MB  Apr 17

mmod_human_face_detector-4cb19393.dat   713 KB  Feb 10

motion-blur-kernels-32.pth  65.4 KB Apr 17

parsing_bisenet.pth 50.8 MB Aug 10, 2022

parsing_parsenet.pth    81.4 MB Aug 10, 2022

RealESRGAN_x2plus.pth   64 MB   Sep 13, 2022

shape_predictor_5_face_landmarks-c4b1e980.dat   8.73 MB Feb 10

shape_predictor_68_face_landmarks-fbdc2cb8.dat  95.1 MB Apr 7

vqgan_code1024.pth  243 MB  Feb 25

vqgan_code512.pth   243 MB  Apr 17

vqgan_discriminator.pth 26.6 MB Apr 18

yolov5l-face.pth    178 MB  Aug 10, 2022

yolov5n-face.pth    6.81 MB Aug 10, 2022

Source code(zip)    Aug 9, 2022

Source code(tar.gz) Aug 9, 2022

1.9 权利License

(不可略过,请爱惜他人成果!)

This project is licensed under NTU S-Lab License 1.0</a>. Redistribution and use should follow this license.

https://github.com/sczhou/CodeFormer/blob/master/LICENSE

1.10 相关知识Acknowledgement

(不可略过,来龙去脉要搞清爽的!)

This project is based on [BasicSR]. Some codes are brought from [Unleashing Transformers], [YOLOv5-face], and [FaceXLib]. We also adopt [Real-ESRGAN] to support background image enhancement. Thanks for their awesome works.

BasicSR GitHub - XPixelGroup/BasicSR: Open Source Image and Video Restoration Toolbox for Super-resolution, Denoise, Deblurring, etc. Currently, it includes EDSR, RCAN, SRResNet, SRGAN, ESRGAN, EDVR, BasicVSR, SwinIR, ECBSR, etc. Also support StyleGAN2, DFDNet.

Transformers GitHub - samb-t/unleashing-transformers: Code for the ECCV 2022 paper "Unleashing Transformers"

YOLOv5-face GitHub - deepcam-cn/yolov5-face: YOLO5Face: Why Reinventing a Face Detector (https://arxiv.org/abs/2105.12931) ECCV Workshops 2022)

FaceXLib GitHub - xinntao/facexlib: FaceXlib aims at providing ready-to-use face-related functions based on current STOA open-source methods.

Real-ESRGAN GitHub - xinntao/Real-ESRGAN: Real-ESRGAN aims at developing Practical Algorithms for General Image/Video Restoration.

1.11 联系 Contact

try it!

If you have any questions, please feel free to reach me out at `shangchenzhou@gmail.com`.

----------------------------------------------------------------------

2 以下字字珠玑,不bu可ke略lue过guo.!.!.!

2.1 安装 python

90年代末期,在一知半解的情况下在某985大学给大家做了一次 python 讲座,最后告诉大家这个语言没有什么前途,听者大多稀里糊涂。没想到借助 AI,这个垃圾语言又死灰复燃了。

python 用于一些算法的验证等等,特别适合在大学、研究机构使用,工业场景就很吃力了。

CodeFormer 是python写的。

当然要先下载、安装 python。

提示:不同的python程序需要不同版本的python。这里随便先安装一个,不用太在意。

访问:

http://www.python.org(文件不太大,一会儿就能下载完成)

下载 windows 64 bit版本:

下载稳定版本 stable releases:

然后在你的计算机 下载 目录,就会有一个 python-.......exe 文件。双击安装。

务必记住:安装过程中,请将所有的可选项 【✔️】都点开,再点击【下一步】什么的!

再请记住:安装差不多的时候,有一个 Disable path length limit! 的项目!一定记得点击!

2.2 安装Anaconda

官网:www.anaconda.com 访问但不要下载哦!太慢啦!用镜像。下面有!

看一下网页左下角的文件名。 Ana.......64.exe 什么的。

 

访问清华的镜像:

https://mirrors.tuna.tsinghua.edu.cn/anaconda/achive/

技巧:CTRL+F,然后输入 2023.09 可以快速定位。如果是其他版本,按上面你看到的 版本信息,取其中几个具有区别性的字母或数字来搜搜。

Anaconda 比较大, 1 GB 多了,慢慢下吧。

下载后双击运行 exe 文件即可。后面照样选择全部的【可选项】,下一步。。。。。。

2.3 无需下载安装 PyCharm(收费)

很多python教程指导大家下载、安装 PyCharm,其实这个软件没有大用处。

那咱们用什么编辑器编写软件呢。

推荐用:

(1)微软 Visual Studio 2022 或者 Visual Studio Code;

(2)或:个人最喜欢用 EditPlus 绿色版本;或其他任何可以写文本文件的软件;

(3)或:用 Windows 写字板 也是可以的。

2.4 开始工作

做完了 2.1 与 2.2 ,再安装代码、模型及设置最基本的运行环境。后面就快了。

2.4.1 下载 CodeFormer 及其模型库 (复习一下,差点忘记了本文的主角!)

作者为大家下载了全部的代码 和 需要的 模型文件,大约是 2GB 左右,主要是模型数据比较大。

下面的网盘连接可以免费下载,不需要积分与铜钱。

链接:https://pan.baidu.com/s/1QLnLKvVQGnmedP7WdxGZ8w?pwd=miff 提取码:miff --来自百度网盘超级会员V5的分享icon-default.png?t=N7T8https://pan.baidu.com/s/1QLnLKvVQGnmedP7WdxGZ8w?pwd=miff

下载后,先创建文件夹(这是我的文件夹结构,你如果能跑,不限啊!):

        C:/Yohoo/

        C:/Yohoo/Face Restoration

将文件复制到该文件夹下,再解压成:

        C:/Yohoo/Face Restoration/CodeFormer-Master 

即可。

2.4.2 设置 CodeFormer 运行环境

Windows 10 或 Windows 11,应用程序。

这样就进入了 Conda Prompt 界面,如下:

 

 进入 CodeFormer-master 目录:

dir 可以看到一些文件:

下面开始逐句执行一些代码,借此设置运行环境。

第一句:

conda create -n codeformer python=3.8 -y

你可以复制本语句(CTRL+C),到黑色窗口后,粘贴(CTRL+V)

如此:

然后回车(下同!)

第二句:

conda activate codeformer

第三句:

pip3 install -r requirements.txt

如果出现错误,一般是你没到达 codeformer-master 目录。

第四句:

python basicsr/setup.py develop

第五句:

conda install -c conda-forge dlib

上面这些过程,一般将持续 20 分钟。

2.5 见证奇迹的时候

画质修复:

第一步:在  mydata 目录下,创建 src 文件夹。将需要处理的图片,复制到该目录下。

第二步:输入代码

python inference_codeformer.py -w 0.7 --input_path mydata/src

一会结果就能出来,保存到 results 文件夹下。

上面的这个图,有彩蛋哦!

环境设置只需要一次!

后面如果多次使用 CodeFormer ,则需要每次都: 

conda activate codeformer

这里只做一个试验,其他的功能看上面的 1.6 节 代码即可。

总结几条:

(1)python 一点儿都不简单;说简单的都是为了买书、买视频;

(2)python 除了AI领域,其他的领域没有多大用处,建议学其他的语言;

(3)即使把 python 玩出花来,也成不了大神。

效果不好,不是我的错,也不是 python 的错。

所有的都是数据的错。

这篇关于Python开源项目CodeFormer——人脸重建(Face Restoration),模糊清晰、划痕修复及黑白上色的实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/385964

相关文章

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

golang内存对齐的项目实践

《golang内存对齐的项目实践》本文主要介绍了golang内存对齐的项目实践,内存对齐不仅有助于提高内存访问效率,还确保了与硬件接口的兼容性,是Go语言编程中不可忽视的重要优化手段,下面就来介绍一下... 目录一、结构体中的字段顺序与内存对齐二、内存对齐的原理与规则三、调整结构体字段顺序优化内存对齐四、内