kvinod15 commited on
Commit
aa66451
·
verified ·
1 Parent(s): e555f20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -13
app.py CHANGED
@@ -5,10 +5,8 @@ from PIL import Image
5
 
6
  # Dummy segmentation function: replace with your actual segmentation model inference if available.
7
  def segment_foreground(img):
8
- # Convert input image to a NumPy array
9
  np_img = np.array(img.convert("RGB"))
10
  h, w, _ = np_img.shape
11
- # Create a circular mask as a dummy example
12
  mask = np.zeros((h, w), dtype=np.uint8)
13
  center = (w // 2, h // 2)
14
  radius = min(center) - 10
@@ -19,11 +17,8 @@ def segment_foreground(img):
19
  def gaussian_blur_background(img, sigma=15):
20
  mask = segment_foreground(img)
21
  np_img = np.array(img.convert("RGB"))
22
- # Apply Gaussian blur to the entire image
23
  blurred = cv2.GaussianBlur(np_img, (0, 0), sigma)
24
- # Prepare the mask in 3 channels
25
  mask_3d = np.stack([mask] * 3, axis=-1) / 255.0
26
- # Combine the original (foreground) with the blurred (background)
27
  combined = np_img * mask_3d + blurred * (1 - mask_3d)
28
  return Image.fromarray(combined.astype(np.uint8))
29
 
@@ -31,7 +26,6 @@ def gaussian_blur_background(img, sigma=15):
31
  def estimate_depth(img):
32
  np_img = np.array(img.convert("RGB"))
33
  h, w, _ = np_img.shape
34
- # Create a gradient depth map: top of the image is close (0), bottom is far (1)
35
  depth = np.tile(np.linspace(0, 1, h)[:, None], (1, w))
36
  return depth
37
 
@@ -41,17 +35,14 @@ def depth_based_blur(img, max_sigma=20):
41
  np_img = np.array(img.convert("RGB"))
42
  output = np.zeros_like(np_img)
43
 
44
- # Normalize the depth map to [0, 1]
45
  depth_norm = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
46
 
47
- # Apply a variable Gaussian blur to each row based on the depth value (using the first column as representative)
48
  for i in range(np_img.shape[0]):
49
  sigma = max_sigma * depth_norm[i, 0]
50
  row = cv2.GaussianBlur(np_img[i:i+1, :, :], (0, 0), sigma)
51
  output[i, :, :] = row
52
  return Image.fromarray(output.astype(np.uint8))
53
 
54
- # Function that dispatches the processing based on user selection.
55
  def process_image(img, effect):
56
  if effect == "Gaussian Blur Background":
57
  return gaussian_blur_background(img)
@@ -60,14 +51,14 @@ def process_image(img, effect):
60
  else:
61
  return img
62
 
63
- # Create the Gradio interface with an image input and a radio button to select the effect.
64
  iface = gr.Interface(
65
  fn=process_image,
66
  inputs=[
67
- gr.inputs.Image(type="pil", label="Input Image"),
68
- gr.inputs.Radio(["Gaussian Blur Background", "Depth-based Lens Blur"], label="Select Effect")
69
  ],
70
- outputs=gr.outputs.Image(type="pil", label="Output Image"),
71
  title="Blur Effects Demo",
72
  description="Upload an image and choose an effect to apply either a Gaussian Blur to the background or a Depth-based Lens Blur."
73
  )
 
5
 
6
  # Dummy segmentation function: replace with your actual segmentation model inference if available.
7
  def segment_foreground(img):
 
8
  np_img = np.array(img.convert("RGB"))
9
  h, w, _ = np_img.shape
 
10
  mask = np.zeros((h, w), dtype=np.uint8)
11
  center = (w // 2, h // 2)
12
  radius = min(center) - 10
 
17
  def gaussian_blur_background(img, sigma=15):
18
  mask = segment_foreground(img)
19
  np_img = np.array(img.convert("RGB"))
 
20
  blurred = cv2.GaussianBlur(np_img, (0, 0), sigma)
 
21
  mask_3d = np.stack([mask] * 3, axis=-1) / 255.0
 
22
  combined = np_img * mask_3d + blurred * (1 - mask_3d)
23
  return Image.fromarray(combined.astype(np.uint8))
24
 
 
26
  def estimate_depth(img):
27
  np_img = np.array(img.convert("RGB"))
28
  h, w, _ = np_img.shape
 
29
  depth = np.tile(np.linspace(0, 1, h)[:, None], (1, w))
30
  return depth
31
 
 
35
  np_img = np.array(img.convert("RGB"))
36
  output = np.zeros_like(np_img)
37
 
 
38
  depth_norm = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
39
 
 
40
  for i in range(np_img.shape[0]):
41
  sigma = max_sigma * depth_norm[i, 0]
42
  row = cv2.GaussianBlur(np_img[i:i+1, :, :], (0, 0), sigma)
43
  output[i, :, :] = row
44
  return Image.fromarray(output.astype(np.uint8))
45
 
 
46
  def process_image(img, effect):
47
  if effect == "Gaussian Blur Background":
48
  return gaussian_blur_background(img)
 
51
  else:
52
  return img
53
 
54
+ # Updated Gradio interface using the new API components.
55
  iface = gr.Interface(
56
  fn=process_image,
57
  inputs=[
58
+ gr.Image(type="pil", label="Input Image"),
59
+ gr.Radio(choices=["Gaussian Blur Background", "Depth-based Lens Blur"], label="Select Effect")
60
  ],
61
+ outputs=gr.Image(type="pil", label="Output Image"),
62
  title="Blur Effects Demo",
63
  description="Upload an image and choose an effect to apply either a Gaussian Blur to the background or a Depth-based Lens Blur."
64
  )