Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 13 |
+
Parameters:
|
| 14 |
+
input_image (PIL.Image ๋๋ str): ์
๋ก๋๋ ์ด๋ฏธ์ง ํ์ผ์ ๊ฐ์ฒด ๋๋ ํ์ผ ๊ฒฝ๋ก
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
PIL.Image ๋๋ str: ๋ฐฐ๊ฒฝ์ด ์ ๊ฑฐ๋ ์ด๋ฏธ์ง ๋๋ ์๋ฌ ๋ฉ์์ง
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
logging.info("Processing image.")
|
| 21 |
+
|
| 22 |
+
if isinstance(input_image, str):
|
| 23 |
+
input_img = Image.open(input_image)
|
| 24 |
+
else:
|
| 25 |
+
input_img = input_image
|
| 26 |
+
|
| 27 |
+
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ
|
| 28 |
+
output_img = remove(input_img)
|
| 29 |
+
logging.info("Background removed successfully.")
|
| 30 |
+
|
| 31 |
+
# ํฌ๋ช
๋๋ฅผ ์ ์งํ๊ธฐ ์ํด PNG ํ์์ผ๋ก ๋ณํ
|
| 32 |
+
return output_img.convert("RGBA")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
logging.error(f"Error removing background: {e}")
|
| 35 |
+
return f"์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 36 |
+
|
| 37 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("# ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๊ธฐ")
|
| 40 |
+
gr.Markdown("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฐฐ๊ฒฝ์ด ์๋์ผ๋ก ์ ๊ฑฐ๋ฉ๋๋ค.")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
input_image = gr.Image(
|
| 44 |
+
type="pil",
|
| 45 |
+
label="์
๋ ฅ ์ด๋ฏธ์ง",
|
| 46 |
+
interactive=True # ์ฌ์ฉ์๊ฐ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ ์ ์๋๋ก ์ค์
|
| 47 |
+
)
|
| 48 |
+
output_image = gr.Image(
|
| 49 |
+
type="pil",
|
| 50 |
+
label="๋ฐฐ๊ฒฝ ์ ๊ฑฐ๋ ์ด๋ฏธ์ง",
|
| 51 |
+
interactive=False # ์ถ๋ ฅ ์ด๋ฏธ์ง๋ ์ํธ์์ฉ ๋ถ๊ฐ๋ฅ
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# ๋ฐฐ๊ฒฝ ์ ๊ฑฐ ๋ฒํผ
|
| 55 |
+
btn = gr.Button("๋ฐฐ๊ฒฝ ์ ๊ฑฐ")
|
| 56 |
+
btn.click(fn=remove_background, inputs=input_image, outputs=output_image)
|
| 57 |
+
|
| 58 |
+
# ์์ ์ด๋ฏธ์ง
|
| 59 |
+
gr.Examples(
|
| 60 |
+
examples=[
|
| 61 |
+
["examples/example1.jpg"],
|
| 62 |
+
["examples/example2.png"]
|
| 63 |
+
],
|
| 64 |
+
inputs=input_image,
|
| 65 |
+
label="์์ ์ด๋ฏธ์ง"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
gr.Markdown("ยฉ 2024 ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ์ ๊ฑฐ๊ธฐ")
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|