Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,13 +4,11 @@ import numpy as np
|
|
| 4 |
import zipfile
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
-
from train import AnimeSegmentation # This will work with step 2 below
|
| 8 |
|
| 9 |
-
#
|
|
|
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
-
|
| 12 |
-
# Load the model using the official mixin method
|
| 13 |
-
model = AnimeSegmentation.from_pretrained("skytnt/anime-seg")
|
| 14 |
model.to(device)
|
| 15 |
model.eval()
|
| 16 |
|
|
@@ -18,7 +16,6 @@ def process_images(file_paths):
|
|
| 18 |
if not file_paths:
|
| 19 |
return None, None
|
| 20 |
|
| 21 |
-
# Create output directories
|
| 22 |
res_dir, mask_dir = "results_out", "masks_out"
|
| 23 |
os.makedirs(res_dir, exist_ok=True)
|
| 24 |
os.makedirs(mask_dir, exist_ok=True)
|
|
@@ -27,17 +24,26 @@ def process_images(file_paths):
|
|
| 27 |
|
| 28 |
for path in file_paths:
|
| 29 |
img = Image.open(path).convert("RGB")
|
| 30 |
-
original_size = img.size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
mask_img = Image.fromarray((mask_np * 255).astype(np.uint8)).resize(original_size)
|
| 35 |
|
| 36 |
-
# Transparent Result
|
| 37 |
result_img = img.copy()
|
| 38 |
result_img.putalpha(mask_img)
|
| 39 |
|
| 40 |
-
# Save files
|
| 41 |
base_name = os.path.splitext(os.path.basename(path))[0]
|
| 42 |
res_path = os.path.join(res_dir, f"{base_name}_no_bg.png")
|
| 43 |
mask_path = os.path.join(mask_dir, f"{base_name}_mask.png")
|
|
@@ -49,7 +55,7 @@ def process_images(file_paths):
|
|
| 49 |
mask_list.append(mask_path)
|
| 50 |
|
| 51 |
# Create ZIP files
|
| 52 |
-
res_zip, mask_zip = "
|
| 53 |
with zipfile.ZipFile(res_zip, 'w') as z:
|
| 54 |
for f in res_list: z.write(f, os.path.basename(f))
|
| 55 |
with zipfile.ZipFile(mask_zip, 'w') as z:
|
|
@@ -57,17 +63,17 @@ def process_images(file_paths):
|
|
| 57 |
|
| 58 |
return res_zip, mask_zip
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
with gr.Blocks(title="
|
| 62 |
-
gr.Markdown("#
|
| 63 |
-
gr.Markdown("Upload multiple images.
|
| 64 |
|
| 65 |
-
input_files = gr.File(label="
|
| 66 |
-
btn = gr.Button("
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
-
out_res = gr.File(label="Download Transparent ZIP")
|
| 70 |
-
out_mask = gr.File(label="Download Masks ZIP")
|
| 71 |
|
| 72 |
btn.click(process_images, inputs=input_files, outputs=[out_res, out_mask])
|
| 73 |
|
|
|
|
| 4 |
import zipfile
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
+
# Load the model directly from the source repository using torch.hub
|
| 9 |
+
# This bypasses the need for local .py files
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
model = torch.hub.load('SkyTNT/anime-segmentation', 'isnetis', pretrained=True, trust_repo=True)
|
|
|
|
|
|
|
| 12 |
model.to(device)
|
| 13 |
model.eval()
|
| 14 |
|
|
|
|
| 16 |
if not file_paths:
|
| 17 |
return None, None
|
| 18 |
|
|
|
|
| 19 |
res_dir, mask_dir = "results_out", "masks_out"
|
| 20 |
os.makedirs(res_dir, exist_ok=True)
|
| 21 |
os.makedirs(mask_dir, exist_ok=True)
|
|
|
|
| 24 |
|
| 25 |
for path in file_paths:
|
| 26 |
img = Image.open(path).convert("RGB")
|
| 27 |
+
original_size = img.size # Store original dimensions
|
| 28 |
+
|
| 29 |
+
# Prepare image for model (the model expects 1024x1024 internally)
|
| 30 |
+
input_img = img.resize((1024, 1024))
|
| 31 |
+
img_np = np.array(input_img).astype(np.float32) / 255.0
|
| 32 |
+
img_tensor = torch.from_numpy(img_np).permute(2, 0, 1).unsqueeze(0).to(device)
|
| 33 |
+
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
# Model returns a list of tensors; the first one is the main mask
|
| 36 |
+
outputs = model(img_tensor)
|
| 37 |
+
mask = outputs[0][0][0].cpu().numpy()
|
| 38 |
|
| 39 |
+
# Resize mask back to original resolution to avoid quality loss
|
| 40 |
+
mask_img = Image.fromarray((mask * 255).astype(np.uint8)).resize(original_size, resample=Image.BILINEAR)
|
|
|
|
| 41 |
|
| 42 |
+
# Create Transparent Result
|
| 43 |
result_img = img.copy()
|
| 44 |
result_img.putalpha(mask_img)
|
| 45 |
|
| 46 |
+
# Save files for zipping
|
| 47 |
base_name = os.path.splitext(os.path.basename(path))[0]
|
| 48 |
res_path = os.path.join(res_dir, f"{base_name}_no_bg.png")
|
| 49 |
mask_path = os.path.join(mask_dir, f"{base_name}_mask.png")
|
|
|
|
| 55 |
mask_list.append(mask_path)
|
| 56 |
|
| 57 |
# Create ZIP files
|
| 58 |
+
res_zip, mask_zip = "transparent_results.zip", "grayscale_masks.zip"
|
| 59 |
with zipfile.ZipFile(res_zip, 'w') as z:
|
| 60 |
for f in res_list: z.write(f, os.path.basename(f))
|
| 61 |
with zipfile.ZipFile(mask_zip, 'w') as z:
|
|
|
|
| 63 |
|
| 64 |
return res_zip, mask_zip
|
| 65 |
|
| 66 |
+
# Build the Interface
|
| 67 |
+
with gr.Blocks(title="Anime Background Remover (Bulk)") as demo:
|
| 68 |
+
gr.Markdown("## 🏮 Bulk Anime Background Remover")
|
| 69 |
+
gr.Markdown("Upload multiple images. This tool processes them at their **original resolution** and provides separate ZIP downloads.")
|
| 70 |
|
| 71 |
+
input_files = gr.File(label="Select Images", file_count="multiple", file_types=["image"])
|
| 72 |
+
btn = gr.Button("🚀 Start Processing", variant="primary")
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
+
out_res = gr.File(label="1. Download Transparent Images (ZIP)")
|
| 76 |
+
out_mask = gr.File(label="2. Download Grayscale Masks (ZIP)")
|
| 77 |
|
| 78 |
btn.click(process_images, inputs=input_files, outputs=[out_res, out_mask])
|
| 79 |
|