2023-01-23 14:24:43 +08:00
|
|
|
from PIL import Image
|
|
|
|
import numpy as np
|
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
from modules import scripts_postprocessing, codeformer_model, ui_components
|
2023-01-23 14:24:43 +08:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptPostprocessingCodeFormer(scripts_postprocessing.ScriptPostprocessing):
|
|
|
|
name = "CodeFormer"
|
|
|
|
order = 3000
|
|
|
|
|
|
|
|
def ui(self):
|
2023-12-02 23:01:11 +08:00
|
|
|
with ui_components.InputAccordion(False, label="CodeFormer") as enable:
|
|
|
|
with gr.Row():
|
|
|
|
codeformer_visibility = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Visibility", value=1.0, elem_id="extras_codeformer_visibility")
|
|
|
|
codeformer_weight = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="Weight (0 = maximum effect, 1 = minimum effect)", value=0, elem_id="extras_codeformer_weight")
|
2023-01-23 14:24:43 +08:00
|
|
|
|
|
|
|
return {
|
2023-12-02 23:01:11 +08:00
|
|
|
"enable": enable,
|
2023-01-23 14:24:43 +08:00
|
|
|
"codeformer_visibility": codeformer_visibility,
|
|
|
|
"codeformer_weight": codeformer_weight,
|
|
|
|
}
|
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
def process(self, pp: scripts_postprocessing.PostprocessedImage, enable, codeformer_visibility, codeformer_weight):
|
|
|
|
if codeformer_visibility == 0 or not enable:
|
2023-01-23 14:24:43 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
restored_img = codeformer_model.codeformer.restore(np.array(pp.image, dtype=np.uint8), w=codeformer_weight)
|
|
|
|
res = Image.fromarray(restored_img)
|
|
|
|
|
|
|
|
if codeformer_visibility < 1.0:
|
|
|
|
res = Image.blend(pp.image, res, codeformer_visibility)
|
|
|
|
|
|
|
|
pp.image = res
|
|
|
|
pp.info["CodeFormer visibility"] = round(codeformer_visibility, 3)
|
|
|
|
pp.info["CodeFormer weight"] = round(codeformer_weight, 3)
|