Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,13 +24,77 @@ print(f"Model loaded on {device}")
|
|
| 24 |
OUTPUT_DIR = "output_images"
|
| 25 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 26 |
|
| 27 |
-
# ... keep all imports and model loading code the same ...
|
| 28 |
-
|
| 29 |
# Define different sizes for different devices
|
| 30 |
DESKTOP_SIZE = (512, 512)
|
| 31 |
-
MOBILE_SIZE = (300, 300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
description = """
|
| 35 |
<style>
|
| 36 |
.custom-container {
|
|
@@ -81,7 +145,7 @@ description = """
|
|
| 81 |
</div>
|
| 82 |
"""
|
| 83 |
|
| 84 |
-
#
|
| 85 |
with gr.Blocks(css="""
|
| 86 |
/* Import fonts */
|
| 87 |
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@300;400;700&display=swap');
|
|
@@ -184,6 +248,12 @@ with gr.Blocks(css="""
|
|
| 184 |
font-size: 14px !important;
|
| 185 |
}
|
| 186 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
""") as demo:
|
| 188 |
gr.Markdown(description)
|
| 189 |
|
|
|
|
| 24 |
OUTPUT_DIR = "output_images"
|
| 25 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 26 |
|
|
|
|
|
|
|
| 27 |
# Define different sizes for different devices
|
| 28 |
DESKTOP_SIZE = (512, 512)
|
| 29 |
+
MOBILE_SIZE = (300, 300)
|
| 30 |
+
|
| 31 |
+
# Resize the input image for model compatibility
|
| 32 |
+
def resize_image(image, size=DESKTOP_SIZE):
|
| 33 |
+
image = image.convert('RGB')
|
| 34 |
+
image = image.resize(size, Image.LANCZOS)
|
| 35 |
+
return image
|
| 36 |
|
| 37 |
+
# Background removal process
|
| 38 |
+
def process(image, progress=gr.Progress()):
|
| 39 |
+
if image is None:
|
| 40 |
+
return None, gr.update(visible=False)
|
| 41 |
+
|
| 42 |
+
progress(0, desc="Starting processing...")
|
| 43 |
+
|
| 44 |
+
# Prepare the input
|
| 45 |
+
progress(0.1, desc="Preparing image...")
|
| 46 |
+
orig_image = Image.fromarray(image)
|
| 47 |
+
# Resize input image to fixed size
|
| 48 |
+
orig_image = resize_image(orig_image, DESKTOP_SIZE)
|
| 49 |
+
w, h = DESKTOP_SIZE
|
| 50 |
+
image = orig_image
|
| 51 |
+
im_np = np.array(image)
|
| 52 |
+
im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
|
| 53 |
+
im_tensor = torch.unsqueeze(im_tensor, 0)
|
| 54 |
+
im_tensor = torch.divide(im_tensor, 255.0)
|
| 55 |
+
im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
|
| 56 |
+
|
| 57 |
+
progress(0.3, desc="Processing with AI model...")
|
| 58 |
+
if torch.cuda.is_available():
|
| 59 |
+
im_tensor = im_tensor.cuda()
|
| 60 |
+
|
| 61 |
+
# Inference with the model
|
| 62 |
+
with torch.no_grad():
|
| 63 |
+
result = net(im_tensor)
|
| 64 |
+
|
| 65 |
+
progress(0.6, desc="Post-processing...")
|
| 66 |
+
# Post-process the result
|
| 67 |
+
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
|
| 68 |
+
ma = torch.max(result)
|
| 69 |
+
mi = torch.min(result)
|
| 70 |
+
result = (result - mi) / (ma - mi)
|
| 71 |
+
|
| 72 |
+
# Convert the result to an image
|
| 73 |
+
result_array = (result * 255).cpu().data.numpy().astype(np.uint8)
|
| 74 |
+
pil_mask = Image.fromarray(np.squeeze(result_array))
|
| 75 |
+
|
| 76 |
+
# Add the mask as alpha channel to the original image
|
| 77 |
+
new_im = orig_image.copy()
|
| 78 |
+
new_im.putalpha(pil_mask)
|
| 79 |
+
|
| 80 |
+
progress(0.8, desc="Preparing download...")
|
| 81 |
+
# Generate a unique filename
|
| 82 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 83 |
+
filename = f"background_removed_{unique_id}.png"
|
| 84 |
+
filepath = os.path.join(OUTPUT_DIR, filename)
|
| 85 |
+
|
| 86 |
+
# Save the processed image
|
| 87 |
+
new_im.save(filepath, format='PNG')
|
| 88 |
+
|
| 89 |
+
# Convert to numpy array for display
|
| 90 |
+
output_array = np.array(new_im.convert("RGBA"))
|
| 91 |
+
|
| 92 |
+
progress(1.0, desc="Done!")
|
| 93 |
+
|
| 94 |
+
return output_array, gr.update(visible=True, value=filepath, interactive=True)
|
| 95 |
+
|
| 96 |
+
# Gradio interface setup
|
| 97 |
+
title = "Background Removal Tool"
|
| 98 |
description = """
|
| 99 |
<style>
|
| 100 |
.custom-container {
|
|
|
|
| 145 |
</div>
|
| 146 |
"""
|
| 147 |
|
| 148 |
+
# Create the Gradio interface
|
| 149 |
with gr.Blocks(css="""
|
| 150 |
/* Import fonts */
|
| 151 |
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@300;400;700&display=swap');
|
|
|
|
| 248 |
font-size: 14px !important;
|
| 249 |
}
|
| 250 |
}
|
| 251 |
+
|
| 252 |
+
/* Additional Animations */
|
| 253 |
+
@keyframes button-glow {
|
| 254 |
+
0% { box-shadow: 0 0 5px rgba(0, 255, 255, 0.5); }
|
| 255 |
+
100% { box-shadow: 0 0 15px rgba(0, 255, 255, 0.8), 0 0 25px rgba(255, 0, 222, 0.5); }
|
| 256 |
+
}
|
| 257 |
""") as demo:
|
| 258 |
gr.Markdown(description)
|
| 259 |
|