Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Edge detectors
Browse files- app.py +85 -0
- examples/doraemon.jpg +0 -0
- examples/huggingface.jpg +0 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
import torch
|
7 |
+
import kornia as K
|
8 |
+
from kornia.core import Tensor
|
9 |
+
|
10 |
+
def edge_detection(file, detector):
|
11 |
+
|
12 |
+
img_bgr: np.ndarray = cv2.imread(file, cv2.IMREAD_COLOR)
|
13 |
+
|
14 |
+
x_bgr: torch.Tensor = K.utils.image_to_tensor(img_bgr) # CxHxWx
|
15 |
+
x_bgr = x_bgr[None,...].float() / 255.
|
16 |
+
|
17 |
+
x_rgb: torch.Tensor = K.color.bgr_to_rgb(x_bgr)
|
18 |
+
x_gray = K.color.rgb_to_grayscale(x_rgb)
|
19 |
+
|
20 |
+
|
21 |
+
if detector == '1st order derivates in x':
|
22 |
+
grads: torch.Tensor = K.filters.spatial_gradient(x_gray, order=1) # BxCx2xHxW
|
23 |
+
grads_x = grads[:, :, 0]
|
24 |
+
grads_y = grads[:, :, 1]
|
25 |
+
|
26 |
+
output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
|
27 |
+
|
28 |
+
elif detector == '1st order derivates in y':
|
29 |
+
grads: torch.Tensor = K.filters.spatial_gradient(x_gray, order=1) # BxCx2xHxW
|
30 |
+
grads_x = grads[:, :, 0]
|
31 |
+
grads_y = grads[:, :, 1]
|
32 |
+
|
33 |
+
output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
|
34 |
+
|
35 |
+
elif detector == '2nd order derivatives in x':
|
36 |
+
grads: torch.Tensor = K.filters.spatial_gradient(x_gray, order=2) # BxCx2xHxW
|
37 |
+
grads_x = grads[:, :, 0]
|
38 |
+
grads_y = grads[:, :, 1]
|
39 |
+
|
40 |
+
output = K.utils.tensor_to_image(1. - grads_x.clamp(0., 1.))
|
41 |
+
|
42 |
+
elif detector == '2nd order derivatives in y':
|
43 |
+
grads: torch.Tensor = K.filters.spatial_gradient(x_gray, order=2) # BxCx2xHxW
|
44 |
+
grads_x = grads[:, :, 0]
|
45 |
+
grads_y = grads[:, :, 1]
|
46 |
+
|
47 |
+
output = K.utils.tensor_to_image(1. - grads_y.clamp(0., 1.))
|
48 |
+
|
49 |
+
elif detector == 'Sobel':
|
50 |
+
x_sobel: torch.Tensor = K.filters.sobel(x_gray)
|
51 |
+
output = K.utils.tensor_to_image(1. - x_sobel)
|
52 |
+
|
53 |
+
elif detector == 'Laplacian':
|
54 |
+
x_laplacian: torch.Tensor = K.filters.laplacian(x_gray, kernel_size=5)
|
55 |
+
output = K.utils.tensor_to_image(1. - x_laplacian.clamp(0., 1.))
|
56 |
+
|
57 |
+
else:
|
58 |
+
x_canny: torch.Tensor = K.filters.canny(x_gray)[0]
|
59 |
+
output = K.utils.tensor_to_image(1. - x_canny.clamp(0., 1.0))
|
60 |
+
|
61 |
+
return output
|
62 |
+
|
63 |
+
|
64 |
+
examples = [
|
65 |
+
["examples/huggingface.jpg"],
|
66 |
+
["examples/doraemon.jpg"]
|
67 |
+
]
|
68 |
+
|
69 |
+
title = "Kornia Edge Detection"
|
70 |
+
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>"
|
71 |
+
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>"
|
72 |
+
|
73 |
+
iface = gr.Interface(fn=edge_detection,
|
74 |
+
inputs=[gr.Image(type="filepath"),
|
75 |
+
gr.Dropdown(choices=["1st order derivates in x", "1st order derivates in y",
|
76 |
+
"2nd order derivatives in x", "2nd order derivatives in y",
|
77 |
+
"Sobel", "Laplacian", "Canny"],
|
78 |
+
value="1st order derivates in x")
|
79 |
+
],
|
80 |
+
outputs="image",
|
81 |
+
examples=examples
|
82 |
+
|
83 |
+
)
|
84 |
+
|
85 |
+
iface.launch()
|
examples/doraemon.jpg
ADDED
examples/huggingface.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
kornia
|
2 |
+
kornia_rs
|
3 |
+
opencv-python
|
4 |
+
torch
|
5 |
+
numpy
|