Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
def process_image(image,
|
| 7 |
convert_bw,
|
|
@@ -11,6 +12,12 @@ def process_image(image,
|
|
| 11 |
brightness,
|
| 12 |
contrast,
|
| 13 |
saturation):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Convert PIL Image to OpenCV format
|
| 15 |
img = np.array(image)
|
| 16 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
|
@@ -57,10 +64,27 @@ def process_image(image,
|
|
| 57 |
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 58 |
transformed_image = Image.fromarray(transformed_image)
|
| 59 |
|
| 60 |
-
return transformed_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
def
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 66 |
def create_interface():
|
|
@@ -68,7 +92,7 @@ def create_interface():
|
|
| 68 |
gr.Markdown("## ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ ํ๋ฆฌ์ผ์ด์
")
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
-
with gr.Column():
|
| 72 |
input_image = gr.Image(type="pil", label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋")
|
| 73 |
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 74 |
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
|
@@ -78,35 +102,45 @@ def create_interface():
|
|
| 78 |
contrast = gr.Slider(label="๋๋น ์กฐ์ ", minimum=0.5, maximum=1.5, step=0.1, value=1.0)
|
| 79 |
saturation = gr.Slider(label="์ฑ๋ ์กฐ์ ", minimum=0.0, maximum=2.0, step=0.1, value=1.0)
|
| 80 |
submit = gr.Button("๋ณํํ๊ธฐ")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
with gr.Column():
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
|
|
|
| 86 |
submit.click(
|
| 87 |
fn=process_image,
|
| 88 |
inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
|
| 89 |
-
outputs=
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
-
gr.Markdown(""
|
| 99 |
-
### ์ด๋ฏธ์ง ๋น๊ต
|
| 100 |
-
์ฌ๋ผ์ด๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ณธ ์ด๋ฏธ์ง์ ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ ๋น๊ตํ ์ ์์ต๋๋ค.
|
| 101 |
-
""")
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
outputs=[original_display, transformed_display])
|
| 110 |
|
| 111 |
return demo
|
| 112 |
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
def process_image(image,
|
| 8 |
convert_bw,
|
|
|
|
| 12 |
brightness,
|
| 13 |
contrast,
|
| 14 |
saturation):
|
| 15 |
+
"""
|
| 16 |
+
์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์
|
| 17 |
+
"""
|
| 18 |
+
# ์๋ณธ ์ด๋ฏธ์ง ๋ณต์ฌ
|
| 19 |
+
original_image = image.copy()
|
| 20 |
+
|
| 21 |
# Convert PIL Image to OpenCV format
|
| 22 |
img = np.array(image)
|
| 23 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
|
|
|
| 64 |
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 65 |
transformed_image = Image.fromarray(transformed_image)
|
| 66 |
|
| 67 |
+
return transformed_image, original_image, transformed_image
|
| 68 |
+
|
| 69 |
+
def blend_images(original, transformed, alpha):
|
| 70 |
+
"""
|
| 71 |
+
๋ ์ด๋ฏธ์ง๋ฅผ ์ํ ๊ฐ์ ๋ฐ๋ผ ํผํฉํ๋ ํจ์
|
| 72 |
+
"""
|
| 73 |
+
if original is None or transformed is None:
|
| 74 |
+
return None
|
| 75 |
+
blended = Image.blend(original, transformed, alpha)
|
| 76 |
+
return blended
|
| 77 |
|
| 78 |
+
def download_image(transformed):
|
| 79 |
+
"""
|
| 80 |
+
๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ํ ์ ์๋๋ก ํ๋ ํจ์
|
| 81 |
+
"""
|
| 82 |
+
if transformed is None:
|
| 83 |
+
return None
|
| 84 |
+
buf = BytesIO()
|
| 85 |
+
transformed.save(buf, format="JPEG")
|
| 86 |
+
buf.seek(0)
|
| 87 |
+
return buf
|
| 88 |
|
| 89 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 90 |
def create_interface():
|
|
|
|
| 92 |
gr.Markdown("## ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ ํ๋ฆฌ์ผ์ด์
")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
+
with gr.Column(scale=1):
|
| 96 |
input_image = gr.Image(type="pil", label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋")
|
| 97 |
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 98 |
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
|
|
|
| 102 |
contrast = gr.Slider(label="๋๋น ์กฐ์ ", minimum=0.5, maximum=1.5, step=0.1, value=1.0)
|
| 103 |
saturation = gr.Slider(label="์ฑ๋ ์กฐ์ ", minimum=0.0, maximum=2.0, step=0.1, value=1.0)
|
| 104 |
submit = gr.Button("๋ณํํ๊ธฐ")
|
| 105 |
+
|
| 106 |
+
# State๋ฅผ ์ฌ์ฉํ์ฌ ์๋ณธ ๋ฐ ๋ณํ๋ ์ด๋ฏธ์ง ์ ์ฅ
|
| 107 |
+
original_state = gr.State()
|
| 108 |
+
transformed_state = gr.State()
|
| 109 |
|
| 110 |
+
with gr.Column(scale=1):
|
| 111 |
+
transformed_image = gr.Image(type="pil", label="๋ณํ๋ ์ด๋ฏธ์ง")
|
| 112 |
+
download_btn = gr.DownloadButton("JPG๋ก ๋ค์ด๋ก๋", file_name="transformed_image.jpg")
|
| 113 |
|
| 114 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ State์ ์ ์ฅํ๊ณ ๋ณํ๋ ์ด๋ฏธ์ง ํ์
|
| 115 |
submit.click(
|
| 116 |
fn=process_image,
|
| 117 |
inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
|
| 118 |
+
outputs=[transformed_image, original_state, transformed_state]
|
| 119 |
)
|
| 120 |
|
| 121 |
+
# ๋ค์ด๋ก๋ ๋ฒํผ์ ๋ณํ๋ ์ด๋ฏธ์ง ์ฐ๊ฒฐ
|
| 122 |
+
download_btn.click(
|
| 123 |
+
fn=download_image,
|
| 124 |
+
inputs=transformed_state,
|
| 125 |
+
outputs=download_btn
|
| 126 |
)
|
| 127 |
|
| 128 |
+
gr.Markdown("### ์ด๋ฏธ์ง ๋น๊ต")
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
with gr.Row():
|
| 131 |
+
with gr.Column(scale=1):
|
| 132 |
+
alpha_slider = gr.Slider(label="๋น๊ต ์ฌ๋ผ์ด๋", minimum=0.0, maximum=1.0, step=0.01, value=0.5)
|
| 133 |
+
blended_image = gr.Image(type="pil", label="ํผํฉ๋ ์ด๋ฏธ์ง")
|
| 134 |
+
|
| 135 |
+
# ์ฌ๋ผ์ด๋ ์กฐ์ ์ ์ด๋ฏธ์ง ํผํฉ
|
| 136 |
+
alpha_slider.change(
|
| 137 |
+
fn=blend_images,
|
| 138 |
+
inputs=[original_state, transformed_state, alpha_slider],
|
| 139 |
+
outputs=blended_image
|
| 140 |
+
)
|
| 141 |
|
| 142 |
+
# ์ด๊ธฐ์๋ ํผํฉ๋ ์ด๋ฏธ์ง๊ฐ ํ์๋์ง ์๋๋ก ์ค์
|
| 143 |
+
blended_image.style(height=300)
|
|
|
|
| 144 |
|
| 145 |
return demo
|
| 146 |
|