File size: 10,383 Bytes
1c9c901 d0db5dd 1c9c901 21725cf 1c9c901 21725cf 1c9c901 21725cf 1c9c901 21725cf 1c9c901 21725cf 1c9c901 21725cf 1c9c901 21725cf ce999b4 21725cf 1c9c901 d816800 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 27 13:56:42 2025
@author: perghect
"""
import gradio as gr
import requests
import io
import torch
import numpy as np
from PIL import Image, ImageFilter
from torchvision import transforms
from transformers import AutoModelForImageSegmentation, AutoImageProcessor, AutoModelForDepthEstimation
# Set device and precision
device = 'cuda' if torch.cuda.is_available() else 'cpu'
torch.set_float32_matmul_precision('high')
# Load models at startup
rmbg_model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-2.0", trust_remote_code=True).to(device).eval()
depth_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
depth_model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf").to(device)
def load_image_from_link(url: str) -> Image.Image:
"""Downloads an image from a URL and returns a Pillow Image."""
response = requests.get(url)
response.raise_for_status()
image = Image.open(io.BytesIO(response.content)).convert("RGB")
return image
# Gaussian Blur Functions
def run_rmbg(image: Image.Image, threshold=0.5):
"""Runs the RMBG-2.0 model on the image and returns a binary mask."""
try:
image_size = (1024, 1024)
transform_image = transforms.Compose([
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
input_images = transform_image(image).unsqueeze(0).to(device)
with torch.no_grad():
preds = rmbg_model(input_images)
if isinstance(preds, list):
mask_logits = preds[-1]
else:
raise ValueError(f"Unexpected output format: {type(preds)}")
mask_prob = mask_logits.sigmoid().cpu()[0].squeeze()
pred_pil = transforms.ToPILImage()(mask_prob)
mask_pil = pred_pil.resize(image.size, resample=Image.BILINEAR)
mask_np = np.array(mask_pil, dtype=np.uint8) / 255.0
binary_mask = (mask_np > threshold).astype(np.uint8)
return binary_mask
except Exception as e:
raise Exception(f"Error in background removal: {str(e)}")
def apply_background_blur(image: Image.Image, mask: np.ndarray, sigma: float = 15):
"""Applies a Gaussian blur to the background while keeping the foreground sharp."""
image_np = np.array(image)
mask_np = mask.astype(np.uint8)
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=sigma))
blurred_np = np.array(blurred_image)
output_np = np.where(mask_np[..., None] == 1, image_np, blurred_np)
output_image = Image.fromarray(output_np.astype(np.uint8))
return output_image
# Lens Blur Functions
def run_depth_estimation(image: Image.Image, target_size=(512, 512)):
"""Runs the Depth-Anything-V2-Small model and returns the depth map."""
try:
image_resized = image.resize(target_size, resample=Image.BILINEAR)
inputs = depth_processor(images=image_resized, return_tensors="pt").to(device)
with torch.no_grad():
outputs = depth_model(**inputs)
predicted_depth = outputs.predicted_depth
prediction = torch.nn.functional.interpolate(
predicted_depth.unsqueeze(1),
size=image.size[::-1],
mode="bicubic",
align_corners=False,
)
depth_map = prediction.squeeze().cpu().numpy()
depth_max = depth_map.max()
depth_min = depth_map.min()
if depth_max == depth_min:
depth_max = depth_min + 1e-6 # Avoid division by zero
depth_map = (depth_map - depth_min) / (depth_max - depth_min)
depth_map = 1 - depth_map # Invert: higher values = farther
return depth_map
except Exception as e:
raise Exception(f"Error in depth estimation: {str(e)}")
def apply_depth_based_blur(image: Image.Image, depth_map: np.ndarray, max_radius: float = 15, foreground_percentile: float = 30):
"""Applies a variable Gaussian blur based on the depth map."""
image_np = np.array(image)
if depth_map.shape != image_np.shape[:2]:
depth_map = np.array(Image.fromarray(depth_map).resize(image.size, resample=Image.BILINEAR))
foreground_threshold = np.percentile(depth_map.flatten(), foreground_percentile)
output_np = np.zeros_like(image_np)
mask_foreground = (depth_map <= foreground_threshold)
output_np[mask_foreground] = image_np[mask_foreground]
depth_max = depth_map.max()
depth_range = depth_max - foreground_threshold
if depth_range == 0:
depth_range = 1e-6
normalized_depth = np.zeros_like(depth_map)
mask_above_foreground = (depth_map > foreground_threshold)
normalized_depth[mask_above_foreground] = (depth_map[mask_above_foreground] - foreground_threshold) / depth_range
normalized_depth = np.clip(normalized_depth, 0, 1)
depth_levels = np.linspace(0, 1, 20)
for i in range(len(depth_levels) - 1):
depth_min = depth_levels[i]
depth_max = depth_levels[i + 1]
mask = (normalized_depth >= depth_min) & (normalized_depth < depth_max) & (depth_map > foreground_threshold)
if not np.any(mask):
continue
avg_depth = (depth_min + depth_max) / 2
blur_radius = max_radius * avg_depth
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=blur_radius))
blurred_np = np.array(blurred_image)
output_np[mask] = blurred_np[mask]
mask_farthest = (normalized_depth >= depth_levels[-1]) & (depth_map > foreground_threshold)
if np.any(mask_farthest):
blurred_max = image.filter(ImageFilter.GaussianBlur(radius=max_radius))
output_np[mask_farthest] = np.array(blurred_max)[mask_farthest]
output_image = Image.fromarray(output_np.astype(np.uint8))
return output_image
# Main Processing Function for Gradio
def process_image(image, blur_type, sigma=15, max_radius=15, foreground_percentile=30):
"""Processes the image based on the selected blur type."""
if image is None:
return None, "Please upload an image."
try:
image = Image.fromarray(image).convert("RGB")
except Exception as e:
return None, f"Error processing image: {str(e)}"
# Resize image if too large
max_size = (1024, 1024)
if image.size[0] > max_size[0] or image.size[1] > max_size[1]:
image.thumbnail(max_size, Image.Resampling.LANCZOS)
try:
if blur_type == "Gaussian Blur":
mask = run_rmbg(image, threshold=0.5)
output_image = apply_background_blur(image, mask, sigma=sigma)
title = f"Gaussian Blur (sigma={sigma})"
else: # Lens Blur
depth_map = run_depth_estimation(image, target_size=(512, 512))
output_image = apply_depth_based_blur(image, depth_map, max_radius=max_radius, foreground_percentile=foreground_percentile)
title = f"Lens Blur (max_radius={max_radius}, foreground_percentile={foreground_percentile})"
except Exception as e:
return None, f"Error applying blur: {str(e)}"
return output_image, title
# Gradio Interface with Conditional Parameter Display
with gr.Blocks() as demo:
gr.Markdown("# Image Blur Effects with Gaussian and Lens Blur")
gr.Markdown("""
This app applies blur effects to your images. Follow these steps to use it:
**Note**: This app is hosted on Hugging Face Spaces’ free tier and may go to "Sleeping" mode after 48 hours of inactivity. If it doesn’t load immediately, please wait a few seconds while it wakes up.
1. **Upload an Image**: Click the "Upload Image" box to upload an image from your device.
2. **Choose a Blur Type**:
- **Gaussian Blur**: Applies a uniform blur to the background, keeping the foreground sharp. Adjust the sigma parameter to control blur intensity.
- **Lens Blur**: Applies a depth-based blur, simulating a depth-of-field effect (closer objects are sharp, farther objects are blurred). Adjust the max radius and foreground percentile to fine-tune the effect.
3. **Adjust Parameters**:
- For Gaussian Blur, use the "Gaussian Blur Sigma" slider to control blur intensity (higher values = more blur).
- For Lens Blur, use the "Max Blur Radius" slider to control the maximum blur intensity and the "Foreground Percentile" slider to adjust the depth threshold for the foreground.
4. **Apply the Blur**: Click the "Apply Blur" button to process the image.
5. **View the Result**: The processed image will appear in the "Output Image" box, along with a description of the effect applied.
**Example**: Try uploading an image with a clear foreground and background (e.g., a person in front of a landscape) to see the effects in action.
""")
with gr.Row():
image_input = gr.Image(label="Upload Image", type="numpy")
with gr.Column():
blur_type = gr.Radio(choices=["Gaussian Blur", "Lens Blur"], label="Blur Type", value="Gaussian Blur")
sigma = gr.Slider(minimum=1, maximum=50, step=1, value=15, label="Gaussian Blur Sigma", visible=True)
max_radius = gr.Slider(minimum=1, maximum=50, step=1, value=15, label="Max Lens Blur Radius", visible=False)
foreground_percentile = gr.Slider(minimum=1, maximum=50, step=1, value=30, label="Foreground Percentile", visible=False)
# Update visibility of parameters based on blur type
def update_visibility(blur_type):
if blur_type == "Gaussian Blur":
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
else: # Lens Blur
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
blur_type.change(
fn=update_visibility,
inputs=blur_type,
outputs=[sigma, max_radius, foreground_percentile]
)
process_button = gr.Button("Apply Blur")
with gr.Row():
output_image = gr.Image(label="Output Image")
output_text = gr.Textbox(label="Effect Applied")
process_button.click(
fn=process_image,
inputs=[image_input, blur_type, sigma, max_radius, foreground_percentile],
outputs=[output_image, output_text]
)
# Launch the app
demo.launch() |