edge_detector / app.py
mischeiwiller's picture
Update app.py
6d95ef7 verified
raw
history blame contribute delete
No virus
3.38 kB
import gradio as gr
import kornia as K
from kornia.core import Tensor
from PIL import Image
import numpy as np
def edge_detection(image, detector):
# Convert PIL Image to Tensor
img_np = np.array(image)
img: Tensor = K.utils.image_to_tensor(img_np).float() / 255.0
img = img.unsqueeze(0) # Add batch dimension
x_gray = K.color.rgb_to_grayscale(img)
if detector == '1st order derivates in x':
grads: Tensor = K.filters.spatial_gradient(x_gray, order=1)
grads_x = grads[:, :, 0]
output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
elif detector == '1st order derivates in y':
grads: Tensor = K.filters.spatial_gradient(x_gray, order=1)
grads_y = grads[:, :, 1]
output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
elif detector == '2nd order derivatives in x':
grads: Tensor = K.filters.spatial_gradient(x_gray, order=2)
grads_x = grads[:, :, 0]
output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
elif detector == '2nd order derivatives in y':
grads: Tensor = K.filters.spatial_gradient(x_gray, order=2)
grads_y = grads[:, :, 1]
output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
elif detector == 'Sobel':
x_sobel: Tensor = K.filters.sobel(x_gray)
output = K.utils.tensor_to_image(1. - x_sobel)
elif detector == 'Laplacian':
x_laplacian: Tensor = K.filters.laplacian(x_gray, kernel_size=5)
output = K.utils.tensor_to_image(1. - x_laplacian.clamp(0., 1.))
else:
x_canny: Tensor = K.filters.canny(x_gray)[0]
output = K.utils.tensor_to_image(1. - x_canny.clamp(0., 1.0))
return output
examples = [
["examples/huggingface.jpg", "1st order derivates in x"],
["examples/doraemon.jpg", "Canny"]
]
title = "Kornia Edge Detection"
description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Edge Detection.</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 edge detector 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/filtering_edges.html' target='_blank'>Kornia Edge Detection Tutorial</a></p>"
with gr.Blocks(title=title) as demo:
gr.Markdown(f"# {title}")
gr.Markdown(description)
with gr.Row():
input_image = gr.Image(type="pil", label="Input Image")
output_image = gr.Image(type="numpy", label="Edge Detection Result")
detector = gr.Dropdown(
choices=["1st order derivates in x", "1st order derivates in y", "2nd order derivatives in x", "2nd order derivatives in y", "Sobel", "Laplacian", "Canny"],
label="Edge Detector",
value="Canny"
)
detect_button = gr.Button("Detect Edges")
detect_button.click(fn=edge_detection, inputs=[input_image, detector], outputs=output_image)
gr.Examples(examples=examples, inputs=[input_image, detector], outputs=output_image)
gr.Markdown(article)
if __name__ == "__main__":
demo.launch()