nickkun commited on
Commit
e815a83
·
verified ·
1 Parent(s): 606bdc0

Update app.py

Browse files

Added Code to the application

Files changed (1) hide show
  1. app.py +51 -13
app.py CHANGED
@@ -1,21 +1,59 @@
1
  import gradio as gr
 
 
 
2
 
3
- def process_image(image):
4
- # Perform segmentation and apply Gaussian blur (steps above)
5
- # Return output images for display
6
-
7
- segmented_output = ... # Segmented output with blurred background
8
- depth_map_output = ... # Depth map visualization
9
- variable_blur_output = ... # Variable Gaussian blur
10
-
11
- return segmented_output, depth_map_output, variable_blur_output
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  app = gr.Interface(
14
  fn=process_image,
15
- inputs=gr.Image(type="pil"),
16
- outputs=[gr.Image(type="pil"), gr.Image(type="pil"), gr.Image(type="pil")],
17
- title="Vision Transformer Segmentation & Depth Estimation",
18
- description="Upload an image to apply segmentation and lens blur effects."
 
 
 
 
 
 
 
 
19
  )
20
 
21
  app.launch()
 
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
 
59
  app.launch()