スポンサーリンク
This time I tried RealESRGAN on google colab, which allows for higher resolution.
This article is a reminder to get RealESRGAN working.
contents
スポンサーリンク
abstract
How to use RealESRGAN on google colab to enhance images
1.requirement
Google Colab
git:clone https://github.com/xinntao/Real-ESRGAN.git
model : RealESRGAN_x4plus
2.Environment construction & code writing
2.1 install&download
Install the necessary modules by cloning from git with the following command.
and then download the weights to be used in the model.
!git clone https://github.com/xinntao/Real-ESRGAN.git %cd Real-ESRGAN !apt-get install ffmpeg !pip install pydub !pip install basicsr !pip install facexlib !pip install gfpgan !pip install -r requirements.txt !python setup.py develop !wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P experiments/pretrained_models
2.2 Model Execution ~ Result Saving
Describes the process of loading the specified model and arguments and saving them as high quality images.
import cv2 import glob import os from basicsr.archs.rrdbnet_arch import RRDBNet from realesrgan import RealESRGANer import subprocess from pydub import AudioSegment from google.colab.patches import cv2_imshow model_path = "experiments/pretrained_models/RealESRGAN_x4plus.pth" #@param {type:"string"} path = 'path/to/image/' #@param {type:"string"} # variable block = 23 netscale = 4 tile = 0 tile_pad = 10 pre_pad = 0 half = False face_enhance = False outscale = 8 #@param {type:"number"} output_dir = '/path/to/output_dir' #@param {type:"string"} resize_rate = 0.5 limit_img_size = 1000 if 'RealESRGAN_x4plus_anime_6B.pth' in model_path: block = 6 elif 'RealESRGAN_x2plus.pth' in model_path: netscale = 2 model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=block, num_grow_ch=32, scale=netscale) upsampler = RealESRGANer( scale=netscale, model_path=model_path, model=model, tile=tile, tile_pad=tile_pad, pre_pad=pre_pad, half=half) if face_enhance: from gfpgan import GFPGANer face_enhancer = GFPGANer( model_path='path/to/model', upscale=outscale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler) else: pass os.makedirs(output_dir, exist_ok=True) img = cv2.imread(path) height = img.shape[0] width = img.shape[1] print('original image:', height, width) if height < width: long_size = width else: long_size = height # Resize the input image if its size is larger than 1000pixel, because it will exceed the memory. if limit_img_size < long_size: resize_rate = limit_img_size/long_size in_img = cv2.resize(img , (int(width*resize_rate), int(height*resize_rate))) print('resize image', in_img.shape[0], in_img.shape[1]) else: in_img = img if face_enhance: # _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True) pass else: output, _ = upsampler.enhance(in_img, outscale=outscale) save_path = path/to/save cv2.imwrite(save_path, output) del output del in_img del img del upsampler
3.result
original image
Zoom in for comparison.
left:normal / right:After high image quality
The skin and eye area are more beautiful in the image after the higher resolution.
The hair was a little crushed and needs to be adjusted