DreamerHC commited on
Commit
aa8ba47
ยท
verified ยท
1 Parent(s): 36882f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -31
app.py CHANGED
@@ -1,37 +1,16 @@
1
  import gradio as gr
2
- from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
3
- from PIL import Image
4
- import torch
5
-
6
- # ๋ชจ๋ธ๊ณผ ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ ๋กœ๋“œ
7
- model_name = "nvidia/mit-b0"
8
- image_processor = SegformerImageProcessor.from_pretrained(model_name)
9
- model = SegformerForSemanticSegmentation.from_pretrained(model_name)
10
 
11
  def remove_background(image):
12
- # ์ด๋ฏธ์ง€์—์„œ ํ”ผ์ฒ˜ ์ถ”์ถœ
13
- inputs = image_processor(images=image, return_tensors="pt")
14
- outputs = model(**inputs)
15
- logits = outputs.logits
16
- # ๊ฒฐ๊ณผ์—์„œ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ ๋†’์€ ํด๋ž˜์Šค ์„ ํƒ
17
- upsampled_logits = torch.nn.functional.interpolate(logits, size=image.size[::-1], mode="bilinear", align_corners=False)
18
- seg = upsampled_logits.argmax(dim=1)[0]
19
- # ๋ฐฐ๊ฒฝ(0)์ด ์•„๋‹Œ ๋ถ€๋ถ„๋งŒ ์ถ”์ถœ
20
- mask = seg != 0
21
- # ์›๋ณธ ์ด๋ฏธ์ง€์— ๋งˆ์Šคํฌ ์ ์šฉ
22
- masked_image = Image.fromarray(image).convert("RGBA")
23
- data = masked_image.getdata()
24
- newData = []
25
- for i, item in enumerate(data):
26
- if mask.flatten()[i]:
27
- newData.append(item)
28
- else:
29
- newData.append((255, 255, 255, 0))
30
- masked_image.putdata(newData)
31
- return masked_image
32
 
33
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
34
- iface = gr.Interface(fn=remove_background, inputs=gr.Image(type="pil"), outputs="image", title="Background Remover", description="Upload an image to remove the background.")
 
 
 
 
 
35
 
36
- # ์•ฑ ์‹คํ–‰
37
  iface.launch()
 
1
  import gradio as gr
2
+ from rembg import remove
 
 
 
 
 
 
 
3
 
4
  def remove_background(image):
5
+ output_image = remove(image)
6
+ return output_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ iface = gr.Interface(
9
+ fn=remove_background,
10
+ inputs=gr.inputs.Image(type="pil"),
11
+ outputs=gr.outputs.Image(type="pil"),
12
+ title="๋ฐฐ๊ฒฝ ์ œ๊ฑฐ ๋„๊ตฌ",
13
+ description="์ด ๋„๊ตฌ๋Š” ์ด๋ฏธ์ง€์—์„œ ๋ฐฐ๊ฒฝ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด ๋ฐฐ๊ฒฝ์ด ์ œ๊ฑฐ๋œ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."
14
+ )
15
 
 
16
  iface.launch()