Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
know-one-1
commited on
Commit
•
8a78ad8
1
Parent(s):
347bc60
Live Display
Browse files- .gitignore +2 -3
- app.py +91 -68
- examples/kornia.png +0 -0
.gitignore
CHANGED
@@ -1,3 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
kornia
|
|
|
1 |
+
kornia
|
2 |
+
flagged
|
|
app.py
CHANGED
@@ -6,12 +6,9 @@ from kornia.core import Tensor
|
|
6 |
|
7 |
def load_img(file):
|
8 |
# load the image using the rust backend
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
img_rgb: Tensor = K.color.bgr_to_rgb(img_bgr)
|
13 |
img_gray = K.color.rgb_to_grayscale(img_rgb)
|
14 |
-
|
15 |
return img_gray
|
16 |
|
17 |
|
@@ -29,98 +26,124 @@ def sobel_edge_detector(file):
|
|
29 |
return K.utils.tensor_to_image(img_out)
|
30 |
|
31 |
|
32 |
-
def simple_edge_detector(file, order
|
33 |
x_gray = load_img(file)
|
34 |
-
grads: Tensor = K.filters.spatial_gradient(
|
|
|
|
|
35 |
grads_x = grads[:, :, 0]
|
36 |
grads_y = grads[:, :, 1]
|
37 |
-
if
|
38 |
img_out = 1.0 - grads_x.clamp(0.0, 1.0)
|
39 |
else:
|
40 |
img_out = 1.0 - grads_y.clamp(0.0, 1.0)
|
41 |
return K.utils.tensor_to_image(img_out)
|
42 |
|
43 |
|
44 |
-
def laplacian_edge_detector(file,
|
45 |
x_gray = load_img(file)
|
46 |
-
|
47 |
-
img_out = 1.0 -
|
48 |
return K.utils.tensor_to_image(img_out)
|
49 |
|
50 |
|
51 |
-
examples = [
|
52 |
-
["examples/doraemon.png"],
|
53 |
-
]
|
54 |
|
55 |
title = "Kornia Edge Detector"
|
56 |
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>"
|
57 |
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>"
|
58 |
|
59 |
|
60 |
-
def
|
|
|
|
|
|
|
61 |
if choice == "Laplacian":
|
62 |
-
return gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
else:
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
return gr.update("Detect").click(
|
77 |
-
canny_edge_detector, inputs=image_input, outputs=image_output
|
78 |
-
)
|
79 |
-
elif choice == "sobel":
|
80 |
-
return gr.update("Detect").click(
|
81 |
-
sobel_edge_detector, inputs=image_input, outputs=image_output
|
82 |
-
)
|
83 |
-
elif choice == "laplacian":
|
84 |
-
return gr.update("Detect").click(
|
85 |
-
laplacian_edge_detector, inputs=image_input, outputs=image_output
|
86 |
-
)
|
87 |
else:
|
88 |
-
|
89 |
-
|
90 |
-
)
|
91 |
|
92 |
|
93 |
with gr.Blocks() as demo:
|
94 |
with gr.Row():
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
)
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
step=1,
|
106 |
-
default=5,
|
107 |
-
label="kernel_size",
|
108 |
-
visible=False,
|
109 |
)
|
110 |
-
order
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
117 |
)
|
118 |
-
Button = gr.Button("Detect")
|
119 |
-
|
120 |
-
radio.change(fn=change_kernel, inputs=radio, outputs=kernel)
|
121 |
-
radio.change(fn=change_order, inputs=radio, outputs=order)
|
122 |
-
radio.change(fn=change_button, inputs=radio, outputs=Button)
|
123 |
-
gr.Examples(examples, inputs=[image_input])
|
124 |
|
125 |
|
126 |
demo.launch()
|
|
|
6 |
|
7 |
def load_img(file):
|
8 |
# load the image using the rust backend
|
9 |
+
img_rgb: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
|
10 |
+
img_rgb = img_rgb[None]
|
|
|
|
|
11 |
img_gray = K.color.rgb_to_grayscale(img_rgb)
|
|
|
12 |
return img_gray
|
13 |
|
14 |
|
|
|
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":
|
37 |
img_out = 1.0 - grads_x.clamp(0.0, 1.0)
|
38 |
else:
|
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):
|
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)
|
60 |
+
direction = gr.update(visible=False)
|
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":
|
75 |
+
img = canny_edge_detector(file)
|
76 |
+
elif choice == "Sobel":
|
77 |
+
img = sobel_edge_detector(file)
|
78 |
+
elif choice == "Laplacian":
|
79 |
+
img = laplacian_edge_detector(file, 5)
|
80 |
else:
|
81 |
+
img = simple_edge_detector(file, 1, "x")
|
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)
|
89 |
+
elif choice == "Sobel":
|
90 |
+
img = sobel_edge_detector(file)
|
91 |
+
elif choice == "Laplacian":
|
92 |
+
img = laplacian_edge_detector(file, kernel)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
else:
|
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="file")
|
102 |
+
kernel = gr.Slider(
|
103 |
+
minimum=1,
|
104 |
+
maximum=7,
|
105 |
+
step=2,
|
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 |
+
label="Type of Edge Detector",
|
123 |
+
)
|
124 |
+
gr.Examples(examples, inputs=[image_input])
|
125 |
+
image_output = gr.Image(shape=(256, 256))
|
126 |
+
|
127 |
+
radio.change(
|
128 |
+
fn=Detect,
|
129 |
+
inputs=[image_input, radio],
|
130 |
+
outputs=[kernel, order, direction, image_output],
|
131 |
)
|
132 |
+
kernel.change(
|
133 |
+
fn=Detect_wo_layout,
|
134 |
+
inputs=[image_input, radio, kernel, order, direction],
|
135 |
+
outputs=[image_output],
|
|
|
|
|
|
|
|
|
136 |
)
|
137 |
+
order.change(
|
138 |
+
fn=Detect_wo_layout,
|
139 |
+
inputs=[image_input, radio, kernel, order, direction],
|
140 |
+
outputs=[image_output],
|
141 |
+
)
|
142 |
+
direction.change(
|
143 |
+
fn=Detect_wo_layout,
|
144 |
+
inputs=[image_input, radio, kernel, order, direction],
|
145 |
+
outputs=[image_output],
|
146 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
|
149 |
demo.launch()
|
examples/kornia.png
ADDED