Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
gr.Image(type="pil", label="Output"),
|
| 73 |
-
title=title,
|
| 74 |
-
description=description,
|
| 75 |
-
article=article,
|
| 76 |
-
examples=[['removebg/girl1.png'],['removebg/girl2.png'],['removebg/girl3.png'],['removebg/gonfu1.jpg'],['removebg/angel.png']],
|
| 77 |
-
#enable_queue=True,
|
| 78 |
-
).launch(debug=False)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision import transforms
|
| 3 |
+
from transformers import AutoModelForImageSegmentation
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
title = "η§»ι€θζ― Demo"
|
| 10 |
+
description = "δΈε³εη ,θͺεε»ι€θζ―."
|
| 11 |
+
|
| 12 |
+
# Set up CUDA if available
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
torch.set_float32_matmul_precision("high")
|
| 15 |
+
|
| 16 |
+
# Load the model
|
| 17 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 18 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
| 19 |
+
)
|
| 20 |
+
birefnet.to(device)
|
| 21 |
+
|
| 22 |
+
# Define image transformations
|
| 23 |
+
transform_image = transforms.Compose([
|
| 24 |
+
transforms.Resize((256, 256)),
|
| 25 |
+
transforms.ToTensor(),
|
| 26 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
| 27 |
+
])
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def load_img(image_path_or_url):
|
| 31 |
+
if image_path_or_url.startswith('http'):
|
| 32 |
+
response = requests.get(image_path_or_url)
|
| 33 |
+
img = Image.open(BytesIO(response.content))
|
| 34 |
+
else:
|
| 35 |
+
img = Image.open(image_path_or_url)
|
| 36 |
+
return img.convert("RGB")
|
| 37 |
+
|
| 38 |
+
def process(image):
|
| 39 |
+
image_size = image.size
|
| 40 |
+
input_images = transform_image(image).unsqueeze(0).to(device)
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
| 44 |
+
|
| 45 |
+
pred = preds[0].squeeze()
|
| 46 |
+
pred_pil = transforms.ToPILImage()(pred)
|
| 47 |
+
mask = pred_pil.resize(image_size)
|
| 48 |
+
|
| 49 |
+
# Create a new image with transparency
|
| 50 |
+
transparent_image = Image.new("RGBA", image.size)
|
| 51 |
+
transparent_image.paste(image, (0, 0))
|
| 52 |
+
transparent_image.putalpha(mask) # Apply mask to the new image
|
| 53 |
+
|
| 54 |
+
return transparent_image # Return the new transparent image
|
| 55 |
+
|
| 56 |
+
def remove_background_gradio(image):
|
| 57 |
+
processed_img = process(image)
|
| 58 |
+
return processed_img
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Create the Gradio interface with drag-and-drop and paste functionality
|
| 62 |
+
demo = gr.Interface(
|
| 63 |
+
fn=remove_background_gradio,
|
| 64 |
+
inputs = gr.Image(type="pil"), # Remove 'source' argument
|
| 65 |
+
outputs = gr.Image(type="pil"),
|
| 66 |
+
title = title,
|
| 67 |
+
description = description,
|
| 68 |
+
# examples=[['girl1.png'],['girl2.png'],['girl3.png'],['gonfu1.jpg'],['removebg/angel.png']],
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
demo.launch(share=True) # Launch the interface and get a shareable link
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|