wtf741 commited on
Commit
cd34982
1 Parent(s): d8e1b2b

add dynamic

Browse files
Files changed (1) hide show
  1. app.py +117 -63
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
 
3
  import cv2
4
  import gradio as gr
@@ -9,21 +10,33 @@ from imgutils.data import load_image
9
  from imgutils.restore import restore_with_nafnet, restore_with_scunet
10
 
11
 
12
- def clean_adverse(
13
  input_image: Image.Image,
14
- diameter: float = 5,
15
- sigma_color: float = 8,
16
- sigma_space: float = 8,
17
- radius: float = 4,
18
- eps: float = 16,
19
- ) -> Image.Image:
 
 
 
 
 
 
 
20
  img = np.array(input_image).astype(np.float32)
21
  y = img.copy()
22
 
23
- for _ in range(64):
 
 
 
24
  y = cv2.bilateralFilter(y, diameter, sigma_color, sigma_space)
25
 
26
- for _ in range(4):
 
 
27
  y = guidedFilter(img, y, radius, eps)
28
 
29
  output_image = Image.fromarray(y.clip(0, 255).astype(np.uint8))
@@ -33,18 +46,33 @@ def clean_adverse(
33
  def clean(
34
  image: Image.Image,
35
 
36
- diameter: float = 5,
37
- sigma_color: float = 8,
38
- sigma_space: float = 8,
39
- radius: float = 4,
40
- eps: float = 16,
 
 
 
 
 
 
 
41
 
42
  use_scunet_clean: bool = False,
43
  use_nafnet_clean: bool = False
44
  ) -> Image.Image:
45
  image = load_image(image)
46
 
47
- image = clean_adverse(image, diameter, sigma_color, sigma_space, radius, eps)
 
 
 
 
 
 
 
 
48
  if use_scunet_clean:
49
  image = restore_with_scunet(image)
50
  if use_nafnet_clean:
@@ -59,51 +87,70 @@ if __name__ == '__main__':
59
  gr_input_image = gr.Image(label='Input Image', type="pil")
60
  gr_submit = gr.Button(value='MIST = MIST is Stupid Trash')
61
  with gr.Accordion("Advanced Config", open=False):
62
- diameter_slider = gr.Slider(
63
- minimum=1,
64
- maximum=30,
65
- step=1,
66
- value=5,
67
- label="Diameter (default = 5)",
68
- interactive=True,
69
- )
70
- sigma_color_slider = gr.Slider(
71
- minimum=1,
72
- maximum=30,
73
- step=1,
74
- value=8,
75
- label="SigmaColor (default = 8)",
76
- interactive=True,
77
- )
78
- sigma_space_slider = gr.Slider(
79
- minimum=1,
80
- maximum=30,
81
- step=1,
82
- value=8,
83
- label="SigmaSpace (default = 8)",
84
- interactive=True,
85
- )
86
-
87
- radius_slider = gr.Slider(
88
- minimum=1,
89
- maximum=30,
90
- step=1,
91
- value=4,
92
- label="Radius (default = 4)",
93
- interactive=True,
94
- )
95
- eps_slider = gr.Slider(
96
- minimum=1,
97
- maximum=30,
98
- step=1,
99
- value=16,
100
- label="Accuracy (default = 16)",
101
- interactive=True,
102
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  with gr.Accordion("Extra Restoration", open=False):
105
- gr_scunet = gr.Checkbox(label='Use SCUNET', value=False)
106
- gr_nafnet = gr.Checkbox(label='Use NAFNET', value=False)
 
107
 
108
  with gr.Column():
109
  gr_output_image = gr.Image(label='Output Image', type="pil")
@@ -113,11 +160,18 @@ if __name__ == '__main__':
113
  inputs=[
114
  gr_input_image,
115
 
116
- diameter_slider,
117
- sigma_color_slider,
118
- sigma_space_slider,
119
- radius_slider,
120
- eps_slider,
 
 
 
 
 
 
 
121
 
122
  gr_scunet,
123
  gr_nafnet,
 
1
  import os
2
+ import random
3
 
4
  import cv2
5
  import gradio as gr
 
10
  from imgutils.restore import restore_with_nafnet, restore_with_scunet
11
 
12
 
13
+ def dynamic_clean_adverse(
14
  input_image: Image.Image,
15
+ diameter_min: int = 4,
16
+ diameter_max: int = 6,
17
+ sigma_color_min: float = 6.0,
18
+ sigma_color_max: float = 10.0,
19
+ sigma_space_min: float = 6.0,
20
+ sigma_space_max: float = 10.0,
21
+ radius_min: int = 3,
22
+ radius_max: int = 6,
23
+ eps_min: float = 16.0,
24
+ eps_max: float = 24.0,
25
+ b_iters: int = 64,
26
+ g_iters: int = 4,
27
+ ):
28
  img = np.array(input_image).astype(np.float32)
29
  y = img.copy()
30
 
31
+ for _ in range(b_iters):
32
+ diameter = random.randint(diameter_min, diameter_max)
33
+ sigma_color = random.random() * (sigma_color_max - sigma_color_min) + sigma_color_min
34
+ sigma_space = random.random() * (sigma_space_max - sigma_space_min) + sigma_space_min
35
  y = cv2.bilateralFilter(y, diameter, sigma_color, sigma_space)
36
 
37
+ for _ in range(g_iters):
38
+ radius = random.randint(radius_min, radius_max)
39
+ eps = random.random() * (eps_max - eps_min) + eps_min
40
  y = guidedFilter(img, y, radius, eps)
41
 
42
  output_image = Image.fromarray(y.clip(0, 255).astype(np.uint8))
 
46
  def clean(
47
  image: Image.Image,
48
 
49
+ diameter_min: int = 4,
50
+ diameter_max: int = 6,
51
+ sigma_color_min: float = 6.0,
52
+ sigma_color_max: float = 10.0,
53
+ sigma_space_min: float = 6.0,
54
+ sigma_space_max: float = 10.0,
55
+ radius_min: int = 3,
56
+ radius_max: int = 6,
57
+ eps_min: float = 16.0,
58
+ eps_max: float = 24.0,
59
+ b_iters: int = 64,
60
+ g_iters: int = 4,
61
 
62
  use_scunet_clean: bool = False,
63
  use_nafnet_clean: bool = False
64
  ) -> Image.Image:
65
  image = load_image(image)
66
 
67
+ image = dynamic_clean_adverse(
68
+ image,
69
+ diameter_min, diameter_max,
70
+ sigma_color_min, sigma_color_max,
71
+ sigma_space_min, sigma_space_max,
72
+ radius_min, radius_max,
73
+ eps_min, eps_max,
74
+ b_iters, g_iters
75
+ )
76
  if use_scunet_clean:
77
  image = restore_with_scunet(image)
78
  if use_nafnet_clean:
 
87
  gr_input_image = gr.Image(label='Input Image', type="pil")
88
  gr_submit = gr.Button(value='MIST = MIST is Stupid Trash')
89
  with gr.Accordion("Advanced Config", open=False):
90
+ with gr.Row():
91
+ gr_diameter_min = gr.Slider(
92
+ minimum=1, maximum=30, step=1, value=4,
93
+ label="Diameter Min (default = 4)", interactive=True,
94
+ )
95
+ gr_diameter_max = gr.Slider(
96
+ minimum=1, maximum=30, step=1, value=6,
97
+ label="Diameter Max (default = 6)", interactive=True,
98
+ )
99
+
100
+ with gr.Row():
101
+ gr_sigma_color_min = gr.Slider(
102
+ minimum=1, maximum=30, step=1, value=6,
103
+ label="SigmaColor Min (default = 6)", interactive=True,
104
+ )
105
+ gr_sigma_color_max = gr.Slider(
106
+ minimum=1, maximum=30, step=1, value=10,
107
+ label="SigmaColor Max (default = 10)", interactive=True,
108
+ )
109
+
110
+ with gr.Row():
111
+ gr_sigma_space_min = gr.Slider(
112
+ minimum=1, maximum=30, step=1, value=6,
113
+ label="SigmaSpace Min (default = 6)", interactive=True,
114
+ )
115
+ gr_sigma_space_max = gr.Slider(
116
+ minimum=1, maximum=30, step=1, value=10,
117
+ label="SigmaSpace Max (default = 10)", interactive=True,
118
+ )
119
+
120
+ with gr.Row():
121
+ gr_radius_min = gr.Slider(
122
+ minimum=1, maximum=30, step=1, value=3,
123
+ label="Radius Min (default = 3)", interactive=True,
124
+ )
125
+ gr_radius_max = gr.Slider(
126
+ minimum=1, maximum=30, step=1, value=6,
127
+ label="Radius Max (default = 6)", interactive=True,
128
+ )
129
+
130
+ with gr.Row():
131
+ gr_eps_min = gr.Slider(
132
+ minimum=1, maximum=30, step=1, value=16,
133
+ label="Accuracy Min (default = 16)", interactive=True,
134
+ )
135
+ gr_eps_max = gr.Slider(
136
+ minimum=1, maximum=30, step=1, value=24,
137
+ label="Accuracy Max (default = 24)", interactive=True,
138
+ )
139
+
140
+ with gr.Row():
141
+ gr_b_iters = gr.Slider(
142
+ minimum=1, maximum=256, step=1, value=64,
143
+ label="Bilateral Filter Iters (default = 64)", interactive=True,
144
+ )
145
+ gr_g_iters = gr.Slider(
146
+ minimum=1, maximum=32, step=1, value=4,
147
+ label="Guided Filter Iters (default = 4)", interactive=True,
148
+ )
149
 
150
  with gr.Accordion("Extra Restoration", open=False):
151
+ with gr.Row():
152
+ gr_scunet = gr.Checkbox(label='Use SCUNET', value=False)
153
+ gr_nafnet = gr.Checkbox(label='Use NAFNET', value=False)
154
 
155
  with gr.Column():
156
  gr_output_image = gr.Image(label='Output Image', type="pil")
 
160
  inputs=[
161
  gr_input_image,
162
 
163
+ gr_diameter_min,
164
+ gr_diameter_max,
165
+ gr_sigma_color_min,
166
+ gr_sigma_color_max,
167
+ gr_sigma_space_min,
168
+ gr_sigma_space_max,
169
+ gr_radius_min,
170
+ gr_radius_max,
171
+ gr_eps_min,
172
+ gr_eps_max,
173
+ gr_b_iters,
174
+ gr_g_iters,
175
 
176
  gr_scunet,
177
  gr_nafnet,