老照片修复与上色

前言

  • 受限于以前拍照设备和时间的冲刷,很多老照片都是黑白照,有的甚至已经损坏严重。随着人工智能的进步,大厂都推出了老照片修复与上色的服务,但有的是收费的,有的担心个人隐私,这里推荐两个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文件夹中就可以看到修复好的文件了,也可以像官方那样指定输入文件夹和输出文件夹

黑白照片上色

基本配置

# 克隆项目到工作目录
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

总结

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

后记

照片记录故事,用心描绘生活!
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于深度学习的老照片上色涉及到多个模型和算法,下面我简单介绍一下其中一种常用的方法,并提供一个对应的 Python 代码实现。 1. 数据准备 首先需要准备一些有标注的彩色照片和对应的黑白照片,作为训练集和测试集。可以从一些公开数据集中获取,如ImageNet,CIFAR等。 2. 模型搭建 接下来需要搭建一个卷积神经网络(CNN)模型,用于学习黑白照片和彩色照片之间的对应关系。这个模型需要包含一个编码器(Encoder)和一个解码器(Decoder),其中编码器用于将输入的黑白照片转换为一个中间表示,解码器用于将这个中间表示转换为彩色照片。 下面是一个简单的模型搭建代码示例: ```python from tensorflow.keras.layers import Conv2D, UpSampling2D, Input from tensorflow.keras.models import Model def build_model(): # 编码器 input_layer = Input(shape=(256, 256, 1)) x = Conv2D(64, (3, 3), activation='relu', padding='same')(input_layer) x = Conv2D(64, (3, 3), activation='relu', padding='same', strides=2)(x) x = Conv2D(128, (3, 3), activation='relu', padding='same')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', strides=2)(x) x = Conv2D(256, (3, 3), activation='relu', padding='same')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', strides=2)(x) x = Conv2D(512, (3, 3), activation='relu', padding='same')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same')(x) # 解码器 x = UpSampling2D((2, 2))(x) x = Conv2D(128, (3, 3), activation='relu', padding='same')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(64, (3, 3), activation='relu', padding='same')(x) x = Conv2D(64, (3, 3), activation='relu', padding='same')(x) x = Conv2D(32, (3, 3), activation='relu', padding='same')(x) output_layer = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x) model = Model(input_layer, output_layer) model.compile(optimizer='adam', loss='mse') return model ``` 3. 模型训练 模型搭建好之后,需要使用训练集对其进行训练。训练的过程中可以使用一些数据增强的技术,如随机裁剪、旋转、翻转等,以增加模型的泛化能力。 下面是一个简单的模型训练代码示例: ```python import numpy as np from tensorflow.keras.preprocessing.image import ImageDataGenerator model = build_model() # 数据增强 datagen = ImageDataGenerator( rescale=1. / 255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') # 加载数据集 train_data = datagen.flow_from_directory( 'train/', target_size=(256, 256), batch_size=32, class_mode=None) # 训练模型 model.fit(train_data, epochs=50) ``` 4. 模型测试 模型训练好之后,可以使用测试集对其进行测试,并将黑白照片转换为彩色照片。 下面是一个简单的模型测试代码示例: ```python import cv2 # 加载模型 model = build_model() model.load_weights('model_weights.h5') # 读取黑白照片 img = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (256, 256)) img = np.expand_dims(img, axis=-1) # 预测彩色照片 pred = model.predict(np.array([img]))[0] pred = pred * 255 pred = pred.astype(np.uint8) # 保存彩色照片 cv2.imwrite('result.jpg', pred) ``` 以上是一个简单的基于深度学习的老照片上色代码实现。由于涉及到多个模型和算法,实现起来比较复杂,需要一定的深度学习和编程知识。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值