narain
commited on
Commit
·
6d9557a
1
Parent(s):
c1a6050
updated code
Browse files
app.py
CHANGED
|
@@ -3,36 +3,57 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
-
|
| 10 |
depth_model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
| 11 |
|
| 12 |
-
def apply_blur(image, blur_type, blur_strength
|
| 13 |
# Convert image to RGB
|
| 14 |
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Apply blur based on selected type
|
| 38 |
if blur_type == "Gaussian":
|
|
@@ -43,7 +64,7 @@ def apply_blur(image, blur_type, blur_strength, depth_threshold):
|
|
| 43 |
blurred_image = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
|
| 44 |
|
| 45 |
# Combine blurred and original images using the mask
|
| 46 |
-
output = np.where(mask == 255,
|
| 47 |
|
| 48 |
return output
|
| 49 |
|
|
@@ -53,12 +74,11 @@ iface = gr.Interface(
|
|
| 53 |
inputs=[
|
| 54 |
gr.Image(label="Input Image"),
|
| 55 |
gr.Radio(["Gaussian", "Lens"], label="Blur Type"),
|
| 56 |
-
gr.Slider(1, 30, value=15, step=1, label="Blur Strength")
|
| 57 |
-
gr.Slider(1, 10, value=3, step=0.1, label="Depth Threshold")
|
| 58 |
],
|
| 59 |
outputs=gr.Image(label="Output Image"),
|
| 60 |
title="Image Segmentation and Blurring",
|
| 61 |
-
description="Upload an image and apply Gaussian or Lens blur to the background
|
| 62 |
)
|
| 63 |
|
| 64 |
# Launch the app
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
+
from transformers import (
|
| 7 |
+
SegformerImageProcessor,
|
| 8 |
+
SegformerForSemanticSegmentation,
|
| 9 |
+
AutoImageProcessor,
|
| 10 |
+
AutoModelForDepthEstimation
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Load Segformer model for Gaussian blur
|
| 14 |
+
segformer_processor = SegformerImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 15 |
+
segformer_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 16 |
|
| 17 |
+
# Load Depth-Anything model for lens blur
|
| 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 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 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() > -4
|
| 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
|
| 43 |
|
| 44 |
+
prediction = torch.nn.functional.interpolate(
|
| 45 |
+
predicted_depth.unsqueeze(1),
|
| 46 |
+
size=img.shape[:2],
|
| 47 |
+
mode="bicubic",
|
| 48 |
+
align_corners=False,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
mask = prediction[0, 0, :, :].detach().cpu().numpy() < 3
|
| 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
|
| 59 |
if blur_type == "Gaussian":
|
|
|
|
| 64 |
blurred_image = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
|
| 65 |
|
| 66 |
# Combine blurred and original images using the mask
|
| 67 |
+
output = np.where(mask == 255, blurred_image, img)
|
| 68 |
|
| 69 |
return output
|
| 70 |
|
|
|
|
| 74 |
inputs=[
|
| 75 |
gr.Image(label="Input Image"),
|
| 76 |
gr.Radio(["Gaussian", "Lens"], label="Blur Type"),
|
| 77 |
+
gr.Slider(1, 30, value=15, step=1, label="Blur Strength")
|
|
|
|
| 78 |
],
|
| 79 |
outputs=gr.Image(label="Output Image"),
|
| 80 |
title="Image Segmentation and Blurring",
|
| 81 |
+
description="Upload an image and apply Gaussian or Lens blur to the background using different segmentation models."
|
| 82 |
)
|
| 83 |
|
| 84 |
# Launch the app
|