update
Browse files- README.md +4 -5
- app.py +87 -0
- packages.txt +1 -0
- requirements.txt +7 -0
README.md
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.6.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
|
11 |
-
short_description: Background Blur using Gaussian Blur and Lens Blur
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Test
|
3 |
+
emoji: π
|
4 |
+
colorFrom: yellow
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.6.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
short_description: gaussian blurs
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
from PIL import Image
|
6 |
+
from transformers import (
|
7 |
+
SegformerImageProcessor,
|
8 |
+
SegformerForSemanticSegmentation,
|
9 |
+
AutoImageProcessor,
|
10 |
+
AutoModelForDepthEstimation
|
11 |
+
)
|
12 |
+
|
13 |
+
# Load Segformer model for Gaussian blur
|
14 |
+
segformer_processor = SegformerImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
15 |
+
segformer_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
16 |
+
|
17 |
+
# Load Depth-Anything model for lens blur
|
18 |
+
depth_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
19 |
+
depth_model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
20 |
+
|
21 |
+
def apply_blur(image, blur_type, blur_strength, depth_threshold):
|
22 |
+
# Convert image to RGB
|
23 |
+
img = image
|
24 |
+
|
25 |
+
if blur_type == "Gaussian":
|
26 |
+
# Use Segformer for Gaussian blur
|
27 |
+
pil_image = Image.fromarray(img)
|
28 |
+
inputs = segformer_processor(images=pil_image, return_tensors="pt")
|
29 |
+
outputs = segformer_model(**inputs)
|
30 |
+
logits = outputs.logits
|
31 |
+
|
32 |
+
mask = logits[0, 12, :, :].detach().cpu().numpy() > depth_threshold
|
33 |
+
mask = cv2.resize(mask.astype(np.uint8), (img.shape[1], img.shape[0]))
|
34 |
+
|
35 |
+
elif blur_type == "Lens":
|
36 |
+
# Use Depth-Anything for lens blur
|
37 |
+
pil_image = Image.fromarray(img)
|
38 |
+
inputs = depth_processor(images=pil_image, return_tensors="pt")
|
39 |
+
|
40 |
+
with torch.no_grad():
|
41 |
+
outputs = depth_model(**inputs)
|
42 |
+
predicted_depth = outputs.predicted_depth
|
43 |
+
|
44 |
+
prediction = torch.nn.functional.interpolate(
|
45 |
+
predicted_depth.unsqueeze(1),
|
46 |
+
size=img.shape[:2],
|
47 |
+
mode="bicubic",
|
48 |
+
align_corners=False,
|
49 |
+
)
|
50 |
+
|
51 |
+
mask = prediction[0, 0, :, :].detach().cpu().numpy() > depth_threshold
|
52 |
+
mask = mask.astype(np.uint8)
|
53 |
+
|
54 |
+
# Invert mask using cv2
|
55 |
+
mask = cv2.bitwise_not(mask)
|
56 |
+
mask = np.repeat(mask[:, :, np.newaxis], 3, axis=2)
|
57 |
+
|
58 |
+
# Apply blur based on selected type
|
59 |
+
if blur_type == "Gaussian":
|
60 |
+
blurred_image = cv2.GaussianBlur(img, (0, 0), sigmaX=blur_strength)
|
61 |
+
elif blur_type == "Lens":
|
62 |
+
# Simulate lens blur using a larger kernel
|
63 |
+
kernel_size = int(blur_strength * 2) * 2 + 1
|
64 |
+
blurred_image = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
|
65 |
+
|
66 |
+
# Combine blurred and original images using the mask
|
67 |
+
output = np.where(mask == 255, blurred_image, img)
|
68 |
+
|
69 |
+
return output
|
70 |
+
|
71 |
+
|
72 |
+
# Define Gradio interface
|
73 |
+
iface = gr.Interface(
|
74 |
+
fn=apply_blur,
|
75 |
+
inputs=[
|
76 |
+
gr.Image(label="Input Image"),
|
77 |
+
gr.Radio(["Gaussian", "Lens"], label="Blur Type", value="Gaussian"),
|
78 |
+
gr.Slider(1, 30, value=15, step=1, label="Blur Strength"),
|
79 |
+
gr.Slider(-20, 20, value=-4, step=0.1, label="Depth Threshold")
|
80 |
+
],
|
81 |
+
outputs=gr.Image(label="Output Image"),
|
82 |
+
title="Image Segmentation and Blurring",
|
83 |
+
description="Upload an image and apply Gaussian or Lens blur to the background using different segmentation models."
|
84 |
+
)
|
85 |
+
|
86 |
+
# Launch the app
|
87 |
+
iface.launch(share=True)
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python3-opencv
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python
|
2 |
+
jinja2
|
3 |
+
gradio
|
4 |
+
numpy
|
5 |
+
torch
|
6 |
+
Pillow
|
7 |
+
transformers
|