SOSSY commited on
Commit
841589e
·
verified ·
1 Parent(s): 0a0e123

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -46
app.py CHANGED
@@ -1,58 +1,88 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from PIL import Image, ImageFilter
4
  import numpy as np
 
 
5
 
6
- # Load models from Hugging Face
7
- segmentation_model = pipeline("image-segmentation", model="nvidia/segformer-b1-finetuned-cityscapes-1024-1024")
8
  depth_estimator = pipeline("depth-estimation", model="Intel/zoedepth-nyu-kitti")
9
 
10
- def process_image(image, blur_type, sigma):
11
- # Step 1: Perform segmentation
12
- segmentation_results = segmentation_model(image)
13
- foreground_mask = segmentation_results[-1]["mask"]
14
 
15
- # Step 2: Apply Gaussian blur to background
16
- blurred_background = image.filter(ImageFilter.GaussianBlur(sigma))
17
- segmented_output = Image.composite(image, blurred_background, foreground_mask)
18
-
19
- # Step 3: Perform depth estimation
20
- depth_results = depth_estimator(image)
21
  depth_map = depth_results["depth"]
22
 
23
- # Step 4: Normalize depth map values
24
  depth_array = np.array(depth_map)
25
- normalized_depth = (depth_array - np.min(depth_array)) / (np.max(depth_array) - np.min(depth_array)) * 255
26
- normalized_depth_image = Image.fromarray(normalized_depth.astype('uint8'))
27
-
28
- # Step 5: Apply variable Gaussian blur based on depth map (Lens Blur)
29
- if blur_type == "Lens Blur":
30
- variable_blur_image = image.copy()
31
- for x in range(variable_blur_image.width):
32
- for y in range(variable_blur_image.height):
33
- blur_intensity = normalized_depth[y, x] / 255 * sigma # Scale blur intensity by depth value
34
- pixel_value = image.getpixel((x, y))
35
- variable_blur_image.putpixel((x, y), tuple(int(p * blur_intensity) for p in pixel_value))
36
- output_image = variable_blur_image
37
- else:
38
- output_image = segmented_output
39
-
40
- return segmented_output, normalized_depth_image, output_image
41
-
42
- # Create Gradio interface
43
- app = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  fn=process_image,
45
- inputs=[
46
- gr.Image(type="pil", label="Upload Image"),
47
- gr.Radio(["Gaussian Blur", "Lens Blur"], label="Blur Type", value="Gaussian Blur"),
48
- gr.Slider(0, 50, step=1, label="Blur Intensity (Sigma)", value=15)
49
- ],
50
- outputs=[
51
- gr.Image(type="pil", label="Segmented Output with Background Blur"),
52
- gr.Image(type="pil", label="Depth Map Visualization"),
53
- gr.Image(type="pil", label="Final Output with Selected Blur")
54
- ],
55
- title="Vision Transformer Segmentation & Depth-Based Blur Effects",
56
- description="Upload an image and select the type of blur effect (Gaussian or Lens). Adjust the blur intensity using the slider."
57
  )
58
- app.launch()
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from PIL import Image
4
  import numpy as np
5
+ from scipy.ndimage import gaussian_filter
6
+ import matplotlib.pyplot as plt
7
 
8
+ # Load the depth estimation model
 
9
  depth_estimator = pipeline("depth-estimation", model="Intel/zoedepth-nyu-kitti")
10
 
11
+ def process_image(input_image):
12
+ # Convert Gradio input (numpy array) to PIL Image
13
+ input_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
 
14
 
15
+ # Perform depth estimation
16
+ depth_results = depth_estimator(input_image)
 
 
 
 
17
  depth_map = depth_results["depth"]
18
 
19
+ # Convert depth map to numpy array and normalize to [0, 1]
20
  depth_array = np.array(depth_map)
21
+ normalized_depth = (depth_array - np.min(depth_array)) / (np.max(depth_array) - np.min(depth_array))
22
+
23
+ # Convert input image to numpy array
24
+ img_array = np.array(input_image)
25
+
26
+ # Create variable blur effect
27
+ max_blur = 5.0 # Maximum blur radius
28
+ min_blur = 0.5 # Minimum blur radius to avoid completely sharp areas
29
+ n_steps = 10 # Number of blur levels
30
+
31
+ # Create output array
32
+ blurred_array = np.zeros_like(img_array, dtype=np.float32)
33
+
34
+ # Apply variable blur by processing the image with multiple blur levels
35
+ for i in range(n_steps):
36
+ sigma = min_blur + (max_blur - min_blur) * i / (n_steps - 1)
37
+ # Apply Gaussian blur with current sigma to the whole image
38
+ blurred_r = gaussian_filter(img_array[:,:,0], sigma=sigma)
39
+ blurred_g = gaussian_filter(img_array[:,:,1], sigma=sigma)
40
+ blurred_b = gaussian_filter(img_array[:,:,2], sigma=sigma)
41
+ blurred_temp = np.stack([blurred_r, blurred_g, blurred_b], axis=2)
42
+
43
+ # Create a mask for this blur level
44
+ lower_bound = i / n_steps
45
+ upper_bound = (i + 1) / n_steps
46
+ mask = (normalized_depth >= lower_bound) & (normalized_depth < upper_bound)
47
+ mask = mask[..., np.newaxis] # Add channel dimension
48
+
49
+ # Apply this blur level to the appropriate regions
50
+ blurred_array = np.where(mask, blurred_temp, blurred_array)
51
+
52
+ # Convert back to uint8
53
+ blurred_image = Image.fromarray(blurred_array.astype('uint8'))
54
+
55
+ # Create side-by-side visualization
56
+ fig, axes = plt.subplots(1, 3, figsize=(15, 5))
57
+ axes[0].imshow(input_image)
58
+ axes[0].set_title("Input Image")
59
+ axes[0].axis("off")
60
+
61
+ axes[1].imshow(depth_map, cmap="gray")
62
+ axes[1].set_title("Depth Map")
63
+ axes[1].axis("off")
64
+
65
+ axes[2].imshow(blurred_image)
66
+ axes[2].set_title("Variable Blur Output")
67
+ axes[2].axis("off")
68
+
69
+ plt.tight_layout()
70
+
71
+ # Save the figure to a temporary file or buffer to display in Gradio
72
+ output_path = "output.png"
73
+ plt.savefig(output_path, bbox_inches='tight')
74
+ plt.close()
75
+
76
+ return output_path
77
+
78
+ # Define Gradio interface
79
+ interface = gr.Interface(
80
  fn=process_image,
81
+ inputs=gr.Image(label="Upload an Image"),
82
+ outputs=gr.Image(label="Processed Output"),
83
+ title="Depth-Based Variable Blur App",
84
+ description="Upload an image to apply a variable blur effect based on depth estimation."
 
 
 
 
 
 
 
 
85
  )
86
+
87
+ # Launch the app
88
+ interface.launch()