Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 2,028 Bytes
38e0113 |
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 |
import gradio as gr
import kornia as K
from kornia.core import Tensor
from kornia import morphology as morph
import torch
def morphological_operators(filepath, operator):
img: Tensor = K.io.load_image(filepath, K.io.ImageLoadType.RGB32)
img = img[None]
device = 'cpu' # 'cuda:0' for GPU
kernel = torch.tensor([[0, 1, 0],[1, 1, 1],[0, 1, 0]]).to(device)
if operator == 'Dilation':
opt = morph.dilation(img, kernel)
elif operator == 'Erosion':
opt = morph.erosion(img, kernel)
elif operator == 'Open':
opt = morph.opening(img, kernel)
elif operator == 'Close':
opt = morph.closing(img, kernel)
elif operator == 'Gradient':
opt = 1. - morph.gradient(img, kernel)
elif operator == 'Bottom Hat':
opt = 1. - morph.bottom_hat(img, kernel)
else:
opt = 1. - morph.top_hat(img, kernel)
output = K.tensor_to_image(opt.squeeze(0))
return output
examples = [
["examples/cat.png", "Dilation"]
]
title = "Kornia Morphological Operators"
description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Morphological Operators.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them, and select any morphological operator to run it! Read more at the links at the bottom.</p>"
article = "<p style='text-align: center'><a href='https://kornia.readthedocs.io/en/latest/' target='_blank'>Kornia Docs</a> | <a href='https://github.com/kornia/kornia' target='_blank'>Kornia Github Repo</a> | <a href='https://kornia-tutorials.readthedocs.io/en/latest/morphology_101.html' target='_blank'>Kornia Morphological Operators Tutorial</a></p>"
iface = gr.Interface(morphological_operators,
[
gr.Image(type="filepath"),
gr.Dropdown(choices=["Dilation", "Erosion", "Open", "Close", "Gradient", "Bottom Hat", "Top Hat"])
],
"image",
examples
)
iface.launch()
|