Spaces:
Running
Running
Initial commit app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageFilter
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
import torch
|
6 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
7 |
+
|
8 |
+
# Load model and feature extractor
|
9 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
10 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
11 |
+
|
12 |
+
# Gaussian Blur function
|
13 |
+
def apply_gaussian_blur(image, blur_radius):
|
14 |
+
return image.filter(ImageFilter.GaussianBlur(blur_radius))
|
15 |
+
|
16 |
+
# Lens Blur function
|
17 |
+
def apply_lens_blur(image):
|
18 |
+
# Get depth map
|
19 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model(**inputs)
|
22 |
+
depth_map = outputs.predicted_depth.squeeze().cpu().numpy()
|
23 |
+
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min()) * 15
|
24 |
+
depth_map_resized = cv2.resize(depth_map, (image.width, image.height))
|
25 |
+
depth_map_resized = 15 - depth_map_resized
|
26 |
+
|
27 |
+
# Convert to OpenCV format
|
28 |
+
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
29 |
+
blurred_image = np.zeros_like(image_cv, dtype=np.float32)
|
30 |
+
|
31 |
+
for blur_radius in range(1, 16):
|
32 |
+
blurred_layer = cv2.GaussianBlur(image_cv, (0, 0), sigmaX=blur_radius)
|
33 |
+
mask = ((depth_map_resized >= (blur_radius - 1)) & (depth_map_resized < blur_radius)).astype(np.float32)
|
34 |
+
mask = cv2.merge([mask] * 3)
|
35 |
+
blurred_image += blurred_layer * mask
|
36 |
+
|
37 |
+
blurred_image = np.clip(blurred_image, 0, 255).astype(np.uint8)
|
38 |
+
return Image.fromarray(cv2.cvtColor(blurred_image, cv2.COLOR_BGR2RGB))
|
39 |
+
|
40 |
+
# Gradio app interface
|
41 |
+
def process_image(image, effect, blur_radius):
|
42 |
+
if effect == "Gaussian Blur":
|
43 |
+
return apply_gaussian_blur(image, blur_radius)
|
44 |
+
elif effect == "Lens Blur":
|
45 |
+
return apply_lens_blur(image)
|
46 |
+
else:
|
47 |
+
return image
|
48 |
+
|
49 |
+
# Gradio Interface
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
gr.Markdown("# Gaussian and Lens Blur Effects")
|
52 |
+
with gr.Row():
|
53 |
+
with gr.Column():
|
54 |
+
uploaded_image = gr.Image(type="pil")
|
55 |
+
effect = gr.Radio(["Gaussian Blur", "Lens Blur"], value="Gaussian Blur", label="Effect")
|
56 |
+
blur_radius = gr.Slider(1, 15, value=5, step=1, label="Blur Radius (for Gaussian Blur)")
|
57 |
+
submit_button = gr.Button("Apply Effect")
|
58 |
+
with gr.Column():
|
59 |
+
output_image = gr.Image(type="pil", label="Processed Image")
|
60 |
+
|
61 |
+
submit_button.click(process_image, inputs=[uploaded_image, effect, blur_radius], outputs=output_image)
|
62 |
+
|
63 |
+
# Launch the app
|
64 |
+
demo.launch()
|