Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
mischeiwiller
commited on
Commit
•
a73d4f5
1
Parent(s):
dac2479
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
import kornia as K
|
4 |
from kornia.core import Tensor
|
5 |
-
|
|
|
6 |
|
7 |
def load_img(file):
|
8 |
-
# load the image using
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
img_gray = K.color.rgb_to_grayscale(img_rgb)
|
12 |
return img_gray
|
13 |
|
14 |
-
|
15 |
def canny_edge_detector(file):
|
16 |
x_gray = load_img(file)
|
17 |
x_canny: Tensor = K.filters.canny(x_gray)[0]
|
18 |
img_out = 1.0 - x_canny.clamp(0.0, 1.0)
|
19 |
return K.utils.tensor_to_image(img_out)
|
20 |
|
21 |
-
|
22 |
def sobel_edge_detector(file):
|
23 |
x_gray = load_img(file)
|
24 |
x_sobel: Tensor = K.filters.sobel(x_gray)
|
25 |
img_out = 1.0 - x_sobel
|
26 |
return K.utils.tensor_to_image(img_out)
|
27 |
|
28 |
-
|
29 |
def simple_edge_detector(file, order, direction):
|
30 |
x_gray = load_img(file)
|
31 |
-
grads: Tensor = K.filters.spatial_gradient(
|
32 |
-
x_gray, order=order
|
33 |
-
) # BxCx2xHxW
|
34 |
grads_x = grads[:, :, 0]
|
35 |
grads_y = grads[:, :, 1]
|
36 |
if direction == "x":
|
@@ -39,21 +36,18 @@ def simple_edge_detector(file, order, direction):
|
|
39 |
img_out = 1.0 - grads_y.clamp(0.0, 1.0)
|
40 |
return K.utils.tensor_to_image(img_out)
|
41 |
|
42 |
-
|
43 |
def laplacian_edge_detector(file, kernel=9):
|
44 |
x_gray = load_img(file)
|
45 |
x_laplacian: Tensor = K.filters.laplacian(x_gray, kernel_size=kernel)
|
46 |
img_out = 1.0 - x_laplacian.clamp(0.0, 1.0)
|
47 |
return K.utils.tensor_to_image(img_out)
|
48 |
|
49 |
-
|
50 |
examples = [["examples/doraemon.png"], ["examples/kornia.png"]]
|
51 |
|
52 |
title = "Kornia Edge Detector"
|
53 |
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>"
|
54 |
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>"
|
55 |
|
56 |
-
|
57 |
def change_layout(choice):
|
58 |
kernel = gr.update(visible=False)
|
59 |
order = gr.update(visible=False)
|
@@ -61,14 +55,9 @@ def change_layout(choice):
|
|
61 |
if choice == "Laplacian":
|
62 |
return [gr.update(value=3, visible=True), order, direction]
|
63 |
elif choice == "Simple":
|
64 |
-
return [
|
65 |
-
kernel,
|
66 |
-
gr.update(value=2, visible=True),
|
67 |
-
gr.update(value="x", visible=True),
|
68 |
-
]
|
69 |
return [kernel, order, direction]
|
70 |
|
71 |
-
|
72 |
def Detect(file, choice):
|
73 |
layout = change_layout(choice)
|
74 |
if choice == "Canny":
|
@@ -82,7 +71,6 @@ def Detect(file, choice):
|
|
82 |
layout.extend([img])
|
83 |
return layout
|
84 |
|
85 |
-
|
86 |
def Detect_wo_layout(file, choice, kernel, order, direction):
|
87 |
if choice == "Canny":
|
88 |
img = canny_edge_detector(file)
|
@@ -94,63 +82,22 @@ def Detect_wo_layout(file, choice, kernel, order, direction):
|
|
94 |
img = simple_edge_detector(file, order, direction)
|
95 |
return img
|
96 |
|
97 |
-
|
98 |
with gr.Blocks() as demo:
|
99 |
with gr.Row():
|
100 |
with gr.Column():
|
101 |
-
image_input = gr.Image(type="
|
102 |
-
kernel = gr.Slider(
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
value=3,
|
107 |
-
label="kernel_size",
|
108 |
-
visible=False,
|
109 |
-
)
|
110 |
-
order = gr.Radio(
|
111 |
-
[1, 2], value=1, label="Derivative Order", visible=False
|
112 |
-
)
|
113 |
-
direction = gr.Radio(
|
114 |
-
["x", "y"],
|
115 |
-
value="x",
|
116 |
-
label="Derivative Direction",
|
117 |
-
visible=False,
|
118 |
-
)
|
119 |
-
|
120 |
-
radio = gr.Radio(
|
121 |
-
["Canny", "Simple", "Sobel", "Laplacian"],
|
122 |
-
value="Canny",
|
123 |
-
label="Type of Edge Detector",
|
124 |
-
)
|
125 |
with gr.Column():
|
126 |
-
image_output = gr.Image(
|
127 |
gr.Examples(examples, inputs=[image_input])
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
kernel.change(
|
136 |
-
fn=Detect_wo_layout,
|
137 |
-
inputs=[image_input, radio, kernel, order, direction],
|
138 |
-
outputs=[image_output],
|
139 |
-
)
|
140 |
-
order.change(
|
141 |
-
fn=Detect_wo_layout,
|
142 |
-
inputs=[image_input, radio, kernel, order, direction],
|
143 |
-
outputs=[image_output],
|
144 |
-
)
|
145 |
-
direction.change(
|
146 |
-
fn=Detect_wo_layout,
|
147 |
-
inputs=[image_input, radio, kernel, order, direction],
|
148 |
-
outputs=[image_output],
|
149 |
-
)
|
150 |
-
image_input.change(
|
151 |
-
fn=Detect_wo_layout,
|
152 |
-
inputs=[image_input, radio, kernel, order, direction],
|
153 |
-
outputs=[image_output],
|
154 |
-
)
|
155 |
|
156 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import kornia as K
|
3 |
from kornia.core import Tensor
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
|
7 |
def load_img(file):
|
8 |
+
# load the image using PIL and convert to tensor
|
9 |
+
img_pil = Image.open(file).convert('RGB')
|
10 |
+
img_np = np.array(img_pil)
|
11 |
+
img_rgb: Tensor = K.utils.image_to_tensor(img_np).float() / 255.0
|
12 |
+
img_rgb = img_rgb.unsqueeze(0) # add batch dimension
|
13 |
img_gray = K.color.rgb_to_grayscale(img_rgb)
|
14 |
return img_gray
|
15 |
|
|
|
16 |
def canny_edge_detector(file):
|
17 |
x_gray = load_img(file)
|
18 |
x_canny: Tensor = K.filters.canny(x_gray)[0]
|
19 |
img_out = 1.0 - x_canny.clamp(0.0, 1.0)
|
20 |
return K.utils.tensor_to_image(img_out)
|
21 |
|
|
|
22 |
def sobel_edge_detector(file):
|
23 |
x_gray = load_img(file)
|
24 |
x_sobel: Tensor = K.filters.sobel(x_gray)
|
25 |
img_out = 1.0 - x_sobel
|
26 |
return K.utils.tensor_to_image(img_out)
|
27 |
|
|
|
28 |
def simple_edge_detector(file, order, direction):
|
29 |
x_gray = load_img(file)
|
30 |
+
grads: Tensor = K.filters.spatial_gradient(x_gray, order=order) # BxCx2xHxW
|
|
|
|
|
31 |
grads_x = grads[:, :, 0]
|
32 |
grads_y = grads[:, :, 1]
|
33 |
if direction == "x":
|
|
|
36 |
img_out = 1.0 - grads_y.clamp(0.0, 1.0)
|
37 |
return K.utils.tensor_to_image(img_out)
|
38 |
|
|
|
39 |
def laplacian_edge_detector(file, kernel=9):
|
40 |
x_gray = load_img(file)
|
41 |
x_laplacian: Tensor = K.filters.laplacian(x_gray, kernel_size=kernel)
|
42 |
img_out = 1.0 - x_laplacian.clamp(0.0, 1.0)
|
43 |
return K.utils.tensor_to_image(img_out)
|
44 |
|
|
|
45 |
examples = [["examples/doraemon.png"], ["examples/kornia.png"]]
|
46 |
|
47 |
title = "Kornia Edge Detector"
|
48 |
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>"
|
49 |
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>"
|
50 |
|
|
|
51 |
def change_layout(choice):
|
52 |
kernel = gr.update(visible=False)
|
53 |
order = gr.update(visible=False)
|
|
|
55 |
if choice == "Laplacian":
|
56 |
return [gr.update(value=3, visible=True), order, direction]
|
57 |
elif choice == "Simple":
|
58 |
+
return [kernel, gr.update(value=2, visible=True), gr.update(value="x", visible=True)]
|
|
|
|
|
|
|
|
|
59 |
return [kernel, order, direction]
|
60 |
|
|
|
61 |
def Detect(file, choice):
|
62 |
layout = change_layout(choice)
|
63 |
if choice == "Canny":
|
|
|
71 |
layout.extend([img])
|
72 |
return layout
|
73 |
|
|
|
74 |
def Detect_wo_layout(file, choice, kernel, order, direction):
|
75 |
if choice == "Canny":
|
76 |
img = canny_edge_detector(file)
|
|
|
82 |
img = simple_edge_detector(file, order, direction)
|
83 |
return img
|
84 |
|
|
|
85 |
with gr.Blocks() as demo:
|
86 |
with gr.Row():
|
87 |
with gr.Column():
|
88 |
+
image_input = gr.Image(type="filepath", label="Input Image")
|
89 |
+
kernel = gr.Slider(minimum=1, maximum=7, step=2, value=3, label="kernel_size", visible=False)
|
90 |
+
order = gr.Radio([1, 2], value=1, label="Derivative Order", visible=False)
|
91 |
+
direction = gr.Radio(["x", "y"], value="x", label="Derivative Direction", visible=False)
|
92 |
+
radio = gr.Radio(["Canny", "Simple", "Sobel", "Laplacian"], value="Canny", label="Type of Edge Detector")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
with gr.Column():
|
94 |
+
image_output = gr.Image(label="Output Image")
|
95 |
gr.Examples(examples, inputs=[image_input])
|
96 |
|
97 |
+
radio.change(fn=Detect, inputs=[image_input, radio], outputs=[kernel, order, direction, image_output])
|
98 |
+
kernel.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
|
99 |
+
order.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
|
100 |
+
direction.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
|
101 |
+
image_input.change(fn=Detect_wo_layout, inputs=[image_input, radio, kernel, order, direction], outputs=[image_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
demo.launch()
|