Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,41 @@ import torch
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
|
|
|
| 7 |
|
| 8 |
def remove_background(input_image):
|
| 9 |
try:
|
| 10 |
-
# Initialize the pipeline
|
| 11 |
segmentor = pipeline("image-segmentation",
|
| 12 |
model="briaai/RMBG-1.4",
|
| 13 |
-
device=-1
|
|
|
|
| 14 |
|
| 15 |
# Process the image
|
| 16 |
result = segmentor(input_image)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
raise gr.Error(f"Error processing image: {str(e)}")
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
css = """
|
| 24 |
.gradio-container {
|
| 25 |
font-family: 'Segoe UI', sans-serif;
|
|
@@ -28,16 +47,43 @@ css = """
|
|
| 28 |
.gr-image {
|
| 29 |
border-radius: 12px;
|
| 30 |
border: 2px solid rgba(255, 215, 0, 0.3);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
.gr-button {
|
| 33 |
background: linear-gradient(45deg, #FFD700, #FFA500);
|
| 34 |
border: none;
|
| 35 |
color: black;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
.gr-button:hover {
|
| 38 |
transform: translateY(-2px);
|
| 39 |
box-shadow: 0 4px 12px rgba(255, 215, 0, 0.3);
|
| 40 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
|
| 43 |
with gr.Blocks(css=css) as demo:
|
|
@@ -59,7 +105,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 59 |
input_image = gr.Image(
|
| 60 |
label="Upload Image",
|
| 61 |
type="pil",
|
| 62 |
-
sources=["upload", "clipboard"]
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
with gr.Column():
|
|
@@ -67,21 +114,53 @@ with gr.Blocks(css=css) as demo:
|
|
| 67 |
label="Result",
|
| 68 |
type="pil"
|
| 69 |
)
|
|
|
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
clear_btn = gr.Button("Clear")
|
| 73 |
process_btn = gr.Button("Remove Background", variant="primary")
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
process_btn.click(
|
| 77 |
-
fn=
|
| 78 |
inputs=[input_image],
|
| 79 |
-
outputs=[output_image]
|
| 80 |
)
|
| 81 |
|
| 82 |
clear_btn.click(
|
| 83 |
-
|
| 84 |
-
outputs=[input_image, output_image]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
-
demo.launch()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
def remove_background(input_image):
|
| 10 |
try:
|
| 11 |
+
# Initialize the pipeline with trust_remote_code=True
|
| 12 |
segmentor = pipeline("image-segmentation",
|
| 13 |
model="briaai/RMBG-1.4",
|
| 14 |
+
device=-1,
|
| 15 |
+
trust_remote_code=True)
|
| 16 |
|
| 17 |
# Process the image
|
| 18 |
result = segmentor(input_image)
|
| 19 |
|
| 20 |
+
# Convert the output image to PNG format
|
| 21 |
+
if isinstance(result['output_image'], Image.Image):
|
| 22 |
+
output = result['output_image']
|
| 23 |
+
else:
|
| 24 |
+
output = Image.fromarray(result['output_image'])
|
| 25 |
+
|
| 26 |
+
return output, output
|
| 27 |
+
|
| 28 |
except Exception as e:
|
| 29 |
raise gr.Error(f"Error processing image: {str(e)}")
|
| 30 |
|
| 31 |
+
def save_image(image):
|
| 32 |
+
if image is None:
|
| 33 |
+
return None
|
| 34 |
+
try:
|
| 35 |
+
# Save image to bytes
|
| 36 |
+
buffer = io.BytesIO()
|
| 37 |
+
image.save(buffer, format="PNG")
|
| 38 |
+
return buffer.getvalue()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
raise gr.Error(f"Error saving image: {str(e)}")
|
| 41 |
+
|
| 42 |
css = """
|
| 43 |
.gradio-container {
|
| 44 |
font-family: 'Segoe UI', sans-serif;
|
|
|
|
| 47 |
.gr-image {
|
| 48 |
border-radius: 12px;
|
| 49 |
border: 2px solid rgba(255, 215, 0, 0.3);
|
| 50 |
+
transition: transform 0.3s ease;
|
| 51 |
+
}
|
| 52 |
+
.gr-image:hover {
|
| 53 |
+
transform: scale(1.02);
|
| 54 |
}
|
| 55 |
.gr-button {
|
| 56 |
background: linear-gradient(45deg, #FFD700, #FFA500);
|
| 57 |
border: none;
|
| 58 |
color: black;
|
| 59 |
+
padding: 10px 20px;
|
| 60 |
+
border-radius: 8px;
|
| 61 |
+
font-weight: bold;
|
| 62 |
+
transition: all 0.3s ease;
|
| 63 |
}
|
| 64 |
.gr-button:hover {
|
| 65 |
transform: translateY(-2px);
|
| 66 |
box-shadow: 0 4px 12px rgba(255, 215, 0, 0.3);
|
| 67 |
}
|
| 68 |
+
.container {
|
| 69 |
+
max-width: 1200px;
|
| 70 |
+
margin: 0 auto;
|
| 71 |
+
padding: 2rem;
|
| 72 |
+
}
|
| 73 |
+
.status-msg {
|
| 74 |
+
text-align: center;
|
| 75 |
+
margin: 1rem 0;
|
| 76 |
+
padding: 0.5rem;
|
| 77 |
+
border-radius: 8px;
|
| 78 |
+
}
|
| 79 |
+
.success {
|
| 80 |
+
background: rgba(0, 255, 0, 0.1);
|
| 81 |
+
color: #00ff00;
|
| 82 |
+
}
|
| 83 |
+
.error {
|
| 84 |
+
background: rgba(255, 0, 0, 0.1);
|
| 85 |
+
color: #ff0000;
|
| 86 |
+
}
|
| 87 |
"""
|
| 88 |
|
| 89 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 105 |
input_image = gr.Image(
|
| 106 |
label="Upload Image",
|
| 107 |
type="pil",
|
| 108 |
+
sources=["upload", "clipboard"],
|
| 109 |
+
tool="select"
|
| 110 |
)
|
| 111 |
|
| 112 |
with gr.Column():
|
|
|
|
| 114 |
label="Result",
|
| 115 |
type="pil"
|
| 116 |
)
|
| 117 |
+
download_btn = gr.Button("Download Result", visible=False)
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
clear_btn = gr.Button("Clear")
|
| 121 |
process_btn = gr.Button("Remove Background", variant="primary")
|
| 122 |
+
|
| 123 |
+
status = gr.Textbox(label="Status", visible=False)
|
| 124 |
|
| 125 |
# Event handlers
|
| 126 |
+
def process_and_show(image):
|
| 127 |
+
if image is None:
|
| 128 |
+
raise gr.Error("Please upload an image first")
|
| 129 |
+
try:
|
| 130 |
+
output, download = remove_background(image)
|
| 131 |
+
download_btn.visible = True
|
| 132 |
+
status.visible = True
|
| 133 |
+
status.value = "✅ Background removed successfully!"
|
| 134 |
+
return output, download, True, status
|
| 135 |
+
except Exception as e:
|
| 136 |
+
status.visible = True
|
| 137 |
+
status.value = f"❌ Error: {str(e)}"
|
| 138 |
+
return None, None, False, status
|
| 139 |
+
|
| 140 |
+
def clear_all():
|
| 141 |
+
return {
|
| 142 |
+
input_image: None,
|
| 143 |
+
output_image: None,
|
| 144 |
+
download_btn: gr.Button(visible=False),
|
| 145 |
+
status: gr.Textbox(visible=False)
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
process_btn.click(
|
| 149 |
+
fn=process_and_show,
|
| 150 |
inputs=[input_image],
|
| 151 |
+
outputs=[output_image, output_image, download_btn, status]
|
| 152 |
)
|
| 153 |
|
| 154 |
clear_btn.click(
|
| 155 |
+
fn=clear_all,
|
| 156 |
+
outputs=[input_image, output_image, download_btn, status]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Download functionality
|
| 160 |
+
download_btn.click(
|
| 161 |
+
fn=save_image,
|
| 162 |
+
inputs=[output_image],
|
| 163 |
+
outputs=[gr.File(label="Download")]
|
| 164 |
)
|
| 165 |
|
| 166 |
+
demo.queue().launch()
|