Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image, ImageFilter
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load the segmentation pipeline
|
7 |
+
pipe = pipeline("image-segmentation", model="mattmdjaga/segformer_b2_clothes")
|
8 |
+
|
9 |
+
# Simplified refine_mask function
|
10 |
+
def refine_mask(mask):
|
11 |
+
"""Simplify and smooth the segmentation mask."""
|
12 |
+
mask_array = np.array(mask)
|
13 |
+
mask_array = (mask_array > 128).astype(np.uint8) * 255 # Threshold to binary mask
|
14 |
+
refined_mask = Image.fromarray(mask_array).filter(ImageFilter.GaussianBlur(0.5)) # Smooth edges
|
15 |
+
return refined_mask
|
16 |
+
|
17 |
+
# Function to blur the background
|
18 |
+
def blur_background(image, blur_radius):
|
19 |
+
# Perform segmentation
|
20 |
+
result = pipe(image)
|
21 |
+
|
22 |
+
# Extract the background mask
|
23 |
+
background_mask = None
|
24 |
+
for entry in result:
|
25 |
+
if entry["label"] == "Background":
|
26 |
+
background_mask = refine_mask(entry["mask"]) # Refine the background mask
|
27 |
+
break
|
28 |
+
|
29 |
+
if background_mask is None:
|
30 |
+
return image # If no background is detected, return the original image
|
31 |
+
|
32 |
+
# Convert the image and mask to NumPy arrays
|
33 |
+
image_np = np.array(image)
|
34 |
+
background_mask_np = np.array(background_mask)
|
35 |
+
|
36 |
+
# Create a blurred version of the entire image
|
37 |
+
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
38 |
+
blurred_np = np.array(blurred_image)
|
39 |
+
|
40 |
+
# Combine the original image and the blurred background
|
41 |
+
final_image = np.where(background_mask_np[..., None] == 255, blurred_np, image_np).astype(np.uint8)
|
42 |
+
|
43 |
+
# Convert back to PIL image
|
44 |
+
return Image.fromarray(final_image)
|
45 |
+
|
46 |
+
# Example inputs for Gradio
|
47 |
+
examples = [["1.jpg"", 10] ,["2.jpg"", 10] ,["3.jpg"", 10] ] # Example: Image with a blur intensity of 10
|
48 |
+
|
49 |
+
# Gradio interface
|
50 |
+
interface = gr.Interface(
|
51 |
+
fn=blur_background,
|
52 |
+
inputs=[
|
53 |
+
gr.Image(type="pil"), # Input image
|
54 |
+
gr.Slider(1, 50, step=1, label="Blur Intensity") # Slider for blur radius
|
55 |
+
],
|
56 |
+
outputs=gr.Image(type="pil"), # Output image with blurred background
|
57 |
+
examples=examples, # Provide examples as a nested list
|
58 |
+
title="Blur Background with Refined Mask",
|
59 |
+
description="Upload an image and adjust the slider to control the background blur level. The background edges are smoothed for better blending."
|
60 |
+
)
|
61 |
+
|
62 |
+
# Launch the app
|
63 |
+
interface.launch()
|