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

Update app.py (#11)

Browse files

- Update app.py (95743c20b2b4551363302fc69e8c8b905d1e9cf4)


Co-authored-by: Lui <[email protected]>

Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -12,6 +12,7 @@ birefnet = AutoModelForImageSegmentation.from_pretrained(
12
  "ZhengPeng7/BiRefNet", trust_remote_code=True
13
  )
14
  birefnet.to("cuda")
 
15
  transform_image = transforms.Compose(
16
  [
17
  transforms.Resize((1024, 1024)),
@@ -24,8 +25,8 @@ 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):
@@ -39,37 +40,33 @@ def process(image):
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")
 
60
 
61
- url = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
62
- tab1 = gr.Interface(
63
- fn, inputs=image, outputs=slider1, examples=[chameleon], api_name="image"
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__":
75
- demo.launch(show_error=True)
 
12
  "ZhengPeng7/BiRefNet", trust_remote_code=True
13
  )
14
  birefnet.to("cuda")
15
+
16
  transform_image = transforms.Compose(
17
  [
18
  transforms.Resize((1024, 1024)),
 
25
  im = load_img(image, output_type="pil")
26
  im = im.convert("RGB")
27
  origin = im.copy()
28
+ processed_image = process(im)
29
+ return (processed_image, origin)
30
 
31
  @spaces.GPU
32
  def process(image):
 
40
  mask = pred_pil.resize(image_size)
41
  image.putalpha(mask)
42
  return image
43
+
44
  def process_file(f):
45
+ name_path = f.rsplit(".", 1)[0] + ".png"
46
  im = load_img(f, output_type="pil")
47
  im = im.convert("RGB")
48
  transparent = process(im)
49
  transparent.save(name_path)
50
  return name_path
51
 
52
+ slider1 = ImageSlider(label="Processed Image", type="pil")
53
+ slider2 = ImageSlider(label="Processed Image from URL", type="pil")
54
+ image_upload = gr.Image(label="Upload an image")
55
+ image_file_upload = gr.Image(label="Upload an image", type="filepath")
56
+ url_input = gr.Textbox(label="Paste an image URL")
57
+ output_file = gr.File(label="Output PNG File")
 
58
 
59
+ # Example images
60
  chameleon = load_img("butterfly.jpg", output_type="pil")
61
+ url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
62
 
63
+ tab1 = gr.Interface(fn, inputs=image_upload, outputs=slider1, examples=[chameleon], api_name="image")
64
+ tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, examples=[url_example], api_name="text")
65
+ tab3 = gr.Interface(process_file, inputs=image_file_upload, outputs=output_file, examples=["butterfly.jpg"], api_name="png")
 
 
 
 
 
66
 
67
  demo = gr.TabbedInterface(
68
+ [tab1, tab2, tab3], ["Image Upload", "URL Input", "File Output"], title="Background Removal Tool"
69
  )
70
 
71
  if __name__ == "__main__":
72
+ demo.launch(show_error=True)