SOSSY commited on
Commit
c580a28
·
verified ·
1 Parent(s): 77b1339

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -112
app.py CHANGED
@@ -2,45 +2,11 @@ import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image, ImageFilter, ImageOps
4
  import numpy as np
5
- import requests
6
  import cv2
7
 
8
- # Dictionary of available segmentation models
9
- SEGMENTATION_MODELS = {
10
- "NVIDIA SegFormer (Cityscapes)": "nvidia/segformer-b1-finetuned-cityscapes-1024-1024",
11
- "NVIDIA SegFormer (ADE20K)": "nvidia/segformer-b0-finetuned-ade-512-512",
12
- "Facebook MaskFormer (COCO)": "facebook/maskformer-swin-base-ade",
13
- "OneFormer (COCO)": "shi-labs/oneformer_coco_swin_large",
14
- "NVIDIA SegFormer (B5)": "nvidia/segformer-b5-finetuned-cityscapes-1024-1024"
15
- }
16
-
17
- # Dictionary of available depth estimation models
18
- DEPTH_MODELS = {
19
- "Intel ZoeDepth (NYU-KITTI)": "Intel/zoedepth-nyu-kitti",
20
- "DPT (Large)": "Intel/dpt-large",
21
- "DPT (Hybrid)": "Intel/dpt-hybrid-midas",
22
- "GLPDepth": "vinvino02/glpn-nyu"
23
- }
24
-
25
- # Initialize model placeholders
26
- segmentation_model = None
27
- depth_estimator = None
28
-
29
- def load_segmentation_model(model_name):
30
- """Load the selected segmentation model"""
31
- global segmentation_model
32
- model_path = SEGMENTATION_MODELS[model_name]
33
- print(f"Loading segmentation model: {model_path}...")
34
- segmentation_model = pipeline("image-segmentation", model=model_path)
35
- return f"Loaded segmentation model: {model_name}"
36
-
37
- def load_depth_model(model_name):
38
- """Load the selected depth estimation model"""
39
- global depth_estimator
40
- model_path = DEPTH_MODELS[model_name]
41
- print(f"Loading depth estimation model: {model_path}...")
42
- depth_estimator = pipeline("depth-estimation", model=model_path)
43
- return f"Loaded depth model: {model_name}"
44
 
45
  def lens_blur(image, radius):
46
  """
@@ -98,10 +64,6 @@ def process_image(input_image, method, blur_intensity, blur_type):
98
  - output_image: final composited image.
99
  - mask_image: the mask used (binary for segmentation, normalized depth for depth-based).
100
  """
101
- # Check if models are loaded
102
- if segmentation_model is None or depth_estimator is None:
103
- return input_image, input_image.convert("L")
104
-
105
  # Ensure image is in RGB mode
106
  input_image = input_image.convert("RGB")
107
 
@@ -114,24 +76,24 @@ def process_image(input_image, method, blur_intensity, blur_type):
114
  blur_fn = lambda img, rad: img.filter(ImageFilter.GaussianBlur(radius=rad))
115
 
116
  if method == "Segmented Background Blur":
117
- # Use segmentation to obtain a foreground mask.
118
  results = segmentation_model(input_image)
119
- # Assume the last result is the main foreground object.
120
  foreground_mask = results[-1]["mask"]
121
- # Ensure the mask is grayscale.
122
  foreground_mask = foreground_mask.convert("L")
123
- # Threshold to create a binary mask.
124
  binary_mask = foreground_mask.point(lambda p: 255 if p > 128 else 0)
125
 
126
- # Blur the background using the selected blur function.
127
  blurred_background = blur_fn(input_image, blur_intensity)
128
 
129
- # Composite the final image: keep foreground and use blurred background elsewhere.
130
  output_image = Image.composite(input_image, blurred_background, binary_mask)
131
  mask_image = binary_mask
132
 
133
  elif method == "Depth-based Variable Blur":
134
- # Generate depth map.
135
  depth_results = depth_estimator(input_image)
136
  depth_map = depth_results["depth"]
137
 
