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

所有的都是数据的错。

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

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

相关文章

【Unity程序小技巧】如何消除多次Destory带来的性能消耗

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;Uni…

Linux学习第36天:Linux RTC 驱动实验:时间是一条流淌的河

Linux版本号4.1.15 芯片I.MX6ULL 大叔学Linux 品人间百味 思文短情长 RTC就是实时时钟。 本笔记主要学习Linux RTC驱动试验&#xff0c;主要内容包括Linux内核RTC驱动简介、I.MX6U内部RTC分析、RTC时间查看与设置。因为Linux内核已经…

Docker - 常用命令

Docker - 常用命令 帮助命令 docker version # 查看docker版本信息 docker info # 显示docker的系统信息&#xff0c;包括镜像和容器的数量 docker 命令 --help # 帮助命令官网帮助文档&#xff1a;https://docs.docker.com/engine/reference/commandline/cli/ 镜像…

接口测试及常用接口测试工具

首先&#xff0c;什么是接口呢&#xff1f; 接口一般来说有两种&#xff0c;一种是程序内部的接口&#xff0c;一种是系统对外的接口。 系统对外的接口&#xff1a;比如你要从别的网站或服务器上获取资源或信息&#xff0c;别人肯定不会把数据库共享给你&#xff0c;他只能给你…

【算法与数据结构】77、LeetCode组合

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;如果k是固定的&#xff0c;最直接的方法就是建立k个for循环&#xff0c;将结果全部压入result容器中。…

联合阿里在职测开工程师耗时一个星期写的 【接口测试+自动化接口接口测试详解]

1&#xff1a;json模块的使用  2&#xff1a;接口自动化测试概叙 3&#xff1a;swagger工具能导出接口文档的 4:前端页面: 5:后端: 6:前端和后端的数据交互&#xff08;接口&#xff09;通过接口 7&#xff1a;接口的概念 8&#xff1a;常用的接口方式&#xff08;协议…

自动化测试中的失败截图和存log

如果我们在执行自动化测试的时候&#xff0c;希望能在失败的时候保存现场&#xff0c;方便事后分析。 对于UI自动化&#xff0c;我们希望截图在测试报告中。 对于api自动化&#xff0c;我们希望截取出错的log在测试报告中。 我开始自己蛮干&#xff0c;写了两个出错截图的方法。…

Essential Math for AI:高效的人工智能数学原理晋级读物

今天给大家介绍一本人工智能数学原理书籍&#xff1a;Essential Math for AI。作者是Hala Nelson&#xff0c;一位应用数学领域的美女博士&#xff0c;James Madison University (JMU) 大学的助理教授。 关注微信公众号&#xff1a;人工智能大讲堂&#xff0c;后台回复【ema】获…

【Android】Debug时禁用主线程ANR限制

ANR全称Application Not Response&#xff0c;指主线程超过5s无响应&#xff0c;应用会自动退出 由于这个线程&#xff0c;如果我们给主线程加了断点&#xff0c;就会触发ANR&#xff0c;导致调试时应用退出 这样调试起来会非常麻烦&#xff0c;其实对于Debug应用&#xff0c…

JVM虚拟机-虚拟机性能监控、故障处理工具

1基础故障处理工具 jps&#xff08;JVM Process Status Tool&#xff09;是&#xff1a;虚拟机进程状况工具 作用&#xff1a;可以列出正在运行的虚拟机进程&#xff0c;并显示虚拟机执行主类&#xff08;Main Class&#xff0c;main()函数所在的类&#xff09;名称以及这些进…

人工智能基础——python:Pandas与数据处理

人工智能的学习之路非常漫长&#xff0c;不少人因为学习路线不对或者学习内容不够专业而举步难行。不过别担心&#xff0c;我为大家整理了一份600多G的学习资源&#xff0c;基本上涵盖了人工智能学习的所有内容。点击下方链接,0元进群领取学习资源,让你的学习之路更加顺畅!记得…

CSS基础:你必须要知道的行高属性 line-height

作者:WangMin 格言:努力做好自己喜欢的每一件事 CSDN原创文章 博客地址 &#x1f449; WangMin 对于初学CSS的同学来说&#xff0c;会有很多属性相关的疑问&#xff0c;行高属性 line-height一定是其中一个&#xff0c;因为它是CSS中非常重要的一个属性&#xff0c;这个属性改变…

AlphaControls控件TsRadioGroup的使用

通常使用AlphaControls控件中的TsRadioGroup时&#xff0c;往往使用默认值&#xff0c;会造成TsRadioGroup标题被TsRadioGroup的ITEMs占用&#xff0c;严重影响美观&#xff1a; 解决方案&#xff0c;通过对TsRadioGroup的ContentVOffset属性&#xff0c;设置为10。即可立即改善…

【ARFoundation学习笔记】点云与参考点

写在前面的话 本系列笔记旨在记录作者在学习Unity中的AR开发过程中需要记录的问题和知识点。主要目的是为了加深记忆。其中难免出现纰漏&#xff0c;更多详细内容请阅读原文以及官方文档。 汪老师博客 文章目录 点云新建点云 参考点参考点的工作原理何时使用参考点使用参考点…

【高等数学】导数的应用

导数的应用 1、洛必达法则1.1、引例1.2、内容1.3、证明1.4、洛必达的应用总结 1.5、注意 2、泰勒公式2.1、解决的问题2.2、引例2.3、内容2.3.1、带Peano余项的泰勒公式2.3.2、带Lagrange余项的泰勒公式2.3.3、麦克劳林公式2.3.4、几个初等函数的麦克劳林公式 2.4、证明2.5、泰勒…

SpringBoot 监控

概述 SpringBoot自带监控功能Actuator&#xff0c;可以帮助实现对程序内部运行情况监控&#xff0c;比如监控状况、Bean加载情况、配置属性、日志信息等。 使用步骤 导入依赖坐标 <dependency><groupId>org.springframework.boot</groupId><artifactI…

Vuex模块概念

一、核心概念 - module 1.目标 掌握核心概念 module 模块的创建 2.问题 由于使用单一状态树&#xff0c;应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时&#xff0c;store 对象就有可能变得相当臃肿。 这句话的意思是&#xff0c;如果把所有的状态都放在s…

智慧城市建设解决方案分享【完整】

文章目录 第1章 前言第2章 智慧城市建设的背景2.1 智慧城市的发展现状2.2 智慧城市的发展趋势 第3章 智慧城市“十二五”规划要点3.1 国民经济和社会发展“十二五”规划要点3.2 “十二五”信息化发展规划要点 第4章 大数据&#xff1a;智慧城市的智慧引擎4.1 大数据技术—智慧城…

公司如何实现多套环境的自动化测试?

实战练习 分别准备两套测试环境&#xff0c;都对其发起 get 请求&#xff0c;传入参数 name&#xff0c;对应值为 hogwarts&#xff0c;并断言其响应值。 测试环境1&#xff1a;http://httpbin.org/get 测试环境2&#xff1a;https://httpbin.ceshiren.com/get <strong>…

浙大恩特客户资源管理系统任意文件上传漏洞复现

0x01 产品简介 浙大恩特客户资源管理系统是一款针对企业客户资源管理的软件产品。该系统旨在帮助企业高效地管理和利用客户资源&#xff0c;提升销售和市场营销的效果。 0x02 漏洞概述 浙大恩特客户资源管理系统中fileupload.jsp接口处存在文件上传漏洞&#xff0c;未经身份认…
最新文章