File size: 1,954 Bytes
0070fce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
import numpy as np
from modules import codeformer_model, scripts_postprocessing
from modules.ui_components import InputAccordion
from PIL import Image


class CodeFormerPostprocessing(scripts_postprocessing.ScriptPostprocessing):
    name = "CodeFormer"
    order = 3000

    def ui(self):
        with InputAccordion(False, label="CodeFormer") as enable:
            with gr.Row():
                codeformer_visibility = gr.Slider(
                    label="Visibility",
                    value=1.0,
                    minimum=0.0,
                    maximum=1.0,
                    step=0.05,
                    elem_id="extras_codeformer_visibility",
                )
                codeformer_weight = gr.Slider(
                    label="Weight (0 = maximum effect, 1 = minimum effect)",
                    value=0,
                    minimum=0.0,
                    maximum=1.0,
                    step=0.05,
                    elem_id="extras_codeformer_weight",
                )

        return {
            "enable": enable,
            "codeformer_visibility": codeformer_visibility,
            "codeformer_weight": codeformer_weight,
        }

    def process(

        self,

        pp: scripts_postprocessing.PostprocessedImage,

        enable,

        codeformer_visibility,

        codeformer_weight,

    ):
        if not enable or codeformer_visibility < 0.05:
            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, 2)
        pp.info["CodeFormer Weight"] = round(codeformer_weight, 2)