Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,3 @@
|
|
1 |
-
# import gradio as gr
|
2 |
-
# from rembg import remove
|
3 |
-
# from PIL import Image
|
4 |
-
# import logging
|
5 |
-
|
6 |
-
# # ๋ก๊น
์ค์
|
7 |
-
# logging.basicConfig(level=logging.INFO)
|
8 |
-
|
9 |
-
# def remove_background(input_image):
|
10 |
-
# """
|
11 |
-
# ์
๋ ฅ๋ ์ด๋ฏธ์ง์์ ๋ฐฐ๊ฒฝ์ ์ ๊ฑฐํ๋ ํจ์
|
12 |
-
# Parameters:
|
13 |
-
# input_image (PIL.Image ๋๋ str): ์
๋ก๋๋ ์ด๋ฏธ์ง ํ์ผ์ ๊ฐ์ฒด ๋๋ ํ์ผ ๊ฒฝ๋ก
|
14 |
-
# Returns:
|
15 |
-
# PIL.Image ๋๋ str: ๋ฐฐ๊ฒฝ์ด ์ ๊ฑฐ๋ ์ด๋ฏธ์ง ๋๋ ์๋ฌ ๋ฉ์์ง
|
16 |
-
# """
|
17 |
-
# try:
|
18 |
-
# logging.info("Processing image.")
|
19 |
-
|
20 |
-
# if isinstance(input_image, str):
|
21 |
-
# input_img = Image.open(input_image)
|
22 |
-
# else:
|
23 |
-
# input_img = input_image
|
24 |
-
|
25 |
-
# # ๋ฐฐ๊ฒฝ ์ ๊ฑฐ
|
26 |
-
# output_img = remove(input_img)
|
27 |
-
# logging.info("Background removed successfully.")
|
28 |
-
|
29 |
-
# # ํฌ๋ช
๋๋ฅผ ์ ์งํ๊ธฐ ์ํด PNG ํ์์ผ๋ก ๋ณํ
|
30 |
-
# return output_img.convert("RGBA")
|
31 |
-
# except Exception as e:
|
32 |
-
# logging.error(f"Error removing background: {e}")
|
33 |
-
# return f"์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
34 |
-
|
35 |
-
# def clear_images():
|
36 |
-
# """์
๋ ฅ ์ด๋ฏธ์ง์ ์ถ๋ ฅ ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ ์ด๊ธฐํํ๋ ํจ์"""
|
37 |
-
# return None, None
|
38 |
-
|
39 |
-
# # Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
40 |
-
# with gr.Blocks(css="""
|
41 |
-
# #btn-remove {
|
42 |
-
# background-color: #4CAF50; /* ์ด๋ก์ */
|
43 |
-
# color: white;
|
44 |
-
# }
|
45 |
-
# #btn-reset {
|
46 |
-
# background-color: #f44336; /* ๋นจ๊ฐ์ */
|
47 |
-
# color: white;
|
48 |
-
# }
|
49 |
-
# """) as demo:
|
50 |
-
# gr.Markdown("# ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๊ธฐ")
|
51 |
-
# gr.Markdown("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฐฐ๊ฒฝ์ด ์๋์ผ๋ก ์ ๊ฑฐ๋ฉ๋๋ค.")
|
52 |
-
|
53 |
-
# with gr.Row():
|
54 |
-
# input_image = gr.Image(
|
55 |
-
# type="pil",
|
56 |
-
# label="์
๋ ฅ ์ด๋ฏธ์ง",
|
57 |
-
# interactive=True # ์ฌ์ฉ์๊ฐ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ ์ ์๋๋ก ์ค์
|
58 |
-
# )
|
59 |
-
# output_image = gr.Image(
|
60 |
-
# type="pil",
|
61 |
-
# label="๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง",
|
62 |
-
# interactive=False # ์ถ๋ ฅ ์ด๋ฏธ์ง๋ ์ํธ์์ฉ ๋ถ๊ฐ๋ฅ
|
63 |
-
# )
|
64 |
-
|
65 |
-
# # ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ๋ฒํผ
|
66 |
-
# with gr.Row():
|
67 |
-
# btn_remove = gr.Button("๋ฐฐ๊ฒฝ ์ ๊ฑฐ", elem_id="btn-remove") # elem_id๋ก ๋ฒํผ ์คํ์ผ ์ง์
|
68 |
-
# btn_remove.click(fn=remove_background, inputs=input_image, outputs=output_image)
|
69 |
-
|
70 |
-
# # ์ด๊ธฐํ ๋ฒํผ ์ถ๊ฐ
|
71 |
-
# btn_reset = gr.Button("์ด๊ธฐํ", elem_id="btn-reset") # elem_id๋ก ๋ฒํผ ์คํ์ผ ์ง์
|
72 |
-
# btn_reset.click(fn=clear_images, inputs=None, outputs=[input_image, output_image])
|
73 |
-
|
74 |
-
# # ์์ ์ด๋ฏธ์ง
|
75 |
-
# gr.Examples(
|
76 |
-
# examples=[
|
77 |
-
# ["./example1.jpg"],
|
78 |
-
# ["./example2.jpg"]
|
79 |
-
# ],
|
80 |
-
# inputs=input_image,
|
81 |
-
# label="์์ ์ด๋ฏธ์ง"
|
82 |
-
# )
|
83 |
-
|
84 |
-
# if __name__ == "__main__":
|
85 |
-
# demo.launch()
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
import gradio as gr
|
90 |
from rembg import remove
|
91 |
from PIL import Image
|
@@ -96,34 +8,26 @@ import os
|
|
96 |
logging.basicConfig(level=logging.INFO)
|
97 |
|
98 |
def remove_background(input_image):
|
99 |
-
"""
|
100 |
-
์
๋ ฅ๋ ์ด๋ฏธ์ง์์ ๋ฐฐ๊ฒฝ์ ์ ๊ฑฐํ๋ ํจ์
|
101 |
-
Parameters:
|
102 |
-
input_image (PIL.Image ๋๋ str): ์
๋ก๋๋ ์ด๋ฏธ์ง ํ์ผ์ ๊ฐ์ฒด ๋๋ ํ์ผ ๊ฒฝ๋ก
|
103 |
-
Returns:
|
104 |
-
PIL.Image ๋๋ str: ๋ฐฐ๊ฒฝ์ด ์ ๊ฑฐ๋ ์ด๋ฏธ์ง ๋๋ ์๋ฌ ๋ฉ์์ง
|
105 |
-
"""
|
106 |
try:
|
107 |
logging.info("Processing image.")
|
108 |
|
109 |
-
# ์
๋ก๋๋ ์ด๋ฏธ์ง๊ฐ ๊ฒฝ๋ก์ธ์ง ํ์ธ ํ ์ด๋ฏธ์ง ๊ฐ์ฒด๋ก ๋ก๋
|
110 |
if isinstance(input_image, str):
|
111 |
input_img = Image.open(input_image)
|
112 |
else:
|
113 |
input_img = input_image
|
114 |
|
115 |
-
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ
|
116 |
output_img = remove(input_img)
|
117 |
-
|
|
|
118 |
|
119 |
-
|
120 |
-
return output_img
|
121 |
except Exception as e:
|
122 |
logging.error(f"Error removing background: {e}")
|
123 |
return f"์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
124 |
|
125 |
def clear_images():
|
126 |
-
"""์
๋ ฅ ์ด๋ฏธ์ง์ ์ถ๋ ฅ ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ ์ด๊ธฐํํ๋ ํจ์"""
|
127 |
return None, None
|
128 |
|
129 |
# ์์ ์ด๋ฏธ์ง ๊ฒฝ๋ก ์ค์
|
@@ -132,7 +36,7 @@ example_images = [
|
|
132 |
"./example2.jpg"
|
133 |
]
|
134 |
|
135 |
-
# ์์ ์ด๋ฏธ์ง
|
136 |
for img_path in example_images:
|
137 |
if not os.path.exists(img_path):
|
138 |
logging.warning(f"Example image {img_path} does not exist. Please provide valid paths.")
|
@@ -144,40 +48,37 @@ with gr.Blocks(css="""
|
|
144 |
color: white;
|
145 |
}
|
146 |
#btn-reset {
|
147 |
-
background-color: #
|
148 |
color: white;
|
149 |
}
|
150 |
""") as demo:
|
151 |
gr.Markdown("# ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๊ธฐ")
|
152 |
-
gr.Markdown("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฐฐ๊ฒฝ์ด ์๋์ผ๋ก
|
153 |
|
154 |
with gr.Row():
|
155 |
input_image = gr.Image(
|
156 |
type="pil",
|
157 |
label="์
๋ ฅ ์ด๋ฏธ์ง",
|
158 |
-
interactive=True
|
159 |
)
|
160 |
output_image = gr.Image(
|
161 |
type="pil",
|
162 |
label="๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง",
|
163 |
-
interactive=False
|
164 |
)
|
165 |
|
166 |
-
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ๋ฒํผ
|
167 |
with gr.Row():
|
168 |
-
btn_remove = gr.Button("๋ฐฐ๊ฒฝ ์ ๊ฑฐ", elem_id="btn-remove")
|
169 |
btn_remove.click(fn=remove_background, inputs=input_image, outputs=output_image)
|
170 |
|
171 |
-
|
172 |
-
btn_reset = gr.Button("์ด๊ธฐํ", elem_id="btn-reset") # elem_id๋ก ๋ฒํผ ์คํ์ผ ์ง์
|
173 |
btn_reset.click(fn=clear_images, inputs=None, outputs=[input_image, output_image])
|
174 |
|
175 |
-
# ์์ ์ด๋ฏธ์ง
|
176 |
gr.Examples(
|
177 |
-
examples=[[img] for img in example_images],
|
178 |
inputs=input_image,
|
179 |
label="์์ ์ด๋ฏธ์ง"
|
180 |
)
|
181 |
|
182 |
if __name__ == "__main__":
|
183 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from rembg import remove
|
3 |
from PIL import Image
|
|
|
8 |
logging.basicConfig(level=logging.INFO)
|
9 |
|
10 |
def remove_background(input_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
try:
|
12 |
logging.info("Processing image.")
|
13 |
|
|
|
14 |
if isinstance(input_image, str):
|
15 |
input_img = Image.open(input_image)
|
16 |
else:
|
17 |
input_img = input_image
|
18 |
|
19 |
+
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ (ํ๋์ ๋ฐฐ๊ฒฝ์ผ๋ก ์ค์ )
|
20 |
output_img = remove(input_img)
|
21 |
+
background = Image.new("RGBA", output_img.size, (0, 0, 255, 255)) # ํ๋์ ๋ฐฐ๊ฒฝ
|
22 |
+
output_img = Image.alpha_composite(background, output_img)
|
23 |
|
24 |
+
logging.info("Background removed successfully.")
|
25 |
+
return output_img
|
26 |
except Exception as e:
|
27 |
logging.error(f"Error removing background: {e}")
|
28 |
return f"์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
29 |
|
30 |
def clear_images():
|
|
|
31 |
return None, None
|
32 |
|
33 |
# ์์ ์ด๋ฏธ์ง ๊ฒฝ๋ก ์ค์
|
|
|
36 |
"./example2.jpg"
|
37 |
]
|
38 |
|
39 |
+
# ์์ ์ด๋ฏธ์ง ๊ฒฝ๋ก ํ์ธ
|
40 |
for img_path in example_images:
|
41 |
if not os.path.exists(img_path):
|
42 |
logging.warning(f"Example image {img_path} does not exist. Please provide valid paths.")
|
|
|
48 |
color: white;
|
49 |
}
|
50 |
#btn-reset {
|
51 |
+
background-color: #FF9800; /* ์ฃผํฉ์์ผ๋ก ๋ณ๊ฒฝ */
|
52 |
color: white;
|
53 |
}
|
54 |
""") as demo:
|
55 |
gr.Markdown("# ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๊ธฐ")
|
56 |
+
gr.Markdown("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฐฐ๊ฒฝ์ด ์๋์ผ๋ก ์ ๊ฑฐ๋๊ณ ํ๋์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋์ฒด๋ฉ๋๋ค.")
|
57 |
|
58 |
with gr.Row():
|
59 |
input_image = gr.Image(
|
60 |
type="pil",
|
61 |
label="์
๋ ฅ ์ด๋ฏธ์ง",
|
62 |
+
interactive=True
|
63 |
)
|
64 |
output_image = gr.Image(
|
65 |
type="pil",
|
66 |
label="๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง",
|
67 |
+
interactive=False
|
68 |
)
|
69 |
|
|
|
70 |
with gr.Row():
|
71 |
+
btn_remove = gr.Button("๋ฐฐ๊ฒฝ ์ ๊ฑฐ", elem_id="btn-remove")
|
72 |
btn_remove.click(fn=remove_background, inputs=input_image, outputs=output_image)
|
73 |
|
74 |
+
btn_reset = gr.Button("์ด๊ธฐํ", elem_id="btn-reset")
|
|
|
75 |
btn_reset.click(fn=clear_images, inputs=None, outputs=[input_image, output_image])
|
76 |
|
|
|
77 |
gr.Examples(
|
78 |
+
examples=[[img] for img in example_images],
|
79 |
inputs=input_image,
|
80 |
label="์์ ์ด๋ฏธ์ง"
|
81 |
)
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
+
demo.launch()
|