Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def resize_to_512(img: Image.Image) -> Image.Image:
|
| 8 |
+
if img.size != (512, 512):
|
| 9 |
+
return img.resize((512, 512))
|
| 10 |
+
return img
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def gaussian_blur(img: Image.Image, kernel_size: int):
|
| 14 |
+
img = resize_to_512(img)
|
| 15 |
+
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 16 |
+
blurred = cv2.GaussianBlur(img_cv, (kernel_size | 1, kernel_size | 1), 0)
|
| 17 |
+
return cv2.cvtColor(blurred, cv2.COLOR_BGR2RGB)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def lens_blur(img: Image.Image, max_blur_radius: int):
|
| 21 |
+
img = resize_to_512(img)
|
| 22 |
+
original = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 23 |
+
original_rgb = cv2.cvtColor(original, cv2.COLOR_BGR2RGB)
|
| 24 |
+
|
| 25 |
+
# Create synthetic depth map
|
| 26 |
+
depth_norm = np.zeros((original.shape[0], original.shape[1]), dtype=np.float32)
|
| 27 |
+
cv2.circle(depth_norm, (original.shape[1] // 2, original.shape[0] // 2), 100, 1, -1)
|
| 28 |
+
depth_norm = cv2.GaussianBlur(depth_norm, (21, 21), 0)
|
| 29 |
+
|
| 30 |
+
blurred_image = np.zeros_like(original_rgb)
|
| 31 |
+
|
| 32 |
+
for i in range(original.shape[0]):
|
| 33 |
+
for j in range(original.shape[1]):
|
| 34 |
+
blur_radius = int(depth_norm[i, j] * max_blur_radius)
|
| 35 |
+
if blur_radius % 2 == 0:
|
| 36 |
+
blur_radius += 1
|
| 37 |
+
|
| 38 |
+
x_min = max(j - blur_radius, 0)
|
| 39 |
+
x_max = min(j + blur_radius, original.shape[1])
|
| 40 |
+
y_min = max(i - blur_radius, 0)
|
| 41 |
+
y_max = min(i + blur_radius, original.shape[0])
|
| 42 |
+
|
| 43 |
+
roi = original_rgb[y_min:y_max, x_min:x_max]
|
| 44 |
+
|
| 45 |
+
if blur_radius > 1:
|
| 46 |
+
blurred_roi = cv2.GaussianBlur(roi, (blur_radius, blur_radius), 0)
|
| 47 |
+
try:
|
| 48 |
+
blurred_image[i, j] = blurred_roi[
|
| 49 |
+
blur_radius // 2, blur_radius // 2
|
| 50 |
+
]
|
| 51 |
+
except:
|
| 52 |
+
blurred_image[i, j] = original_rgb[i, j]
|
| 53 |
+
else:
|
| 54 |
+
blurred_image[i, j] = original_rgb[i, j]
|
| 55 |
+
|
| 56 |
+
return blurred_image
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("## Gaussian and Lens Blur App")
|
| 61 |
+
with gr.Row():
|
| 62 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
kernel_slider = gr.Slider(1, 49, value=11, step=2, label="Gaussian Kernel Size")
|
| 65 |
+
max_blur_slider = gr.Slider(
|
| 66 |
+
1, 50, value=15, step=1, label="Max Lens Blur Radius"
|
| 67 |
+
)
|
| 68 |
+
with gr.Row():
|
| 69 |
+
gaussian_output = gr.Image(label="Gaussian Blurred Image")
|
| 70 |
+
lens_output = gr.Image(label="Depth-Based Lens Blurred Image")
|
| 71 |
+
with gr.Row():
|
| 72 |
+
blur_btn = gr.Button("Apply Blur")
|
| 73 |
+
|
| 74 |
+
blur_btn.click(
|
| 75 |
+
fn=gaussian_blur, inputs=[image_input, kernel_slider], outputs=gaussian_output
|
| 76 |
+
)
|
| 77 |
+
blur_btn.click(
|
| 78 |
+
fn=lens_blur, inputs=[image_input, max_blur_slider], outputs=lens_output
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
demo.launch()
|