Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,66 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import numpy as np
|
3 |
from sklearn.cluster import KMeans
|
4 |
from sklearn.neighbors import NearestNeighbors
|
5 |
|
|
|
6 |
def knn_color_quantization(image, n_colors):
|
|
|
7 |
pixels = np.reshape(image, (image.shape[0] * image.shape[1], image.shape[2]))
|
8 |
|
|
|
9 |
kmeans = KMeans(n_clusters=n_colors, random_state=42)
|
10 |
kmeans.fit(pixels)
|
11 |
|
|
|
12 |
kmeans_centers = kmeans.cluster_centers_
|
|
|
|
|
13 |
knn = NearestNeighbors(n_neighbors=1)
|
14 |
knn.fit(kmeans_centers)
|
15 |
|
|
|
16 |
distances, indices = knn.kneighbors(pixels)
|
17 |
quantized_pixels = kmeans_centers[indices.flatten()]
|
18 |
-
|
19 |
quantized_image = np.reshape(quantized_pixels, image.shape)
|
20 |
-
quantized_image = quantized_image / 255.0
|
21 |
|
|
|
|
|
22 |
|
23 |
return quantized_image
|
24 |
|
|
|
25 |
def color_quantization_app(image, num_colors):
|
|
|
26 |
quantized_image = knn_color_quantization(image, num_colors)
|
|
|
27 |
quantized_image = (quantized_image * 255).astype(np.uint8)
|
28 |
return quantized_image
|
29 |
|
30 |
-
|
31 |
-
#themes
|
32 |
-
#freddyaboulton/dracula_revamped
|
33 |
-
#freddyaboulton/test-blue
|
34 |
-
#Insuz/Mocha
|
35 |
-
#Taithrah/Minimal
|
36 |
-
|
37 |
-
dark_minimalist = gr.Theme.from_hub("Taithrah/Minimal")
|
38 |
-
|
39 |
iface = gr.Interface(
|
|
|
40 |
fn=color_quantization_app,
|
|
|
41 |
inputs=[gr.Image(label="Uploaded Image", sources='upload', type="numpy"), gr.Slider(minimum=2,maximum=300,value=8, label='Number of Colors',interactive=True)],
|
|
|
42 |
outputs=gr.Image(label="Quantized Image"),
|
|
|
43 |
live=False,
|
|
|
44 |
allow_flagging="never",
|
|
|
45 |
theme=dark_minimalist,
|
|
|
46 |
css="""
|
47 |
footer {
|
48 |
visibility: hidden;
|
49 |
}
|
50 |
"""
|
51 |
)
|
52 |
-
|
|
|
|
|
|
1 |
+
# Importing the Gradio library for creating interactive web interfaces
|
2 |
import gradio as gr
|
3 |
+
|
4 |
+
# Importing necessary libraries for image processing
|
5 |
import numpy as np
|
6 |
from sklearn.cluster import KMeans
|
7 |
from sklearn.neighbors import NearestNeighbors
|
8 |
|
9 |
+
# Function to perform k-means color quantization on an image
|
10 |
def knn_color_quantization(image, n_colors):
|
11 |
+
# Reshaping the image into a 2D array of pixels
|
12 |
pixels = np.reshape(image, (image.shape[0] * image.shape[1], image.shape[2]))
|
13 |
|
14 |
+
# Performing k-means clustering to find the dominant colors
|
15 |
kmeans = KMeans(n_clusters=n_colors, random_state=42)
|
16 |
kmeans.fit(pixels)
|
17 |
|
18 |
+
# Getting the cluster centers as the representative colors
|
19 |
kmeans_centers = kmeans.cluster_centers_
|
20 |
+
|
21 |
+
# Fitting a nearest neighbors model to find the closest color for each pixel
|
22 |
knn = NearestNeighbors(n_neighbors=1)
|
23 |
knn.fit(kmeans_centers)
|
24 |
|
25 |
+
# Finding the closest color for each pixel and reconstructing the quantized image
|
26 |
distances, indices = knn.kneighbors(pixels)
|
27 |
quantized_pixels = kmeans_centers[indices.flatten()]
|
|
|
28 |
quantized_image = np.reshape(quantized_pixels, image.shape)
|
|
|
29 |
|
30 |
+
# Scaling the image values to the range [0, 1]
|
31 |
+
quantized_image = quantized_image / 255.0
|
32 |
|
33 |
return quantized_image
|
34 |
|
35 |
+
# Function to create the Gradio interface for color quantization
|
36 |
def color_quantization_app(image, num_colors):
|
37 |
+
# Calling the knn_color_quantization function to quantize the image
|
38 |
quantized_image = knn_color_quantization(image, num_colors)
|
39 |
+
# Converting the pixel values back to the range [0, 255] and casting to uint8
|
40 |
quantized_image = (quantized_image * 255).astype(np.uint8)
|
41 |
return quantized_image
|
42 |
|
43 |
+
# Creating a Gradio interface object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
iface = gr.Interface(
|
45 |
+
# Specifying the function to be executed
|
46 |
fn=color_quantization_app,
|
47 |
+
# Defining the inputs for the interface (an uploaded image and a slider for number of colors)
|
48 |
inputs=[gr.Image(label="Uploaded Image", sources='upload', type="numpy"), gr.Slider(minimum=2,maximum=300,value=8, label='Number of Colors',interactive=True)],
|
49 |
+
# Defining the output for the interface (quantized image)
|
50 |
outputs=gr.Image(label="Quantized Image"),
|
51 |
+
# Disabling live updates
|
52 |
live=False,
|
53 |
+
# Disabling user flagging of outputs
|
54 |
allow_flagging="never",
|
55 |
+
# Applying a custom theme retrieved from Gradio Hub
|
56 |
theme=dark_minimalist,
|
57 |
+
# Adding custom CSS to hide the footer of the interface
|
58 |
css="""
|
59 |
footer {
|
60 |
visibility: hidden;
|
61 |
}
|
62 |
"""
|
63 |
)
|
64 |
+
|
65 |
+
# Launching the Gradio interface
|
66 |
+
iface.launch()
|