Ash2505 commited on
Commit
246a819
·
verified ·
1 Parent(s): 9b7d147

Slider changes

Browse files
Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -57,7 +57,6 @@ def segmentation_blur_effect(input_image: Image.Image):
57
 
58
  # Convert predicted mask to a PIL image and ensure it matches imageResized's size
59
  pred_pil = transforms.ToPILImage()(pred)
60
- # Resize mask to match imageResized to avoid size mismatch in OpenCV operations
61
  mask = pred_pil.resize(imageResized.size)
62
 
63
  # Convert mask to grayscale and threshold to create a binary mask
@@ -84,14 +83,24 @@ def segmentation_blur_effect(input_image: Image.Image):
84
  return finalImg_pil, mask
85
 
86
  # -----------------------------
87
- # Define the Depth-Based Lens Blur Effect
88
  # -----------------------------
89
- def lens_blur_effect(input_image: Image.Image):
90
  """
91
  Uses DepthPro to estimate a depth map and applies a dynamic lens blur effect
92
- by blending three versions of the image (foreground, middleground, background)
93
- with increasing blur levels. Returns the depth map, the final lens-blurred image,
94
- and the depth masks.
 
 
 
 
 
 
 
 
 
 
95
  """
96
  # Process the image with the depth estimation model
97
  inputs = depth_processor(images=input_image, return_tensors="pt").to(device)
@@ -117,9 +126,9 @@ def lens_blur_effect(input_image: Image.Image):
117
  img_middleground = cv2.GaussianBlur(img, (0, 0), sigmaX=7, sigmaY=7)
118
  img_background = cv2.GaussianBlur(img, (0, 0), sigmaX=15, sigmaY=15)
119
 
120
- # Define depth thresholds (using 1/3 and 2/3 of 255)
121
- threshold1 = 255 / 3 # ~85
122
- threshold2 = 2 * 255 / 3 # ~170
123
 
124
  # Create masks for foreground, middleground, and background based on depth
125
  mask_fg = (depth_map < threshold1).astype(np.float32)
@@ -131,7 +140,7 @@ def lens_blur_effect(input_image: Image.Image):
131
  mask_mg_3 = np.stack([mask_mg]*3, axis=-1)
132
  mask_bg_3 = np.stack([mask_bg]*3, axis=-1)
133
 
134
- # Blend the images using the masks (vectorized operation)
135
  final_img = (img_foreground * mask_fg_3 +
136
  img_middleground * mask_mg_3 +
137
  img_background * mask_bg_3).astype(np.uint8)
@@ -149,17 +158,21 @@ def lens_blur_effect(input_image: Image.Image):
149
  # -----------------------------
150
  # Gradio App: Process Image and Display Multiple Effects
151
  # -----------------------------
152
- def process_image(input_image: Image.Image):
153
  """
154
  Processes the uploaded image to generate:
155
  1. Segmentation-based Gaussian blur effect.
156
  2. Segmentation mask.
157
  3. Depth map.
158
  4. Depth-based lens blur effect.
159
- 5. Depth-based masks for foreground, middleground, and background.
 
 
160
  """
161
  seg_blur, seg_mask = segmentation_blur_effect(input_image)
162
- depth_map_img, lens_blur_img, mask_fg_img, mask_mg_img, mask_bg_img = lens_blur_effect(input_image)
 
 
163
 
164
  return (
165
  seg_blur,
@@ -171,17 +184,21 @@ def process_image(input_image: Image.Image):
171
  mask_bg_img
172
  )
173
 
174
- title = "Blur Effects: Gaussian Blur & Depth-Based Lens Blur"
175
  description = (
176
  "Upload an image to apply two distinct effects:\n\n"
177
  "1. A segmentation-based Gaussian blur that blurs the background (using RMBG-2.0).\n"
178
  "2. A depth-based lens blur effect that simulates realistic lens blur based on depth (using DepthPro).\n\n"
179
- "Outputs include the blurred image, segmentation mask, depth map, lens-blurred image, and individual depth masks."
180
  )
181
 
