白黒動画のカラー化(pyTorch編) | python3Xのブログ

python3Xのブログ

ここでは40代、50代の方が日々の生活で役に立つ情報や私の趣味であるプログラム、Excelや科学に関する内容で投稿する予定です。

今回は白黒動画のカラー化をPyTorchでも出来ることを証明したいと思います。

動画はアップしません。ですが・・・

(作成途中のカラー化動画はアップします。)

今までのimage colorization と pyTorchの違いが分かれば幸いです。

 

注)あまり大きいサイズの画像だとカラー化が無理のようです。

私は「640 x 480」のサイズまで落としてカラー化を試みました。

あとは「render factor」(解像度でしょうか?)の変更です。

私は全て「45」にせっていしました。

 

今回はの連番の白黒画像を連番でカラー画像に変換するところのみお伝えいたします。

(splitted_monoフォルダの白黒画像をtest_imagesに変えれば良いだけです)

あとは、同じやり方で完成します。

左下の検索スペースに「cmd」と入力(コマンドプロンプトを立ち上げる)

================================================

git clone https://github.com/jantic/DeOldify.git DeOldify

と入力し「Enterを押す」
(ダウンロードできない時は直接上記のURLに飛んでZIPファイルをダウンロード
それを「yourname」下に解凍し

yourname以下の「Deoldifi か DeOldify-master」に移動する

cd DeOldify または cd DeOldify-master」 + Ente

拡張子を ipynb から py に変更する

jupyter nbconvert --to script *.ipynb + Enter

ipynbファイルを削除する

del /s *.ipynb + Enter

modelsフォルダの作成

「mkdir 'models'」 + Enter

ColorizerArtistic.phのダウンロード

wget https://data.deepai.org/deoldify/ColorizeArtistic_gen.pth -O ./models/ColorizeArtistic_gen.pth + Enter

必要なライブラリをインストールする

pip install -r requirements.txt +Enter

pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio --extra-index-url https://download.pytorch.org/whl/cu113

 

ImageColorizer.pyの「source_path」を以下のように変更する

source_path = 'test_images/*.png'

render factorを45に変更する

ImageColorizerColab.pyを以下のように書き換える

--------------------------------------------
# coding: utf-8

#NOTE: This must be the first call in order to work properly!
from deoldify import device
from deoldify.device_id import DeviceId
#choices: CPU, GPU0...GPU7
device.set(device=DeviceId.GPU0)
from PIL import Image as PilImage
import torch

if not torch.cuda.is_available():

print('GPU not available.')

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

get_ipython().system('wget https://media.githubusercontent.com/media/jantic/DeOldify/master/resource_images/watermark.png -O ./resource_images/watermark.png')

colorizer = get_image_colorizer(artistic=True)
import glob

source_url = '' #@param {type:"string"}
render_factor = 45 #@param {type: "slider", min: 7, max: 40}
watermarked = True #@param {type:"boolean"}

if source_url is not None and source_url !='':

image_path = colorizer.plot_transformed_image_from_url(url=source_url, render_factor=render_factor, compare=True, watermarked=watermarked)
show_image_in_notebook(image_path)

else:

print('Provide an image url and try again.')

files = sorted(glob.glob('test_images' + '/*.png'))

for i, file in enumerate (files):

image = cv2.imread(file, cv2.IMREAD_COLOR)
colorizer.plot_transformed_image(file, render_factor=45)
plt.close() # 追加
print(i+1)
---------------------------------------------------------------------

あとは、ImageColorizerColab.pyを実行するだけです。

「python ImageColorizerColab.py」 + Enter

白黒画像

カラー化画像

 

 

 

========================================================================