Spaces:
Runtime error
Runtime error
File size: 7,238 Bytes
2cd2c3f cd34982 4e3a320 2cd2c3f cd34982 2cd2c3f cd34982 8a87e00 cd34982 2cd2c3f cd34982 2cd2c3f cd34982 2cd2c3f bf71338 cd34982 8a87e00 bf71338 2cd2c3f bf71338 cd34982 2cd2c3f 4e3a320 50097ce 4e3a320 2cd2c3f 4e3a320 bf71338 cd34982 8a87e00 cd34982 bf71338 cd34982 2cd2c3f bf71338 cd34982 bf71338 2cd2c3f |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
import os
import random
import textwrap
import cv2
import gradio as gr
import numpy as np
from PIL import Image
from cv2.ximgproc import guidedFilter
from imgutils.data import load_image
from imgutils.restore import restore_with_nafnet, restore_with_scunet
def dynamic_clean_adverse(
input_image: Image.Image,
diameter_min: int = 4,
diameter_max: int = 6,
sigma_color_min: float = 6.0,
sigma_color_max: float = 10.0,
sigma_space_min: float = 6.0,
sigma_space_max: float = 10.0,
radius_min: int = 3,
radius_max: int = 6,
eps_min: float = 16.0,
eps_max: float = 24.0,
b_iters: int = 64,
g_iters: int = 8,
):
img = np.array(input_image).astype(np.float32)
y = img.copy()
for _ in range(b_iters):
diameter = random.randint(diameter_min, diameter_max)
sigma_color = random.random() * (sigma_color_max - sigma_color_min) + sigma_color_min
sigma_space = random.random() * (sigma_space_max - sigma_space_min) + sigma_space_min
y = cv2.bilateralFilter(y, diameter, sigma_color, sigma_space)
for _ in range(g_iters):
radius = random.randint(radius_min, radius_max)
eps = random.random() * (eps_max - eps_min) + eps_min
y = guidedFilter(img, y, radius, eps)
output_image = Image.fromarray(y.clip(0, 255).astype(np.uint8))
return output_image
def clean(
image: Image.Image,
diameter_min: int = 4,
diameter_max: int = 6,
sigma_color_min: float = 6.0,
sigma_color_max: float = 10.0,
sigma_space_min: float = 6.0,
sigma_space_max: float = 10.0,
radius_min: int = 3,
radius_max: int = 6,
eps_min: float = 16.0,
eps_max: float = 24.0,
b_iters: int = 64,
g_iters: int = 8,
use_scunet_clean: bool = False,
use_nafnet_clean: bool = False
) -> Image.Image:
image = load_image(image)
image = dynamic_clean_adverse(
image,
diameter_min, diameter_max,
sigma_color_min, sigma_color_max,
sigma_space_min, sigma_space_max,
radius_min, radius_max,
eps_min, eps_max,
b_iters, g_iters
)
if use_scunet_clean:
image = restore_with_scunet(image)
if use_nafnet_clean:
image = restore_with_nafnet(image)
return image
if __name__ == '__main__':
with gr.Blocks() as demo:
with gr.Row():
gr_markdown = gr.Markdown(textwrap.dedent("""
Cleaner for [MIST](https://github.com/mist-project/mist-v2)(**M**IST **I**s **S**tupid **T**rash) noises.
Inspired by https://huggingface.co/spaces/p1atdev/AdverseCleaner
* **Update 2023.12.18**, allow random dynamic adversarial clean and iterate steps.
""").strip())
with gr.Row():
with gr.Column():
gr_input_image = gr.Image(label='Input Image', type="pil")
gr_submit = gr.Button(value='MIST = MIST is Stupid Trash', variant='primary')
with gr.Accordion("Advanced Config", open=False):
with gr.Row():
gr_diameter_min = gr.Slider(
minimum=1, maximum=30, step=1, value=4,
label="Diameter Min (default = 4)", interactive=True,
)
gr_diameter_max = gr.Slider(
minimum=1, maximum=30, step=1, value=6,
label="Diameter Max (default = 6)", interactive=True,
)
with gr.Row():
gr_sigma_color_min = gr.Slider(
minimum=1, maximum=30, step=1, value=6,
label="SigmaColor Min (default = 6)", interactive=True,
)
gr_sigma_color_max = gr.Slider(
minimum=1, maximum=30, step=1, value=10,
label="SigmaColor Max (default = 10)", interactive=True,
)
with gr.Row():
gr_sigma_space_min = gr.Slider(
minimum=1, maximum=30, step=1, value=6,
label="SigmaSpace Min (default = 6)", interactive=True,
)
gr_sigma_space_max = gr.Slider(
minimum=1, maximum=30, step=1, value=10,
label="SigmaSpace Max (default = 10)", interactive=True,
)
with gr.Row():
gr_radius_min = gr.Slider(
minimum=1, maximum=30, step=1, value=3,
label="Radius Min (default = 3)", interactive=True,
)
gr_radius_max = gr.Slider(
minimum=1, maximum=30, step=1, value=6,
label="Radius Max (default = 6)", interactive=True,
)
with gr.Row():
gr_eps_min = gr.Slider(
minimum=1, maximum=30, step=1, value=16,
label="Accuracy Min (default = 16)", interactive=True,
)
gr_eps_max = gr.Slider(
minimum=1, maximum=30, step=1, value=24,
label="Accuracy Max (default = 24)", interactive=True,
)
with gr.Row():
gr_b_iters = gr.Slider(
minimum=1, maximum=256, step=1, value=64,
label="Bilateral Filter Iters (default = 64)", interactive=True,
)
gr_g_iters = gr.Slider(
minimum=1, maximum=32, step=1, value=8,
label="Guided Filter Iters (default = 8)", interactive=True,
)
with gr.Accordion("Extra Restoration", open=False):
with gr.Row():
gr_scunet = gr.Checkbox(label='Use SCUNET', value=False)
gr_nafnet = gr.Checkbox(label='Use NAFNET', value=False)
with gr.Column():
gr_output_image = gr.Image(label='Output Image', type="pil")
gr_submit.click(
fn=clean,
inputs=[
gr_input_image,
gr_diameter_min,
gr_diameter_max,
gr_sigma_color_min,
gr_sigma_color_max,
gr_sigma_space_min,
gr_sigma_space_max,
gr_radius_min,
gr_radius_max,
gr_eps_min,
gr_eps_max,
gr_b_iters,
gr_g_iters,
gr_scunet,
gr_nafnet,
],
outputs=[gr_output_image],
)
demo.queue(os.cpu_count()).launch()
|