182
  demo = gr.Interface(
183
  fn=process_image,
184
- inputs=gr.Image(type="pil", label="Input Image"),
 
 
 
 
185
  outputs=[
186
  gr.Image(type="pil", label="Segmentation-Based Blur"),
187
  gr.Image(type="pil", label="Segmentation Mask"),
 
57
 
58
  # Convert predicted mask to a PIL image and ensure it matches imageResized's size
59
  pred_pil = transforms.ToPILImage()(pred)
 
60
  mask = pred_pil.resize(imageResized.size)
61
 
62
  # Convert mask to grayscale and threshold to create a binary mask
 
83
  return finalImg_pil, mask
84
 
85
  # -----------------------------
86
+ # Define the Depth-Based Lens Blur Effect with Slider-Controlled Thresholds
87
  # -----------------------------
88
+ def lens_blur_effect(input_image: Image.Image, fg_threshold: float = 85, mg_threshold: float = 170):
89
  """
90
  Uses DepthPro to estimate a depth map and applies a dynamic lens blur effect
91
+ by blending three versions of the image with increasing blur levels.
92
+
93
+ Parameters:
94
+ input_image: The original PIL image.
95
+ fg_threshold: Foreground threshold (0-255). Pixels with depth below this are considered foreground.
96
+ mg_threshold: Middleground threshold (0-255). Pixels with depth between fg_threshold and mg_threshold are middleground.
97
+
98
+ Returns:
99
+ depthImg: The computed depth map (PIL Image).
100
+ lensBlurImage: The final lens-blurred image (PIL Image).
101
+ mask_fg_img: Foreground depth mask.
102
+ mask_mg_img: Middleground depth mask.
103
+ mask_bg_img: Background depth mask.
104
  """
105
  # Process the image with the depth estimation model
106
  inputs = depth_processor(images=input_image, return_tensors="pt").to(device)
 
126
  img_middleground = cv2.GaussianBlur(img, (0, 0), sigmaX=7, sigmaY=7)
127
  img_background = cv2.GaussianBlur(img, (0, 0), sigmaX=15, sigmaY=15)
128
 
129
+ # Use slider values as thresholds
130
+ threshold1 = fg_threshold # e.g., default 85
131
+ threshold2 = mg_threshold # e.g., default 170
132
 
133
  # Create masks for foreground, middleground, and background based on depth
134
  mask_fg = (depth_map < threshold1).astype(np.float32)
 
140
  mask_mg_3 = np.stack([mask_mg]*3, axis=-1)
141
  mask_bg_3 = np.stack([mask_bg]*3, axis=-1)
142
 
143
+ # Blend the images using the masks
144
  final_img = (img_foreground * mask_fg_3 +
145
  img_middleground * mask_mg_3 +
146
  img_background * mask_bg_3).astype(np.uint8)
 
158
  # -----------------------------
159
  # Gradio App: Process Image and Display Multiple Effects
160
  # -----------------------------
161
+ def process_image(input_image: Image.Image, fg_threshold: float, mg_threshold: float):
162
  """
163
  Processes the uploaded image to generate:
164
  1. Segmentation-based Gaussian blur effect.
165
  2. Segmentation mask.
166
  3. Depth map.
167
  4. Depth-based lens blur effect.
168
+ 5. Depth masks for foreground, middleground, and background.
169
+
170
+ The depth thresholds for foreground and middleground regions are adjustable via sliders.
171
  """
172
  seg_blur, seg_mask = segmentation_blur_effect(input_image)
173
+ depth_map_img, lens_blur_img, mask_fg_img, mask_mg_img, mask_bg_img = lens_blur_effect(
174
+ input_image, fg_threshold, mg_threshold
175
+ )
176
 
177
  return (
178
  seg_blur,
 
184
  mask_bg_img
185
  )
186
 
187
+ title = "Blur Effects: Gaussian & Depth-Based Lens Blur with Adjustable Depth Thresholds"
188
  description = (
189
  "Upload an image to apply two distinct effects:\n\n"
190
  "1. A segmentation-based Gaussian blur that blurs the background (using RMBG-2.0).\n"
191
  "2. A depth-based lens blur effect that simulates realistic lens blur based on depth (using DepthPro).\n\n"
192
+ "Use the sliders to adjust the foreground and middleground depth thresholds."
193
  )
194
 
195
  demo = gr.Interface(
196
  fn=process_image,
197
+ inputs=[
198
+ gr.Image(type="pil", label="Input Image"),
199
+ gr.Slider(minimum=0, maximum=255, step=1, value=85, label="Foreground Depth Threshold"),
200
+ gr.Slider(minimum=0, maximum=255, step=1, value=170, label="Middleground Depth Threshold")
201
+ ],
202
  outputs=[
203
  gr.Image(type="pil", label="Segmentation-Based Blur"),
204
  gr.Image(type="pil", label="Segmentation Mask"),