File size: 4,832 Bytes
7e2640a
 
 
 
 
 
 
 
8a78ad8
 
7e2640a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a78ad8
7e2640a
8a78ad8
 
 
7e2640a
 
8a78ad8
7e2640a
 
 
 
 
 
8a78ad8
7e2640a
8a78ad8
 
7e2640a
 
 
8a78ad8
7e2640a
 
 
 
 
 
8a78ad8
 
 
 
7e2640a
8a78ad8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e2640a
8a78ad8
 
 
 
 
 
 
 
 
 
 
 
7e2640a
8a78ad8
 
7e2640a
 
 
 
8a78ad8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e2640a
8a78ad8
 
 
 
7e2640a
8a78ad8
 
 
 
 
 
 
 
 
7e2640a
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import gradio as gr

import kornia as K
from kornia.core import Tensor


def load_img(file):
    # load the image using the rust backend
    img_rgb: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
    img_rgb = img_rgb[None]
    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):
    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 href='https://kornia-tutorials.readthedocs.io/en/latest/image_enhancement.html' 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="file")
            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"],
                label="Type of Edge Detector",
            )
            gr.Examples(examples, inputs=[image_input])
        image_output = gr.Image(shape=(256, 256))

        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],
        )


demo.launch()