2022-09-29 22:46:23 +00:00
|
|
|
import os
|
|
|
|
from abc import abstractmethod
|
|
|
|
|
|
|
|
import PIL
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
import modules.shared
|
|
|
|
from modules import modelloader, shared
|
|
|
|
|
|
|
|
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
|
2022-10-30 11:52:50 +00:00
|
|
|
NEAREST = (Image.Resampling.NEAREST if hasattr(Image, 'Resampling') else Image.NEAREST)
|
2022-09-29 22:46:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Upscaler:
|
|
|
|
name = None
|
|
|
|
model_path = None
|
|
|
|
model_name = None
|
|
|
|
model_url = None
|
|
|
|
enable = True
|
|
|
|
filter = None
|
|
|
|
model = None
|
|
|
|
user_path = None
|
|
|
|
scalers: []
|
|
|
|
tile = True
|
|
|
|
|
|
|
|
def __init__(self, create_dirs=False):
|
|
|
|
self.mod_pad_h = None
|
|
|
|
self.tile_size = modules.shared.opts.ESRGAN_tile
|
|
|
|
self.tile_pad = modules.shared.opts.ESRGAN_tile_overlap
|
|
|
|
self.device = modules.shared.device
|
|
|
|
self.img = None
|
|
|
|
self.output = None
|
|
|
|
self.scale = 1
|
|
|
|
self.half = not modules.shared.cmd_opts.no_half
|
|
|
|
self.pre_pad = 0
|
|
|
|
self.mod_scale = None
|
2023-05-19 06:09:00 +00:00
|
|
|
self.model_download_path = None
|
2022-10-06 18:36:08 +00:00
|
|
|
|
2022-10-09 10:48:04 +00:00
|
|
|
if self.model_path is None and self.name:
|
2023-01-25 16:15:42 +00:00
|
|
|
self.model_path = os.path.join(shared.models_path, self.name)
|
2022-10-06 18:36:08 +00:00
|
|
|
if self.model_path and create_dirs:
|
|
|
|
os.makedirs(self.model_path, exist_ok=True)
|
2022-09-29 22:46:23 +00:00
|
|
|
|
|
|
|
try:
|
2023-05-10 06:02:23 +00:00
|
|
|
import cv2 # noqa: F401
|
2022-09-29 22:46:23 +00:00
|
|
|
self.can_tile = True
|
2023-05-10 05:25:25 +00:00
|
|
|
except Exception:
|
2022-09-29 22:46:23 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def do_upscale(self, img: PIL.Image, selected_model: str):
|
|
|
|
return img
|
|
|
|
|
2023-01-02 17:01:16 +00:00
|
|
|
def upscale(self, img: PIL.Image, scale, selected_model: str = None):
|
2022-09-29 22:46:23 +00:00
|
|
|
self.scale = scale
|
2023-01-02 17:01:16 +00:00
|
|
|
dest_w = int(img.width * scale)
|
|
|
|
dest_h = int(img.height * scale)
|
2022-11-04 19:56:18 +00:00
|
|
|
|
2023-05-10 08:37:18 +00:00
|
|
|
for _ in range(3):
|
2022-11-04 19:56:18 +00:00
|
|
|
shape = (img.width, img.height)
|
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
img = self.do_upscale(img, selected_model)
|
2022-11-04 19:56:18 +00:00
|
|
|
|
|
|
|
if shape == (img.width, img.height):
|
|
|
|
break
|
|
|
|
|
|
|
|
if img.width >= dest_w and img.height >= dest_h:
|
|
|
|
break
|
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
if img.width != dest_w or img.height != dest_h:
|
2022-09-30 07:38:48 +00:00
|
|
|
img = img.resize((int(dest_w), int(dest_h)), resample=LANCZOS)
|
2022-09-29 22:46:23 +00:00
|
|
|
|
|
|
|
return img
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def load_model(self, path: str):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def find_models(self, ext_filter=None) -> list:
|
|
|
|
return modelloader.load_models(model_path=self.model_path, model_url=self.model_url, command_path=self.user_path)
|
|
|
|
|
|
|
|
def update_status(self, prompt):
|
|
|
|
print(f"\nextras: {prompt}", file=shared.progress_print_out)
|
|
|
|
|
|
|
|
|
|
|
|
class UpscalerData:
|
|
|
|
name = None
|
|
|
|
data_path = None
|
|
|
|
scale: int = 4
|
|
|
|
scaler: Upscaler = None
|
|
|
|
model: None
|
|
|
|
|
|
|
|
def __init__(self, name: str, path: str, upscaler: Upscaler = None, scale: int = 4, model=None):
|
|
|
|
self.name = name
|
|
|
|
self.data_path = path
|
2023-01-17 10:57:55 +00:00
|
|
|
self.local_data_path = path
|
2022-09-29 22:46:23 +00:00
|
|
|
self.scaler = upscaler
|
|
|
|
self.scale = scale
|
|
|
|
self.model = model
|
|
|
|
|
|
|
|
|
|
|
|
class UpscalerNone(Upscaler):
|
|
|
|
name = "None"
|
|
|
|
scalers = []
|
|
|
|
|
|
|
|
def load_model(self, path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def do_upscale(self, img, selected_model=None):
|
|
|
|
return img
|
|
|
|
|
|
|
|
def __init__(self, dirname=None):
|
|
|
|
super().__init__(False)
|
|
|
|
self.scalers = [UpscalerData("None", None, self)]
|
|
|
|
|
|
|
|
|
|
|
|
class UpscalerLanczos(Upscaler):
|
|
|
|
scalers = []
|
|
|
|
|
|
|
|
def do_upscale(self, img, selected_model=None):
|
|
|
|
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=LANCZOS)
|
|
|
|
|
|
|
|
def load_model(self, _):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self, dirname=None):
|
|
|
|
super().__init__(False)
|
|
|
|
self.name = "Lanczos"
|
|
|
|
self.scalers = [UpscalerData("Lanczos", None, self)]
|
|
|
|
|
2022-10-30 11:52:50 +00:00
|
|
|
|
|
|
|
class UpscalerNearest(Upscaler):
|
|
|
|
scalers = []
|
|
|
|
|
|
|
|
def do_upscale(self, img, selected_model=None):
|
|
|
|
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=NEAREST)
|
|
|
|
|
|
|
|
def load_model(self, _):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self, dirname=None):
|
|
|
|
super().__init__(False)
|
|
|
|
self.name = "Nearest"
|
2023-01-25 16:15:42 +00:00
|
|
|
self.scalers = [UpscalerData("Nearest", None, self)]
|