Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Initialize the pipeline for image segmentation
|
6 |
+
pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
|
7 |
+
|
8 |
+
|
9 |
+
def process_image(image_path, include_background, background_color):
|
10 |
+
# Apply the mask on the input image
|
11 |
+
pillow_image = pipe(image_path)
|
12 |
+
|
13 |
+
if include_background:
|
14 |
+
# Create a background of the specified color
|
15 |
+
background = Image.new("RGB", pillow_image.size, background_color)
|
16 |
+
# Combine the background and the image
|
17 |
+
background.paste(pillow_image, (0, 0), pillow_image)
|
18 |
+
return background
|
19 |
+
else:
|
20 |
+
return pillow_image
|
21 |
+
|
22 |
+
|
23 |
+
# Define Gradio interface
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
gr.Markdown("# Background Removal with Optional Custom Background")
|
26 |
+
with gr.Row():
|
27 |
+
with gr.Column():
|
28 |
+
image_input = gr.Image(type="filepath", label="Upload Image")
|
29 |
+
include_background = gr.Checkbox(label="Include Background", value=False)
|
30 |
+
background_color = gr.ColorPicker(label="Background Color", value="#FFFFFF")
|
31 |
+
with gr.Column():
|
32 |
+
output_image = gr.Image(label="Processed Image")
|
33 |
+
|
34 |
+
submit_button = gr.Button("Process Image")
|
35 |
+
submit_button.click(process_image, inputs=[image_input, include_background, background_color], outputs=output_image)
|
36 |
+
|
37 |
+
# Launch the Gradio app
|
38 |
+
demo.launch()
|