Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,6 @@ import spaces
|
|
6 |
from transformers import AutoModelForImageSegmentation
|
7 |
import torch
|
8 |
from torchvision import transforms
|
9 |
-
import uuid # Thêm thư viện uuid để tạo UUID
|
10 |
|
11 |
torch.set_float32_matmul_precision(["high", "highest"][0])
|
12 |
|
@@ -22,20 +21,16 @@ transform_image = transforms.Compose(
|
|
22 |
]
|
23 |
)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
session_folder = os.path.join('output_images', session_id)
|
29 |
-
os.makedirs(session_folder, exist_ok=True)
|
30 |
-
return session_folder, session_id
|
31 |
|
32 |
def fn(image):
|
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(
|
39 |
image.save(image_path)
|
40 |
return (image, origin), image_path
|
41 |
|
@@ -53,8 +48,7 @@ def process(image):
|
|
53 |
return image
|
54 |
|
55 |
def process_file(f):
|
56 |
-
|
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)
|
@@ -64,10 +58,11 @@ def process_file(f):
|
|
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",
|
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"
|
@@ -79,6 +74,7 @@ tab1 = gr.Interface(
|
|
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 |
)
|
|
|
6 |
from transformers import AutoModelForImageSegmentation
|
7 |
import torch
|
8 |
from torchvision import transforms
|
|
|
9 |
|
10 |
torch.set_float32_matmul_precision(["high", "highest"][0])
|
11 |
|
|
|
21 |
]
|
22 |
)
|
23 |
|
24 |
+
output_folder = 'output_images'
|
25 |
+
if not os.path.exists(output_folder):
|
26 |
+
os.makedirs(output_folder)
|
|
|
|
|
|
|
27 |
|
28 |
def fn(image):
|
|
|
29 |
im = load_img(image, output_type="pil")
|
30 |
im = im.convert("RGB")
|
31 |
origin = im.copy()
|
32 |
image = process(im)
|
33 |
+
image_path = os.path.join(output_folder, "no_bg_image.png")
|
34 |
image.save(image_path)
|
35 |
return (image, origin), image_path
|
36 |
|
|
|
48 |
return image
|
49 |
|
50 |
def process_file(f):
|
51 |
+
name_path = f.rsplit(".",1)[0]+".png"
|
|
|
52 |
im = load_img(f, output_type="pil")
|
53 |
im = im.convert("RGB")
|
54 |
transparent = process(im)
|
|
|
58 |
slider1 = ImageSlider(label="RMBG-2.0", type="pil")
|
59 |
slider2 = ImageSlider(label="RMBG-2.0", type="pil")
|
60 |
image = gr.Image(label="Upload an image")
|
61 |
+
image2 = gr.Image(label="Upload an image",type="filepath")
|
62 |
text = gr.Textbox(label="Paste an image URL")
|
63 |
png_file = gr.File(label="output png file")
|
64 |
|
65 |
+
|
66 |
chameleon = load_img("giraffe.jpg", output_type="pil")
|
67 |
|
68 |
url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"
|
|
|
74 |
tab2 = gr.Interface(fn, inputs=text, outputs=[slider2, gr.File(label="output png file")], examples=[url], api_name="text")
|
75 |
tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["giraffe.jpg"], api_name="png")
|
76 |
|
77 |
+
|
78 |
demo = gr.TabbedInterface(
|
79 |
[tab1, tab2], ["input image", "input url"], title="RMBG-2.0 for background removal"
|
80 |
)
|