Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
from loadimg import load_img
|
4 |
import spaces
|
5 |
from transformers import AutoModelForImageSegmentation
|
6 |
import torch
|
7 |
from torchvision import transforms
|
8 |
-
import uuid #
|
9 |
|
10 |
torch.set_float32_matmul_precision(["high", "highest"][0])
|
11 |
|
@@ -24,10 +25,20 @@ transform_image = transforms.Compose(
|
|
24 |
def create_session_folder():
|
25 |
"""Tạo thư mục duy nhất dựa trên UUID cho mỗi phiên làm việc."""
|
26 |
session_id = str(uuid.uuid4()) # Tạo UUID duy nhất
|
27 |
-
session_folder = os.path.join(
|
28 |
os.makedirs(session_folder, exist_ok=True)
|
29 |
return session_folder, session_id
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
@spaces.GPU
|
32 |
def process(image):
|
33 |
image_size = image.size
|
@@ -38,38 +49,38 @@ def process(image):
|
|
38 |
pred = preds[0].squeeze()
|
39 |
pred_pil = transforms.ToPILImage()(pred)
|
40 |
mask = pred_pil.resize(image_size)
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
pixels = result.load()
|
46 |
-
|
47 |
-
for i in range(result.width):
|
48 |
-
for j in range(result.height):
|
49 |
-
# Đặt màu đen với độ trong suốt
|
50 |
-
alpha = transparent_mask.getpixel((i, j))
|
51 |
-
pixels[i, j] = (0, 0, 0, alpha)
|
52 |
-
|
53 |
-
return result
|
54 |
-
|
55 |
-
def fn(image):
|
56 |
session_folder, session_id = create_session_folder()
|
57 |
-
|
|
|
58 |
im = im.convert("RGB")
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
return image_path
|
63 |
|
|
|
|
|
64 |
image = gr.Image(label="Upload an image")
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
tab1 = gr.Interface(
|
68 |
-
fn, inputs=image, outputs=
|
69 |
)
|
70 |
|
|
|
|
|
|
|
71 |
demo = gr.TabbedInterface(
|
72 |
-
[tab1], ["
|
73 |
)
|
74 |
|
75 |
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
|
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 |
|
|
|
25 |
def create_session_folder():
|
26 |
"""Tạo thư mục duy nhất dựa trên UUID cho mỗi phiên làm việc."""
|
27 |
session_id = str(uuid.uuid4()) # Tạo UUID duy nhất
|
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(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):
|
44 |
image_size = image.size
|
|
|
49 |
pred = preds[0].squeeze()
|
50 |
pred_pil = transforms.ToPILImage()(pred)
|
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__":
|