Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
know-one-1
commited on
Commit
•
7e2640a
1
Parent(s):
aa508c4
update files
Browse files- app.py +124 -0
- examples/doraemon.png +0 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 the rust backend
|
9 |
+
img_bgr: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
|
10 |
+
img_bgr = img_bgr[None, ...].float() / 255.0
|
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 |
+
|
18 |
+
def canny_edge_detector(file):
|
19 |
+
x_gray = load_img(file)
|
20 |
+
x_canny: Tensor = K.filters.canny(x_gray)[0]
|
21 |
+
img_out = 1.0 - x_canny.clamp(0.0, 1.0)
|
22 |
+
return K.utils.tensor_to_image(img_out)
|
23 |
+
|
24 |
+
|
25 |
+
def sobel_edge_detector(file):
|
26 |
+
x_gray = load_img(file)
|
27 |
+
x_sobel: Tensor = K.filters.sobel(x_gray)
|
28 |
+
img_out = 1.0 - x_sobel
|
29 |
+
return K.utils.tensor_to_image(img_out)
|
30 |
+
|
31 |
+
|
32 |
+
def simple_edge_detector(file, order=1, dir="x"):
|
33 |
+
x_gray = load_img(file)
|
34 |
+
grads: Tensor = K.filters.spatial_gradient(x_gray, order=2) # BxCx2xHxW
|
35 |
+
grads_x = grads[:, :, 0]
|
36 |
+
grads_y = grads[:, :, 1]
|
37 |
+
if dir == "x":
|
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, kernel_size=5):
|
45 |
+
x_gray = load_img(file)
|
46 |
+
x_canny: Tensor = K.filters.canny(x_gray)[0]
|
47 |
+
img_out = 1.0 - x_canny.clamp(0.0, 1.0)
|
48 |
+
return K.utils.tensor_to_image(img_out)
|
49 |
+
|
50 |
+
|
51 |
+
examples = [
|
52 |
+
["examples/doraemon.jpg"],
|
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 change_kernel(choice):
|
61 |
+
if choice == "Laplacian":
|
62 |
+
return gr.update(visible=True)
|
63 |
+
else:
|
64 |
+
return gr.update(visible=False)
|
65 |
+
|
66 |
+
|
67 |
+
def change_order(choice):
|
68 |
+
if choice == "simple":
|
69 |
+
return gr.update(visible=True)
|
70 |
+
else:
|
71 |
+
return gr.update(visible=False)
|
72 |
+
|
73 |
+
|
74 |
+
def change_button(choice):
|
75 |
+
if choice == "simple":
|
76 |
+
return gr.Button("Detect").click(
|
77 |
+
canny_edge_detector, inputs=image_input, outputs=image_output
|
78 |
+
)
|
79 |
+
elif choice == "sobel":
|
80 |
+
return gr.Button("Detect").click(
|
81 |
+
sobel_edge_detector, inputs=image_input, outputs=image_output
|
82 |
+
)
|
83 |
+
elif choice == "laplacian":
|
84 |
+
return gr.Button("Detect").click(
|
85 |
+
laplacian_edge_detector, inputs=image_input, outputs=image_output
|
86 |
+
)
|
87 |
+
else:
|
88 |
+
return gr.Button("Detect").click(
|
89 |
+
simple_edge_detector, inputs=image_input, outputs=image_output
|
90 |
+
)
|
91 |
+
|
92 |
+
|
93 |
+
with gr.Blocks() as demo:
|
94 |
+
with gr.Row():
|
95 |
+
image_input = gr.Image()
|
96 |
+
image_output = gr.Image()
|
97 |
+
radio = gr.Radio(
|
98 |
+
["canny", "simple", "sobel", "laplacian"],
|
99 |
+
label="Essay Length to Write?",
|
100 |
+
)
|
101 |
+
kernel = gr.Slider(
|
102 |
+
minimum=1,
|
103 |
+
maximum=6,
|
104 |
+
step=1,
|
105 |
+
default=5,
|
106 |
+
label="kernel_size",
|
107 |
+
visible=False,
|
108 |
+
)
|
109 |
+
order = gr.Slider(
|
110 |
+
minimum=1,
|
111 |
+
maximum=2,
|
112 |
+
step=1,
|
113 |
+
default=1,
|
114 |
+
label="Derivative order",
|
115 |
+
visible=False,
|
116 |
+
)
|
117 |
+
Button = gr.Button("Detect")
|
118 |
+
|
119 |
+
radio.change(fn=change_kernel, inputs=radio, outputs=kernel)
|
120 |
+
radio.change(fn=change_order, inputs=radio, outputs=order)
|
121 |
+
radio.change(fn=change_button, inputs=radio, outputs=Button)
|
122 |
+
|
123 |
+
|
124 |
+
demo.launch()
|
examples/doraemon.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
kornia
|
2 |
+
kornia_rs
|
3 |
+
opencv-python
|
4 |
+
torch
|