Update app.py
Browse files
app.py
CHANGED
|
@@ -1,121 +1,99 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import cv2
|
| 4 |
from PIL import Image
|
| 5 |
-
import requests
|
| 6 |
import torch
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
"BRIA": None,
|
| 12 |
-
"INSPyReNet": None,
|
| 13 |
-
"U2Net": None,
|
| 14 |
-
"U2NetHumanSeg": None,
|
| 15 |
-
"ISNetGeneral": None,
|
| 16 |
-
"ISNetAnime": None
|
| 17 |
-
}
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
if model_name == "BRIA":
|
| 22 |
-
from transformers import AutoModelForImageSegmentation
|
| 23 |
-
return AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-1.4", trust_remote_code=True)
|
| 24 |
-
elif model_name == "INSPyReNet":
|
| 25 |
-
from IS2.models.inspyrenet import INSPyReNet
|
| 26 |
-
model = INSPyReNet()
|
| 27 |
-
model.load_state_dict(torch.hub.load_state_dict_from_url("https://github.com/helloyufei/INSPyReNet/releases/download/v1.0.0/inspyrenet.pth"))
|
| 28 |
-
return model
|
| 29 |
-
elif model_name == "U2Net":
|
| 30 |
-
import u2net
|
| 31 |
-
return u2net.load_model(model_name="u2net")
|
| 32 |
-
elif model_name == "U2NetHumanSeg":
|
| 33 |
-
import u2net
|
| 34 |
-
return u2net.load_model(model_name="u2net_human_seg")
|
| 35 |
-
elif model_name == "ISNetGeneral":
|
| 36 |
-
from isnet import ISNet
|
| 37 |
-
model = ISNet()
|
| 38 |
-
model.load_state_dict(torch.hub.load_state_dict_from_url("https://github.com/xuebinqin/DIS/raw/main/IS-Net/isnet-general-use.pth"))
|
| 39 |
-
return model
|
| 40 |
-
elif model_name == "ISNetAnime":
|
| 41 |
-
from isnet import ISNet
|
| 42 |
-
model = ISNet()
|
| 43 |
-
model.load_state_dict(torch.hub.load_state_dict_from_url("https://github.com/xuebinqin/DIS/raw/main/IS-Net/isnet-anime.pth"))
|
| 44 |
-
return model
|
| 45 |
|
| 46 |
-
def
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
model
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
with torch.no_grad():
|
| 66 |
-
mask = model(input_img).squeeze().cpu().numpy()
|
| 67 |
-
else: # INSPyReNet, ISNet models
|
| 68 |
-
input_img = np.array(image)
|
| 69 |
-
input_img = cv2.resize(input_img, (1024, 1024))
|
| 70 |
-
input_img = ToTensor()(input_img).unsqueeze(0)
|
| 71 |
-
with torch.no_grad():
|
| 72 |
-
mask = model(input_img).squeeze().cpu().numpy()
|
| 73 |
-
|
| 74 |
-
# Post-process mask
|
| 75 |
mask = (mask - mask.min()) / (mask.max() - mask.min())
|
| 76 |
mask = (mask * 255).astype(np.uint8)
|
| 77 |
-
mask = cv2.resize(mask, (image.width, image.height))
|
| 78 |
return Image.fromarray(mask)
|
| 79 |
|
| 80 |
-
def
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
combined = np.clip(combined, 0, 255).astype(np.uint8)
|
| 89 |
-
return Image.fromarray(combined)
|
| 90 |
|
| 91 |
def remove_background(image):
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
image
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
# Gradio interface
|
| 112 |
interface = gr.Interface(
|
| 113 |
fn=remove_background,
|
| 114 |
inputs=gr.Image(label="Input Image"),
|
| 115 |
-
outputs=gr.Image(label="
|
| 116 |
-
title="
|
| 117 |
-
description="Combines
|
| 118 |
)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
import torch
|
| 5 |
+
import warnings
|
| 6 |
|
| 7 |
+
# Suppress warnings for cleaner output
|
| 8 |
+
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Initialize models dictionary to cache loaded models
|
| 11 |
+
models = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def load_bria_model():
|
| 14 |
+
from transformers import AutoModelForImageSegmentation, AutoImageProcessor
|
| 15 |
+
model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-1.4", trust_remote_code=True)
|
| 16 |
+
processor = AutoImageProcessor.from_pretrained("briaai/RMBG-1.4")
|
| 17 |
+
return model, processor
|
| 18 |
+
|
| 19 |
+
def load_rembg_model(model_name):
|
| 20 |
+
from rembg import new_session
|
| 21 |
+
return new_session(model_name)
|
| 22 |
+
|
| 23 |
+
def load_isnet_model(model_url):
|
| 24 |
+
# Placeholder - you would implement proper ISNet loading here
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
def apply_bria(image, model, processor):
|
| 28 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
mask = outputs.logits.squeeze().cpu().numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
mask = (mask - mask.min()) / (mask.max() - mask.min())
|
| 33 |
mask = (mask * 255).astype(np.uint8)
|
|
|
|
| 34 |
return Image.fromarray(mask)
|
| 35 |
|
| 36 |
+
def apply_rembg(image, session):
|
| 37 |
+
from rembg import remove
|
| 38 |
+
return remove(image, session=session)
|
| 39 |
+
|
| 40 |
+
def apply_isnet(image, model):
|
| 41 |
+
# Placeholder for ISNet implementation
|
| 42 |
+
return image
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def remove_background(image):
|
| 45 |
+
try:
|
| 46 |
+
# Convert input to PIL Image
|
| 47 |
+
if isinstance(image, np.ndarray):
|
| 48 |
+
image = Image.fromarray(image)
|
| 49 |
+
|
| 50 |
+
# Initialize models if not already loaded
|
| 51 |
+
if "bria" not in models:
|
| 52 |
+
bria_model, bria_processor = load_bria_model()
|
| 53 |
+
models["bria"] = (bria_model, bria_processor)
|
| 54 |
+
|
| 55 |
+
if "u2net" not in models:
|
| 56 |
+
models["u2net"] = load_rembg_model("u2net")
|
| 57 |
+
|
| 58 |
+
if "isnet" not in models:
|
| 59 |
+
models["isnet"] = load_isnet_model("https://example.com/isnet.pth")
|
| 60 |
+
|
| 61 |
+
# Apply models in sequence
|
| 62 |
+
results = []
|
| 63 |
+
|
| 64 |
+
# BRIA
|
| 65 |
+
bria_model, bria_processor = models["bria"]
|
| 66 |
+
bria_result = apply_bria(image, bria_model, bria_processor)
|
| 67 |
+
results.append(bria_result)
|
| 68 |
+
|
| 69 |
+
# U2Net
|
| 70 |
+
u2net_result = apply_rembg(image, models["u2net"])
|
| 71 |
+
results.append(u2net_result)
|
| 72 |
+
|
| 73 |
+
# Combine results (simple average for demonstration)
|
| 74 |
+
combined = np.zeros_like(np.array(results[0]), dtype=np.float32)
|
| 75 |
+
for res in results:
|
| 76 |
+
combined += np.array(res).astype(np.float32) / len(results)
|
| 77 |
+
combined = np.clip(combined, 0, 255).astype(np.uint8)
|
| 78 |
+
|
| 79 |
+
# Apply mask to original image
|
| 80 |
+
final = image.copy()
|
| 81 |
+
final.putalpha(Image.fromarray(combined))
|
| 82 |
+
|
| 83 |
+
return final
|
| 84 |
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error: {e}")
|
| 87 |
+
return image # Return original image on error
|
| 88 |
|
| 89 |
+
# Create Gradio interface
|
| 90 |
interface = gr.Interface(
|
| 91 |
fn=remove_background,
|
| 92 |
inputs=gr.Image(label="Input Image"),
|
| 93 |
+
outputs=gr.Image(label="Result with Transparent Background"),
|
| 94 |
+
title="Advanced Background Removal",
|
| 95 |
+
description="Combines multiple state-of-the-art models for high-quality background removal"
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
+
interface.launch(share=True) # Set share=True for public link
|