gchallar commited on
Commit
6ebec3f
·
verified ·
1 Parent(s): 438cbb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -26
app.py CHANGED
@@ -3,23 +3,54 @@ from PIL import Image, ImageFilter
3
  import numpy as np
4
  import torch
5
  import cv2
6
- from transformers import AutoImageProcessor, AutoModelForDepthEstimation
7
 
8
  # Load depth estimation model
9
  image_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
10
- model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
11
 
12
- def apply_gaussian_blur(image, mask):
13
- """Applies Gaussian blur to the background based on a user-drawn mask."""
 
 
 
 
14
 
15
- if mask is None:
16
- return image # If no mask is provided, return the original image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Ensure mask is grayscale and resized to match image dimensions
19
- mask_pil = Image.fromarray(mask).convert("L").resize(image.size)
20
- mask_array = np.array(mask_pil)
21
 
22
- # Create a blurred background
23
  blurred_background = image.filter(ImageFilter.GaussianBlur(radius=15))
24
 
25
  # Convert images to NumPy arrays
@@ -27,7 +58,7 @@ def apply_gaussian_blur(image, mask):
27
  blurred_array = np.array(blurred_background)
28
 
29
  # Create a boolean mask (foreground = True, background = False)
30
- foreground_mask = mask_array > 0
31
  foreground_mask_3d = np.stack([foreground_mask] * 3, axis=-1)
32
 
33
  # Blend the original image with the blurred background
@@ -47,25 +78,26 @@ def apply_lens_blur(image):
47
  inputs = image_processor(images=resized_image, return_tensors="pt")
48
 
49
  with torch.no_grad():
50
- outputs = model(**inputs)
51
  predicted_depth = outputs.predicted_depth
52
 
53
- # Interpolate depth map to match the image size
54
  prediction = torch.nn.functional.interpolate(
55
  predicted_depth.unsqueeze(1),
56
  size=resized_image.size[::-1],
57
  mode="bicubic",
58
  align_corners=False,
59
- ).squeeze()
60
 
61
  # Convert prediction to a NumPy array
62
  depth_map = prediction.cpu().numpy()
63
 
64
- # Normalize the depth map
65
  depth_norm = (depth_map - np.min(depth_map)) / (np.max(depth_map) - np.min(depth_map))
66
 
67
- num_blur_levels = 5
68
  blurred_layers = []
 
69
  for i in range(num_blur_levels):
70
  sigma = i * 0.5
71
  if sigma == 0:
@@ -77,6 +109,7 @@ def apply_lens_blur(image):
77
  depth_indices = ((1 - depth_norm) * (num_blur_levels - 1)).astype(np.uint8)
78
 
79
  final_blurred_image = np.zeros_like(image_np)
 
80
  for y in range(image_np.shape[0]):
81
  for x in range(image_np.shape[1]):
82
  depth_index = depth_indices[y, x]
@@ -87,27 +120,24 @@ def apply_lens_blur(image):
87
 
88
  return final_blurred_pil_image
89
 
90
- def process_image(image, mask, blur_type):
91
  """Processes the image based on the selected blur type."""
92
  if blur_type == "Gaussian Blur":
93
- return apply_gaussian_blur(image, mask)
94
- elif blur_type == "Lens Blur":
95
- return apply_lens_blur(image)
96
  else:
97
- return image
98
 
99
  interface = gr.Interface(
100
  fn=process_image,
101
  inputs=[
102
  gr.Image(type="pil", label="Upload an Image"),
103
- gr.Sketchpad(height=256, width=256, label="Draw Mask (Only for Gaussian Blur)"),
104
- gr.Radio(["Gaussian Blur", "Lens Blur"], label="Choose Blur Effect")
105
  ],
106
- outputs=gr.Image(type="pil"),
107
  title="Gaussian & Lens Blur Effects",
108
- description="Upload an image and select either Gaussian blur (with mask) or depth-based lens blur."
109
  )
110
 
111
-
112
  if __name__ == "__main__":
113
  interface.launch()
 
3
  import numpy as np
4
  import torch
5
  import cv2
6
+ from transformers import AutoImageProcessor, AutoModelForDepthEstimation, OneFormerProcessor, OneFormerForUniversalSegmentation
7
 
8
  # Load depth estimation model
9
  image_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
10
+ depth_model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
11
 
12
+ # Load OneFormer processor and model
13
+ processor = OneFormerProcessor.from_pretrained("shi-labs/oneformer_coco_swin_large")
14
+ segmentation_model = OneFormerForUniversalSegmentation.from_pretrained("shi-labs/oneformer_coco_swin_large")
15
+
16
+ def apply_gaussian_blur(image, foreground_label='person'):
17
+ """Applies Gaussian blur to the background based on a segmentation mask for the foreground."""
18
 
