2023-01-16 00:04:10 +00:00
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
from basicsr.utils.download_util import load_file_from_url
|
|
|
|
from gfpgan import GFPGANer
|
|
|
|
from os import path
|
|
|
|
from PIL import Image
|
|
|
|
from realesrgan import RealESRGANer
|
|
|
|
|
2023-01-16 00:13:28 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2023-01-16 00:04:10 +00:00
|
|
|
denoise_strength = 0.5
|
|
|
|
gfpgan_url = 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth'
|
|
|
|
resrgan_url = [
|
|
|
|
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth']
|
|
|
|
fp32 = True
|
|
|
|
model_name = 'RealESRGAN_x4plus'
|
|
|
|
netscale = 4
|
|
|
|
outscale = 4
|
|
|
|
pre_pad = 0
|
|
|
|
tile = 0
|
|
|
|
tile_pad = 10
|
|
|
|
|
|
|
|
|
|
|
|
def upscale_resrgan(source_image: Image) -> Image:
|
|
|
|
model_path = path.join('weights', model_name + '.pth')
|
|
|
|
if not path.isfile(model_path):
|
2023-01-16 00:13:28 +00:00
|
|
|
ROOT_DIR = path.dirname(path.abspath(__file__))
|
2023-01-16 00:04:10 +00:00
|
|
|
for url in resrgan_url:
|
|
|
|
model_path = load_file_from_url(
|
|
|
|
url=url, model_dir=path.join(ROOT_DIR, 'weights'), progress=True, file_name=None)
|
|
|
|
|
|
|
|
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
|
|
|
|
num_block=23, num_grow_ch=32, scale=4)
|
|
|
|
|
|
|
|
dni_weight = None
|
|
|
|
if model_name == 'realesr-general-x4v3' and denoise_strength != 1:
|
|
|
|
wdn_model_path = model_path.replace(
|
|
|
|
'realesr-general-x4v3', 'realesr-general-wdn-x4v3')
|
|
|
|
model_path = [model_path, wdn_model_path]
|
|
|
|
dni_weight = [denoise_strength, 1 - denoise_strength]
|
|
|
|
|
|
|
|
upsampler = RealESRGANer(
|
|
|
|
scale=netscale,
|
|
|
|
model_path=model_path,
|
|
|
|
dni_weight=dni_weight,
|
|
|
|
model=model,
|
|
|
|
tile=tile,
|
|
|
|
tile_pad=tile_pad,
|
|
|
|
pre_pad=pre_pad,
|
|
|
|
half=fp32)
|
|
|
|
|
2023-01-16 00:13:28 +00:00
|
|
|
image = np.array(source_image)
|
|
|
|
output, _ = upsampler.enhance(image, outscale=outscale)
|
2023-01-16 00:04:10 +00:00
|
|
|
|
2023-01-16 00:13:28 +00:00
|
|
|
return upscale_gfpgan(image, upsampler)
|
2023-01-16 00:04:10 +00:00
|
|
|
|
|
|
|
|
2023-01-16 00:13:28 +00:00
|
|
|
def upscale_gfpgan(image, upsampler) -> Image:
|
2023-01-16 00:04:10 +00:00
|
|
|
face_enhancer = GFPGANer(
|
|
|
|
model_path=gfpgan_url,
|
|
|
|
upscale=outscale,
|
|
|
|
arch='clean',
|
|
|
|
channel_multiplier=2,
|
|
|
|
bg_upsampler=upsampler)
|
|
|
|
|
2023-01-16 00:13:28 +00:00
|
|
|
_, _, output = face_enhancer.enhance(image, has_aligned=False, only_center_face=False, paste_back=True)
|
2023-01-16 00:04:10 +00:00
|
|
|
|
|
|
|
return output
|