Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,29 @@ import gradio as gr
|
|
| 5 |
from io import BytesIO
|
| 6 |
import tempfile
|
| 7 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ๋ก๊น
์ค์ - INFO ๋ ๋ฒจ๋ก ๋ณ๊ฒฝ
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def adjust_brightness(image, value):
|
| 14 |
"""์ด๋ฏธ์ง ๋ฐ๊ธฐ ์กฐ์ """
|
| 15 |
value = float(value - 1) * 100 # 0-2 ๋ฒ์๋ฅผ -100์์ +100์ผ๋ก ๋ณํ
|
|
@@ -50,96 +68,221 @@ def adjust_temperature(image, value):
|
|
| 50 |
b = np.clip(b, 0, 255)
|
| 51 |
return cv2.merge([b, g, r])
|
| 52 |
|
| 53 |
-
def
|
| 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 |
-
def
|
| 84 |
-
"""
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
factor = 1 + (float(value) / 100.0)
|
| 88 |
-
pil_image = enhancer.enhance(factor)
|
| 89 |
-
return cv2.cvtColor(np.array(pil_image), cv2.COLOR_BGR2RGB)
|
| 90 |
|
| 91 |
-
def
|
| 92 |
-
"""
|
| 93 |
if image is None:
|
| 94 |
return None
|
| 95 |
-
|
| 96 |
-
# PIL ์ด๋ฏธ์ง๋ฅผ OpenCV ํ์์ผ๋ก ๋ณํ
|
| 97 |
-
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
image = adjust_color_mixer_blues(image, color_mixer_blues)
|
| 108 |
-
image = adjust_shadows(image, shadows)
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
def
|
| 114 |
-
"""
|
| 115 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
return korea_time.strftime('%Y%m%d_%H%M%S')
|
| 123 |
|
| 124 |
timestamp = get_korean_timestamp()
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# ํ์ผ ์ ์ฅ
|
| 133 |
temp_file_path = tempfile.gettempdir() + "/" + file_name
|
| 134 |
-
image.save(temp_file_path, format=
|
| 135 |
return temp_file_path
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def create_interface():
|
| 138 |
css = """
|
| 139 |
footer {
|
| 140 |
visibility: hidden;
|
| 141 |
}
|
| 142 |
-
.download-button, .download-output {
|
| 143 |
width: 100%;
|
| 144 |
}
|
| 145 |
.download-container {
|
|
@@ -149,110 +292,216 @@ def create_interface():
|
|
| 149 |
width: 100%;
|
| 150 |
}
|
| 151 |
#gradio-app {
|
| 152 |
-
margin: 0 !important;
|
| 153 |
-
text-align: left !important;
|
| 154 |
-
padding:
|
| 155 |
}
|
| 156 |
.gradio-container {
|
| 157 |
-
max-width: 100% !important;
|
| 158 |
-
margin
|
| 159 |
-
padding:
|
| 160 |
}
|
| 161 |
-
.download-button {
|
| 162 |
background-color: black !important;
|
| 163 |
color: white !important;
|
| 164 |
border: none !important;
|
| 165 |
padding: 10px !important;
|
| 166 |
font-size: 16px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
}
|
| 168 |
"""
|
| 169 |
|
| 170 |
with gr.Blocks(theme=gr.themes.Soft(
|
| 171 |
primary_hue=gr.themes.Color(
|
| 172 |
-
c50="#FFF7ED",
|
| 173 |
c100="#FFEDD5",
|
| 174 |
c200="#FED7AA",
|
| 175 |
c300="#FDBA74",
|
| 176 |
c400="#FB923C",
|
| 177 |
-
c500="#F97316",
|
| 178 |
c600="#EA580C",
|
| 179 |
c700="#C2410C",
|
| 180 |
c800="#9A3412",
|
| 181 |
-
c900="#7C2D12",
|
| 182 |
c950="#431407",
|
| 183 |
),
|
| 184 |
-
secondary_hue="zinc",
|
| 185 |
neutral_hue="zinc",
|
| 186 |
font=("Pretendard", "sans-serif")
|
| 187 |
), css=css) as interface:
|
| 188 |
|
|
|
|
|
|
|
|
|
|
| 189 |
with gr.Row():
|
| 190 |
-
# ์ผ์ชฝ ์ด:
|
| 191 |
-
with gr.Column(scale=
|
| 192 |
# ์ด๋ฏธ์ง ์
๋ก๋
|
| 193 |
input_image = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
temperature_slider,
|
| 224 |
-
tint_slider,
|
| 225 |
-
exposure_slider,
|
| 226 |
-
vibrance_slider,
|
| 227 |
-
color_mixer_blues_slider,
|
| 228 |
-
shadows_slider
|
| 229 |
-
]
|
| 230 |
-
|
| 231 |
-
input_components = [
|
| 232 |
brightness_slider,
|
| 233 |
contrast_slider,
|
| 234 |
saturation_slider,
|
| 235 |
-
|
| 236 |
-
tint_slider,
|
| 237 |
-
exposure_slider,
|
| 238 |
-
vibrance_slider,
|
| 239 |
-
color_mixer_blues_slider,
|
| 240 |
-
shadows_slider
|
| 241 |
]
|
| 242 |
-
|
| 243 |
-
|
|
|
|
| 244 |
input_component.change(
|
| 245 |
-
fn=
|
| 246 |
-
inputs=
|
| 247 |
-
outputs=
|
| 248 |
)
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
# ๋ค์ด๋ก๋ ๋ฒํผ ๊ธฐ๋ฅ
|
| 251 |
download_button.click(
|
| 252 |
fn=download_image,
|
| 253 |
-
inputs=[
|
| 254 |
outputs=download_output
|
| 255 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
return interface
|
| 258 |
|
|
@@ -261,4 +510,4 @@ if __name__ == "__main__":
|
|
| 261 |
logger.info("์ ํ๋ฆฌ์ผ์ด์
์์")
|
| 262 |
interface = create_interface()
|
| 263 |
interface.queue()
|
| 264 |
-
interface.launch()
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
import tempfile
|
| 7 |
import logging
|
| 8 |
+
import replicate
|
| 9 |
+
import requests
|
| 10 |
+
import os
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import base64
|
| 13 |
+
from datetime import datetime, timedelta
|
| 14 |
+
import uuid
|
| 15 |
+
import time
|
| 16 |
+
|
| 17 |
+
# .env ํ์ผ์์ ํ๊ฒฝ ๋ณ์ ๋ก๋ (์์ผ๋ฉด ๋ฌด์)
|
| 18 |
+
load_dotenv()
|
| 19 |
|
| 20 |
# ๋ก๊น
์ค์ - INFO ๋ ๋ฒจ๋ก ๋ณ๊ฒฝ
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
+
# Replicate API ํ ํฐ ์ค์
|
| 25 |
+
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN", "์ฌ๊ธฐ์_API_ํ ํฐ์_์
๋ ฅํ์ธ์")
|
| 26 |
+
os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
|
| 27 |
+
|
| 28 |
+
# ์ธ์
๋ณ ์ด๋ฏธ์ง ์ ์ฅ์ฉ ๋์
๋๋ฆฌ
|
| 29 |
+
session_images = {}
|
| 30 |
+
|
| 31 |
def adjust_brightness(image, value):
|
| 32 |
"""์ด๋ฏธ์ง ๋ฐ๊ธฐ ์กฐ์ """
|
| 33 |
value = float(value - 1) * 100 # 0-2 ๋ฒ์๋ฅผ -100์์ +100์ผ๋ก ๋ณํ
|
|
|
|
| 68 |
b = np.clip(b, 0, 255)
|
| 69 |
return cv2.merge([b, g, r])
|
| 70 |
|
| 71 |
+
def remove_background(image, session_id):
|
| 72 |
+
"""๋ฐฐ๊ฒฝ ์ ๊ฑฐ ๊ธฐ๋ฅ - Replicate API ์ฌ์ฉ"""
|
| 73 |
+
logger.info(f"์ธ์
{session_id}: ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ์์")
|
| 74 |
+
|
| 75 |
+
# ์์ ํ์ผ๋ก ์ด๋ฏธ์ง ์ ์ฅ
|
| 76 |
+
temp_file = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 77 |
+
temp_file_path = temp_file.name
|
| 78 |
+
temp_file.close()
|
| 79 |
+
|
| 80 |
+
# PIL ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
| 81 |
+
image.save(temp_file_path, format="PNG")
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
# Replicate API๋ก ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ์์ฒญ
|
| 85 |
+
output = replicate.run(
|
| 86 |
+
"851-labs/background-remover:a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc",
|
| 87 |
+
input={
|
| 88 |
+
"image": open(temp_file_path, "rb"),
|
| 89 |
+
"format": "png",
|
| 90 |
+
"reverse": False,
|
| 91 |
+
"threshold": 0,
|
| 92 |
+
"background_type": "rgba"
|
| 93 |
+
}
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# URL์์ ์ด๋ฏธ์ง ๋ค์ด๋ก๋
|
| 97 |
+
response = requests.get(output)
|
| 98 |
+
if response.status_code == 200:
|
| 99 |
+
# ๋ฐ์ดํธ ๋ฐ์ดํฐ๋ฅผ PIL ์ด๋ฏธ์ง๋ก ๋ณํ
|
| 100 |
+
img = Image.open(BytesIO(response.content))
|
| 101 |
+
|
| 102 |
+
# PNG ํ์ผ๋ก ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง ์ ์ฅ (๋ง์คํฌ์ฉ)
|
| 103 |
+
timestamp = get_korean_timestamp()
|
| 104 |
+
bg_removed_path = tempfile.gettempdir() + f"/bg_removed_{session_id}_{timestamp}.png"
|
| 105 |
+
img.save(bg_removed_path, format="PNG")
|
| 106 |
+
|
| 107 |
+
logger.info(f"์ธ์
{session_id}: ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ์๋ฃ: {bg_removed_path}")
|
| 108 |
+
|
| 109 |
+
# ์ธ์
์ ๋ณด ์ ์ฅ
|
| 110 |
+
session_images[session_id]['bg_removed_image'] = img
|
| 111 |
+
session_images[session_id]['bg_removed_path'] = bg_removed_path
|
| 112 |
+
|
| 113 |
+
return img
|
| 114 |
+
else:
|
| 115 |
+
logger.error(f"์ธ์
{session_id}: API ์๋ต ์ค๋ฅ: {response.status_code}")
|
| 116 |
+
return image
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
logger.error(f"์ธ์
{session_id}: ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ์ค๋ฅ: {e}")
|
| 120 |
+
return image
|
| 121 |
+
finally:
|
| 122 |
+
# ์์ ํ์ผ ์ญ์
|
| 123 |
+
if os.path.exists(temp_file_path):
|
| 124 |
+
os.unlink(temp_file_path)
|
| 125 |
|
| 126 |
+
def get_korean_timestamp():
|
| 127 |
+
"""ํ๊ตญ ์๊ฐ ํ์์คํฌํ ์์ฑ"""
|
| 128 |
+
korea_time = datetime.utcnow() + timedelta(hours=9)
|
| 129 |
+
return korea_time.strftime('%Y%m%d_%H%M%S')
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
def handle_upload(image, session_id=None):
|
| 132 |
+
"""๏ฟฝ๏ฟฝ๋ฏธ์ง ์
๋ก๋ ์ฒ๋ฆฌ ๋ฐ ๋ฐฐ๊ฒฝ ์ ๊ฑฐ"""
|
| 133 |
if image is None:
|
| 134 |
return None
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
# ์ธ์
ID ์์ฑ ๋๋ ํ์ธ
|
| 137 |
+
if session_id is None or session_id not in session_images:
|
| 138 |
+
session_id = str(uuid.uuid4())
|
| 139 |
+
session_images[session_id] = {}
|
| 140 |
+
|
| 141 |
+
# ์๋ณธ ์ด๋ฏธ์ง ์ ์ฅ
|
| 142 |
+
original_image = Image.fromarray(np.array(image))
|
| 143 |
+
session_images[session_id]['original_image'] = original_image
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ์คํ
|
| 146 |
+
bg_removed_image = remove_background(original_image, session_id)
|
| 147 |
+
|
| 148 |
+
# ํํฐ ์ ์ฉ ์ ๊ธฐ๋ณธ ์ด๋ฏธ์ง ๋ฐํ (ํํฐ ์์ด ์๋ณธ ์ด๋ฏธ์ง)
|
| 149 |
+
return original_image, session_id
|
| 150 |
|
| 151 |
+
def process_image(session_id, temperature, brightness, contrast, saturation, filter_mode):
|
| 152 |
+
"""๋ชจ๋์ ๋ฐ๋ฅธ ์ด๋ฏธ์ง ์ฒ๋ฆฌ"""
|
| 153 |
+
if session_id is None or session_id not in session_images:
|
| 154 |
+
logger.warning(f"์ธ์
ID๊ฐ ์ ํจํ์ง ์์ต๋๋ค: {session_id}")
|
| 155 |
+
return None
|
| 156 |
+
|
| 157 |
+
original_image = session_images[session_id].get('original_image')
|
| 158 |
+
bg_removed_image = session_images[session_id].get('bg_removed_image')
|
| 159 |
+
|
| 160 |
+
if original_image is None:
|
| 161 |
return None
|
| 162 |
+
|
| 163 |
+
if bg_removed_image is None:
|
| 164 |
+
logger.warning(f"์ธ์
{session_id}: ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๊ฐ ์์ต๋๋ค. ์ ์ฒด ํํฐ ๋ชจ๋๋ก ์ฒ๋ฆฌํฉ๋๋ค.")
|
| 165 |
+
# ์ ์ฒด ํํฐ๋ก ์ฒ๋ฆฌ
|
| 166 |
+
cv_image = cv2.cvtColor(np.array(original_image), cv2.COLOR_RGB2BGR)
|
| 167 |
+
cv_image = adjust_temperature(cv_image, temperature)
|
| 168 |
+
cv_image = adjust_brightness(cv_image, brightness)
|
| 169 |
+
cv_image = adjust_contrast(cv_image, contrast)
|
| 170 |
+
cv_image = adjust_saturation(cv_image, saturation)
|
| 171 |
+
return Image.fromarray(cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB))
|
| 172 |
+
|
| 173 |
+
# ํํฐ ๋ชจ๋์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์ฒ๋ฆฌ
|
| 174 |
+
if filter_mode == "์ ์ฒด ํํฐ":
|
| 175 |
+
# ์๋ณธ ์ด๋ฏธ์ง์ ํํฐ ์ ์ฉ
|
| 176 |
+
cv_image = cv2.cvtColor(np.array(original_image), cv2.COLOR_RGB2BGR)
|
| 177 |
+
cv_image = adjust_temperature(cv_image, temperature)
|
| 178 |
+
cv_image = adjust_brightness(cv_image, brightness)
|
| 179 |
+
cv_image = adjust_contrast(cv_image, contrast)
|
| 180 |
+
cv_image = adjust_saturation(cv_image, saturation)
|
| 181 |
+
return Image.fromarray(cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB))
|
| 182 |
+
|
| 183 |
+
elif filter_mode == "๋ฐฐ๊ฒฝ๋ง ํํฐ ์ ์ฉ":
|
| 184 |
+
# ์๋ณธ์ ํํฐ ์ ์ฉ ํ ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง ํฉ์ฑ
|
| 185 |
+
cv_image = cv2.cvtColor(np.array(original_image), cv2.COLOR_RGB2BGR)
|
| 186 |
+
cv_image = adjust_temperature(cv_image, temperature)
|
| 187 |
+
cv_image = adjust_brightness(cv_image, brightness)
|
| 188 |
+
cv_image = adjust_contrast(cv_image, contrast)
|
| 189 |
+
cv_image = adjust_saturation(cv_image, saturation)
|
| 190 |
+
filtered_original = Image.fromarray(cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB))
|
| 191 |
+
|
| 192 |
+
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๊ฐ RGBA ๋ชจ๋์ธ์ง ํ์ธ
|
| 193 |
+
if bg_removed_image.mode != 'RGBA':
|
| 194 |
+
logger.warning(f"์ธ์
{session_id}: ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๊ฐ RGBA ๋ชจ๋๊ฐ ์๋๋๋ค.")
|
| 195 |
+
return filtered_original
|
| 196 |
+
|
| 197 |
+
# ํํฐ๋ง๋ ์ด๋ฏธ์ง๋ฅผ RGBA๋ก ๋ณํ (์ํ ์ฑ๋ ์ถ๊ฐ)
|
| 198 |
+
if filtered_original.mode != 'RGBA':
|
| 199 |
+
filtered_original = filtered_original.convert('RGBA')
|
| 200 |
+
|
| 201 |
+
# ํฉ์ฑ (๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๋ฅผ ํํฐ๋ง๋ ์๋ณธ ์์ ๋ฐฐ์น)
|
| 202 |
+
result = Image.new('RGBA', filtered_original.size, (0, 0, 0, 0))
|
| 203 |
+
result.paste(filtered_original, (0, 0), None)
|
| 204 |
+
result.paste(bg_removed_image, (0, 0), bg_removed_image)
|
| 205 |
+
|
| 206 |
+
return result.convert('RGB')
|
| 207 |
+
|
| 208 |
+
elif filter_mode == "์ํ๋ง ํํฐ ์ ์ฉ":
|
| 209 |
+
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๋ง ํํฐ ์ ์ฉ
|
| 210 |
+
cv_image = cv2.cvtColor(np.array(bg_removed_image), cv2.COLOR_RGBA2BGRA)
|
| 211 |
+
# ์ํ ์ฑ๋ ๋ถ๋ฆฌ
|
| 212 |
+
b, g, r, a = cv2.split(cv_image)
|
| 213 |
+
# BGR ์ด๋ฏธ์ง๋ก ํฉ์น๊ธฐ
|
| 214 |
+
bgr = cv2.merge([b, g, r])
|
| 215 |
+
# ํํฐ ์ ์ฉ
|
| 216 |
+
bgr = adjust_temperature(bgr, temperature)
|
| 217 |
+
bgr = adjust_brightness(bgr, brightness)
|
| 218 |
+
bgr = adjust_contrast(bgr, contrast)
|
| 219 |
+
bgr = adjust_saturation(bgr, saturation)
|
| 220 |
+
# ๋ค์ ์ํ ์ฑ๋๊ณผ ํฉ์น๊ธฐ
|
| 221 |
+
filtered_bgra = cv2.merge([bgr[:,:,0], bgr[:,:,1], bgr[:,:,2], a])
|
| 222 |
+
filtered_bg_removed = Image.fromarray(cv2.cvtColor(filtered_bgra, cv2.COLOR_BGRA2RGBA))
|
| 223 |
+
|
| 224 |
+
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๊ฐ RGBA ๋ชจ๋์ธ์ง ํ์ธ
|
| 225 |
+
if bg_removed_image.mode != 'RGBA':
|
| 226 |
+
logger.warning(f"์ธ์
{session_id}: ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๊ฐ RGBA ๋ชจ๋๊ฐ ์๋๋๋ค.")
|
| 227 |
+
# ํํฐ๋ง ์ ์ฉํ๊ณ ๋ฐํ
|
| 228 |
+
return filtered_bg_removed
|
| 229 |
+
|
| 230 |
+
# ์๋ณธ ์ด๋ฏธ์ง๋ฅผ RGBA๋ก ๋ณํ
|
| 231 |
+
original_rgba = original_image.convert('RGBA')
|
| 232 |
+
|
| 233 |
+
# ์๋ณธ ์ด๋ฏธ์ง์ ํํฐ๋ง๋ ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ์ด๋ฏธ์ง๋ฅผ ํฉ์ฑ
|
| 234 |
+
result = Image.new('RGBA', original_rgba.size, (0, 0, 0, 0))
|
| 235 |
+
result.paste(original_rgba, (0, 0), None)
|
| 236 |
+
result.paste(filtered_bg_removed, (0, 0), bg_removed_image)
|
| 237 |
+
|
| 238 |
+
return result.convert('RGB')
|
| 239 |
+
|
| 240 |
+
# ๊ธฐ๋ณธ๊ฐ์ ์ ์ฒด ํํฐ
|
| 241 |
+
cv_image = cv2.cvtColor(np.array(original_image), cv2.COLOR_RGB2BGR)
|
| 242 |
+
cv_image = adjust_temperature(cv_image, temperature)
|
| 243 |
+
cv_image = adjust_brightness(cv_image, brightness)
|
| 244 |
+
cv_image = adjust_contrast(cv_image, contrast)
|
| 245 |
+
cv_image = adjust_saturation(cv_image, saturation)
|
| 246 |
+
return Image.fromarray(cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB))
|
| 247 |
|
| 248 |
+
def download_image(image, session_id, format_type):
|
| 249 |
+
"""์ด๋ฏธ์ง๋ฅผ ์ง์ ๋ ํ์์ผ๋ก ์ ์ฅํ๊ณ ๊ฒฝ๋ก ๋ฐํ"""
|
| 250 |
+
if image is None or session_id is None or session_id not in session_images:
|
| 251 |
+
return None
|
|
|
|
| 252 |
|
| 253 |
timestamp = get_korean_timestamp()
|
| 254 |
+
|
| 255 |
+
# ์๋ณธ ํ์ผ๋ช
๊ฐ์ ธ์ค๊ธฐ (์๋ ๊ฒฝ์ฐ)
|
| 256 |
+
original_name = "์ด๋ฏธ์ง"
|
| 257 |
+
if 'original_name' in session_images[session_id]:
|
| 258 |
+
original_name = session_images[session_id]['original_name']
|
| 259 |
|
| 260 |
+
if format_type == "JPG":
|
| 261 |
+
file_name = f"[๋์ฅAI]๋์ฅํํฐ_{original_name}_{timestamp}.jpg"
|
| 262 |
+
format_str = "JPEG"
|
| 263 |
+
else: # PNG
|
| 264 |
+
file_name = f"[๋์ฅAI]๋์ฅํํฐ_{original_name}_{timestamp}.png"
|
| 265 |
+
format_str = "PNG"
|
| 266 |
|
| 267 |
# ํ์ผ ์ ์ฅ
|
| 268 |
temp_file_path = tempfile.gettempdir() + "/" + file_name
|
| 269 |
+
image.save(temp_file_path, format=format_str)
|
| 270 |
return temp_file_path
|
| 271 |
|
| 272 |
+
def reset_filters(session_id):
|
| 273 |
+
"""ํํฐ ์ค์ ์ด๊ธฐํ"""
|
| 274 |
+
if session_id is None or session_id not in session_images:
|
| 275 |
+
return None, 0.0, 1.0, 1.0, 1.0
|
| 276 |
+
|
| 277 |
+
original_image = session_images[session_id].get('original_image')
|
| 278 |
+
return original_image, 0.0, 1.0, 1.0, 1.0
|
| 279 |
+
|
| 280 |
def create_interface():
|
| 281 |
css = """
|
| 282 |
footer {
|
| 283 |
visibility: hidden;
|
| 284 |
}
|
| 285 |
+
.download-button, .download-output, .reset-button {
|
| 286 |
width: 100%;
|
| 287 |
}
|
| 288 |
.download-container {
|
|
|
|
| 292 |
width: 100%;
|
| 293 |
}
|
| 294 |
#gradio-app {
|
| 295 |
+
margin: 0 !important;
|
| 296 |
+
text-align: left !important;
|
| 297 |
+
padding: 0 !important;
|
| 298 |
}
|
| 299 |
.gradio-container {
|
| 300 |
+
max-width: 100% !important;
|
| 301 |
+
margin: 0 !important;
|
| 302 |
+
padding: 0 !important;
|
| 303 |
}
|
| 304 |
+
.download-button, .reset-button {
|
| 305 |
background-color: black !important;
|
| 306 |
color: white !important;
|
| 307 |
border: none !important;
|
| 308 |
padding: 10px !important;
|
| 309 |
font-size: 16px !important;
|
| 310 |
+
border-radius: 4px !important;
|
| 311 |
+
}
|
| 312 |
+
.reset-button {
|
| 313 |
+
background-color: #666 !important;
|
| 314 |
+
}
|
| 315 |
+
.filter-mode {
|
| 316 |
+
margin-top: 20px;
|
| 317 |
+
margin-bottom: 20px;
|
| 318 |
+
}
|
| 319 |
+
.input-panel {
|
| 320 |
+
padding: 20px;
|
| 321 |
+
}
|
| 322 |
+
.output-panel {
|
| 323 |
+
padding: 20px;
|
| 324 |
+
height: 100%;
|
| 325 |
+
display: flex;
|
| 326 |
+
flex-direction: column;
|
| 327 |
+
}
|
| 328 |
+
.output-image {
|
| 329 |
+
flex-grow: 1;
|
| 330 |
+
min-height: 400px;
|
| 331 |
+
}
|
| 332 |
+
.filter-section {
|
| 333 |
+
margin: 15px 0;
|
| 334 |
+
padding: 15px;
|
| 335 |
+
border: 1px solid #eee;
|
| 336 |
+
border-radius: 8px;
|
| 337 |
+
}
|
| 338 |
+
.session-id {
|
| 339 |
+
display: none;
|
| 340 |
}
|
| 341 |
"""
|
| 342 |
|
| 343 |
with gr.Blocks(theme=gr.themes.Soft(
|
| 344 |
primary_hue=gr.themes.Color(
|
| 345 |
+
c50="#FFF7ED",
|
| 346 |
c100="#FFEDD5",
|
| 347 |
c200="#FED7AA",
|
| 348 |
c300="#FDBA74",
|
| 349 |
c400="#FB923C",
|
| 350 |
+
c500="#F97316",
|
| 351 |
c600="#EA580C",
|
| 352 |
c700="#C2410C",
|
| 353 |
c800="#9A3412",
|
| 354 |
+
c900="#7C2D12",
|
| 355 |
c950="#431407",
|
| 356 |
),
|
| 357 |
+
secondary_hue="zinc",
|
| 358 |
neutral_hue="zinc",
|
| 359 |
font=("Pretendard", "sans-serif")
|
| 360 |
), css=css) as interface:
|
| 361 |
|
| 362 |
+
# ์ธ์
ID (์ฌ์ฉ์์๊ฒ ๋ณด์ด์ง ์์)
|
| 363 |
+
session_id = gr.Textbox(visible=False, elem_classes="session-id")
|
| 364 |
+
|
| 365 |
with gr.Row():
|
| 366 |
+
# ์ผ์ชฝ ์ด: ์
๋ ฅ ํจ๋
|
| 367 |
+
with gr.Column(scale=1, elem_classes="input-panel"):
|
| 368 |
# ์ด๋ฏธ์ง ์
๋ก๋
|
| 369 |
input_image = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
| 370 |
+
|
| 371 |
+
# ํํฐ ๋ชจ๋ ์ ํ
|
| 372 |
+
with gr.Group(elem_classes="filter-section"):
|
| 373 |
+
filter_mode = gr.Radio(
|
| 374 |
+
["์ ์ฒด ํํฐ", "๋ฐฐ๊ฒฝ๋ง ํํฐ ์ ์ฉ", "์ํ๋ง ํํฐ ์ ์ฉ"],
|
| 375 |
+
label="ํํฐ ์ ์ฉ ๋ชจ๋",
|
| 376 |
+
value="์ ์ฒด ํํฐ",
|
| 377 |
+
elem_classes="filter-mode"
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
# ์กฐ์ ์ฌ๋ผ์ด๋ ๊ทธ๋ฃน (์ค์ ์ฌ๋ผ์ด๋๋ง ๋จ๊น)
|
| 381 |
+
with gr.Group(elem_classes="filter-section"):
|
| 382 |
+
gr.Markdown("### ์ด๋ฏธ์ง ํํฐ ์ค์ ")
|
| 383 |
+
|
| 384 |
+
# ์์จ๋๋ฅผ ๊ฐ์ฅ ์๋ก ๋ฐฐ์น
|
| 385 |
+
temperature_slider = gr.Slider(-1.0, 1.0, value=0.0, step=0.1, label="์์จ๋ ์กฐ์ ")
|
| 386 |
+
brightness_slider = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="๋ฐ๊ธฐ ์กฐ์ ")
|
| 387 |
+
contrast_slider = gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="๋๋น ์กฐ์ ")
|
| 388 |
+
saturation_slider = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="์ฑ๋ ์กฐ์ ")
|
| 389 |
+
|
| 390 |
+
# ๋ฆฌ์
๋ฒํผ
|
| 391 |
+
reset_button = gr.Button("ํํฐ ์ด๊ธฐํ", elem_classes="reset-button")
|
| 392 |
|
| 393 |
+
# ์ค๋ฅธ์ชฝ ์ด: ์ถ๋ ฅ ํจ๋
|
| 394 |
+
with gr.Column(scale=1, elem_classes="output-panel"):
|
| 395 |
+
# ํํฐ ์ ์ฉ๋ ์ด๋ฏธ์ง ์ถ๋ ฅ
|
| 396 |
+
filtered_output = gr.Image(type="pil", label="ํํฐ ์ ์ฉ๋ ์ด๋ฏธ์ง", elem_classes="output-image")
|
| 397 |
+
|
| 398 |
+
# ์ด๋ฏธ์ง ์ ์ฅ ์ต์
๋ฐ ๋ค์ด๋ก๋
|
| 399 |
+
with gr.Row():
|
| 400 |
+
with gr.Column(scale=1):
|
| 401 |
+
format_select = gr.Radio(
|
| 402 |
+
["JPG", "PNG"],
|
| 403 |
+
label="์ ์ฅ ํ์",
|
| 404 |
+
value="JPG"
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
+
with gr.Column(scale=2):
|
| 408 |
+
download_button = gr.Button("์ด๋ฏธ์ง ๋ณํํ๊ธฐ", elem_classes="download-button")
|
| 409 |
+
|
| 410 |
+
# ๋ค์ด๋ก๋ ๋งํฌ
|
| 411 |
+
download_output = gr.File(label="๋ณํ๋ ์ด๋ฏธ์ง ๋ค์ด๋ก๋")
|
| 412 |
|
| 413 |
+
# ์ด๋ฏธ์ง ์
๋ก๋ ์ฒ๋ฆฌ ๋ฐ ์ด๊ธฐ ์ถ๋ ฅ ์ค์
|
| 414 |
+
def update_image_and_session(image):
|
| 415 |
+
if image is not None:
|
| 416 |
+
# ํ์ผ ์ด๋ฆ ์ ์ฅ (๊ฐ๋ฅํ ๊ฒฝ์ฐ)
|
| 417 |
+
original_name = "์ด๋ฏธ์ง"
|
| 418 |
+
if hasattr(image, 'name'):
|
| 419 |
+
original_name = image.name.split('.')[0]
|
| 420 |
+
|
| 421 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ฐ ์ธ์
์์ฑ
|
| 422 |
+
result_image, new_session_id = handle_upload(image)
|
| 423 |
+
|
| 424 |
+
# ์๋ณธ ํ์ผ๋ช
์ ์ฅ
|
| 425 |
+
if new_session_id in session_images:
|
| 426 |
+
session_images[new_session_id]['original_name'] = original_name
|
| 427 |
+
|
| 428 |
+
return result_image, new_session_id
|
| 429 |
+
return None, None
|
| 430 |
+
|
| 431 |
+
input_image.change(
|
| 432 |
+
fn=update_image_and_session,
|
| 433 |
+
inputs=[input_image],
|
| 434 |
+
outputs=[filtered_output, session_id]
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
# ํํฐ ์ ์ฉ ํจ์
|
| 438 |
+
def apply_filters(session_id, temperature, brightness, contrast, saturation, filter_mode):
|
| 439 |
+
return process_image(session_id, temperature, brightness, contrast, saturation, filter_mode)
|
| 440 |
+
|
| 441 |
+
# ํํฐ ์
๋ ฅ ๋ณ๊ฒฝ ์ ์ฒ๋ฆฌ
|
| 442 |
+
filter_inputs = [
|
| 443 |
+
session_id,
|
| 444 |
temperature_slider,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
brightness_slider,
|
| 446 |
contrast_slider,
|
| 447 |
saturation_slider,
|
| 448 |
+
filter_mode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
]
|
| 450 |
+
|
| 451 |
+
# ์ฌ๋ผ์ด๋ ๋ณ๊ฒฝ ์ ์ฒ๋ฆฌ
|
| 452 |
+
for input_component in [temperature_slider, brightness_slider, contrast_slider, saturation_slider]:
|
| 453 |
input_component.change(
|
| 454 |
+
fn=apply_filters,
|
| 455 |
+
inputs=filter_inputs,
|
| 456 |
+
outputs=filtered_output
|
| 457 |
)
|
| 458 |
+
|
| 459 |
+
# ํํฐ ๋ชจ๋ ๋ณ๊ฒฝ ์ ์ฒ๋ฆฌ
|
| 460 |
+
filter_mode.change(
|
| 461 |
+
fn=apply_filters,
|
| 462 |
+
inputs=filter_inputs,
|
| 463 |
+
outputs=filtered_output
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
# ๋ฆฌ์
๋ฒํผ ๊ธฐ๋ฅ
|
| 467 |
+
reset_button.click(
|
| 468 |
+
fn=reset_filters,
|
| 469 |
+
inputs=[session_id],
|
| 470 |
+
outputs=[filtered_output, temperature_slider, brightness_slider, contrast_slider, saturation_slider]
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
# ๋ค์ด๋ก๋ ๋ฒํผ ๊ธฐ๋ฅ
|
| 474 |
download_button.click(
|
| 475 |
fn=download_image,
|
| 476 |
+
inputs=[filtered_output, session_id, format_select],
|
| 477 |
outputs=download_output
|
| 478 |
)
|
| 479 |
+
|
| 480 |
+
# ์ธ์
์ ๋ฆฌ ํ์ด๋จธ (30๋ถ ์ด์ ๋ ์ธ์
์ ๊ฑฐ)
|
| 481 |
+
def cleanup_sessions():
|
| 482 |
+
current_time = time.time()
|
| 483 |
+
to_remove = []
|
| 484 |
+
|
| 485 |
+
for sess_id in session_images:
|
| 486 |
+
if 'created_at' in session_images[sess_id]:
|
| 487 |
+
if current_time - session_images[sess_id]['created_at'] > 1800: # 30๋ถ
|
| 488 |
+
to_remove.append(sess_id)
|
| 489 |
+
|
| 490 |
+
for sess_id in to_remove:
|
| 491 |
+
# ์์ ํ์ผ ์ ๊ฑฐ
|
| 492 |
+
if 'bg_removed_path' in session_images[sess_id]:
|
| 493 |
+
path = session_images[sess_id]['bg_removed_path']
|
| 494 |
+
if os.path.exists(path):
|
| 495 |
+
os.unlink(path)
|
| 496 |
+
|
| 497 |
+
# ์ธ์
์ ๋ณด ์ ๊ฑฐ
|
| 498 |
+
del session_images[sess_id]
|
| 499 |
+
|
| 500 |
+
# 10๋ถ๋ง๋ค ์คํ
|
| 501 |
+
gr.set_interval(cleanup_sessions, 600)
|
| 502 |
+
|
| 503 |
+
# ์ธ์
์ ๋ฆฌ ์์
|
| 504 |
+
interface.load(cleanup_sessions)
|
| 505 |
|
| 506 |
return interface
|
| 507 |
|
|
|
|
| 510 |
logger.info("์ ํ๋ฆฌ์ผ์ด์
์์")
|
| 511 |
interface = create_interface()
|
| 512 |
interface.queue()
|
| 513 |
+
interface.launch()
|