File size: 3,014 Bytes
44d3e73
 
 
 
 
b522580
44d3e73
314893c
 
44d3e73
314893c
44d3e73
 
 
 
314893c
44d3e73
 
 
 
 
 
314893c
44d3e73
 
 
 
 
 
314893c
44d3e73
 
 
 
 
 
314893c
44d3e73
 
 
 
 
 
314893c
44d3e73
 
 
314893c
44d3e73
 
 
314893c
44d3e73
 
 
 
 
 
4ee69d5
 
44d3e73
 
 
 
 
 
eb0c80a
 
 
f8d1a61
eb0c80a
 
 
44d3e73
 
 
 
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
import gradio as gr

import kornia as K
from kornia.core import Tensor

def edge_detection(filepath, detector): 

    img: Tensor = K.io.load_image(filepath, K.io.ImageLoadType.RGB32)    
    img = img[None]

    x_rgb: Tensor = K.color.bgr_to_rgb(img)
    x_gray = K.color.rgb_to_grayscale(x_rgb)
    

    if detector == '1st order derivates in x':
        grads: Tensor = K.filters.spatial_gradient(x_gray, order=1) 
        grads_x = grads[:, :, 0]
        grads_y = grads[:, :, 1]
        
        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_x = grads[:, :, 0]
        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]
        grads_y = grads[:, :, 1]
        
        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_x = grads[:, :, 0]
        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>"

iface = gr.Interface(edge_detection, 
            [
            	gr.Image(type="filepath"),
	            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"])
            ],
            "image",
            examples
            
)

iface.launch()