Spaces:
Running
Running
Upload 2 files
Browse files- app.py +76 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
import cv2
|
6 |
+
import os # Import the os module
|
7 |
+
|
8 |
+
from transformers import DPTImageProcessor, DPTForDepthEstimation
|
9 |
+
|
10 |
+
# Access the token from the Space's secrets
|
11 |
+
HF_TOKEN = os.environ.get("HUGGING_FACE_TOKEN")
|
12 |
+
if HF_TOKEN is None:
|
13 |
+
raise ValueError("Hugging Face token not found. Make sure you've added it as a secret to your Space.")
|
14 |
+
|
15 |
+
# Load the image processor and model
|
16 |
+
image_processor = DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas", token=HF_TOKEN)
|
17 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas", low_cpu_mem_usage=True, token=HF_TOKEN)
|
18 |
+
model.to('cuda')
|
19 |
+
model.eval()
|
20 |
+
|
21 |
+
def apply_depth_aware_blur(image, foreground_blur, midground_blur, background_blur, foreground_threshold, midground_lower, midground_upper, background_threshold):
|
22 |
+
original_image = Image.fromarray(image).convert("RGB")
|
23 |
+
original_image = original_image.resize((512, 512))
|
24 |
+
image_np = np.array(original_image)
|
25 |
+
|
26 |
+
inputs = image_processor(images=original_image, return_tensors="pt").to('cuda')
|
27 |
+
|
28 |
+
with torch.no_grad():
|
29 |
+
outputs = model(**inputs)
|
30 |
+
predicted_depth = outputs.predicted_depth
|
31 |
+
|
32 |
+
prediction = torch.nn.functional.interpolate(
|
33 |
+
predicted_depth.unsqueeze(1),
|
34 |
+
size=(512, 512),
|
35 |
+
mode="bicubic",
|
36 |
+
align_corners=False,
|
37 |
+
)
|
38 |
+
|
39 |
+
depth_map = prediction.squeeze().cpu().numpy()
|
40 |
+
normalized_depth_map = (depth_map - np.min(depth_map)) / (np.max(depth_map) - np.min(depth_map))
|
41 |
+
|
42 |
+
foreground_mask = (normalized_depth_map < foreground_threshold).astype(np.uint8) * 255
|
43 |
+
midground_mask = ((normalized_depth_map >= midground_lower) & (normalized_depth_map < midground_upper)).astype(np.uint8) * 255
|
44 |
+
background_mask = (normalized_depth_map >= background_threshold).astype(np.uint8) * 255
|
45 |
+
|
46 |
+
blurred_image = np.copy(np.array(original_image))
|
47 |
+
|
48 |
+
if foreground_blur > 0:
|
49 |
+
blurred_image = np.where((foreground_mask[..., None] == 255), cv2.GaussianBlur(blurred_image, (foreground_blur, foreground_blur), 10), blurred_image)
|
50 |
+
if midground_blur > 0:
|
51 |
+
blurred_image = np.where((midground_mask[..., None] == 255), cv2.GaussianBlur(blurred_image, (midground_blur, midground_blur), 8), blurred_image)
|
52 |
+
if background_blur > 0:
|
53 |
+
blurred_image = np.where((background_mask[..., None] == 255), cv2.GaussianBlur(blurred_image, (background_blur, background_blur), 20), blurred_image)
|
54 |
+
|
55 |
+
return Image.fromarray(blurred_image.astype(np.uint8))
|
56 |
+
|
57 |
+
iface = gr.Interface(
|
58 |
+
fn=apply_depth_aware_blur,
|
59 |
+
inputs=[
|
60 |
+
gr.Image(label="Input Image"),
|
61 |
+
gr.Slider(0, 51, step=2, label="Foreground Blur Kernel Size", default=0),
|
62 |
+
gr.Slider(0, 51, step=2, label="Midground Blur Kernel Size", default=0),
|
63 |
+
gr.Slider(0, 51, step=2, label="Background Blur Kernel Size", default=35),
|
64 |
+
gr.Slider(0, 1, label="Foreground Threshold", default=0.2),
|
65 |
+
gr.Slider(0, 1, label="Midground Lower Threshold", default=0.2),
|
66 |
+
gr.Slider(0, 1, label="Midground Upper Threshold", default=0.6),
|
67 |
+
gr.Slider(0, 1, label="Background Threshold", default=0.6)
|
68 |
+
|
69 |
+
],
|
70 |
+
outputs=gr.Image(label="Blurred Image"),
|
71 |
+
title="Depth-Aware Lens Blur App",
|
72 |
+
description="Apply depth-based blur to uploaded images. Adjust blur intensity for foreground, midground, and background.",
|
73 |
+
)
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
opencv-python
|
4 |
+
Pillow
|
5 |
+
torch
|