Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,180 Bytes
5825d5b 2006f30 5825d5b 346da5d 2006f30 e3c3f88 2006f30 3caf593 2006f30 8efad9d 2006f30 8efad9d 2006f30 8efad9d 2006f30 8efad9d 346da5d 2006f30 346da5d 2006f30 346da5d 2006f30 346da5d 2006f30 5825d5b 346da5d 5825d5b 2006f30 5825d5b 346da5d 2a39662 5825d5b 2006f30 12441f2 e3c3f88 346da5d 2a39662 e3c3f88 2006f30 12441f2 5825d5b 346da5d 8efad9d 2006f30 12441f2 8efad9d 346da5d 2006f30 12441f2 346da5d 2006f30 12441f2 346da5d e3c3f88 2006f30 e3c3f88 2006f30 e3c3f88 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import gradio as gr
import kornia as K
from kornia.core import Tensor
import torch
import numpy as np
# Define Functions
def process_image(file):
if isinstance(file, np.ndarray):
# If the input is already a numpy array, convert it to a tensor
img = K.image_to_tensor(file).float() / 255.0
else:
# If it's a file path, load it using kornia
img = K.io.load_image(file, K.io.ImageLoadType.RGB32)
return img.unsqueeze(0) # Add batch dimension: 1xCxHxW
def box_blur_fn(file, box_blur):
img = process_image(file)
x_out: Tensor = K.filters.box_blur(img, (int(box_blur), int(box_blur)))
return K.utils.tensor_to_image(x_out.squeeze())
def blur_pool2d_fn(file, blur_pool2d):
img = process_image(file)
x_out: Tensor = K.filters.blur_pool2d(img, int(blur_pool2d))
return K.utils.tensor_to_image(x_out.squeeze())
def gaussian_blur_fn(file, gaussian_blur2d):
img = process_image(file)
x_out: Tensor = K.filters.gaussian_blur2d(img,
(int(gaussian_blur2d), int(gaussian_blur2d)),
(float(gaussian_blur2d)/2, float(gaussian_blur2d)/2))
return K.utils.tensor_to_image(x_out.squeeze())
def max_blur_pool2d_fn(file, max_blur_pool2d):
img = process_image(file)
x_out: Tensor = K.filters.max_blur_pool2d(img, int(max_blur_pool2d))
return K.utils.tensor_to_image(x_out.squeeze())
def median_blur_fn(file, median_blur):
img = process_image(file)
x_out: Tensor = K.filters.median_blur(img, (int(median_blur), int(median_blur)))
return K.utils.tensor_to_image(x_out.squeeze())
# Define Examples
examples = [
["examples/monkey.jpg", 1],
["examples/pikachu.jpg", 1]
]
# Define Demos
box_blur_demo = gr.Interface(
box_blur_fn,
[
gr.Image(type="numpy"),
gr.Slider(minimum=1, maximum=20, step=1, value=10, label="Box Blur")
],
"image",
examples=examples,
)
blur_pool2d_demo = gr.Interface(
blur_pool2d_fn,
[
gr.Image(type="numpy"),
gr.Slider(minimum=1, maximum=40, step=1, value=20, label="Blur Pool")
],
"image",
examples=examples,
)
gaussian_blur_demo = gr.Interface(
gaussian_blur_fn,
[
gr.Image(type="numpy"),
gr.Slider(minimum=1, maximum=30, step=2, value=15, label="Gaussian Blur")
],
"image",
examples=examples,
)
max_blur_pool2d_demo = gr.Interface(
max_blur_pool2d_fn,
[
gr.Image(type="numpy"),
gr.Slider(minimum=1, maximum=40, step=1, value=20, label="Max Pool")
],
"image",
examples=examples,
)
median_blur_demo = gr.Interface(
median_blur_fn,
[
gr.Image(type="numpy"),
gr.Slider(minimum=1, maximum=30, step=2, value=15, label="Median Blur")
],
"image",
examples=examples,
)
# 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() |