Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,8 @@ import numpy as np
|
|
| 8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
|
| 10 |
st.title("Image blur using segmentation and depth estimation.")
|
| 11 |
-
st.
|
| 12 |
-
|
| 13 |
-
**Note**: The lens blur option takes a long time to process (>5min) since this space isn't linked to a GPU.""")
|
| 14 |
|
| 15 |
@st.cache_resource(show_spinner="Pushing pixels...")
|
| 16 |
def load_gblur_model():
|
|
@@ -28,7 +27,7 @@ def load_lblur_model():
|
|
| 28 |
gblur_model = load_gblur_model()
|
| 29 |
lblur_model, lblur_img_proc = load_lblur_model()
|
| 30 |
|
| 31 |
-
def gaussian_blur(image):
|
| 32 |
# Image transform
|
| 33 |
image_size = (512, 512)
|
| 34 |
transform_image = transforms.Compose([
|
|
@@ -46,13 +45,13 @@ def gaussian_blur(image):
|
|
| 46 |
mask = np.array(pred_pil.resize(image.size))
|
| 47 |
|
| 48 |
# Blurring
|
| 49 |
-
blur = np.array(image.filter(ImageFilter.GaussianBlur(radius=
|
| 50 |
mask = np.expand_dims(mask, axis=2)
|
| 51 |
output_image = np.where(mask, np.array(image), blur)
|
| 52 |
|
| 53 |
return Image.fromarray(output_image)
|
| 54 |
|
| 55 |
-
def lens_blur(image):
|
| 56 |
# Process image
|
| 57 |
inputs = lblur_img_proc(images=image, return_tensors="pt").to(device)
|
| 58 |
|
|
@@ -75,7 +74,7 @@ def lens_blur(image):
|
|
| 75 |
|
| 76 |
# No of discrete blurs and max blur intensity
|
| 77 |
num_levels = 15
|
| 78 |
-
max_radius =
|
| 79 |
|
| 80 |
# Pre-compute all blur images
|
| 81 |
blurred_images = []
|
|
@@ -127,18 +126,21 @@ if up_img:
|
|
| 127 |
image = Image.open(up_img).convert("RGB")
|
| 128 |
|
| 129 |
gblur, lblur = st.columns(2)
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
disp_left, disp_right = st.columns(2)
|
| 132 |
with disp_left:
|
| 133 |
og_img = image.copy()
|
| 134 |
st.image(og_img, caption="Original Image", use_container_width=True)
|
| 135 |
with disp_right:
|
| 136 |
-
if gblur.button("Gaussian Blur", use_container_width=
|
| 137 |
with st.spinner(f"Spinning violently around the y-axis..."):
|
| 138 |
-
result = gaussian_blur(image)
|
| 139 |
-
if lblur.button("Lens Blur", use_container_width=
|
| 140 |
with st.spinner(f"One mississippi, two mississippi..."):
|
| 141 |
-
result = lens_blur(image)
|
| 142 |
st.image(result, "Blurred Image", use_container_width=True)
|
| 143 |
# apply_func(image, options)
|
| 144 |
|
|
|
|
| 8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
|
| 10 |
st.title("Image blur using segmentation and depth estimation.")
|
| 11 |
+
st.info("Upload your image and choose a blur style")
|
| 12 |
+
st.warning("**Note**: The lens blur option takes a long time to process (>5min) since this space isn't linked to a GPU.")
|
|
|
|
| 13 |
|
| 14 |
@st.cache_resource(show_spinner="Pushing pixels...")
|
| 15 |
def load_gblur_model():
|
|
|
|
| 27 |
gblur_model = load_gblur_model()
|
| 28 |
lblur_model, lblur_img_proc = load_lblur_model()
|
| 29 |
|
| 30 |
+
def gaussian_blur(image, blur_str):
|
| 31 |
# Image transform
|
| 32 |
image_size = (512, 512)
|
| 33 |
transform_image = transforms.Compose([
|
|
|
|
| 45 |
mask = np.array(pred_pil.resize(image.size))
|
| 46 |
|
| 47 |
# Blurring
|
| 48 |
+
blur = np.array(image.filter(ImageFilter.GaussianBlur(radius=blur_str)))
|
| 49 |
mask = np.expand_dims(mask, axis=2)
|
| 50 |
output_image = np.where(mask, np.array(image), blur)
|
| 51 |
|
| 52 |
return Image.fromarray(output_image)
|
| 53 |
|
| 54 |
+
def lens_blur(image, blur_str):
|
| 55 |
# Process image
|
| 56 |
inputs = lblur_img_proc(images=image, return_tensors="pt").to(device)
|
| 57 |
|
|
|
|
| 74 |
|
| 75 |
# No of discrete blurs and max blur intensity
|
| 76 |
num_levels = 15
|
| 77 |
+
max_radius = blur_str
|
| 78 |
|
| 79 |
# Pre-compute all blur images
|
| 80 |
blurred_images = []
|
|
|
|
| 126 |
image = Image.open(up_img).convert("RGB")
|
| 127 |
|
| 128 |
gblur, lblur = st.columns(2)
|
| 129 |
+
blur_str = st.slider(
|
| 130 |
+
"Blur Strength", value=(5, 50)
|
| 131 |
+
)
|
| 132 |
|
| 133 |
disp_left, disp_right = st.columns(2)
|
| 134 |
with disp_left:
|
| 135 |
og_img = image.copy()
|
| 136 |
st.image(og_img, caption="Original Image", use_container_width=True)
|
| 137 |
with disp_right:
|
| 138 |
+
if gblur.button("Gaussian Blur", use_container_width=True):
|
| 139 |
with st.spinner(f"Spinning violently around the y-axis..."):
|
| 140 |
+
result = gaussian_blur(image, blur_str)
|
| 141 |
+
if lblur.button("Lens Blur", use_container_width=True):
|
| 142 |
with st.spinner(f"One mississippi, two mississippi..."):
|
| 143 |
+
result = lens_blur(image, blur_str)
|
| 144 |
st.image(result, "Blurred Image", use_container_width=True)
|
| 145 |
# apply_func(image, options)
|
| 146 |
|