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 的错。

所有的都是数据的错。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/129671.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

如何记录血压的波动情况

import pandas as pd from plotnine import * import matplotlib.pyplot as plt plt.rcParams[font.sans-serif] [Microsoft YaHei] 记录时间(time)、收缩压(SBP)、舒张压(DBP)&#xff1a; df pd.DataFrame({ time: [2023-11-01 08:30, 2023-11-02 21:00, 2023-11-0…

C++之旅(学习笔记)第6章 基本操作

C之旅&#xff08;学习笔记&#xff09;第6章 基本操作 6.1 基本操作 class X{ public:X(Sometype); // "普通的构造函数": 创建一个对象X(); // 默认构造函数X(const X&); // 拷贝构造函数X(X&&); // 移动构造函数X& operator(const …

规划文献阅读——Obstacle Avoidance, Path Planning and Control for Autonomous Vehicles

摘要 自动驾驶汽车避障需要三个主要层面&#xff0c;即感知、路径规划和制导控制。在本文中&#xff0c;考虑到这三个层次之间的联系&#xff0c;提出了一个全局架构。在环境感知层面&#xff0c;采用基于证据占用网格的方法进行动态障碍物检测。因此&#xff0c;考虑物体的姿…

鸿蒙原生应用开发-DevEco Studio远程模拟器的使用

使用单设备模拟器运行应用/服务 Remote Emulator支持Phone、Wearable、Tablet、TV等设备类型&#xff0c;但不同区域&#xff08;开发者帐号注册地&#xff09;支持的设备类型可能不同&#xff0c;请以实际可申请的设备类型为准。 Remote Emulator中的单设备模拟器&#xff08…

Sprint Boot 学习路线 3

嵌入式服务器 Spring Boot 的嵌入式服务器功能是一项方便而强大的功能&#xff0c;它允许你在应用程序中直接运行 Web 服务器&#xff0c;无需将其部署到单独的独立 Web 服务器中。这使得开发、测试和部署 Web 应用程序变得容易&#xff0c;而且它还是轻量级的、易于启动和停止…

linuxC语言缓冲区及小程序的实现

文章目录 1.文件缓冲区1.1介绍1.2缓冲文件系统1.3冲刷函数fflush1.4认识linux下的缓冲区 2.linux小程序的实现2.1 回车\r和换行\n2.2倒计时程序2.3进度条小程序sleep/usleep代码运行结果 1.文件缓冲区 1.1介绍 为缓和 CPU 与 I/O 设备之间速度不匹配&#xff0c;文件缓冲区用以…

【Maven教程】(十):使用 Hudson 进行持续集成—— 从Hudson的安装到任务创建 ~

Maven 使用 Hudson 进行持续集成 1️⃣ 持续集成的作用、过程和优势2️⃣ Hudson 简介与安装3️⃣ 准备 Subversion 仓库4️⃣ Hudson 的基本系统设置5️⃣ 创建 Hudson 任务5.1 Hudson 任务的基本配置5.2 Hudson 任务的源码仓库配置5.3 Hudson 任务的构建触发配置5.4 Hudson …

python之SPC:计算Cpk

目录 1、Ca、Cp和Cpk的理解 2、python计算Cp,Cpk与Pp,Ppk 3、总结 1、Ca、Cp和Cpk的理解 Ca、Cp和Cpk是制程能力指数&#xff0c;它们分别代表制程准确度、制程精密度和制程能力指数。 制程准确度&#xff08;Ca&#xff09;反映实际平均值与规格中心值之一致性。对于单边…

GF0-57CQD-002 测量参数:加速度、速度、位移–现场可配置

GF0-57CQD-002 测量参数:加速度、速度、位移–现场可配置 GF0-57CQD-002 是一款创新的双通道变送器&#xff0c;专为精确的振动测量而设计。它激励并读取来自加速度计的信号&#xff0c;并将整体振动值作为电流/电压信号传输。它测量加速度、速度和位移等不同参数的振动。配置…

竞赛 车道线检测(自动驾驶 机器视觉)

