Spaces:
Sleeping
Sleeping
Update pages/Image_Augmentation_Handson.py
Browse files
pages/Image_Augmentation_Handson.py
CHANGED
|
@@ -3,31 +3,43 @@ from PIL import Image, ImageOps
|
|
| 3 |
import numpy as np
|
| 4 |
import io
|
| 5 |
|
| 6 |
-
#
|
| 7 |
st.markdown(
|
| 8 |
"""
|
| 9 |
<style>
|
| 10 |
.stApp {
|
| 11 |
-
background-color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
</style>
|
| 14 |
""",
|
| 15 |
unsafe_allow_html=True,
|
| 16 |
)
|
| 17 |
|
| 18 |
-
st.title("Image
|
| 19 |
|
| 20 |
-
|
| 21 |
-
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
| 22 |
|
| 23 |
if uploaded_image:
|
| 24 |
try:
|
| 25 |
image = Image.open(uploaded_image)
|
| 26 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 27 |
-
|
| 28 |
-
st.subheader("Select Augmentation Options")
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
augmentation_option = st.radio(
|
| 32 |
"Choose an augmentation:",
|
| 33 |
[
|
|
@@ -35,8 +47,8 @@ if uploaded_image:
|
|
| 35 |
"Cropping",
|
| 36 |
"Flipping",
|
| 37 |
"Rotation",
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
"Translation",
|
| 41 |
"Shearing",
|
| 42 |
],
|
|
@@ -55,7 +67,7 @@ if uploaded_image:
|
|
| 55 |
|
| 56 |
# Flipping
|
| 57 |
elif augmentation_option == "Flipping":
|
| 58 |
-
flip_option = st.selectbox("Flip Type", ["Horizontal", "Vertical"])
|
| 59 |
if flip_option == "Horizontal":
|
| 60 |
augmented_image = ImageOps.mirror(image)
|
| 61 |
elif flip_option == "Vertical":
|
|
@@ -63,68 +75,63 @@ if uploaded_image:
|
|
| 63 |
|
| 64 |
# Rotation
|
| 65 |
elif augmentation_option == "Rotation":
|
| 66 |
-
rotation_angle = st.slider("Rotation (°)", 0, 360, 0)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
# Translation
|
| 94 |
elif augmentation_option == "Translation":
|
| 95 |
-
translation_x = st.slider("Horizontal Shift (Pixels)", -100, 100, 0)
|
| 96 |
-
translation_y = st.slider("Vertical Shift (Pixels)", -100, 100, 0)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
)
|
| 102 |
|
| 103 |
# Shearing
|
| 104 |
elif augmentation_option == "Shearing":
|
| 105 |
-
shear_angle = st.slider("Shearing Angle (°)", -45, 45, 0)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
)
|
| 114 |
except Exception as aug_error:
|
| 115 |
-
st.error(f"
|
| 116 |
|
| 117 |
-
|
| 118 |
-
st.subheader("Augmented Image")
|
| 119 |
st.image(augmented_image, caption=f"{augmentation_option} Applied", use_container_width=True)
|
| 120 |
|
| 121 |
-
# Download augmented image
|
| 122 |
buffer = io.BytesIO()
|
| 123 |
augmented_image.save(buffer, format="PNG")
|
| 124 |
buffer.seek(0)
|
| 125 |
-
st.download_button(
|
| 126 |
-
"Download Augmented Image🎃", buffer, file_name="augmented_image.png"
|
| 127 |
-
)
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
-
st.error(f"An error occurred: {e}")
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
# Custom CSS for enhanced styling
|
| 7 |
st.markdown(
|
| 8 |
"""
|
| 9 |
<style>
|
| 10 |
.stApp {
|
| 11 |
+
background-color: #121212;
|
| 12 |
+
color: #E0E0E0;
|
| 13 |
+
}
|
| 14 |
+
.stTitle {
|
| 15 |
+
text-align: center;
|
| 16 |
+
color: #4CAF50;
|
| 17 |
+
}
|
| 18 |
+
.css-1d391kg p, .css-1v0mbdj p {
|
| 19 |
+
color: #B0BEC5;
|
| 20 |
+
}
|
| 21 |
+
.css-1aumxhk .stSlider {
|
| 22 |
+
background-color: #4CAF50;
|
| 23 |
+
}
|
| 24 |
+
.css-2trqyj, .css-16idsys p {
|
| 25 |
+
color: #BB86FC;
|
| 26 |
}
|
| 27 |
</style>
|
| 28 |
""",
|
| 29 |
unsafe_allow_html=True,
|
| 30 |
)
|
| 31 |
|
| 32 |
+
st.title("🌟 Enhanced Image Augmentation Tool", anchor=False)
|
| 33 |
|
| 34 |
+
uploaded_image = st.file_uploader("📤 Upload an Image", type=["jpg", "png", "jpeg"])
|
|
|
|
| 35 |
|
| 36 |
if uploaded_image:
|
| 37 |
try:
|
| 38 |
image = Image.open(uploaded_image)
|
| 39 |
+
st.image(image, caption="📸 Uploaded Image", use_container_width=True)
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
st.subheader("✨ Select Augmentation Options")
|
| 42 |
+
|
| 43 |
augmentation_option = st.radio(
|
| 44 |
"Choose an augmentation:",
|
| 45 |
[
|
|
|
|
| 47 |
"Cropping",
|
| 48 |
"Flipping",
|
| 49 |
"Rotation",
|
| 50 |
+
"Zoom In",
|
| 51 |
+
"Zoom Out",
|
| 52 |
"Translation",
|
| 53 |
"Shearing",
|
| 54 |
],
|
|
|
|
| 67 |
|
| 68 |
# Flipping
|
| 69 |
elif augmentation_option == "Flipping":
|
| 70 |
+
flip_option = st.selectbox("🔄 Flip Type", ["Horizontal", "Vertical"])
|
| 71 |
if flip_option == "Horizontal":
|
| 72 |
augmented_image = ImageOps.mirror(image)
|
| 73 |
elif flip_option == "Vertical":
|
|
|
|
| 75 |
|
| 76 |
# Rotation
|
| 77 |
elif augmentation_option == "Rotation":
|
| 78 |
+
rotation_angle = st.slider("⤾ Rotation (°)", 0, 360, 0)
|
| 79 |
+
augmented_image = image.rotate(rotation_angle, expand=True)
|
| 80 |
+
|
| 81 |
+
# Zoom In
|
| 82 |
+
elif augmentation_option == "Zoom In":
|
| 83 |
+
scale_factor = st.slider("🔍 Zoom Factor (%)", 100, 200, 100) / 100.0
|
| 84 |
+
width, height = image.size
|
| 85 |
+
new_width = int(width * scale_factor)
|
| 86 |
+
new_height = int(height * scale_factor)
|
| 87 |
+
zoomed_image = image.resize((new_width, new_height))
|
| 88 |
+
augmented_image = zoomed_image.crop((
|
| 89 |
+
(new_width - width) // 2,
|
| 90 |
+
(new_height - height) // 2,
|
| 91 |
+
(new_width + width) // 2,
|
| 92 |
+
(new_height + height) // 2,
|
| 93 |
+
))
|
| 94 |
+
|
| 95 |
+
# Zoom Out
|
| 96 |
+
elif augmentation_option == "Zoom Out":
|
| 97 |
+
scale_factor = st.slider("🔎 Zoom Out Factor (%)", 50, 100, 100) / 100.0
|
| 98 |
+
width, height = image.size
|
| 99 |
+
new_width = int(width * scale_factor)
|
| 100 |
+
new_height = int(height * scale_factor)
|
| 101 |
+
zoomed_image = image.resize((new_width, new_height))
|
| 102 |
+
padded_image = Image.new("RGB", (width, height), (0, 0, 0))
|
| 103 |
+
padded_image.paste(zoomed_image, ((width - new_width) // 2, (height - new_height) // 2))
|
| 104 |
+
augmented_image = padded_image
|
| 105 |
|
| 106 |
# Translation
|
| 107 |
elif augmentation_option == "Translation":
|
| 108 |
+
translation_x = st.slider("↔ Horizontal Shift (Pixels)", -100, 100, 0)
|
| 109 |
+
translation_y = st.slider("↕ Vertical Shift (Pixels)", -100, 100, 0)
|
| 110 |
+
translation_matrix = (1, 0, translation_x, 0, 1, translation_y)
|
| 111 |
+
augmented_image = image.transform(
|
| 112 |
+
image.size, Image.AFFINE, translation_matrix, fillcolor=(0, 0, 0)
|
| 113 |
+
)
|
|
|
|
| 114 |
|
| 115 |
# Shearing
|
| 116 |
elif augmentation_option == "Shearing":
|
| 117 |
+
shear_angle = st.slider("🌀 Shearing Angle (°)", -45, 45, 0)
|
| 118 |
+
shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
|
| 119 |
+
augmented_image = image.transform(
|
| 120 |
+
(image.width + abs(int(image.height * np.tan(np.radians(shear_angle)))), image.height),
|
| 121 |
+
Image.AFFINE,
|
| 122 |
+
shear_matrix,
|
| 123 |
+
fillcolor=(0, 0, 0),
|
| 124 |
+
)
|
|
|
|
| 125 |
except Exception as aug_error:
|
| 126 |
+
st.error(f"⚠️ Augmentation Error: {aug_error}")
|
| 127 |
|
| 128 |
+
st.subheader("🖼️ Augmented Image")
|
|
|
|
| 129 |
st.image(augmented_image, caption=f"{augmentation_option} Applied", use_container_width=True)
|
| 130 |
|
|
|
|
| 131 |
buffer = io.BytesIO()
|
| 132 |
augmented_image.save(buffer, format="PNG")
|
| 133 |
buffer.seek(0)
|
| 134 |
+
st.download_button("📥 Download Augmented Image", buffer, file_name="augmented_image.png")
|
|
|
|
|
|
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
+
st.error(f"❌ An error occurred: {e}")
|