tuan2308 commited on
Commit
2ccc78e
1 Parent(s): 037b1ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import gradio as gr
 
3
  from loadimg import load_img
4
  import spaces
5
  from transformers import AutoModelForImageSegmentation
@@ -32,10 +33,11 @@ def fn(image):
32
  session_folder, session_id = create_session_folder()
33
  im = load_img(image, output_type="pil")
34
  im = im.convert("RGB")
35
- processed_image = process(im)
36
- output_path = os.path.join(session_folder, "no_bg_image.png")
37
- processed_image.save(output_path)
38
- return output_path # Chỉ trả về file đầu ra
 
39
 
40
  @spaces.GPU
41
  def process(image):
@@ -49,31 +51,36 @@ def process(image):
49
  mask = pred_pil.resize(image_size)
50
  image.putalpha(mask)
51
  return image
52
-
53
  def process_file(f):
54
  session_folder, session_id = create_session_folder()
55
- output_path = os.path.join(session_folder, os.path.basename(f).rsplit(".", 1)[0] + ".png")
56
  im = load_img(f, output_type="pil")
57
  im = im.convert("RGB")
58
- processed_image = process(im)
59
- processed_image.save(output_path)
60
- return output_path # Chỉ trả về file đầu ra
 
 
 
 
 
 
 
61
 
62
- # Input output cho giao diện Gradio
63
- image_input = gr.Image(label="Upload an image", type="filepath")
64
- output_file = gr.File(label="Download output image")
65
 
66
  tab1 = gr.Interface(
67
- fn, inputs=image_input, outputs=output_file, examples=["giraffe.jpg"], api_name="image"
68
  )
69
 
70
- tab2 = gr.Interface(
71
- process_file, inputs=image_input, outputs=output_file, examples=["giraffe.jpg"], api_name="file"
72
- )
73
 
74
- # Giao diện chính
75
  demo = gr.TabbedInterface(
76
- [tab1, tab2], ["Process Uploaded Image", "Process File"], title="RMBG-2.0 for Background Removal"
77
  )
78
 
79
  if __name__ == "__main__":
 
1
  import os
2
  import gradio as gr
3
+ from gradio_imageslider import ImageSlider
4
  from loadimg import load_img
5
  import spaces
6
  from transformers import AutoModelForImageSegmentation
 
33
  session_folder, session_id = create_session_folder()
34
  im = load_img(image, output_type="pil")
35
  im = im.convert("RGB")
36
+ origin = im.copy()
37
+ image = process(im)
38
+ image_path = os.path.join(session_folder, "no_bg_image.png")
39
+ image.save(image_path)
40
+ return (image, origin), image_path
41
 
42
  @spaces.GPU
43
  def process(image):
 
51
  mask = pred_pil.resize(image_size)
52
  image.putalpha(mask)
53
  return image
54
+
55
  def process_file(f):
56
  session_folder, session_id = create_session_folder()
57
+ name_path = os.path.join(session_folder, os.path.basename(f).rsplit(".", 1)[0] + ".png")
58
  im = load_img(f, output_type="pil")
59
  im = im.convert("RGB")
60
+ transparent = process(im)
61
+ transparent.save(name_path)
62
+ return name_path
63
+
64
+ slider1 = ImageSlider(label="RMBG-2.0", type="pil")
65
+ slider2 = ImageSlider(label="RMBG-2.0", type="pil")
66
+ image = gr.Image(label="Upload an image")
67
+ image2 = gr.Image(label="Upload an image", type="filepath")
68
+ text = gr.Textbox(label="Paste an image URL")
69
+ png_file = gr.File(label="output png file")
70
 
71
+ chameleon = load_img("giraffe.jpg", output_type="pil")
72
+
73
+ url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"
74
 
75
  tab1 = gr.Interface(
76
+ fn, inputs=image, outputs=[slider1, gr.File(label="output png file")], examples=[chameleon], api_name="image"
77
  )
78
 
79
+ tab2 = gr.Interface(fn, inputs=text, outputs=[slider2, gr.File(label="output png file")], examples=[url], api_name="text")
80
+ tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["giraffe.jpg"], api_name="png")
 
81
 
 
82
  demo = gr.TabbedInterface(
83
+ [tab1, tab2], ["input image", "input url"], title="RMBG-2.0 for background removal"
84
  )
85
 
86
  if __name__ == "__main__":