app.py
Browse filesThis is an app for Segmenting the picture of greek coins in regions using Spectral Clustering, using one of the following algorithms: kmeans, discrete and QR factorization.
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# load the coins as a numpy array
|
3 |
+
orig_coins = coins()
|
4 |
+
|
5 |
+
# Resize it to 20% of the original size to speed up the processing
|
6 |
+
# Applying a Gaussian filter for smoothing prior to down-scaling
|
7 |
+
# reduces aliasing artifacts.
|
8 |
+
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
|
9 |
+
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect", anti_aliasing=False)
|
10 |
+
|
11 |
+
# Convert the image into a graph with the value of the gradient on the
|
12 |
+
# edges.
|
13 |
+
graph = image.img_to_graph(rescaled_coins)
|
14 |
+
|
15 |
+
# Take a decreasing function of the gradient: an exponential
|
16 |
+
# The smaller beta is, the more independent the segmentation is of the
|
17 |
+
# actual image. For beta=1, the segmentation is close to a voronoi
|
18 |
+
beta = 10
|
19 |
+
eps = 1e-6
|
20 |
+
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
|
21 |
+
|
22 |
+
# The number of segmented regions to display needs to be chosen manually.
|
23 |
+
# The current version of 'spectral_clustering' does not support determining
|
24 |
+
# the number of good quality clusters automatically.
|
25 |
+
n_regions = 26
|
26 |
+
|
27 |
+
# Computing a few extra eigenvectors may speed up the eigen_solver.
|
28 |
+
# The spectral clustering quality may also benetif from requesting
|
29 |
+
# extra regions for segmentation.
|
30 |
+
n_regions_plus = 3
|
31 |
+
|
32 |
+
#Function for retrieving the plot
|
33 |
+
def getClusteringPlot(algorithm):
|
34 |
+
t0 = time.time()
|
35 |
+
labels = spectral_clustering(
|
36 |
+
graph,
|
37 |
+
n_clusters=(n_regions + n_regions_plus),
|
38 |
+
eigen_tol=1e-7,
|
39 |
+
assign_labels=algorithm,
|
40 |
+
random_state=42,
|
41 |
+
)
|
42 |
+
|
43 |
+
t1 = time.time()
|
44 |
+
labels = labels.reshape(rescaled_coins.shape)
|
45 |
+
plt.figure(figsize=(5, 5))
|
46 |
+
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
|
47 |
+
|
48 |
+
plt.xticks(())
|
49 |
+
plt.yticks(())
|
50 |
+
title = "Spectral clustering: %s, %.2fs" % (algorithm, (t1 - t0))
|
51 |
+
print(title)
|
52 |
+
plt.title(title)
|
53 |
+
for l in range(n_regions):
|
54 |
+
colors = [plt.cm.nipy_spectral((l + 4) / float(n_regions + 4))]
|
55 |
+
plt.contour(labels == l, colors=colors)
|
56 |
+
# To view individual segments as appear comment in plt.pause(0.5)
|
57 |
+
return plt
|
58 |
+
|
59 |
+
import gradio as gr
|
60 |
+
|
61 |
+
def welcome(name):
|
62 |
+
return f"Welcome to Gradio, {name}!"
|
63 |
+
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
gr.Markdown(
|
66 |
+
"""
|
67 |
+
# Segmenting the picture of greek coins in regions 🪙
|
68 |
+
An application of spectral clustering.
|
69 |
+
![Image of coins](coins.jpeg "a title")
|
70 |
+
|
71 |
+
""")
|
72 |
+
inp = gr.Radio(["kmeans", "discretize", "cluster_qr"], label="Solver", info="Choose a clustering algorithm", value="kmeans")
|
73 |
+
plot = gr.Plot(label="Plot")
|
74 |
+
inp.change(getClusteringPlot, inputs=inp, outputs=[plot])
|
75 |
+
demo.load(getClusteringPlot, inputs=[inp], outputs=[plot])
|
76 |
+
|
77 |
+
if __name__ = "main":
|
78 |
+
demo.launch()
|