Update app.py
Browse files
app.py
CHANGED
@@ -7,40 +7,31 @@ from skimage.data import coins
|
|
7 |
from skimage.transform import rescale
|
8 |
from sklearn.feature_extraction import image
|
9 |
from sklearn.cluster import spectral_clustering
|
|
|
10 |
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Resize it to 20% of the original size to speed up the processing
|
16 |
-
# Applying a Gaussian filter for smoothing prior to down-scaling
|
17 |
-
# reduces aliasing artifacts.
|
18 |
-
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
|
19 |
-
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect", anti_aliasing=False)
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
# actual image. For beta=1, the segmentation is close to a voronoi
|
28 |
-
beta = 10
|
29 |
-
eps = 1e-6
|
30 |
-
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
n_regions = 26
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
# extra regions for segmentation.
|
40 |
-
n_regions_plus = 3
|
41 |
|
42 |
-
#
|
43 |
-
|
|
|
|
|
44 |
t0 = time.time()
|
45 |
labels = spectral_clustering(
|
46 |
graph,
|
@@ -66,11 +57,6 @@ def getClusteringPlot(algorithm):
|
|
66 |
# To view individual segments as appear comment in plt.pause(0.5)
|
67 |
return plt
|
68 |
|
69 |
-
import gradio as gr
|
70 |
-
|
71 |
-
def welcome(name):
|
72 |
-
return f"Welcome to Gradio, {name}!"
|
73 |
-
|
74 |
with gr.Blocks() as demo:
|
75 |
gr.Markdown(
|
76 |
"""
|
@@ -83,6 +69,5 @@ with gr.Blocks() as demo:
|
|
83 |
plot = gr.Plot(label="Plot")
|
84 |
inp.change(getClusteringPlot, inputs=inp, outputs=[plot])
|
85 |
demo.load(getClusteringPlot, inputs=[inp], outputs=[plot])
|
86 |
-
|
87 |
-
|
88 |
-
demo.launch()
|
|
|
7 |
from skimage.transform import rescale
|
8 |
from sklearn.feature_extraction import image
|
9 |
from sklearn.cluster import spectral_clustering
|
10 |
+
import gradio as gr
|
11 |
|
12 |
|
13 |
+
def getClusteringPlot(algorithm):
|
14 |
+
# load the coins as a numpy array
|
15 |
+
orig_coins = coins()
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Pre-processing the image
|
18 |
+
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
|
19 |
+
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect", anti_aliasing=False)
|
20 |
|
21 |
+
# Convert the image into a graph
|
22 |
+
graph = image.img_to_graph(rescaled_coins)
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
beta = 10
|
25 |
+
eps = 1e-6
|
26 |
+
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
|
|
|
27 |
|
28 |
+
# The number of segmented regions to display needs to be chosen manually
|
29 |
+
n_regions = 26
|
|
|
|
|
30 |
|
31 |
+
# The spectral clustering quality may also benetif from requesting
|
32 |
+
# extra regions for segmentation.
|
33 |
+
n_regions_plus = 3
|
34 |
+
|
35 |
t0 = time.time()
|
36 |
labels = spectral_clustering(
|
37 |
graph,
|
|
|
57 |
# To view individual segments as appear comment in plt.pause(0.5)
|
58 |
return plt
|
59 |
|
|
|
|
|
|
|
|
|
|
|
60 |
with gr.Blocks() as demo:
|
61 |
gr.Markdown(
|
62 |
"""
|
|
|
69 |
plot = gr.Plot(label="Plot")
|
70 |
inp.change(getClusteringPlot, inputs=inp, outputs=[plot])
|
71 |
demo.load(getClusteringPlot, inputs=[inp], outputs=[plot])
|
72 |
+
|
73 |
+
demo.launch()
|
|