Blur-Anything / app.py
David-Chew-HL's picture
Create app.py
7fd213f
raw
history blame
1.06 kB
import gradio as gr
from transformers import pipeline
from PIL import Image, ImageFilter
import numpy as np
pipe = pipeline(task="depth-estimation", model="LiheYoung/depth-anything-small-hf")
def blur_img(img, blur):
depth = pipe(img)["depth"]
depth_array = np.array(depth) / 255.0
blurred_image_pil = img.filter(ImageFilter.GaussianBlur(blur))
blurred_image_array = np.array(blurred_image_pil)
mask_array = np.expand_dims(depth_array, axis=-1)
result_array = np.uint8(np.array(img) * mask_array + blurred_image_array * (1 - mask_array))
result_pil = Image.fromarray(result_array)
return result_pil
#interface
inputs = [
gr.inputs.Image(type="pil", label="Input Image"),
gr.inputs.Slider(minimum=0, maximum=10, default=5, label="Blur Amount", step=1)
]
output = gr.outputs.Image(type="pil", label="Blurred Image")
gr.Interface(fn=blur_img, inputs=inputs, outputs=output, title="Depth-Based Image Blurring",
description="Upload an image and adjust the blur amount using the slider.").launch()