Spaces:
Runtime error
Runtime error
File size: 4,620 Bytes
7e2640a a73d4f5 7e2640a a73d4f5 7e2640a 8a78ad8 7e2640a a73d4f5 7e2640a 8a78ad8 7e2640a 679163e 7e2640a 8a78ad8 7e2640a 8a78ad8 7e2640a 076a47f 7e2640a 8a78ad8 7e2640a 8a78ad8 a73d4f5 8a78ad8 7e2640a 8a78ad8 7e2640a 8a78ad8 7e2640a 8a78ad8 a73d4f5 d21e975 a73d4f5 8a78ad8 a73d4f5 7e2640a a73d4f5 |
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 |
import gradio as gr
import kornia as K
from kornia.core import Tensor
from PIL import Image
import numpy as np
def load_img(file):
# load the image using PIL and convert to tensor
img_pil = Image.open(file).convert('RGB')
img_np = np.array(img_pil)
img_rgb: Tensor = K.utils.image_to_tensor(img_np).float() / 255.0
img_rgb = img_rgb.unsqueeze(0) # add batch dimension
img_gray = K.color.rgb_to_grayscale(img_rgb)
return img_gray
def canny_edge_detector(file):
x_gray = load_img(file)
x_canny: Tensor = K.filters.canny(x_gray)[0]
img_out = 1.0 - x_canny.clamp(0.0, 1.0)
return K.utils.tensor_to_image(img_out)
def sobel_edge_detector(file):
x_gray = load_img(file)
x_sobel: Tensor = K.filters.sobel(x_gray)
img_out = 1.0 - x_sobel
return K.utils.tensor_to_image(img_out)
def simple_edge_detector(file, order, direction):
x_gray = load_img(file)
grads: Tensor = K.filters.spatial_gradient(x_gray, order=order) # BxCx2xHxW
grads_x = grads[:, :, 0]
grads_y = grads[:, :, 1]
if direction == "x":
img_out = 1.0 - grads_x.clamp(0.0, 1.0)
else:
img_out = 1.0 - grads_y.clamp(0.0, 1.0)
return K.utils.tensor_to_image(img_out)
def laplacian_edge_detector(file, kernel=9):
x_gray = load_img(file)
x_laplacian: Tensor = K.filters.laplacian(x_gray, kernel_size=kernel)
img_out = 1.0 - x_laplacian.clamp(0.0, 1.0)
return K.utils.tensor_to_image(img_out)
examples = [["examples/doraemon.png"], ["examples/kornia.png"]]
title = "Kornia Edge Detector"
description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Edge Detector.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them, and use the sliders to enhance! 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 https://kornia.github.io/tutorials/#category=Edge%20Detection' target='_blank'>Kornia Enhancements Tutorial</a></p>"
def change_layout(choice):
kernel = gr.update(visible=False)
order = gr.update(visible=False)
direction = gr.update(visible=False)
if choice == "Laplacian":
return [gr.update(value=3, visible=True), order, direction]
elif choice == "Simple":
return [kernel, gr.update(value=2, visible=True), gr.update(value="x", visible=True)]
return [kernel, order, direction]
def Detect(file, choice):
layout = change_layout(choice)
if choice == "Canny":
img = canny_edge_detector(file)
elif choice == "Sobel":
img = sobel_edge_detector(file)
elif choice == "Laplacian":
img = laplacian_edge_detector(file, 5)
else:
img = simple_edge_detector(file, 1, "x")
layout.extend([img])
return layout
def Detect_wo_layout(file, choice, kernel, order, direction):
if choice == "Canny":
img = canny_edge_detector(file)
elif choice == "Sobel":
img = sobel_edge_detector(file)
elif choice == "Laplacian":
img = laplacian_edge_detector(file, kernel)
else:
img = simple_edge_detector(file, order, direction)
return img
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
image_input = gr.Image(type="filepath", label="Input Image")
kernel = gr.Slider(minimum=1, maximum=7, step=2, value=3, label="kernel_size", visible=False)
order = gr.Radio([1, 2], value=1, label="Derivative Order", visible=False)
direction = gr.Radio(["x", "y"], value="x", label="Derivative Direction", visible=False)
radio = gr.Radio(["Canny", "Simple", "Sobel", "Laplacian"], value="Canny", label="Type of Edge Detector")
with gr.Column():
image_output = gr.Image(label="Output Image")
gr.Examples(examples, inputs=[image_input])
radio.change(fn=Detect, inputs=[image_input, radio], outputs=[kernel, order, direction, image_output])
kernel.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
order.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
direction.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
image_input.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
demo.launch() |