@@ -141,16 +103,16 @@ def process_image(input_image, method, blur_intensity, blur_type):
141
  normalized_depth = (norm * 255).astype(np.uint8)
142
  mask_image = Image.fromarray(normalized_depth)
143
 
144
- # Create fully blurred version using the selected blur function.
145
  blurred_image = blur_fn(input_image, blur_intensity)
146
 
147
- # Convert images to arrays for blending.
148
  orig_np = np.array(input_image).astype(np.float32)
149
  blur_np = np.array(blurred_image).astype(np.float32)
150
- # Reshape mask for broadcasting.
151
  alpha = normalized_depth[..., np.newaxis] / 255.0
152
 
153
- # Blend pixels: 0 = original; 1 = fully blurred.
154
  blended_np = (1 - alpha) * orig_np + alpha * blur_np
155
  blended_np = np.clip(blended_np, 0, 255).astype(np.uint8)
156
  output_image = Image.fromarray(blended_np)
@@ -165,71 +127,28 @@ def process_image(input_image, method, blur_intensity, blur_type):
165
  with gr.Blocks() as demo:
166
  gr.Markdown("## Image Processing App: Segmentation & Depth-based Blur")
167
 
168
- with gr.Tab("Model Selection"):
169
- with gr.Row():
170
- with gr.Column():
171
- seg_model_dropdown = gr.Dropdown(
172
- label="Segmentation Model",
173
- choices=list(SEGMENTATION_MODELS.keys()),
174
- value=list(SEGMENTATION_MODELS.keys())[0]
175
- )
176
- seg_model_load_btn = gr.Button("Load Segmentation Model")
177
- seg_model_status = gr.Textbox(label="Status", value="No model loaded")
178
-
179
- with gr.Column():
180
- depth_model_dropdown = gr.Dropdown(
181
- label="Depth Estimation Model",
182
- choices=list(DEPTH_MODELS.keys()),
183
- value=list(DEPTH_MODELS.keys())[0]
184
- )
185
- depth_model_load_btn = gr.Button("Load Depth Model")
186
- depth_model_status = gr.Textbox(label="Status", value="No model loaded")
187
-
188
- with gr.Tab("Image Processing"):
189
- with gr.Row():
190
- with gr.Column():
191
- input_image = gr.Image(label="Input Image", type="pil")
192
- method = gr.Radio(label="Processing Method",
193
- choices=["Segmented Background Blur", "Depth-based Variable Blur"],
194
- value="Segmented Background Blur")
195
- blur_intensity = gr.Slider(label="Blur Intensity (Maximum Blur Radius)",
196
- minimum=1, maximum=30, step=1, value=15)
197
- blur_type = gr.Dropdown(label="Blur Type",
198
- choices=["Gaussian Blur", "Lens Blur"],
199
- value="Gaussian Blur")
200
- run_button = gr.Button("Process Image")
201
- with gr.Column():
202
- output_image = gr.Image(label="Output Image")
203
- mask_output = gr.Image(label="Mask")
204
-
205
- # Set up event handlers
206
- seg_model_load_btn.click(
207
- fn=load_segmentation_model,
208
- inputs=[seg_model_dropdown],
209
- outputs=[seg_model_status]
210
- )
211
-
212
- depth_model_load_btn.click(
213
- fn=load_depth_model,
214
- inputs=[depth_model_dropdown],
215
- outputs=[depth_model_status]
216
- )
217
-
218
  run_button.click(
219
  fn=process_image,
220
  inputs=[input_image, method, blur_intensity, blur_type],
221
  outputs=[output_image, mask_output]
222
  )
223
 
224
- # Load default models on startup
225
- demo.load(
226
- fn=lambda: (
227
- load_segmentation_model(list(SEGMENTATION_MODELS.keys())[0]),
228
- load_depth_model(list(DEPTH_MODELS.keys())[0])
229
- ),
230
- inputs=None,
231
- outputs=[seg_model_status, depth_model_status]
232
- )
233
-
234
  # Launch the app
235
  demo.launch()
 
2
  from transformers import pipeline
