aboutsummaryrefslogtreecommitdiff
path: root/modules/realesrgan_model.py
diff options
context:
space:
mode:
authorAUTOMATIC <16777216c@gmail.com>2022-09-03 12:08:45 +0300
committerAUTOMATIC <16777216c@gmail.com>2022-09-03 12:08:45 +0300
commit345028099d893f8a66726cfd13627d8cc1bcc724 (patch)
treeacb1da553620b7e7139db840ef43accf71b786a8 /modules/realesrgan_model.py
parentd7b67d9b40e47ede766d3beb149b0c2b74651ece (diff)
split codebase into multiple files; to anyone this affects negatively: sorry
Diffstat (limited to 'modules/realesrgan_model.py')
-rw-r--r--modules/realesrgan_model.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/modules/realesrgan_model.py b/modules/realesrgan_model.py
new file mode 100644
index 00000000..5a6666a3
--- /dev/null
+++ b/modules/realesrgan_model.py
@@ -0,0 +1,70 @@
+import sys
+import traceback
+from collections import namedtuple
+import numpy as np
+from PIL import Image
+
+from modules.shared import cmd_opts
+
+RealesrganModelInfo = namedtuple("RealesrganModelInfo", ["name", "location", "model", "netscale"])
+
+realesrgan_models = []
+have_realesrgan = False
+RealESRGANer_constructor = None
+
+def setup_realesrgan():
+ global realesrgan_models
+ global have_realesrgan
+ global RealESRGANer_constructor
+
+ try:
+ from basicsr.archs.rrdbnet_arch import RRDBNet
+ from realesrgan import RealESRGANer
+ from realesrgan.archs.srvgg_arch import SRVGGNetCompact
+
+ realesrgan_models = [
+ RealesrganModelInfo(
+ name="Real-ESRGAN 4x plus",
+ location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
+ netscale=4, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
+ ),
+ RealesrganModelInfo(
+ name="Real-ESRGAN 4x plus anime 6B",
+ location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
+ netscale=4, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
+ ),
+ RealesrganModelInfo(
+ name="Real-ESRGAN 2x plus",
+ location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
+ netscale=2, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
+ ),
+ ]
+ have_realesrgan = True
+ RealESRGANer_constructor = RealESRGANer
+
+ except Exception:
+ print("Error importing Real-ESRGAN:", file=sys.stderr)
+ print(traceback.format_exc(), file=sys.stderr)
+
+ realesrgan_models = [RealesrganModelInfo('None', '', 0, None)]
+ have_realesrgan = False
+
+
+def upscale_with_realesrgan(image, RealESRGAN_upscaling, RealESRGAN_model_index):
+ if not have_realesrgan or RealESRGANer_constructor is None:
+ return image
+
+ info = realesrgan_models[RealESRGAN_model_index]
+
+ model = info.model()
+ upsampler = RealESRGANer_constructor(
+ scale=info.netscale,
+ model_path=info.location,
+ model=model,
+ half=not cmd_opts.no_half
+ )
+
+ upsampled = upsampler.enhance(np.array(image), outscale=RealESRGAN_upscaling)[0]
+
+ image = Image.fromarray(upsampled)
+ return image