Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,40 @@
|
|
1 |
import numpy as np
|
2 |
-
import gradio as gr
|
3 |
from skimage import io, color
|
4 |
-
from
|
5 |
-
from
|
|
|
6 |
|
7 |
-
def
|
8 |
-
"""
|
9 |
-
|
10 |
-
compressed_image = np.zeros_like(image, dtype=np.float64)
|
11 |
-
|
12 |
-
# Process each color channel separately
|
13 |
-
for channel in range(3): # Assuming RGB
|
14 |
-
U, S, Vt = np.linalg.svd(image[:, :, channel], full_matrices=False)
|
15 |
-
compressed_channel = np.dot(U[:, :k], np.dot(np.diag(S[:k]), Vt[:k, :]))
|
16 |
-
compressed_image[:, :, channel] = compressed_channel
|
17 |
-
|
18 |
-
# Clip values to valid range [0, 255] and convert to uint8
|
19 |
-
compressed_image = np.clip(compressed_image, 0, 255)
|
20 |
-
return compressed_image.astype(np.uint8)
|
21 |
|
22 |
-
def
|
23 |
-
"""
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
|
37 |
-
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
fn=process_image_color,
|
42 |
-
inputs=[
|
43 |
-
gr.Image(type="pil", label="Upload Image"),
|
44 |
-
gr.Slider(1, 100, step=1, value=50, label="Compression Rank")
|
45 |
-
],
|
46 |
-
outputs=gr.Image(type="pil", label="Compressed Image"),
|
47 |
-
title="Interactive Image Compression using SVD",
|
48 |
-
description="Upload a color image and adjust the compression rank to see the compressed version while retaining color."
|
49 |
-
)
|
50 |
|
51 |
-
|
|
|
|
1 |
import numpy as np
|
|
|
2 |
from skimage import io, color
|
3 |
+
from skimage.transform import resize
|
4 |
+
from sklearn.decomposition import TruncatedSVD
|
5 |
+
import streamlit as st
|
6 |
|
7 |
+
def resize_image(image, target_shape=(500, 500)):
|
8 |
+
"""Resize image to reduce processing time."""
|
9 |
+
return resize(image, target_shape, anti_aliasing=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
def truncated_svd_compress(image, k):
|
12 |
+
"""Compress the image using Truncated SVD by keeping only the top k singular values."""
|
13 |
+
svd = TruncatedSVD(n_components=k)
|
14 |
+
U = svd.fit_transform(image)
|
15 |
+
V = svd.components_
|
16 |
+
S = svd.singular_values_
|
17 |
+
compressed_image = np.dot(U, np.dot(np.diag(S), V))
|
18 |
+
return compressed_image
|
19 |
+
|
20 |
+
# Streamlit app
|
21 |
+
st.title("Fast Image Compression using Truncated SVD")
|
22 |
+
|
23 |
+
# Upload an image
|
24 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
25 |
+
if uploaded_file is not None:
|
26 |
+
# Load the image
|
27 |
+
image = io.imread(uploaded_file)
|
28 |
+
gray_image = color.rgb2gray(image)
|
29 |
|
30 |
+
# Resize the image for faster processing
|
31 |
+
resized_image = resize_image(gray_image)
|
32 |
|
33 |
+
# Select compression rank
|
34 |
+
k = st.slider("Select the rank for compression", min_value=1, max_value=min(resized_image.shape), value=50)
|
35 |
|
36 |
+
# Compress the image using Truncated SVD
|
37 |
+
compressed_image = truncated_svd_compress(resized_image, k)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Display original and compressed images
|
40 |
+
st.image([gray_image, compressed_image], caption=["Original", "Compressed"])
|