Spaces:
Sleeping
Sleeping
add all
Browse files- requirements.txt +4 -0
- svd_compression.py +105 -0
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy==2.0.2
|
| 2 |
+
matplotlib==3.9.4
|
| 3 |
+
scikit-image==0.24.0
|
| 4 |
+
streamlit==1.41.1
|
svd_compression.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
from skimage.io import imread
|
| 4 |
+
from skimage.color import rgb2gray
|
| 5 |
+
from skimage.metrics import structural_similarity as ssim
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
def load_image(path, gray=False):
|
| 10 |
+
img = imread(path)
|
| 11 |
+
if gray:
|
| 12 |
+
img = rgb2gray(img)
|
| 13 |
+
return img / 255.0 # Normalize to [0, 1]
|
| 14 |
+
|
| 15 |
+
def svd_compression(image, num_singular_values):
|
| 16 |
+
U, S, V = np.linalg.svd(image, full_matrices=False)
|
| 17 |
+
U_reduced = U[:, :num_singular_values]
|
| 18 |
+
S_reduced = np.diag(S[:num_singular_values])
|
| 19 |
+
V_reduced = V[:num_singular_values, :]
|
| 20 |
+
compressed_image = np.dot(U_reduced, np.dot(S_reduced, V_reduced))
|
| 21 |
+
return compressed_image, U, S, V
|
| 22 |
+
|
| 23 |
+
def svd_compression_rgb(image, num_singular_values):
|
| 24 |
+
channels = []
|
| 25 |
+
for i in range(image.shape[2]): # R, G, B channels
|
| 26 |
+
compressed_channel, _, _, _ = svd_compression(image[:, :, i], num_singular_values)
|
| 27 |
+
channels.append(compressed_channel)
|
| 28 |
+
return np.clip(np.stack(channels, axis=2), 0, 1)
|
| 29 |
+
|
| 30 |
+
def compute_metrics(original, compressed):
|
| 31 |
+
mse = np.mean((original - compressed) ** 2)
|
| 32 |
+
ssim_value = sum(ssim(original[:, :, i], compressed[:, :, i], data_range=compressed[:, :, i].max() - compressed[:, :, i].min()) for i in range(3)) / 3
|
| 33 |
+
return mse, ssim_value
|
| 34 |
+
|
| 35 |
+
def visualize_results(original, compressed, num_singular_values, mse, ssim_value):
|
| 36 |
+
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
|
| 37 |
+
ax[0].imshow(original)
|
| 38 |
+
ax[0].set_title("Original Image")
|
| 39 |
+
ax[0].axis("off")
|
| 40 |
+
|
| 41 |
+
ax[1].imshow(compressed)
|
| 42 |
+
ax[1].set_title(f"Compressed (k={num_singular_values})\nMSE: {mse:.2f}, SSIM: {ssim_value:.2f}")
|
| 43 |
+
ax[1].axis("off")
|
| 44 |
+
|
| 45 |
+
plt.tight_layout()
|
| 46 |
+
plt.show()
|
| 47 |
+
|
| 48 |
+
# Main script
|
| 49 |
+
# image = load_image('cow.jpg', gray=False)
|
| 50 |
+
# num_singular_values = 50
|
| 51 |
+
|
| 52 |
+
# compressed_image = svd_compression_rgb(image, num_singular_values)
|
| 53 |
+
|
| 54 |
+
# mse, ssim_value = compute_metrics(image, compressed_image)
|
| 55 |
+
# print(f"MSE: {mse:.2f}, SSIM: {ssim_value:.2f}")
|
| 56 |
+
|
| 57 |
+
# visualize_results(image, compressed_image, num_singular_values, mse, ssim_value)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def save_image_as_bytes(image):
|
| 62 |
+
buf = BytesIO()
|
| 63 |
+
plt.imsave(buf, image, format="jpg", cmap='gray')
|
| 64 |
+
buf.seek(0)
|
| 65 |
+
return buf
|
| 66 |
+
|
| 67 |
+
# Streamlit app code
|
| 68 |
+
st.title("Image Compression using SVD")
|
| 69 |
+
|
| 70 |
+
# Upload Image
|
| 71 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 72 |
+
|
| 73 |
+
if uploaded_file is not None:
|
| 74 |
+
# Load the image
|
| 75 |
+
image = load_image(uploaded_file, gray=False)
|
| 76 |
+
|
| 77 |
+
# Show original image
|
| 78 |
+
st.image(image, caption="Original Image", use_container_width=True)
|
| 79 |
+
|
| 80 |
+
# User input for the number of singular values
|
| 81 |
+
num_singular_values = st.slider("Select number of singular values (k)", min_value=1, max_value=100, value=50)
|
| 82 |
+
|
| 83 |
+
# Perform SVD compression
|
| 84 |
+
compressed_image = svd_compression_rgb(image, num_singular_values)
|
| 85 |
+
|
| 86 |
+
# Compute metrics
|
| 87 |
+
mse, ssim_value = compute_metrics(image, compressed_image)
|
| 88 |
+
|
| 89 |
+
# Show compressed image
|
| 90 |
+
st.image(compressed_image, caption=f"Compressed Image (k={num_singular_values})", use_container_width=True)
|
| 91 |
+
|
| 92 |
+
# Display metrics
|
| 93 |
+
st.write(f"MSE: {mse:.2f}")
|
| 94 |
+
st.write(f"SSIM: {ssim_value:.2f}")
|
| 95 |
+
|
| 96 |
+
# Save compressed image as bytes for download
|
| 97 |
+
compressed_image_bytes = save_image_as_bytes(compressed_image)
|
| 98 |
+
|
| 99 |
+
# Download button for the compressed image
|
| 100 |
+
st.download_button(
|
| 101 |
+
label="Download Compressed Image",
|
| 102 |
+
data=compressed_image_bytes,
|
| 103 |
+
file_name="compressed_image.png",
|
| 104 |
+
mime="image/png"
|
| 105 |
+
)
|