not-lain commited on
Commit
2d64873
β€’
1 Parent(s): d86f2fa

🌘wπŸŒ–

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -20,14 +20,16 @@ transform_image = transforms.Compose(
20
  ]
21
  )
22
 
23
-
24
- @spaces.GPU
25
  def fn(image):
26
  im = load_img(image, output_type="pil")
27
  im = im.convert("RGB")
28
- image_size = im.size
29
  origin = im.copy()
30
- image = load_img(im)
 
 
 
 
 
31
  input_images = transform_image(image).unsqueeze(0).to("cuda")
32
  # Prediction
33
  with torch.no_grad():
@@ -36,13 +38,22 @@ def fn(image):
36
  pred_pil = transforms.ToPILImage()(pred)
37
  mask = pred_pil.resize(image_size)
38
  image.putalpha(mask)
39
- return (image, origin)
40
-
 
 
 
 
 
 
 
41
 
42
  slider1 = ImageSlider(label="birefnet", type="pil")
43
  slider2 = ImageSlider(label="birefnet", type="pil")
44
  image = gr.Image(label="Upload an image")
 
45
  text = gr.Textbox(label="Paste an image URL")
 
46
 
47
 
48
  chameleon = load_img("butterfly.jpg", output_type="pil")
@@ -53,10 +64,11 @@ tab1 = gr.Interface(
53
  )
54
 
55
  tab2 = gr.Interface(fn, inputs=text, outputs=slider2, examples=[url], api_name="text")
 
56
 
57
 
58
  demo = gr.TabbedInterface(
59
- [tab1, tab2], ["image", "text"], title="birefnet for background removal"
60
  )
61
 
62
  if __name__ == "__main__":
 
20
  ]
21
  )
22
 
 
 
23
  def fn(image):
24
  im = load_img(image, output_type="pil")
25
  im = im.convert("RGB")
 
26
  origin = im.copy()
27
+ image = process(im)
28
+ return (image, origin)
29
+
30
+ @spaces.GPU
31
+ def process(image):
32
+ image_size = image.size
33
  input_images = transform_image(image).unsqueeze(0).to("cuda")
34
  # Prediction
35
  with torch.no_grad():
 
38
  pred_pil = transforms.ToPILImage()(pred)
39
  mask = pred_pil.resize(image_size)
40
  image.putalpha(mask)
41
+ return image
42
+
43
+ def process_file(f):
44
+ name_path = f.rsplit(".",1)[0]+".png"
45
+ im = load_img(f, output_type="pil")
46
+ im = im.convert("RGB")
47
+ transparent = process(im)
48
+ transparent.save(name_path)
49
+ return name_path
50
 
51
  slider1 = ImageSlider(label="birefnet", type="pil")
52
  slider2 = ImageSlider(label="birefnet", type="pil")
53
  image = gr.Image(label="Upload an image")
54
+ image2 = gr.Image(label="Upload an image",type="filepath")
55
  text = gr.Textbox(label="Paste an image URL")
56
+ png_file = gr.File(label="output png file")
57
 
58
 
59
  chameleon = load_img("butterfly.jpg", output_type="pil")
 
64
  )
65
 
66
  tab2 = gr.Interface(fn, inputs=text, outputs=slider2, examples=[url], api_name="text")
67
+ tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["butterfly.jpg"], api_name="png")
68
 
69
 
70
  demo = gr.TabbedInterface(
71
+ [tab1, tab2,tab3], ["image", "text","png"], title="birefnet for background removal"
72
  )
73
 
74
  if __name__ == "__main__":