jpjp9292 commited on
Commit
188242c
ยท
verified ยท
1 Parent(s): 585c975

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -113
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
- logging.info("Background removed successfully.")
 
118
 
119
- # ํˆฌ๋ช…๋„๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด PNG ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
120
- return output_img.convert("RGBA")
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: #f44336; /* ๋นจ๊ฐ„์ƒ‰ */
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") # elem_id๋กœ ๋ฒ„ํŠผ ์Šคํƒ€์ผ ์ง€์ •
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()