Spaces:
Runtime error
Runtime error
Johannes
commited on
Commit
·
b443107
1
Parent(s):
5b747a4
more description + bins slider
Browse files
app.py
CHANGED
@@ -44,21 +44,21 @@ def build_compressed_plot(compressed_image, img_array, sampling: str) -> plt.Fig
|
|
44 |
return compressed_text, fig
|
45 |
|
46 |
|
47 |
-
def infer(img_array: np.ndarray, sampling: str):
|
48 |
# greyscale_image = input_image.convert("L")
|
49 |
# img_array = np.array(greyscale_image)
|
50 |
|
51 |
#raccoon_face = face(gray=True)
|
52 |
init_text, init_fig = build_init_plot(img_array)
|
53 |
|
54 |
-
n_bins =
|
55 |
encoder = KBinsDiscretizer(
|
56 |
n_bins=n_bins, encode="ordinal", strategy=sampling, random_state=0
|
57 |
)
|
58 |
compressed_image = encoder.fit_transform(img_array.reshape(-1, 1)).reshape(
|
59 |
img_array.shape
|
60 |
)
|
61 |
-
|
62 |
compressed_text, compressed_fig = build_compressed_plot(compressed_image,
|
63 |
img_array,
|
64 |
sampling)
|
@@ -83,13 +83,19 @@ Demo by <a href='https://huggingface.co/johko' target='_blank'>Johannes (johko)
|
|
83 |
gr.Interface(
|
84 |
title="Vector Quantization with scikit-learn",
|
85 |
description="""<p style="text-align: center;">This is an interactive demo for the <a href="https://scikit-learn.org/stable/auto_examples/cluster/plot_face_compress.html">Vector Quantization Tutorial</a> from scikit-learn.
|
86 |
-
</br>
|
87 |
-
</
|
|
|
|
|
|
|
|
|
|
|
88 |
</p>""",
|
89 |
article=article,
|
90 |
fn=infer,
|
91 |
inputs=[gr.Image(image_mode="L", label="Input Image"),
|
92 |
-
gr.Dropdown(choices=["uniform", "kmeans"], label="Sampling Method")
|
|
|
93 |
outputs=[gr.Text(label="Original Image Stats"),
|
94 |
gr.Plot(label="Original Image Histogram"),
|
95 |
gr.Text(label="Compressed Image Stats"),
|
|
|
44 |
return compressed_text, fig
|
45 |
|
46 |
|
47 |
+
def infer(img_array: np.ndarray, sampling: str, number_of_bins: int):
|
48 |
# greyscale_image = input_image.convert("L")
|
49 |
# img_array = np.array(greyscale_image)
|
50 |
|
51 |
#raccoon_face = face(gray=True)
|
52 |
init_text, init_fig = build_init_plot(img_array)
|
53 |
|
54 |
+
n_bins = number_of_bins
|
55 |
encoder = KBinsDiscretizer(
|
56 |
n_bins=n_bins, encode="ordinal", strategy=sampling, random_state=0
|
57 |
)
|
58 |
compressed_image = encoder.fit_transform(img_array.reshape(-1, 1)).reshape(
|
59 |
img_array.shape
|
60 |
)
|
61 |
+
compressed_image = compressed_image.astype(np.uint8)
|
62 |
compressed_text, compressed_fig = build_compressed_plot(compressed_image,
|
63 |
img_array,
|
64 |
sampling)
|
|
|
83 |
gr.Interface(
|
84 |
title="Vector Quantization with scikit-learn",
|
85 |
description="""<p style="text-align: center;">This is an interactive demo for the <a href="https://scikit-learn.org/stable/auto_examples/cluster/plot_face_compress.html">Vector Quantization Tutorial</a> from scikit-learn.
|
86 |
+
</br><b>Vector Quantization</b> is a compression technique to reduce the number of color values that are used in an image and with this save memory while trying to keep a good quality.
|
87 |
+
In this demo this can be done naively via <i>uniform</i> sampling, which just uses <i>N</i> color values (specified via slider) uniformly sampled from the whole spectrum or via <i>k-means</i> which pays closer attention
|
88 |
+
to the actual pixel distribution and potentially leads to a better quality of the compressed image.
|
89 |
+
In this demo we actually won't see a compression effect, because we cannot go smaller than <i>uint8</i> in datatype size here.
|
90 |
+
</br>
|
91 |
+
</br><b>Usage</b>: To run the demo you can simply upload an image and choose from two sampling methods - <i>uniform</i> and <i>kmeans</i>. Choose the number of bins and then click 'submit'.
|
92 |
+
You will get information about the histogram, pixels distribution and other image statistics for your orginial image as grayscale and the quantized version of it.
|
93 |
</p>""",
|
94 |
article=article,
|
95 |
fn=infer,
|
96 |
inputs=[gr.Image(image_mode="L", label="Input Image"),
|
97 |
+
gr.Dropdown(choices=["uniform", "kmeans"], label="Sampling Method"),
|
98 |
+
gr.Slider(minimum=2, maximum=50, value=8, step=1, label="Number of Bins")],
|
99 |
outputs=[gr.Text(label="Original Image Stats"),
|
100 |
gr.Plot(label="Original Image Histogram"),
|
101 |
gr.Text(label="Compressed Image Stats"),
|