Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import altair as alt
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
import io
|
| 8 |
+
import zipfile
|
| 9 |
+
|
| 10 |
+
st.title("Image Augmentation App")
|
| 11 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 12 |
+
|
| 13 |
+
if uploaded_image:
|
| 14 |
+
img = np.array(Image.open(uploaded_image))
|
| 15 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 16 |
+
|
| 17 |
+
st.write("**Applying augmentations...**")
|
| 18 |
+
|
| 19 |
+
transformations = {
|
| 20 |
+
"Translated": cv2.warpAffine(img, np.float32([[1, 0, 50], [0, 1, 50]]), (img.shape[1], img.shape[0])),
|
| 21 |
+
"Scaled": cv2.resize(img, None, fx=1.2, fy=1.2),
|
| 22 |
+
"Rotated": cv2.warpAffine(img, cv2.getRotationMatrix2D((img.shape[1] // 2, img.shape[0] // 2), 45, 1), (img.shape[1], img.shape[0])),
|
| 23 |
+
"Cropped": img[50:img.shape[0] - 50, 50:img.shape[1] - 50]
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
augmented_images = []
|
| 27 |
+
zip_buffer = io.BytesIO()
|
| 28 |
+
|
| 29 |
+
with zipfile.ZipFile(zip_buffer, "w") as zipf:
|
| 30 |
+
for i, (name, transformed_img) in enumerate(transformations.items()):
|
| 31 |
+
img_pil = Image.fromarray(cv2.cvtColor(transformed_img, cv2.COLOR_BGR2RGB))
|
| 32 |
+
img_bytes = io.BytesIO()
|
| 33 |
+
img_pil.save(img_bytes, format="JPEG")
|
| 34 |
+
zipf.writestr(f"aug_{i + 1}_{name}.jpg", img_bytes.getvalue())
|
| 35 |
+
augmented_images.append(img_pil)
|
| 36 |
+
|
| 37 |
+
st.write("**Augmentation completed!**")
|
| 38 |
+
st.image(augmented_images, width=150)
|
| 39 |
+
|
| 40 |
+
zip_buffer.seek(0)
|
| 41 |
+
st.download_button("Download Augmented Images", data=zip_buffer, file_name="augmented_images.zip", mime="application/zip")
|
| 42 |
+
|