Spaces:
Sleeping
Sleeping
Update pages/Image_Augmentation_Handson.py
Browse files
pages/Image_Augmentation_Handson.py
CHANGED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Background color for the app
|
| 7 |
+
st.markdown(
|
| 8 |
+
"""
|
| 9 |
+
<style>
|
| 10 |
+
.stApp {
|
| 11 |
+
background-color: #1E1E1E;
|
| 12 |
+
color: #FFFFFF;
|
| 13 |
+
}
|
| 14 |
+
</style>
|
| 15 |
+
""",
|
| 16 |
+
unsafe_allow_html=True,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
st.title("🔧 Image Data Augmentation Tool")
|
| 20 |
+
st.write("Enhance your images with various augmentation techniques.")
|
| 21 |
+
|
| 22 |
+
# Image uploader
|
| 23 |
+
uploaded_image = st.file_uploader("📤 Upload an Image (jpg, png, jpeg)", type=["jpg", "png", "jpeg"])
|
| 24 |
+
|
| 25 |
+
if uploaded_image:
|
| 26 |
+
try:
|
| 27 |
+
image = Image.open(uploaded_image)
|
| 28 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 29 |
+
|
| 30 |
+
st.subheader("🎨 Choose Augmentation Options")
|
| 31 |
+
|
| 32 |
+
# Augmentation option
|
| 33 |
+
augmentation_option = st.selectbox(
|
| 34 |
+
"Select an Augmentation Technique:",
|
| 35 |
+
[
|
| 36 |
+
"None",
|
| 37 |
+
"Cropping",
|
| 38 |
+
"Flipping",
|
| 39 |
+
"Rotation",
|
| 40 |
+
"Zooming In",
|
| 41 |
+
"Zooming Out",
|
| 42 |
+
"Translation",
|
| 43 |
+
"Shearing",
|
| 44 |
+
"Grayscale",
|
| 45 |
+
"Invert Colors",
|
| 46 |
+
],
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
augmented_image = image.copy()
|
| 50 |
+
|
| 51 |
+
# Cropping
|
| 52 |
+
if augmentation_option == "Cropping":
|
| 53 |
+
col1, col2 = st.columns(2)
|
| 54 |
+
with col1:
|
| 55 |
+
left = st.slider("Left Crop", 0, image.width // 2, 0)
|
| 56 |
+
top = st.slider("Top Crop", 0, image.height // 2, 0)
|
| 57 |
+
with col2:
|
| 58 |
+
right = st.slider("Right Crop", 0, image.width // 2, 0)
|
| 59 |
+
bottom = st.slider("Bottom Crop", 0, image.height // 2, 0)
|
| 60 |
+
augmented_image = image.crop((left, top, image.width - right, image.height - bottom))
|
| 61 |
+
|
| 62 |
+
# Flipping
|
| 63 |
+
elif augmentation_option == "Flipping":
|
| 64 |
+
flip_option = st.radio("Flip Type", ["Horizontal", "Vertical"])
|
| 65 |
+
augmented_image = ImageOps.mirror(image) if flip_option == "Horizontal" else ImageOps.flip(image)
|
| 66 |
+
|
| 67 |
+
# Rotation
|
| 68 |
+
elif augmentation_option == "Rotation":
|
| 69 |
+
rotation_angle = st.slider("Rotation Angle (°)", -180, 180, 0)
|
| 70 |
+
augmented_image = image.rotate(rotation_angle, expand=True)
|
| 71 |
+
|
| 72 |
+
# Zooming In
|
| 73 |
+
elif augmentation_option == "Zooming In":
|
| 74 |
+
scale_factor = st.slider("Zoom Factor (%)", 100, 200, 100) / 100.0
|
| 75 |
+
if scale_factor != 1.0:
|
| 76 |
+
new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
|
| 77 |
+
zoomed_image = image.resize(new_size)
|
| 78 |
+
augmented_image = zoomed_image.crop(((new_size[0] - image.width) // 2,
|
| 79 |
+
(new_size[1] - image.height) // 2,
|
| 80 |
+
(new_size[0] + image.width) // 2,
|
| 81 |
+
(new_size[1] + image.height) // 2))
|
| 82 |
+
|
| 83 |
+
# Zooming Out
|
| 84 |
+
elif augmentation_option == "Zooming Out":
|
| 85 |
+
scale_factor = st.slider("Zoom Factor (%)", 50, 100, 100) / 100.0
|
| 86 |
+
new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
|
| 87 |
+
zoomed_image = image.resize(new_size)
|
| 88 |
+
padded_image = Image.new("RGB", (image.width, image.height), (255, 255, 255))
|
| 89 |
+
padded_image.paste(zoomed_image, ((image.width - new_size[0]) // 2, (image.height - new_size[1]) // 2))
|
| 90 |
+
augmented_image = padded_image
|
| 91 |
+
|
| 92 |
+
# Translation
|
| 93 |
+
elif augmentation_option == "Translation":
|
| 94 |
+
translation_x = st.slider("Horizontal Shift (px)", -100, 100, 0)
|
| 95 |
+
translation_y = st.slider("Vertical Shift (px)", -100, 100, 0)
|
| 96 |
+
augmented_image = image.transform(
|
| 97 |
+
image.size, Image.AFFINE, (1, 0, translation_x, 0, 1, translation_y), fillcolor=(255, 255, 255)
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Shearing
|
| 101 |
+
elif augmentation_option == "Shearing":
|
| 102 |
+
shear_angle = st.slider("Shearing Angle (°)", -45, 45, 0)
|
| 103 |
+
shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
|
| 104 |
+
augmented_image = image.transform(
|
| 105 |
+
(image.width + abs(int(image.height * np.tan(np.radians(shear_angle)))), image.height),
|
| 106 |
+
Image.AFFINE,
|
| 107 |
+
shear_matrix,
|
| 108 |
+
fillcolor=(255, 255, 255),
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Grayscale
|
| 112 |
+
elif augmentation_option == "Grayscale":
|
| 113 |
+
augmented_image = ImageOps.grayscale(image)
|
| 114 |
+
|
| 115 |
+
# Invert Colors
|
| 116 |
+
elif augmentation_option == "Invert Colors":
|
| 117 |
+
augmented_image = ImageOps.invert(image)
|
| 118 |
+
|
| 119 |
+
# Display augmented image
|
| 120 |
+
st.subheader("🖼️ Augmented Image Preview")
|
| 121 |
+
st.image(augmented_image, caption=f"{augmentation_option} Applied", use_column_width=True)
|
| 122 |
+
|
| 123 |
+
# Download button
|
| 124 |
+
buffer = io.BytesIO()
|
| 125 |
+
augmented_image.save(buffer, format="PNG")
|
| 126 |
+
buffer.seek(0)
|
| 127 |
+
st.download_button("💾 Download Augmented Image", buffer, file_name="augmented_image.png")
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
st.error(f"❌ An error occurred: {e}")
|