当前位置: 首页 > news >正文

C#,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 的错。

所有的都是数据的错。

相关文章:

  • uniapp-实现微信授权登录
  • 网络安全(黑客)—小白自学
  • Pytorch 里面torch.no_grad 和model.eval(), model.train() 的作用
  • Docker本地部署Drupal并实现公网访问
  • 【课程笔记 - 目录】2023 微信公众号开发教程全开源
  • Docker安装、卸载,以及各种操作
  • MySQL性能分析工具的使用
  • python自动化测试selenium核心技术3种等待方式详解
  • OC-编译错误
  • 前端小技巧: 防抖和节流的区别
  • 根据json生成Java类
  • 关于navigator.userAgent判断苹果手机的问题和踩坑
  • Java中的反射机制
  • 09-MySQL主从复制
  • idea 一直卡在maven正在解析maven依赖
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • CentOS 7 修改主机名
  • CSS 三角实现
  • Git初体验
  • Java-详解HashMap
  • laravel with 查询列表限制条数
  • mysql_config not found
  • spring boot 整合mybatis 无法输出sql的问题
  • SpriteKit 技巧之添加背景图片
  • SQLServer之创建显式事务
  • Vue.js-Day01
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 关于Java中分层中遇到的一些问题
  • 力扣(LeetCode)22
  • 阿里云ACE认证之理解CDN技术
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • (3)nginx 配置(nginx.conf)
  • (C语言)字符分类函数
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (转)3D模板阴影原理
  • (转)可以带来幸福的一本书
  • .Net Core和.Net Standard直观理解
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
  • @staticmethod和@classmethod的作用与区别
  • [ C++ ] STL_vector -- 迭代器失效问题
  • [20150629]简单的加密连接.txt
  • [AAuto]给百宝箱增加娱乐功能
  • [Angular] 笔记 7:模块
  • [AX]AX2012 R2 出差申请和支出报告
  • [C]整形提升(转载)
  • [CareerCup] 14.5 Object Reflection 对象反射
  • [CSS] 点击事件触发的动画
  • [c语言]小课堂 day2
  • [Django开源学习 1]django-vue-admin