Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -136,21 +136,56 @@ def process_image(input_image: Image.Image, fg_threshold: float, mg_threshold: f
|
|
136 |
# mask_bg_img
|
137 |
)
|
138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
title = "Blur Effects: Gaussian & Depth-Based Lens Blur with Adjustable Depth Thresholds"
|
140 |
description = (
|
141 |
-
"
|
142 |
"1. A segmentation-based Gaussian blur that blurs the background (using RMBG-2.0).\n"
|
143 |
"2. A depth-based lens blur effect that simulates realistic lens blur based on depth (using DepthPro).\n\n"
|
144 |
-
"
|
145 |
)
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
demo = gr.Interface(
|
148 |
fn=process_image,
|
149 |
-
inputs=[
|
150 |
-
gr.Image(type="pil", label="Input Image", value="https://i.ibb.co/fznz2b2b/hw3-q2.jpg"),
|
151 |
-
gr.Slider(minimum=0, maximum=1, step=0.01, value=0.33, label="Foreground Depth Threshold"),
|
152 |
-
gr.Slider(minimum=0, maximum=1, step=0.01, value=0.66, label="Middleground Depth Threshold")
|
153 |
-
],
|
154 |
outputs=[
|
155 |
gr.Image(type="pil", label="Segmentation-Based Blur"),
|
156 |
gr.Image(type="pil", label="Depth Map"),
|
@@ -163,3 +198,31 @@ demo = gr.Interface(
|
|
163 |
|
164 |
if __name__ == "__main__":
|
165 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
# mask_bg_img
|
137 |
)
|
138 |
|
139 |
+
def update_preset(preset: str):
|
140 |
+
presets = {
|
141 |
+
"Preset 1": {
|
142 |
+
"image_url": "https://i.ibb.co/fznz2b2b/hw3-q2.jpg",
|
143 |
+
"fg_threshold": 0.33,
|
144 |
+
"mg_threshold": 0.66
|
145 |
+
},
|
146 |
+
"Preset 2": {
|
147 |
+
"image_url": "https://i.ibb.co/HLZGW7qH/q26.jpg",
|
148 |
+
"fg_threshold": 0.2,
|
149 |
+
"mg_threshold": 0.66
|
150 |
+
}
|
151 |
+
}
|
152 |
+
preset_info = presets[preset]
|
153 |
+
response = requests.get(preset_info["image_url"])
|
154 |
+
image = Image.open(BytesIO(response.content)).convert("RGB")
|
155 |
+
return image, preset_info["fg_threshold"], preset_info["mg_threshold"]
|
156 |
+
|
157 |
+
# -----------------------------
|
158 |
+
# Gradio Interface Setup
|
159 |
+
# -----------------------------
|
160 |
title = "Blur Effects: Gaussian & Depth-Based Lens Blur with Adjustable Depth Thresholds"
|
161 |
description = (
|
162 |
+
"Choose a preset image or upload your own image to apply two distinct effects:\n\n"
|
163 |
"1. A segmentation-based Gaussian blur that blurs the background (using RMBG-2.0).\n"
|
164 |
"2. A depth-based lens blur effect that simulates realistic lens blur based on depth (using DepthPro).\n\n"
|
165 |
+
"The preset selection will automatically set the image and its default foreground/middleground depth thresholds."
|
166 |
)
|
167 |
|
168 |
+
# A radio to select between two preset images
|
169 |
+
preset_radio = gr.Radio(
|
170 |
+
choices=["Preset 1", "Preset 2"],
|
171 |
+
label="Select Preset Image",
|
172 |
+
value="Preset 1"
|
173 |
+
)
|
174 |
+
|
175 |
+
# An image component that will be updated based on the preset selection
|
176 |
+
preset_img = gr.Image(type="pil", label="Input Image")
|
177 |
+
|
178 |
+
# Slider components for depth thresholds (will be updated when a preset is selected)
|
179 |
+
fg_slider = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.33, label="Foreground Depth Threshold")
|
180 |
+
mg_slider = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.66, label="Middleground Depth Threshold")
|
181 |
+
|
182 |
+
# When the preset is changed, update the image and sliders with corresponding defaults.
|
183 |
+
preset_radio.change(fn=update_preset, inputs=preset_radio, outputs=[preset_img, fg_slider, mg_slider])
|
184 |
+
|
185 |
+
# Build the Gradio interface using the updated components.
|
186 |
demo = gr.Interface(
|
187 |
fn=process_image,
|
188 |
+
inputs=[preset_img, fg_slider, mg_slider],
|
|
|
|
|
|
|
|
|
189 |
outputs=[
|
190 |
gr.Image(type="pil", label="Segmentation-Based Blur"),
|
191 |
gr.Image(type="pil", label="Depth Map"),
|
|
|
198 |
|
199 |
if __name__ == "__main__":
|
200 |
demo.launch()
|
201 |
+
|
202 |
+
# title = "Blur Effects: Gaussian & Depth-Based Lens Blur with Adjustable Depth Thresholds"
|
203 |
+
# description = (
|
204 |
+
# "Upload an image to apply two distinct effects:\n\n"
|
205 |
+
# "1. A segmentation-based Gaussian blur that blurs the background (using RMBG-2.0).\n"
|
206 |
+
# "2. A depth-based lens blur effect that simulates realistic lens blur based on depth (using DepthPro).\n\n"
|
207 |
+
# "Use the sliders to adjust the foreground and middleground depth thresholds."
|
208 |
+
# )
|
209 |
+
|
210 |
+
# demo = gr.Interface(
|
211 |
+
# fn=process_image,
|
212 |
+
# inputs=[
|
213 |
+
# gr.Image(type="pil", label="Input Image", value="https://i.ibb.co/fznz2b2b/hw3-q2.jpg"),
|
214 |
+
# gr.Slider(minimum=0, maximum=1, step=0.01, value=0.33, label="Foreground Depth Threshold"),
|
215 |
+
# gr.Slider(minimum=0, maximum=1, step=0.01, value=0.66, label="Middleground Depth Threshold")
|
216 |
+
# ],
|
217 |
+
# outputs=[
|
218 |
+
# gr.Image(type="pil", label="Segmentation-Based Blur"),
|
219 |
+
# gr.Image(type="pil", label="Depth Map"),
|
220 |
+
# gr.Image(type="pil", label="Depth-Based Lens Blur")
|
221 |
+
# ],
|
222 |
+
# title=title,
|
223 |
+
# description=description,
|
224 |
+
# allow_flagging="never"
|
225 |
+
# )
|
226 |
+
|
227 |
+
# if __name__ == "__main__":
|
228 |
+
# demo.launch()
|