19
+ # Prepare input for semantic segmentation
20
+ inputs = processor(images=image, task_inputs=["semantic"], return_tensors="pt")
21
+
22
+ # Semantic segmentation
23
+ with torch.no_grad():
24
+ outputs = segmentation_model(**inputs)
25
+
26
+ # Processing semantic segmentation output
27
+ predicted_semantic_map = processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
28
+ segmentation_mask = predicted_semantic_map.cpu().numpy()
29
+
30
+ # Get the mapping of class IDs to labels from the processor
31
+ id2label = segmentation_model.config.id2label
32
+ foreground_class_id = None
33
+ for id, label in id2label.items():
34
+ if label == foreground_label:
35
+ foreground_class_id = id
36
+ break
37
+
38
+ if foreground_class_id is None:
39
+ print(f"Error: Could not find the label '{foreground_label}' in the model's class mapping.")
40
+ return image # Return original image if foreground label is not found
41
+
42
+ # Create a black background mask and set the pixels corresponding to the foreground object to white
43
+ output_mask_array = np.zeros(segmentation_mask.shape, dtype=np.uint8)
44
+ output_mask_array[segmentation_mask == foreground_class_id] = 255
45
+
46
+ # Convert the output mask to a PIL Image (Grayscale)
47
+ mask_pil = Image.fromarray(output_mask_array, mode='L')
48
 
49
+ # Resize the mask to match the image size
50
+ mask_pil = mask_pil.resize(image.size)
51
+ output_mask_array = np.array(mask_pil)
52
 
53
+ # Create a blurred version of the input image
54
  blurred_background = image.filter(ImageFilter.GaussianBlur(radius=15))
55
 
56
  # Convert images to NumPy arrays
 
58
  blurred_array = np.array(blurred_background)
59
 
60
  # Create a boolean mask (foreground = True, background = False)
61
+ foreground_mask = output_mask_array > 0
62
  foreground_mask_3d = np.stack([foreground_mask] * 3, axis=-1)
63
 
64
  # Blend the original image with the blurred background
 
78
  inputs = image_processor(images=resized_image, return_tensors="pt")
79
 
80
  with torch.no_grad():
81
+ outputs = depth_model(**inputs)
82
  predicted_depth = outputs.predicted_depth
83
 
84
+ # Interpolate to the original size
85
  prediction = torch.nn.functional.interpolate(
86
  predicted_depth.unsqueeze(1),
87
  size=resized_image.size[::-1],
88
  mode="bicubic",
89
  align_corners=False,
90
+ ).squeeze()
91
 
92
  # Convert prediction to a NumPy array
93
  depth_map = prediction.cpu().numpy()
94
 
95
+ # Normalize the depth map to the range 0-1
96
  depth_norm = (depth_map - np.min(depth_map)) / (np.max(depth_map) - np.min(depth_map))
97
 
98
+ num_blur_levels = 5
99
  blurred_layers = []
100
+
101
  for i in range(num_blur_levels):
102
  sigma = i * 0.5
103
  if sigma == 0:
 
109
  depth_indices = ((1 - depth_norm) * (num_blur_levels - 1)).astype(np.uint8)
110
 
111
  final_blurred_image = np.zeros_like(image_np)
112
+
113
  for y in range(image_np.shape[0]):
114
  for x in range(image_np.shape[1]):
115
  depth_index = depth_indices[y, x]
 
120
 
121
  return final_blurred_pil_image
122
 
123
+ def process_image(image, blur_type, foreground_label='person'):
124
  """Processes the image based on the selected blur type."""
125
  if blur_type == "Gaussian Blur":
126
+ return apply_gaussian_blur(image, foreground_label=foreground_label)
 
 
127
  else:
128
+ return apply_lens_blur(image)
129
 
130
  interface = gr.Interface(
131
  fn=process_image,
132
  inputs=[
133
  gr.Image(type="pil", label="Upload an Image"),
134
+ gr.Radio(["Gaussian Blur", "Lens Blur"], label="Choose Blur Effect"),
135
+ gr.Textbox(label="Foreground Label (for Gaussian Blur)", default="person")
136
  ],
137
+ outputs=[gr.Image(type="pil"), gr.Image(type="pil")],
138
  title="Gaussian & Lens Blur Effects",
139
+ description="Upload an image and select either Gaussian blur (with foreground segmentation) or depth-based lens blur."
140
  )
141
 
 
142
  if __name__ == "__main__":
143
  interface.launch()