Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
iface.launch()
|