Update app.py
Browse files
app.py
CHANGED
|
@@ -13,35 +13,41 @@ def process_image(
|
|
| 13 |
contrast,
|
| 14 |
saturation
|
| 15 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Pillow Image -> Numpy array ๋ณํ
|
| 17 |
img_np = np.array(img)
|
|
|
|
| 18 |
# ================ 1. ๋
ธ์ด์ฆ ์ ๊ฑฐ ================
|
| 19 |
-
#
|
| 20 |
if len(img_np.shape) == 3:
|
| 21 |
-
# RGB ์ด๋ฏธ์ง
|
| 22 |
-
|
| 23 |
-
sigma_est = np.mean(estimate_sigma(img_np, multichannel=True))
|
| 24 |
-
# denoise_strength๋ฅผ h ๊ฐ์ผ๋ก ์ฌ์ฉ (์์ ์ค์ ๊ฐ๋ฅ)
|
| 25 |
denoised = denoise_nl_means(
|
| 26 |
img_np,
|
| 27 |
h=denoise_strength * sigma_est,
|
| 28 |
patch_size=5,
|
| 29 |
patch_distance=3,
|
| 30 |
-
multichannel=True
|
| 31 |
fast_mode=True
|
| 32 |
)
|
| 33 |
else:
|
| 34 |
-
#
|
| 35 |
-
sigma_est = np.mean(estimate_sigma(img_np,
|
| 36 |
denoised = denoise_nl_means(
|
| 37 |
img_np,
|
| 38 |
h=denoise_strength * sigma_est,
|
| 39 |
patch_size=5,
|
| 40 |
patch_distance=3,
|
| 41 |
-
multichannel=False
|
| 42 |
fast_mode=True
|
| 43 |
)
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
denoised = (denoised * 255).astype(np.uint8)
|
| 46 |
denoised_img = Image.fromarray(denoised)
|
| 47 |
|
|
@@ -50,6 +56,7 @@ def process_image(
|
|
| 50 |
sharpened_img = enhancer_sharpness.enhance(sharpen_strength)
|
| 51 |
|
| 52 |
# ================ 3. ๊ฐ๋ง ๋ณด์ ================
|
|
|
|
| 53 |
gamma_np = np.array(sharpened_img).astype(np.float32) / 255.0
|
| 54 |
gamma_corrected = np.power(gamma_np, 1.0 / gamma)
|
| 55 |
gamma_corrected = (gamma_corrected * 255).astype(np.uint8)
|
|
@@ -81,10 +88,14 @@ def generate_output(
|
|
| 81 |
contrast,
|
| 82 |
saturation
|
| 83 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
if input_image is None:
|
| 85 |
return None, None
|
| 86 |
|
| 87 |
-
# ๋ณํ๋ ์ด๋ฏธ์ง
|
| 88 |
transformed = process_image(
|
| 89 |
input_image,
|
| 90 |
denoise_strength,
|
|
@@ -95,19 +106,21 @@ def generate_output(
|
|
| 95 |
saturation
|
| 96 |
)
|
| 97 |
|
| 98 |
-
# ๋ค์ด๋ก๋ ๊ฐ๋ฅํ JPG ํ์ผ ์์ฑ
|
| 99 |
with io.BytesIO() as output:
|
| 100 |
transformed.save(output, format="JPEG")
|
| 101 |
contents = output.getvalue()
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
def main():
|
| 107 |
with gr.Blocks() as demo:
|
| 108 |
gr.Markdown("## ํ๋ฐฑ ๋ถ์๊ธฐ ์ฌ์ง ๋ณํ ๋ฐ๋ชจ")
|
| 109 |
|
| 110 |
-
# ์ด๋ฏธ์ง ์
๋ก๋
|
| 111 |
with gr.Row():
|
| 112 |
input_image = gr.Image(
|
| 113 |
label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋",
|
|
@@ -140,10 +153,15 @@ def main():
|
|
| 140 |
label="์ฑ๋ (saturation)"
|
| 141 |
)
|
| 142 |
|
| 143 |
-
# ๊ฒฐ๊ณผ
|
| 144 |
with gr.Row():
|
| 145 |
-
output_image = gr.Image(
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
# ๋ณํ ์คํ ๋ฒํผ
|
| 149 |
btn = gr.Button("๋ณํ ์คํ")
|
|
@@ -161,10 +179,10 @@ def main():
|
|
| 161 |
outputs=[output_image, download_file]
|
| 162 |
)
|
| 163 |
|
| 164 |
-
# ์๋ณธ vs ๊ฒฐ๊ณผ ๋น๊ต
|
| 165 |
compare_output = gr.Image(
|
| 166 |
type="pil",
|
| 167 |
-
label="๋น๊ต ๊ฒฐ๊ณผ
|
| 168 |
height=300
|
| 169 |
)
|
| 170 |
compare_btn = gr.Button("์๋ณธ vs ๋ณํ๋ณธ ๋น๊ต")
|
|
@@ -189,13 +207,14 @@ def main():
|
|
| 189 |
contrast,
|
| 190 |
saturation
|
| 191 |
)
|
| 192 |
-
# ๋ ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ก๋ก ํฉ์ณ
|
| 193 |
input_w, input_h = input_image.size
|
| 194 |
transformed_w, transformed_h = transformed.size
|
| 195 |
new_w = input_w + transformed_w
|
| 196 |
new_h = max(input_h, transformed_h)
|
| 197 |
|
| 198 |
new_image = Image.new("RGB", (new_w, new_h))
|
|
|
|
| 199 |
new_image.paste(input_image.convert("RGB"), (0, 0))
|
| 200 |
new_image.paste(transformed.convert("RGB"), (input_w, 0))
|
| 201 |
|
|
|
|
| 13 |
contrast,
|
| 14 |
saturation
|
| 15 |
):
|
| 16 |
+
"""
|
| 17 |
+
์
๋ก๋๋ ์ด๋ฏธ์ง๋ฅผ ํ๋ผ๋ฏธํฐ(๋
ธ์ด์ฆ ์ ๊ฑฐ, ์คํ๋, ๊ฐ๋ง ๋ณด์ , ๋ฐ๊ธฐ, ๋๋น, ์ฑ๋)์ ๋ฐ๋ผ
|
| 18 |
+
์ต์ข
์ ์ผ๋ก ํ๋ฐฑ ์ฌ์ง์ผ๋ก ๋ณํํ์ฌ ๋ฐํํฉ๋๋ค.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
# Pillow Image -> Numpy array ๋ณํ
|
| 22 |
img_np = np.array(img)
|
| 23 |
+
|
| 24 |
# ================ 1. ๋
ธ์ด์ฆ ์ ๊ฑฐ ================
|
| 25 |
+
# ์ด๋ฏธ์ง๋ฅผ RGB(3์ฐจ์)์ธ์ง, ํ๋ฐฑ(2์ฐจ์)์ธ์ง ํ๋ณ
|
| 26 |
if len(img_np.shape) == 3:
|
| 27 |
+
# RGB ์ด๋ฏธ์ง์ ๊ฒฝ์ฐ channel_axis=-1 ์ฌ์ฉ
|
| 28 |
+
sigma_est = np.mean(estimate_sigma(img_np, channel_axis=-1))
|
|
|
|
|
|
|
| 29 |
denoised = denoise_nl_means(
|
| 30 |
img_np,
|
| 31 |
h=denoise_strength * sigma_est,
|
| 32 |
patch_size=5,
|
| 33 |
patch_distance=3,
|
| 34 |
+
channel_axis=-1, # multichannel=True ๋์ channel_axis=-1
|
| 35 |
fast_mode=True
|
| 36 |
)
|
| 37 |
else:
|
| 38 |
+
# ํ๋ฐฑ(2์ฐจ์) ์ด๋ฏธ์ง์ ๊ฒฝ์ฐ channel_axis=None
|
| 39 |
+
sigma_est = np.mean(estimate_sigma(img_np, channel_axis=None))
|
| 40 |
denoised = denoise_nl_means(
|
| 41 |
img_np,
|
| 42 |
h=denoise_strength * sigma_est,
|
| 43 |
patch_size=5,
|
| 44 |
patch_distance=3,
|
| 45 |
+
channel_axis=None, # multichannel=False ๋์ channel_axis=None
|
| 46 |
fast_mode=True
|
| 47 |
)
|
| 48 |
+
|
| 49 |
+
# denoise ๊ฒฐ๊ณผ๋ [0,1] ๋ฒ์์ด๋ฏ๋ก, ๋ค์ [0,255]๋ก ๋ณํ
|
| 50 |
+
denoised = np.clip(denoised, 0, 1)
|
| 51 |
denoised = (denoised * 255).astype(np.uint8)
|
| 52 |
denoised_img = Image.fromarray(denoised)
|
| 53 |
|
|
|
|
| 56 |
sharpened_img = enhancer_sharpness.enhance(sharpen_strength)
|
| 57 |
|
| 58 |
# ================ 3. ๊ฐ๋ง ๋ณด์ ================
|
| 59 |
+
# Pillow์ ๊ฐ๋ง ๋ณด์ ์ด ์์ผ๋ฏ๋ก, NumPy๋ก ์ฒ๋ฆฌ
|
| 60 |
gamma_np = np.array(sharpened_img).astype(np.float32) / 255.0
|
| 61 |
gamma_corrected = np.power(gamma_np, 1.0 / gamma)
|
| 62 |
gamma_corrected = (gamma_corrected * 255).astype(np.uint8)
|
|
|
|
| 88 |
contrast,
|
| 89 |
saturation
|
| 90 |
):
|
| 91 |
+
"""
|
| 92 |
+
Gradio์์ ํธ์ถ๋๋ ํจ์๋ก, ๋ณํ๋ ํ๋ฐฑ ์ด๋ฏธ์ง๋ฅผ
|
| 93 |
+
๋ฐ๋ก ํ์ํ ์ด๋ฏธ์ง์ ๋ค์ด๋ก๋์ฉ ํ์ผ๋ก ํจ๊ป ๋ฐํํฉ๋๋ค.
|
| 94 |
+
"""
|
| 95 |
if input_image is None:
|
| 96 |
return None, None
|
| 97 |
|
| 98 |
+
# 1) ๋ณํ๋ ์ด๋ฏธ์ง (PIL)
|
| 99 |
transformed = process_image(
|
| 100 |
input_image,
|
| 101 |
denoise_strength,
|
|
|
|
| 106 |
saturation
|
| 107 |
)
|
| 108 |
|
| 109 |
+
# 2) ๋ค์ด๋ก๋ ๊ฐ๋ฅํ JPG ํ์ผ ์์ฑ
|
| 110 |
with io.BytesIO() as output:
|
| 111 |
transformed.save(output, format="JPEG")
|
| 112 |
contents = output.getvalue()
|
| 113 |
|
| 114 |
+
# Gradio์ File ํ์: (ํ์ผ ๋ฐ์ดํธ, MIME ํ์
, ๋ค์ด๋ก๋ ์ ํ์๋ ํ์ผ๋ช
)
|
| 115 |
+
file_data = (contents, "image/jpeg", "transformed.jpg")
|
| 116 |
+
|
| 117 |
+
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ Gradio์ Image๋ก ๋ณด์ฌ์ฃผ๊ณ , ๋ค์ด๋ก๋๋ ํ ์ ์๊ฒ๋ ๋ ๊ฐ์ง๋ฅผ ๋ฐํ
|
| 118 |
+
return transformed, file_data
|
| 119 |
|
| 120 |
def main():
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
gr.Markdown("## ํ๋ฐฑ ๋ถ์๊ธฐ ์ฌ์ง ๋ณํ ๋ฐ๋ชจ")
|
| 123 |
|
|
|
|
| 124 |
with gr.Row():
|
| 125 |
input_image = gr.Image(
|
| 126 |
label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋",
|
|
|
|
| 153 |
label="์ฑ๋ (saturation)"
|
| 154 |
)
|
| 155 |
|
| 156 |
+
# ๊ฒฐ๊ณผ(๋ณํ๋ ์ด๋ฏธ์ง, ๋ค์ด๋ก๋ ๋ฒํผ)
|
| 157 |
with gr.Row():
|
| 158 |
+
output_image = gr.Image(
|
| 159 |
+
label="๋ณํ๋ ์ด๋ฏธ์ง",
|
| 160 |
+
height=300
|
| 161 |
+
)
|
| 162 |
+
download_file = gr.File(
|
| 163 |
+
label="JPG ๋ค์ด๋ก๋"
|
| 164 |
+
)
|
| 165 |
|
| 166 |
# ๋ณํ ์คํ ๋ฒํผ
|
| 167 |
btn = gr.Button("๋ณํ ์คํ")
|
|
|
|
| 179 |
outputs=[output_image, download_file]
|
| 180 |
)
|
| 181 |
|
| 182 |
+
# ์๋ณธ vs ๊ฒฐ๊ณผ ๋น๊ต
|
| 183 |
compare_output = gr.Image(
|
| 184 |
type="pil",
|
| 185 |
+
label="์๋ณธ vs ๋ณํ๋ณธ ๋น๊ต ๊ฒฐ๊ณผ",
|
| 186 |
height=300
|
| 187 |
)
|
| 188 |
compare_btn = gr.Button("์๋ณธ vs ๋ณํ๋ณธ ๋น๊ต")
|
|
|
|
| 207 |
contrast,
|
| 208 |
saturation
|
| 209 |
)
|
| 210 |
+
# ๋ ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ก๋ก ํฉ์ณ ํ๋์ ์ด๋ฏธ์ง๋ก ๋ง๋ ๋ค
|
| 211 |
input_w, input_h = input_image.size
|
| 212 |
transformed_w, transformed_h = transformed.size
|
| 213 |
new_w = input_w + transformed_w
|
| 214 |
new_h = max(input_h, transformed_h)
|
| 215 |
|
| 216 |
new_image = Image.new("RGB", (new_w, new_h))
|
| 217 |
+
# ์๋ณธ(RGB), ๋ณํ๋ณธ(RGB)
|
| 218 |
new_image.paste(input_image.convert("RGB"), (0, 0))
|
| 219 |
new_image.paste(transformed.convert("RGB"), (input_w, 0))
|
| 220 |
|