Image_Augmentation / src /streamlit_app.py
Ramyamaheswari's picture
Rename src / streamlit_app.py to src/streamlit_app.py
147448d verified
import streamlit as st
from PIL import Image
import cv2
import numpy as np
import os
from zipfile import ZipFile
# Title and description of the app
st.title("Image Processing App - Image Augmentations")
st.markdown("""
Upload an image to perform augmentations such as translation, scaling, rotation, cropping, and shearing.
Ten augmented images will be generated, and you can download them.
""")
# Upload an image
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
# Load and display the uploaded image
img = Image.open(uploaded_image)
img_array = np.array(img)
st.image(img, caption="Uploaded Image", use_column_width=True)
# Prepare the output directory for augmented images
output_dir = "augmented_images"
os.makedirs(output_dir, exist_ok=True)
# Define augmentations
st.write("**Performing augmentations...**")
transformations = [
("Translated", cv2.warpAffine(img_array, np.float32([[1, 0, 50], [0, 1, 50]]), (img_array.shape[1], img_array.shape[0]))),
("Scaled", cv2.resize(img_array, None, fx=1.2, fy=1.2)),
("Rotated", cv2.warpAffine(img_array, cv2.getRotationMatrix2D((img_array.shape[1] // 2, img_array.shape[0] // 2), 45, 1), (img_array.shape[1], img_array.shape[0]))),
("Cropped", img_array[50:img_array.shape[0] - 50, 50:img_array.shape[1] - 50]),
("Sheared", cv2.warpAffine(img_array, np.float32([[1, 0.5, 0], [0.5, 1, 0]]), (img_array.shape[1], img_array.shape[0])))
]
# Apply and save augmentations
augmented_images = []
for i, (name, aug_img) in enumerate(transformations * 2): # Duplicate to generate 10 images
output_path = os.path.join(output_dir, f"aug_{i + 1}_{name}.jpg")
cv2.imwrite(output_path, aug_img)
augmented_images.append(output_path)
st.write("**Augmentation completed!**")
# Display augmented images
st.image([Image.open(img_path) for img_path in augmented_images], caption=[f"Image {i + 1}" for i in range(10)], width=150)
# Create a ZIP file for download
zip_filename = "augmented_images.zip"
with ZipFile(zip_filename, 'w') as zipf:
for file_path in augmented_images:
zipf.write(file_path, os.path.basename(file_path))
# Add a download button for the ZIP file
with open(zip_filename, "rb") as fp:
st.download_button(
label="Download Augmented Images",
data=fp,
file_name="augmented_images.zip",
mime="application/zip"
)