Commit
·
2b4b81f
1
Parent(s):
2d64873
change to white background
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import spaces
|
|
| 5 |
from transformers import AutoModelForImageSegmentation
|
| 6 |
import torch
|
| 7 |
from torchvision import transforms
|
|
|
|
| 8 |
|
| 9 |
torch.set_float32_matmul_precision(["high", "highest"][0])
|
| 10 |
|
|
@@ -25,9 +26,8 @@ def fn(image):
|
|
| 25 |
im = im.convert("RGB")
|
| 26 |
origin = im.copy()
|
| 27 |
image = process(im)
|
| 28 |
-
return
|
| 29 |
|
| 30 |
-
@spaces.GPU
|
| 31 |
def process(image):
|
| 32 |
image_size = image.size
|
| 33 |
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
|
@@ -37,8 +37,12 @@ def process(image):
|
|
| 37 |
pred = preds[0].squeeze()
|
| 38 |
pred_pil = transforms.ToPILImage()(pred)
|
| 39 |
mask = pred_pil.resize(image_size)
|
|
|
|
|
|
|
| 40 |
image.putalpha(mask)
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
def process_file(f):
|
| 44 |
name_path = f.rsplit(".",1)[0]+".png"
|
|
@@ -48,7 +52,7 @@ def process_file(f):
|
|
| 48 |
transparent.save(name_path)
|
| 49 |
return name_path
|
| 50 |
|
| 51 |
-
slider1 =
|
| 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")
|
|
@@ -68,8 +72,8 @@ tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["bu
|
|
| 68 |
|
| 69 |
|
| 70 |
demo = gr.TabbedInterface(
|
| 71 |
-
[
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
demo.launch(
|
|
|
|
| 5 |
from transformers import AutoModelForImageSegmentation
|
| 6 |
import torch
|
| 7 |
from torchvision import transforms
|
| 8 |
+
from PIL import Image
|
| 9 |
|
| 10 |
torch.set_float32_matmul_precision(["high", "highest"][0])
|
| 11 |
|
|
|
|
| 26 |
im = im.convert("RGB")
|
| 27 |
origin = im.copy()
|
| 28 |
image = process(im)
|
| 29 |
+
return image
|
| 30 |
|
|
|
|
| 31 |
def process(image):
|
| 32 |
image_size = image.size
|
| 33 |
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
|
|
|
| 37 |
pred = preds[0].squeeze()
|
| 38 |
pred_pil = transforms.ToPILImage()(pred)
|
| 39 |
mask = pred_pil.resize(image_size)
|
| 40 |
+
|
| 41 |
+
white_background = Image.new("RGBA", image_size, (255, 255, 255, 255))
|
| 42 |
image.putalpha(mask)
|
| 43 |
+
combined = Image.alpha_composite(white_background, image)
|
| 44 |
+
|
| 45 |
+
return combined
|
| 46 |
|
| 47 |
def process_file(f):
|
| 48 |
name_path = f.rsplit(".",1)[0]+".png"
|
|
|
|
| 52 |
transparent.save(name_path)
|
| 53 |
return name_path
|
| 54 |
|
| 55 |
+
slider1 = gr.Image()
|
| 56 |
slider2 = ImageSlider(label="birefnet", type="pil")
|
| 57 |
image = gr.Image(label="Upload an image")
|
| 58 |
image2 = gr.Image(label="Upload an image",type="filepath")
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
demo = gr.TabbedInterface(
|
| 75 |
+
[tab3], ["png"], title="Na Na"
|
| 76 |
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
demo.launch(auth=("nana", "2024"), share=True)
|