Spaces:
Sleeping
Sleeping
Update src / streamlit_app.py
Browse files- src / streamlit_app.py +63 -0
src / streamlit_app.py
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
from zipfile import ZipFile
|
| 7 |
+
|
| 8 |
+
# Title and description of the app
|
| 9 |
+
st.title("Image Processing App - Image Augmentations")
|
| 10 |
+
st.markdown("""
|
| 11 |
+
Upload an image to perform augmentations such as translation, scaling, rotation, cropping, and shearing.
|
| 12 |
+
Ten augmented images will be generated, and you can download them.
|
| 13 |
+
""")
|
| 14 |
+
|
| 15 |
+
# Upload an image
|
| 16 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 17 |
+
|
| 18 |
+
if uploaded_image is not None:
|
| 19 |
+
# Load and display the uploaded image
|
| 20 |
+
img = Image.open(uploaded_image)
|
| 21 |
+
img_array = np.array(img)
|
| 22 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 23 |
+
|
| 24 |
+
# Prepare the output directory for augmented images
|
| 25 |
+
output_dir = "augmented_images"
|
| 26 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 27 |
+
|
| 28 |
+
# Define augmentations
|
| 29 |
+
st.write("**Performing augmentations...**")
|
| 30 |
+
transformations = [
|
| 31 |
+
("Translated", cv2.warpAffine(img_array, np.float32([[1, 0, 50], [0, 1, 50]]), (img_array.shape[1], img_array.shape[0]))),
|
| 32 |
+
("Scaled", cv2.resize(img_array, None, fx=1.2, fy=1.2)),
|
| 33 |
+
("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]))),
|
| 34 |
+
("Cropped", img_array[50:img_array.shape[0] - 50, 50:img_array.shape[1] - 50]),
|
| 35 |
+
("Sheared", cv2.warpAffine(img_array, np.float32([[1, 0.5, 0], [0.5, 1, 0]]), (img_array.shape[1], img_array.shape[0])))
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
# Apply and save augmentations
|
| 39 |
+
augmented_images = []
|
| 40 |
+
for i, (name, aug_img) in enumerate(transformations * 2): # Duplicate to generate 10 images
|
| 41 |
+
output_path = os.path.join(output_dir, f"aug_{i + 1}_{name}.jpg")
|
| 42 |
+
cv2.imwrite(output_path, aug_img)
|
| 43 |
+
augmented_images.append(output_path)
|
| 44 |
+
|
| 45 |
+
st.write("**Augmentation completed!**")
|
| 46 |
+
|
| 47 |
+
# Display augmented images
|
| 48 |
+
st.image([Image.open(img_path) for img_path in augmented_images], caption=[f"Image {i + 1}" for i in range(10)], width=150)
|
| 49 |
+
|
| 50 |
+
# Create a ZIP file for download
|
| 51 |
+
zip_filename = "augmented_images.zip"
|
| 52 |
+
with ZipFile(zip_filename, 'w') as zipf:
|
| 53 |
+
for file_path in augmented_images:
|
| 54 |
+
zipf.write(file_path, os.path.basename(file_path))
|
| 55 |
+
|
| 56 |
+
# Add a download button for the ZIP file
|
| 57 |
+
with open(zip_filename, "rb") as fp:
|
| 58 |
+
st.download_button(
|
| 59 |
+
label="Download Augmented Images",
|
| 60 |
+
data=fp,
|
| 61 |
+
file_name="augmented_images.zip",
|
| 62 |
+
mime="application/zip"
|
| 63 |
+
)
|