3
  from PIL import Image, ImageFilter, ImageOps
4
  import numpy as np
 
5
  import cv2
6
 
7
+ # Initialize models with fixed choices
8
+ segmentation_model = pipeline("image-segmentation", model="nvidia/segformer-b1-finetuned-cityscapes-1024-1024")
9
+ depth_estimator = pipeline("depth-estimation", model="Intel/zoedepth-nyu-kitti")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def lens_blur(image, radius):
12
  """
 
64
  - output_image: final composited image.
65
  - mask_image: the mask used (binary for segmentation, normalized depth for depth-based).
66
  """
 
 
 
 
67
  # Ensure image is in RGB mode
68
  input_image = input_image.convert("RGB")
69
 
 
76
  blur_fn = lambda img, rad: img.filter(ImageFilter.GaussianBlur(radius=rad))
77
 
78
  if method == "Segmented Background Blur":
79
+ # Use segmentation to obtain a foreground mask
80
  results = segmentation_model(input_image)
81
+ # Assume the last result is the main foreground object
82
  foreground_mask = results[-1]["mask"]
83
+ # Ensure the mask is grayscale
84
  foreground_mask = foreground_mask.convert("L")
85
+ # Threshold to create a binary mask
86
  binary_mask = foreground_mask.point(lambda p: 255 if p > 128 else 0)
87
 
88
+ # Blur the background using the selected blur function
89
  blurred_background = blur_fn(input_image, blur_intensity)
90
 
91
+ # Composite the final image: keep foreground and use blurred background elsewhere
92
  output_image = Image.composite(input_image, blurred_background, binary_mask)
93
  mask_image = binary_mask
94
 
95
  elif method == "Depth-based Variable Blur":
96
+ # Generate depth map
97
  depth_results = depth_estimator(input_image)
98
  depth_map = depth_results["depth"]
99
 
 
103
  normalized_depth = (norm * 255).astype(np.uint8)
104
  mask_image = Image.fromarray(normalized_depth)
105
 
106
+ # Create fully blurred version using the selected blur function
107
  blurred_image = blur_fn(input_image, blur_intensity)
108
 
109
+ # Convert images to arrays for blending
110
  orig_np = np.array(input_image).astype(np.float32)
111
  blur_np = np.array(blurred_image).astype(np.float32)
112
+ # Reshape mask for broadcasting
113
  alpha = normalized_depth[..., np.newaxis] / 255.0
114
 
115
+ # Blend pixels: 0 = original; 1 = fully blurred
116
  blended_np = (1 - alpha) * orig_np + alpha * blur_np
117
  blended_np = np.clip(blended_np, 0, 255).astype(np.uint8)
118
  output_image = Image.fromarray(blended_np)
 
127
  with gr.Blocks() as demo:
128
  gr.Markdown("## Image Processing App: Segmentation & Depth-based Blur")
129
 
130
+ with gr.Row():
131
+ with gr.Column():
132
+ input_image = gr.Image(label="Input Image", type="pil")
133
+ method = gr.Radio(label="Processing Method",
134
+ choices=["Segmented Background Blur", "Depth-based Variable Blur"],
135
+ value="Segmented Background Blur")
136
+ blur_intensity = gr.Slider(label="Blur Intensity (Maximum Blur Radius)",
137
+ minimum=1, maximum=30, step=1, value=15)
138
+ blur_type = gr.Dropdown(label="Blur Type",
139
+ choices=["Gaussian Blur", "Lens Blur"],
140
+ value="Gaussian Blur")
141
+ run_button = gr.Button("Process Image")
142
+ with gr.Column():
143
+ output_image = gr.Image(label="Output Image")
144
+ mask_output = gr.Image(label="Mask")
145
+
146
+ # Set up event handler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  run_button.click(
148
  fn=process_image,
149
  inputs=[input_image, method, blur_intensity, blur_type],
150
  outputs=[output_image, mask_output]
151
  )
152
 
 
 
 
 
 
 
 
 
 
 
153
  # Launch the app
154
  demo.launch()