0 前言 无人驾驶技术是机器学习为主的一门前沿领域&#xff0c;在无人驾驶领域中机器学习的各种算法随处可见&#xff0c;今天学长给大家介绍无人驾驶技术中的车道线检测。 1 车道线检测 在无人驾驶领域每一个任务都是相当复杂&#xff0c;看上去无从下手。那么面对这样极其…

Vxe table - 基于Vue的宝藏级 table 组件

文章目录 前言一、Vxe-table功能点计划 二&#xff0c;安装三&#xff0c;引入四&#xff0c;示例用法 前言 对于表格来说&#xff0c;也许我们会遇到一个需求就是表格中的单元格可编辑&#xff0c;如果我们使用的是ElementUI也许不太好办&#xff0c;因为官方没有可编辑的这个…

Spring封装数据结果

Spring封装数据结果 POST请求JSON格式 基本数据类型 public class Demo {private byte aByte;private short aShort;private int anInt;private long aLong;private float aFloat;private double aDouble;private char aChar;private boolean aBoolean; }没有传键 封装时就会…

【Spring】SpringBoot配置文件

SpringBoot配置文件 配置文件作用SpringBoot配置文件配置文件快速入手配置文件的格式properties配置文件说明基本语法读取配置文件properties缺点分析 yml配置文件说明yml基本语法yml使用进阶yml配置读取配置对象配置集合配置Mapyml优缺点 配置文件作用 计算机上有数以千计的配…

Unity 一些内置宏定义

在Unity中&#xff0c;有一些内置的宏定义可用于不同的平台。以下是一些常见的平台内置宏定义&#xff1a; 1、UNITY_EDITOR&#xff1a;在Unity编辑器中运行。 2、UNITY_EDITOR_WIN&#xff1a;在Unity编辑器运行在Windows操作系统时被定义。 3、UNITY_STANDALONE&#xff1a…

Linux学习第37天:Linux I2C 驱动实验(一):哥俩好

Linux版本号4.1.15 芯片I.MX6ULL 大叔学Linux 品人间百味 思文短情长 世界上的很多事物都是成双成对出现的。也包括在驱动开发的过程中&#xff0c;比如I2C中其实就是数据线和时钟线的相互配合才能完成的。 I2C常用于连接各种外设、…

Ubuntu 22.04 安装水星无线 USB 网卡

我的 USB 网卡是水星 Mercury 的&#xff0c; 在 Ubuntu 22.04 下面没有自动识别。 没有无线网卡的时候只能用有线接到路由器上&#xff0c;非常不方便。 寻思着把无线网卡驱动装好。折腾了几个小时装好了驱动。 1.检查网卡类型 & 安装驱动 使用 lsusb 看到的不一定是准确…

node插件MongoDB(四)—— 库mongoose 的条件控制(三)

文章目录 前言一、运算符二、逻辑运算1. $or 逻辑或2. $and 逻辑与 三、正则匹配 前言 在mongodb 不能使用 > < > < ! 等运算符&#xff0c;需要使用替代符号。 一、运算符 > 使用 $gt< 使用 $lt> 使用 $gte< 使用 $lte! 使用 $ne 例子&#xff1a;获…

Mysql 一步到位实现插入或替换数据(REPLACE INTO语句)

单条数据插入/替换 比如有一个数据表叫test_table&#xff0c;包含: 主键&#xff1a;key_id数据&#xff1a;value 运行&#xff1a; REPLACE INTO test_table (key_id,value) VALUES ("id_1","value_1"); REPLACE INTO test_table (key_id,value) VAL…

Qt 各种数据类型

目录 1. 基础类型 2. log 输出 3. 字符串类型 3.2 QByteArray 构造函数 数据操作 子字符串查找和判断 遍历 查看字节数 类型转换 3.3 QString 4. QVariant 4.1 标准类型 4.2 自定义类型 5. 位置和尺寸 5.1 QPoint 5.2 QLine 5.3 QSize 5.4 QRect 6. 日期和…

gcc [linux]

目录 背景知识 gcc如何完成 格式 预处理&#xff08;进行宏替换&#xff09; 编译&#xff08;生成汇编&#xff09; 汇编&#xff08;生成机器可执行码&#xff09; 连接&#xff08;生成可执行文件或库文件&#xff09; 函数库 静态库 静态链接优势 动态库 动态链…
最新文章