ArunLouis commited on
Commit
7fc5388
·
verified ·
1 Parent(s): 522777b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -65
app.py CHANGED
@@ -11,16 +11,7 @@ from transformers import pipeline
11
  pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf")
12
 
13
 
14
- def apply_depth_aware_blur(
15
- image,
16
- foreground_blur,
17
- midground_blur,
18
- background_blur,
19
- foreground_threshold,
20
- midground_lower,
21
- midground_upper,
22
- background_threshold,
23
- ):
24
  original_image = Image.fromarray(image).convert("RGB")
25
  original_image = original_image.resize((512, 512))
26
  image_np = np.array(original_image)
@@ -33,70 +24,36 @@ def apply_depth_aware_blur(
33
  # Normalize the depth map
34
  normalized_depth_map = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
35
 
36
- # Create masks
37
- foreground_mask = (normalized_depth_map < foreground_threshold).astype(np.uint8) * 255
38
- midground_mask = (
39
- (normalized_depth_map >= midground_lower)
40
- & (normalized_depth_map < midground_upper)
41
- ).astype(np.uint8) * 255
42
- background_mask = (normalized_depth_map >= background_threshold).astype(
43
- np.uint8
44
- ) * 255
45
-
46
  blurred_image = np.copy(np.array(original_image))
47
 
48
- # Apply blur, ensuring kernel size is valid
49
- if foreground_blur > 0 and foreground_blur % 2 == 1:
50
- blurred_image = np.where(
51
- (foreground_mask[..., None] == 255),
52
- cv2.GaussianBlur(blurred_image, (foreground_blur, foreground_blur), 10),
53
- blurred_image,
54
- )
55
- if midground_blur > 0 and midground_blur % 2 == 1:
56
- blurred_image = np.where(
57
- (midground_mask[..., None] == 255),
58
- cv2.GaussianBlur(blurred_image, (midground_blur, midground_blur), 8),
59
- blurred_image,
60
- )
61
- if background_blur > 0 and background_blur % 2 == 1:
62
- blurred_image = np.where(
63
- (background_mask[..., None] == 255),
64
- cv2.GaussianBlur(blurred_image, (background_blur, background_blur), 20),
65
- blurred_image,
66
- )
67
 
68
- return Image.fromarray(blurred_image.astype(np.uint8))
69
 
 
70
 
71
- # Example input values (including defaults)
72
- example_image = np.zeros((512, 512, 3), dtype=np.uint8) # Placeholder for an image
73
- example_inputs = [
74
- example_image,
75
- 15, # foreground_blur
76
- 7, # midground_blur
77
- 35, # background_blur (default)
78
- 0.2, # foreground_threshold (default)
79
- 0.2, # midground_lower (default)
80
- 0.6, # midground_upper (default)
81
- 0.6, # background_threshold (default)
82
- ]
83
 
84
  iface = gr.Interface(
85
  fn=apply_depth_aware_blur,
86
- inputs=[
87
- gr.Image(label="Input Image"),
88
- gr.Slider(1, 51, step=2, label="Foreground Blur Kernel Size"),
89
- gr.Slider(1, 51, step=2, label="Midground Blur Kernel Size"),
90
- gr.Slider(1, 51, step=2, label="Background Blur Kernel Size"),
91
- gr.Slider(0, 1, label="Foreground Threshold"),
92
- gr.Slider(0, 1, label="Midground Lower Threshold"),
93
- gr.Slider(0, 1, label="Midground Upper Threshold"),
94
- gr.Slider(0, 1, label="Background Threshold"),
95
- ],
96
  outputs=gr.Image(label="Blurred Image"),
97
- title="Depth-Aware Lens Blur App",
98
- description="Apply depth-based blur to uploaded images using Depth Anything V2. Adjust blur intensity for foreground, midground, and background.",
99
- examples=[example_inputs], # Provide example inputs
100
  )
101
 
102
  if __name__ == "__main__":
 
11
  pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf")
12
 
13
 
14
+ def apply_depth_aware_blur(image): # Removed blur parameters
 
 
 
 
 
 
 
 
 
15
  original_image = Image.fromarray(image).convert("RGB")
16
  original_image = original_image.resize((512, 512))
17
  image_np = np.array(original_image)
 
24
  # Normalize the depth map
25
  normalized_depth_map = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
26
 
 
 
 
 
 
 
 
 
 
 
27
  blurred_image = np.copy(np.array(original_image))
28
 
29
+ # Calculate blur kernel size based on depth
30
+ max_kernel_size = 35 # Maximum blur kernel size
31
+ for y in range(512):
32
+ for x in range(512):
33
+ blur_amount = normalized_depth_map[y, x]
34
+ kernel_size = int(blur_amount * max_kernel_size)
35
+ # Ensure kernel size is odd and at least 1
36
+ kernel_size = max(1, kernel_size)
37
+ if kernel_size % 2 == 0:
38
+ kernel_size += 1
39
+
40
+ if kernel_size > 1 and kernel_size <= max_kernel_size:
41
+ blurred_image[y:y + 1, x:x + 1] = cv2.GaussianBlur(
42
+ np.array(original_image)[y:y + 1, x:x + 1], (kernel_size, kernel_size), 10
43
+ )
44
+ else:
45
+ blurred_image[y:y + 1, x:x + 1] = np.array(original_image)[y:y + 1, x:x + 1] #Keep original pixel
 
 
46
 
 
47
 
48
+ return Image.fromarray(blurred_image.astype(np.uint8))
49
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  iface = gr.Interface(
52
  fn=apply_depth_aware_blur,
53
+ inputs=gr.Image(label="Input Image"),
 
 
 
 
 
 
 
 
 
54
  outputs=gr.Image(label="Blurred Image"),
55
+ title="Depth-Proportional Lens Blur App",
56
+ description="Apply blur to an image where the blur intensity is proportional to the estimated depth. Farther objects are more blurred, closer objects are sharper.",
 
57
  )
58
 
59
  if __name__ == "__main__":