Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,421 Bytes
38e0113 f327c90 38e0113 cda78eb f327c90 cda78eb f327c90 cda78eb ade6aac 38e0113 3f4b654 38e0113 04a91c6 38e0113 cda78eb 38e0113 cda78eb |
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 |
import gradio as gr
import kornia as K
from kornia.core import Tensor
from kornia import morphology as morph
import torch
import numpy as np
def morphological_operators(img, operator, kernel, kernel_size):
# Convert input to tensor
if isinstance(img, np.ndarray):
img = torch.from_numpy(img).permute(2, 0, 1).float() / 255.0
elif isinstance(img, torch.Tensor):
img = img.permute(2, 0, 1).float()
if img.max() > 1.0:
img = img / 255.0
else:
raise ValueError(f"Unsupported image type: {type(img)}")
img = img.unsqueeze(0) # Add batch dimension
device = 'cpu' # 'cuda:0' for GPU
img = img.to(device)
kernels = {
"Ones": torch.ones(kernel_size, kernel_size).to(device),
"Eye": torch.eye(kernel_size).to(device),
"Cross": torch.tensor([[0, 1, 0],[1, 1, 1],[0, 1, 0]]).to(device),
}
operations = {
'Dilation': morph.dilation(img, kernels[kernel]),
'Erosion': morph.erosion(img, kernels[kernel]),
'Open': morph.opening(img, kernels[kernel]),
'Close': morph.closing(img, kernels[kernel]),
'Gradient': 1. - morph.gradient(img, kernels[kernel]),
'Bottom Hat': 1. - morph.bottom_hat(img, kernels[kernel]),
'Top Hat': 1. - morph.top_hat(img, kernels[kernel])
}
output = K.tensor_to_image(operations[operator].squeeze(0))
return output
examples = [
["examples/cat.png", "Dilation", "Ones", 3],
["examples/huggingface.jpg", "Close", "Eye", 5]
]
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.github.io/tutorials/#category=kornia.morphology' target='_blank'>Kornia Morphological Operators Tutorial</a></p>"
with gr.Blocks(title=title) as demo:
gr.Markdown(f"# {title}")
gr.Markdown(description)
with gr.Row():
input_image = gr.Image(label="Input Image")
output_image = gr.Image(label="Output Image")
with gr.Row():
operator = gr.Dropdown(
choices=["Dilation", "Erosion", "Open", "Close", "Gradient", "Bottom Hat", "Top Hat"],
label="Operator"
)
kernel = gr.Radio(
choices=["Ones", "Eye", "Cross"],
label="Kernel Type"
)
kernel_size = gr.Slider(
minimum=1,
maximum=7,
step=2,
value=3,
label="Kernel size"
)
apply_button = gr.Button("Apply Operator")
apply_button.click(
fn=morphological_operators,
inputs=[input_image, operator, kernel, kernel_size],
outputs=output_image
)
gr.Examples(
examples=examples,
inputs=[input_image, operator, kernel, kernel_size],
outputs=output_image,
fn=morphological_operators,
cache_examples=True
)
gr.Markdown(article)
if __name__ == "__main__":
demo.launch() |