narain
commited on
Commit
·
52261a5
1
Parent(s):
f82c801
update
Browse files
app.py
CHANGED
@@ -18,26 +18,25 @@ segformer_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segfo
|
|
18 |
depth_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
19 |
depth_model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
20 |
|
21 |
-
def apply_blur(image, blur_type, blur_strength):
|
22 |
# Convert image to RGB
|
23 |
img = image
|
24 |
-
|
25 |
if blur_type == "Gaussian":
|
26 |
# Use Segformer for Gaussian blur
|
27 |
pil_image = Image.fromarray(img)
|
28 |
inputs = segformer_processor(images=pil_image, return_tensors="pt")
|
29 |
outputs = segformer_model(**inputs)
|
30 |
logits = outputs.logits
|
31 |
-
|
32 |
-
mask = logits[0, 12, :, :].detach().cpu().numpy() >
|
33 |
mask = cv2.resize(mask.astype(np.uint8), (img.shape[1], img.shape[0]))
|
34 |
-
|
35 |
-
|
36 |
elif blur_type == "Lens":
|
37 |
# Use Depth-Anything for lens blur
|
38 |
pil_image = Image.fromarray(img)
|
39 |
inputs = depth_processor(images=pil_image, return_tensors="pt")
|
40 |
-
|
41 |
with torch.no_grad():
|
42 |
outputs = depth_model(**inputs)
|
43 |
predicted_depth = outputs.predicted_depth
|
@@ -48,10 +47,12 @@ def apply_blur(image, blur_type, blur_strength):
|
|
48 |
mode="bicubic",
|
49 |
align_corners=False,
|
50 |
)
|
51 |
-
|
52 |
-
mask = prediction[0, 0, :, :].detach().cpu().numpy() <
|
53 |
mask = mask.astype(np.uint8)
|
54 |
-
|
|
|
|
|
55 |
mask = np.repeat(mask[:, :, np.newaxis], 3, axis=2)
|
56 |
|
57 |
# Apply blur based on selected type
|
@@ -67,12 +68,13 @@ def apply_blur(image, blur_type, blur_strength):
|
|
67 |
|
68 |
return output
|
69 |
|
|
|
70 |
# Define Gradio interface
|
71 |
iface = gr.Interface(
|
72 |
fn=apply_blur,
|
73 |
inputs=[
|
74 |
gr.Image(label="Input Image"),
|
75 |
-
gr.Radio(["Gaussian", "Lens"], label="Blur Type"),
|
76 |
gr.Slider(1, 30, value=15, step=1, label="Blur Strength")
|
77 |
],
|
78 |
outputs=gr.Image(label="Output Image"),
|
|
|
18 |
depth_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
19 |
depth_model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
20 |
|
21 |
+
def apply_blur(image, blur_type, blur_strength, depth_threshold):
|
22 |
# Convert image to RGB
|
23 |
img = image
|
24 |
+
|
25 |
if blur_type == "Gaussian":
|
26 |
# Use Segformer for Gaussian blur
|
27 |
pil_image = Image.fromarray(img)
|
28 |
inputs = segformer_processor(images=pil_image, return_tensors="pt")
|
29 |
outputs = segformer_model(**inputs)
|
30 |
logits = outputs.logits
|
31 |
+
|
32 |
+
mask = logits[0, 12, :, :].detach().cpu().numpy() > depth_threshold
|
33 |
mask = cv2.resize(mask.astype(np.uint8), (img.shape[1], img.shape[0]))
|
34 |
+
|
|
|
35 |
elif blur_type == "Lens":
|
36 |
# Use Depth-Anything for lens blur
|
37 |
pil_image = Image.fromarray(img)
|
38 |
inputs = depth_processor(images=pil_image, return_tensors="pt")
|
39 |
+
|
40 |
with torch.no_grad():
|
41 |
outputs = depth_model(**inputs)
|
42 |
predicted_depth = outputs.predicted_depth
|
|
|
47 |
mode="bicubic",
|
48 |
align_corners=False,
|
49 |
)
|
50 |
+
|
51 |
+
mask = prediction[0, 0, :, :].detach().cpu().numpy() < depth_threshold
|
52 |
mask = mask.astype(np.uint8)
|
53 |
+
|
54 |
+
# Invert mask using cv2
|
55 |
+
mask = cv2.bitwise_not(mask)
|
56 |
mask = np.repeat(mask[:, :, np.newaxis], 3, axis=2)
|
57 |
|
58 |
# Apply blur based on selected type
|
|
|
68 |
|
69 |
return output
|
70 |
|
71 |
+
|
72 |
# Define Gradio interface
|
73 |
iface = gr.Interface(
|
74 |
fn=apply_blur,
|
75 |
inputs=[
|
76 |
gr.Image(label="Input Image"),
|
77 |
+
gr.Radio(["Gaussian", "Lens"], label="Blur Type", value="Gaussian"),
|
78 |
gr.Slider(1, 30, value=15, step=1, label="Blur Strength")
|
79 |
],
|
80 |
outputs=gr.Image(label="Output Image"),
|