MuskanMjn commited on
Commit
2ca5ac0
1 Parent(s): 53a3e8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -35
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
- # load the coins as a numpy array
13
- orig_coins = coins()
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
- # Convert the image into a graph with the value of the gradient on the
22
- # edges.
23
- graph = image.img_to_graph(rescaled_coins)
24
 
25
- # Take a decreasing function of the gradient: an exponential
26
- # The smaller beta is, the more independent the segmentation is of the
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
- # The number of segmented regions to display needs to be chosen manually.
33
- # The current version of 'spectral_clustering' does not support determining
34
- # the number of good quality clusters automatically.
35
- n_regions = 26
36
 
37
- # Computing a few extra eigenvectors may speed up the eigen_solver.
38
- # The spectral clustering quality may also benetif from requesting
39
- # extra regions for segmentation.
40
- n_regions_plus = 3
41
 
42
- #Function for retrieving the plot
43
- def getClusteringPlot(algorithm):
 
 
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
- if __name__ == "main":
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()