老照片修复与上色

前言

  • 受限于以前拍照设备和时间的冲刷,很多老照片都是黑白照,有的甚至已经损坏严重。随着人工智能的进步,大厂都推出了老照片修复与上色的服务,但有的是收费的,有的担心个人隐私,这里推荐两个Github项目用来上色和修复。

我的配置

  • Ubuntu20.04
  • python3.6
  • Intel i7 + GTX1060Ti(使用GPU需要安装CUDA)

照片修复

项目配置

# 克隆项目到工作目录
git clone https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life.git

# clone 此项目,将里面的sync_batchnorm文件夹复制到项目的Global/detection_models和Face_Enhancement/models/networks/文件夹中
git clone https://github.com/vacancy/Synchronized-BatchNorm-PyTorch

# 下载权重文件,这里不建议自己训练,耗时不说还不一定有官方给的好
cd Face_Detection/
wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
bzip2 -d shape_predictor_68_face_landmarks.dat.bz2

cd ../Face_Enhancement/
wget https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life/releases/download/v1.0/face_checkpoints.zip
unzip face_checkpoints.zip

cd ../Global/
wget https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life/releases/download/v1.0/global_checkpoints.zip
unzip global_checkpoints.zip

# 安装依赖
cd ../
pip install -r requirements.txt

项目使用

# 直接将照片放到test_iamges中,GPU 0/-1 0:使用GPU,-1:不使用GPU,--with_scratch照片有折痕

python run.py --GPU 0

# 在ouput文件夹中就可以看到修复好的文件了,也可以像官方那样指定输入文件夹和输出文件夹

黑白照片上色

  • DeOldify,大名鼎鼎的黑白照片与视频上色项目,之前大火的老北京黑白视频上色就用的它,项目地址https://github.com/jantic/DeOldify

基本配置

# 克隆项目到工作目录
git clone https://github.com/jantic/DeOldify.git

# 安装依赖
pip install -r requirements.txt

# 创建models文件夹并下载权重文件
mkdir DeOldify/models
cd DeOldify/models

wget https://data.deepai.org/deoldify/ColorizeArtistic_gen.pth
wget https://www.dropbox.com/s/usf7uifrctqw9rl/ColorizeStable_gen.pth
wget https://data.deepai.org/deoldify/ColorizeVideo_gen.pth

使用

  • 在项目中新建一个run.py,编写可参考项目中的.ipynb文件
from deoldify import device
from deoldify.device_id import DeviceId
#choices:  CPU, GPU0...GPU7
device.set(device=DeviceId.GPU0)
from deoldify.visualize import *

plt.style.use('dark_background')
torch.backends.cudnn.benchmark=True

import warnings
warnings.filterwarnings("ignore", category=UserWarning, message=".*?Your .*? set is empty.*?")

#true:为artistic模式,false:为stable模式
colorizer = get_image_colorizer(artistic=True) 

# 可以修改
render_factor=35

# 输出文件
source_path = 'input_images/2_s.jpg'

result_path = None

colorizer.plot_transformed_image(path=source_path, render_factor=render_factor, compare=True)

使用

python run.py

总结

如果同时需要照片修复以及上色,建议先上色再进行修复操作。

后记

照片记录故事,用心描绘生活!

猜你喜欢

转载自blog.csdn.net/qq_34935373/article/details/121563324