Spaces:
Sleeping
Sleeping
creating app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import pipeline
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
def process_image(image, effect_type="Gaussian Blur", blur_intensity=15):
|
9 |
+
"""
|
10 |
+
Process the image with selected effect
|
11 |
+
"""
|
12 |
+
# Resize image to 512x512
|
13 |
+
image = Image.fromarray(image).resize((512, 512))
|
14 |
+
|
15 |
+
if effect_type == "Gaussian Blur":
|
16 |
+
# Generate segmentation mask
|
17 |
+
segmenter = pipeline("image-segmentation",
|
18 |
+
model="openmmlab/upernet-swin-base",
|
19 |
+
device=0 if torch.cuda.is_available() else -1)
|
20 |
+
|
21 |
+
results = segmenter(image)
|
22 |
+
mask = np.zeros((512, 512), dtype=np.uint8)
|
23 |
+
|
24 |
+
for segment in results:
|
25 |
+
if segment['label'].lower() == 'person':
|
26 |
+
segment_mask = np.array(segment['mask'])
|
27 |
+
mask[segment_mask > 0] = 255
|
28 |
+
|
29 |
+
# Apply gaussian blur
|
30 |
+
img_np = np.array(image)
|
31 |
+
blurred = cv2.GaussianBlur(img_np, (0, 0), blur_intensity)
|
32 |
+
|
33 |
+
mask_np = mask / 255.0
|
34 |
+
mask_np = np.stack([mask_np] * 3, axis=-1)
|
35 |
+
|
36 |
+
result = img_np * mask_np + blurred * (1 - mask_np)
|
37 |
+
return result.astype(np.uint8)
|
38 |
+
|
39 |
+
else: # Depth-based blur
|
40 |
+
# Generate depth map
|
41 |
+
depth_estimator = pipeline("depth-estimation",
|
42 |
+
model="Intel/dpt-large",
|
43 |
+
device=0 if torch.cuda.is_available() else -1)
|
44 |
+
|
45 |
+
depth_result = depth_estimator(image)
|
46 |
+
depth_map = depth_result['predicted_depth']
|
47 |
+
if torch.is_tensor(depth_map):
|
48 |
+
depth_map = depth_map.cpu().numpy()
|
49 |
+
|
50 |
+
# Apply depth-based blur
|
51 |
+
img_np = np.array(image)
|
52 |
+
depth_norm = blur_intensity * (1 - (depth_map - depth_map.min()) /
|
53 |
+
(depth_map.max() - depth_map.min()))
|
54 |
+
|
55 |
+
result = np.zeros_like(img_np)
|
56 |
+
for sigma in range(int(blur_intensity) + 1):
|
57 |
+
if sigma == 0:
|
58 |
+
continue
|
59 |
+
|
60 |
+
kernel_size = 2 * int(4 * sigma + 0.5) + 1
|
61 |
+
mask = (depth_norm >= sigma - 0.5) & (depth_norm < sigma + 0.5)
|
62 |
+
|
63 |
+
if not mask.any():
|
64 |
+
continue
|
65 |
+
|
66 |
+
blurred = cv2.GaussianBlur(img_np, (kernel_size, kernel_size), sigma)
|
67 |
+
result[mask] = blurred[mask]
|
68 |
+
|
69 |
+
min_depth_mask = depth_norm > blur_intensity-0.5
|
70 |
+
result[min_depth_mask] = img_np[min_depth_mask]
|
71 |
+
|
72 |
+
return result
|
73 |
+
|
74 |
+
# Create Gradio interface
|
75 |
+
demo = gr.Interface(
|
76 |
+
fn=process_image,
|
77 |
+
inputs=[
|
78 |
+
gr.Image(label="Upload Image", type="numpy"),
|
79 |
+
gr.Radio(["Gaussian Blur", "Depth-based Blur"], label="Effect Type", value="Gaussian Blur"),
|
80 |
+
gr.Slider(minimum=1, maximum=30, value=15, label="Blur Intensity")
|
81 |
+
],
|
82 |
+
outputs=gr.Image(label="Result"),
|
83 |
+
title="Image Background Effects",
|
84 |
+
description="""Upload an image to apply background effects:
|
85 |
+
1. Gaussian Blur: Blurs the background while keeping the person sharp
|
86 |
+
2. Depth-based Blur: Applies varying blur based on depth (bokeh effect)""",
|
87 |
+
examples=[], # You can add example images later
|
88 |
+
cache_examples=False
|
89 |
+
)
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
demo.launch()
|