Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
# created with great guidance from https://github.com/NimaBoscarino | |
import gradio as gr | |
import kornia as K | |
from kornia.core import Tensor | |
# Define Functions | |
def box_blur_fn(file, box_blur): | |
# load the image using the rust backend | |
img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32) | |
img = img[None] # 1xCxHxW / fp32 / [0, 1] | |
x_out: Tensor = K.filters.box_blur(img, (int(box_blur), int(box_blur))) | |
return K.utils.tensor_to_image(x_out) | |
def blur_pool2d_fn(file, blur_pool2d): | |
# load the image using the rust backend | |
img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32) | |
img = img[None] # 1xCxHxW / fp32 / [0, 1] | |
x_out: Tensor = K.filters.blur_pool2d(img, int(blur_pool2d)) | |
return K.utils.tensor_to_image(x_out) | |
def gaussian_blur_fn(file, gaussian_blur2d): | |
# load the image using the rust backend | |
img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32) | |
img = img[None] # 1xCxHxW / fp32 / [0, 1] | |
x_out: Tensor = K.filters.gaussian_blur2d(img, | |
(int(gaussian_blur2d), int(gaussian_blur2d)), | |
(float(gaussian_blur2d), float(gaussian_blur2d))) | |
return K.utils.tensor_to_image(x_out) | |
def max_blur_pool2d_fn(file, max_blur_pool2d): | |
# load the image using the rust backend | |
img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32) | |
img = img[None] # 1xCxHxW / fp32 / [0, 1] | |
x_out: Tensor = K.filters.max_blur_pool2d(img, int(max_blur_pool2d)) | |
return K.utils.tensor_to_image(x_out) | |
def median_blur_fn(file, median_blur): | |
# load the image using the rust backend | |
img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32) | |
img = img[None] # 1xCxHxW / fp32 / [0, 1] | |
x_out: Tensor = K.filters.median_blur(img, (int(median_blur), int(median_blur))) | |
return K.utils.tensor_to_image(x_out) | |
# Define Examples | |
examples = [ | |
["examples/monkey.jpg", 1, 1, 1, 1, 1], | |
["examples/pikachu.jpg", 1, 1, 1, 1, 1] | |
] | |
# Define Demos | |
box_blur_demo = gr.Interface( | |
box_blur_fn, | |
[ | |
gr.inputs.Image(type="file"), | |
gr.inputs.Slider(minimum=1, maximum=20, step=1, default=10, label="Box Blur") | |
], | |
"image", | |
examples=examples, | |
# title=title, | |
# description=description, | |
# article=article, | |
# live=True # --> 2022.09.08: Bug with click and dragging button. See: https://huggingface.co/spaces/kornia/kornia-image-filtering/discussions/2#63206d3b54a96c8e65c1fed2 | |
) | |
blur_pool2d_demo = gr.Interface( | |
blur_pool2d_fn, | |
[ | |
gr.inputs.Image(type="file"), | |
gr.inputs.Slider(minimum=1, maximum=40, step=1, default=20, label="Blur Pool") | |
], | |
"image", | |
examples=examples, | |
# title=title, | |
# description=description, | |
# article=article, | |
# live=True # --> 2022.09.08: Bug with click and dragging button. See: https://huggingface.co/spaces/kornia/kornia-image-filtering/discussions/2#63206d3b54a96c8e65c1fed2 | |
) | |
gaussian_blur_demo = gr.Interface( | |
gaussian_blur_fn, | |
[ | |
gr.inputs.Image(type="file"), | |
gr.inputs.Slider(minimum=1, maximum=30, step=2, default=15, label="Gaussian Blur") | |
], | |
"image", | |
examples=examples, | |
# title=title, | |
# description=description, | |
# article=article, | |
# live=True # --> 2022.09.08: Bug with click and dragging button. See: https://huggingface.co/spaces/kornia/kornia-image-filtering/discussions/2#63206d3b54a96c8e65c1fed2 | |
) | |
max_blur_pool2d_demo = gr.Interface( | |
max_blur_pool2d_fn, | |
[ | |
gr.inputs.Image(type="file"), | |
gr.inputs.Slider(minimum=1, maximum=40, step=1, default=20, label="Max Pool") | |
], | |
"image", | |
examples=examples, | |
# title=title, | |
# description=description, | |
# article=article, | |
# live=True # --> 2022.09.08: Bug with click and dragging button. See: https://huggingface.co/spaces/kornia/kornia-image-filtering/discussions/2#63206d3b54a96c8e65c1fed2 | |
) | |
median_blur_demo = gr.Interface( | |
median_blur_fn, | |
[ | |
gr.inputs.Image(type="file"), | |
gr.inputs.Slider(minimum=1, maximum=30, step=2, default=15, label="Median Blur") | |
], | |
"image", | |
examples=examples, | |
# title=title, | |
# description=description, | |
# article=article, | |
# live=True # --> 2022.09.08: Bug with click and dragging button. See: https://huggingface.co/spaces/kornia/kornia-image-filtering/discussions/2#63206d3b54a96c8e65c1fed2 | |
) | |
# Create Interface | |
demo = gr.TabbedInterface( | |
[ | |
box_blur_demo, | |
blur_pool2d_demo, | |
gaussian_blur_demo, | |
max_blur_pool2d_demo, | |
median_blur_demo | |
], | |
[ | |
"Box Blur", | |
"Blur Pool", | |
"Gaussian Blur", | |
"Max Pool", | |
"Median Blur" | |
] | |
) | |
demo.launch() |