Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from transformers import (
|
|
8 |
from PIL import Image, ImageFilter
|
9 |
import numpy as np
|
10 |
import gradio as gr
|
|
|
11 |
|
12 |
# Suppress specific warnings
|
13 |
import warnings
|
@@ -51,7 +52,7 @@ def process_image(image):
|
|
51 |
normalized_depth = (predicted_depth - min_depth) / (max_depth - min_depth)
|
52 |
depth_map_image = Image.fromarray((normalized_depth * 255).astype(np.uint8))
|
53 |
|
54 |
-
# ------------------ Blurred Background Effect ------------------
|
55 |
# Invert the depth map
|
56 |
inverted_depth = 1 - normalized_depth
|
57 |
inverted_depth = (inverted_depth - inverted_depth.min()) / (inverted_depth.max() - inverted_depth.min())
|
@@ -62,17 +63,39 @@ def process_image(image):
|
|
62 |
depth_weight_resized = np.expand_dims(depth_weight_resized, axis=-1)
|
63 |
|
64 |
# Apply Gaussian blur to the entire image
|
65 |
-
|
|
|
66 |
|
67 |
-
#
|
68 |
original_np = np.array(image).astype(np.float32)
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# Blend images based on the depth weight
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
return image, binary_mask_image, depth_map_image,
|
76 |
|
77 |
# Define Gradio interface using the updated API
|
78 |
interface = gr.Interface(
|
@@ -80,12 +103,13 @@ interface = gr.Interface(
|
|
80 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
81 |
outputs=[
|
82 |
gr.Image(type="pil", label="Original Image"),
|
83 |
-
gr.Image(type="pil", label="Segmentation Mask"),
|
84 |
gr.Image(type="pil", label="Depth Map"),
|
85 |
-
gr.Image(type="pil", label="Blurred Background
|
|
|
86 |
],
|
87 |
-
title="Semantic Segmentation and
|
88 |
-
description="Upload an image to generate a segmentation mask, depth map, and
|
89 |
examples=[
|
90 |
["Selfie_1.jpg"],
|
91 |
["Selfie_2.jpg"]
|
|
|
8 |
from PIL import Image, ImageFilter
|
9 |
import numpy as np
|
10 |
import gradio as gr
|
11 |
+
import cv2
|
12 |
|
13 |
# Suppress specific warnings
|
14 |
import warnings
|
|
|
52 |
normalized_depth = (predicted_depth - min_depth) / (max_depth - min_depth)
|
53 |
depth_map_image = Image.fromarray((normalized_depth * 255).astype(np.uint8))
|
54 |
|
55 |
+
# ------------------ Gaussian Blurred Background Effect ------------------
|
56 |
# Invert the depth map
|
57 |
inverted_depth = 1 - normalized_depth
|
58 |
inverted_depth = (inverted_depth - inverted_depth.min()) / (inverted_depth.max() - inverted_depth.min())
|
|
|
63 |
depth_weight_resized = np.expand_dims(depth_weight_resized, axis=-1)
|
64 |
|
65 |
# Apply Gaussian blur to the entire image
|
66 |
+
gaussian_blurred_image = image.filter(ImageFilter.GaussianBlur(radius=15))
|
67 |
+
gaussian_blurred_np = np.array(gaussian_blurred_image).astype(np.float32)
|
68 |
|
69 |
+
# Blend images based on the depth weight
|
70 |
original_np = np.array(image).astype(np.float32)
|
71 |
+
composite_gaussian_np = (1 - depth_weight_resized) * original_np + depth_weight_resized * gaussian_blurred_np
|
72 |
+
composite_gaussian_image = Image.fromarray(np.clip(composite_gaussian_np, 0, 255).astype(np.uint8))
|
73 |
+
|
74 |
+
# ------------------ Lens Blurred Background Effect ------------------
|
75 |
+
# Convert PIL image to OpenCV format
|
76 |
+
original_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
77 |
+
|
78 |
+
# Apply Lens Blur using OpenCV's blur with a larger kernel
|
79 |
+
# Note: OpenCV does not have a direct lens blur function, but we can approximate it
|
80 |
+
# by using a larger kernel size. For a more realistic lens blur, additional processing is required.
|
81 |
+
lens_blur_kernel_size = 21 # Adjust kernel size for stronger blur
|
82 |
+
lens_blurred_cv = cv2.GaussianBlur(original_cv, (lens_blur_kernel_size, lens_blur_kernel_size), 0)
|
83 |
+
|
84 |
+
# Convert back to PIL Image
|
85 |
+
lens_blurred_image = Image.fromarray(cv2.cvtColor(lens_blurred_cv, cv2.COLOR_BGR2RGB))
|
86 |
+
lens_blurred_np = np.array(lens_blurred_image).astype(np.float32)
|
87 |
|
88 |
# Blend images based on the depth weight
|
89 |
+
composite_lens_np = (1 - depth_weight_resized) * original_np + depth_weight_resized * lens_blurred_np
|
90 |
+
composite_lens_image = Image.fromarray(np.clip(composite_lens_np, 0, 255).astype(np.uint8))
|
91 |
+
|
92 |
+
# Return results
|
93 |
+
binary_mask_image = binary_mask_image.convert("L") # Ensure it's in grayscale
|
94 |
+
depth_map_image = depth_map_image.convert("L") # Ensure it's in grayscale
|
95 |
+
gaussian_blurred_image = composite_gaussian_image
|
96 |
+
lens_blurred_image = composite_lens_image
|
97 |
|
98 |
+
return image, binary_mask_image, depth_map_image, gaussian_blurred_image, lens_blurred_image
|
99 |
|
100 |
# Define Gradio interface using the updated API
|
101 |
interface = gr.Interface(
|
|
|
103 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
104 |
outputs=[
|
105 |
gr.Image(type="pil", label="Original Image"),
|
106 |
+
gr.Image(type="pil", label="Segmentation Mask (B/W)"),
|
107 |
gr.Image(type="pil", label="Depth Map"),
|
108 |
+
gr.Image(type="pil", label="Gaussian Blurred Background"),
|
109 |
+
gr.Image(type="pil", label="Lens Blurred Background"),
|
110 |
],
|
111 |
+
title="Semantic Segmentation and Dual Blur Effects",
|
112 |
+
description="Upload an image to generate a segmentation mask, depth map, Gaussian blurred background, and lens blurred background effect.",
|
113 |
examples=[
|
114 |
["Selfie_1.jpg"],
|
115 |
["Selfie_2.jpg"]
|