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 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http