Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
init project
Browse files- app.py +54 -0
- examples/cat.png +0 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import kornia as K
|
4 |
+
from kornia.core import Tensor
|
5 |
+
from kornia import morphology as morph
|
6 |
+
|
7 |
+
import torch
|
8 |
+
|
9 |
+
def morphological_operators(filepath, operator):
|
10 |
+
|
11 |
+
img: Tensor = K.io.load_image(filepath, K.io.ImageLoadType.RGB32)
|
12 |
+
img = img[None]
|
13 |
+
|
14 |
+
device = 'cpu' # 'cuda:0' for GPU
|
15 |
+
kernel = torch.tensor([[0, 1, 0],[1, 1, 1],[0, 1, 0]]).to(device)
|
16 |
+
|
17 |
+
if operator == 'Dilation':
|
18 |
+
opt = morph.dilation(img, kernel)
|
19 |
+
elif operator == 'Erosion':
|
20 |
+
opt = morph.erosion(img, kernel)
|
21 |
+
elif operator == 'Open':
|
22 |
+
opt = morph.opening(img, kernel)
|
23 |
+
elif operator == 'Close':
|
24 |
+
opt = morph.closing(img, kernel)
|
25 |
+
elif operator == 'Gradient':
|
26 |
+
opt = 1. - morph.gradient(img, kernel)
|
27 |
+
elif operator == 'Bottom Hat':
|
28 |
+
opt = 1. - morph.bottom_hat(img, kernel)
|
29 |
+
else:
|
30 |
+
opt = 1. - morph.top_hat(img, kernel)
|
31 |
+
|
32 |
+
output = K.tensor_to_image(opt.squeeze(0))
|
33 |
+
return output
|
34 |
+
|
35 |
+
|
36 |
+
examples = [
|
37 |
+
["examples/cat.png", "Dilation"]
|
38 |
+
]
|
39 |
+
|
40 |
+
title = "Kornia Morphological Operators"
|
41 |
+
description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Morphological Operators.</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 morphological operator to run it! Read more at the links at the bottom.</p>"
|
42 |
+
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/morphology_101.html' target='_blank'>Kornia Morphological Operators Tutorial</a></p>"
|
43 |
+
|
44 |
+
iface = gr.Interface(morphological_operators,
|
45 |
+
[
|
46 |
+
gr.Image(type="filepath"),
|
47 |
+
gr.Dropdown(choices=["Dilation", "Erosion", "Open", "Close", "Gradient", "Bottom Hat", "Top Hat"])
|
48 |
+
],
|
49 |
+
"image",
|
50 |
+
examples
|
51 |
+
|
52 |
+
)
|
53 |
+
|
54 |
+
iface.launch()
|
examples/cat.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
kornia
|
2 |
+
kornia_rs
|
3 |
+
opencv-python
|
4 |
+
torch
|