File size: 3,999 Bytes
fdc356b
 
 
 
 
 
 
591a8fb
 
 
 
 
 
 
 
fdc356b
 
 
 
591a8fb
fdc356b
 
591a8fb
fdc356b
 
 
 
 
 
 
 
 
1a9d5fb
 
 
 
fdc356b
 
 
 
 
 
 
591a8fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdc356b
 
 
 
 
 
591a8fb
 
 
fdc356b
 
 
 
 
 
33ac189
 
 
1a9d5fb
fdc356b
aa0506b
 
 
 
c4136d4
 
 
 
 
 
 
 
 
 
 
 
1a9d5fb
 
 
fdc356b
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import numpy as np
import cv2
from PIL import Image
from cv2.ximgproc import guidedFilter
import gradio as gr


def clean_image(
    input_image: Image,
    diameter: float = 5,
    sigma_color: float = 8,
    sigma_space: float = 8,
    radius: float = 4,
    eps: float = 16,
) -> Image:
    img = np.array(input_image).astype(np.float32)
    y = img.copy()

    for _ in range(64):
        y = cv2.bilateralFilter(y, diameter, sigma_color, sigma_space)

    for _ in range(4):
        y = guidedFilter(img, y, radius, eps)

    output_image = Image.fromarray(y.clip(0, 255).astype(np.uint8))
    return output_image


def example(_image):
    pass


def send_to_input(output):
    return output


def ui():
    with gr.Blocks() as app:
        with gr.Row():
            with gr.Column():
                input_image = gr.Image(type="pil", label="Input Image")
                start_btn = gr.Button(value="Start", variant="primary")

                with gr.Accordion("Advanced Config", open=False):
                    # ref: https://github.com/gogodr/AdverseCleanerExtension
                    gr.Markdown("#### Bilateral Filter")
                    diameter_slider = gr.Slider(
                        minimum=1,
                        maximum=30,
                        step=1,
                        value=5,
                        label="Diameter",
                        interactive=True,
                    )
                    sigma_color_slider = gr.Slider(
                        minimum=1,
                        maximum=30,
                        step=1,
                        value=8,
                        label="SigmaColor",
                        interactive=True,
                    )
                    sigma_space_slider = gr.Slider(
                        minimum=1,
                        maximum=30,
                        step=1,
                        value=8,
                        label="SigmaSpace",
                        interactive=True,
                    )

                    gr.Markdown("#### Guided Filter")
                    radius_slider = gr.Slider(
                        minimum=1,
                        maximum=30,
                        step=1,
                        value=4,
                        label="Radius",
                        interactive=True,
                    )
                    eps_slider = gr.Slider(
                        minimum=1,
                        maximum=30,
                        step=1,
                        value=16,
                        label="Accuracy",
                        interactive=True,
                    )

                gr.Examples(
                    examples=[
                        ["./examples/sample1.jpg"],
                        ["./examples/sample2.jpg"],
                        ["./examples/sample3.jpg"],
                    ],
                    inputs=[
                        input_image,
                    ],
                    outputs=[],
                    fn=example,
                    cache_examples=True,
                )

            with gr.Column():
                output_image = gr.Image(
                    type="pil", label="Output Image", interactive=False
                )
                send_to_input_btn = gr.Button(value="Use as input", variant="secondary")

        gr.Markdown(
            "The lllyasviel's original repo is [here](https://github.com/lllyasviel/AdverseCleaner/tree/main)."
        )

        start_btn.click(
            fn=clean_image,
            inputs=[
                input_image,
                diameter_slider,
                sigma_color_slider,
                sigma_space_slider,
                radius_slider,
                eps_slider,
            ],
            outputs=[output_image],
        )
        send_to_input_btn.click(
            fn=send_to_input, inputs=[output_image], outputs=[input_image]
        )

    app.launch()


if __name__ == "__main__":
    ui()