Spaces:
Sleeping
Sleeping
Update pages/Image_Augmentation_Handson.py
Browse files
pages/Image_Augmentation_Handson.py
CHANGED
|
@@ -3,35 +3,33 @@ 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: #
|
| 12 |
-
color: #FFFFFF;
|
| 13 |
}
|
| 14 |
</style>
|
| 15 |
""",
|
| 16 |
unsafe_allow_html=True,
|
| 17 |
)
|
| 18 |
|
| 19 |
-
st.title("
|
| 20 |
-
st.write("Enhance your images with various augmentation techniques.")
|
| 21 |
|
| 22 |
# Image uploader
|
| 23 |
-
uploaded_image = st.file_uploader("
|
| 24 |
|
| 25 |
if uploaded_image:
|
| 26 |
try:
|
| 27 |
image = Image.open(uploaded_image)
|
| 28 |
-
st.image(image, caption="Uploaded Image",
|
| 29 |
|
| 30 |
-
st.subheader("
|
| 31 |
|
| 32 |
# Augmentation option
|
| 33 |
-
augmentation_option = st.
|
| 34 |
-
"
|
| 35 |
[
|
| 36 |
"None",
|
| 37 |
"Cropping",
|
|
@@ -41,90 +39,92 @@ if uploaded_image:
|
|
| 41 |
"Zooming Out",
|
| 42 |
"Translation",
|
| 43 |
"Shearing",
|
| 44 |
-
"Grayscale",
|
| 45 |
-
"Invert Colors",
|
| 46 |
],
|
| 47 |
)
|
| 48 |
|
| 49 |
-
augmented_image = image
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# Display augmented image
|
| 120 |
-
st.subheader("
|
| 121 |
-
st.image(augmented_image, caption=f"{augmentation_option} Applied",
|
| 122 |
|
| 123 |
-
# Download
|
| 124 |
buffer = io.BytesIO()
|
| 125 |
augmented_image.save(buffer, format="PNG")
|
| 126 |
buffer.seek(0)
|
| 127 |
-
st.download_button(
|
|
|
|
|
|
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
-
st.error(f"
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
# Set Background color for the app while uploading an image
|
| 7 |
st.markdown(
|
| 8 |
"""
|
| 9 |
<style>
|
| 10 |
.stApp {
|
| 11 |
+
background-color: #000000;
|
|
|
|
| 12 |
}
|
| 13 |
</style>
|
| 14 |
""",
|
| 15 |
unsafe_allow_html=True,
|
| 16 |
)
|
| 17 |
|
| 18 |
+
st.title("Image Data Augmentation")
|
|
|
|
| 19 |
|
| 20 |
# Image uploader
|
| 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 |
# Augmentation option
|
| 31 |
+
augmentation_option = st.radio(
|
| 32 |
+
"Choose an augmentation:",
|
| 33 |
[
|
| 34 |
"None",
|
| 35 |
"Cropping",
|
|
|
|
| 39 |
"Zooming Out",
|
| 40 |
"Translation",
|
| 41 |
"Shearing",
|
|
|
|
|
|
|
| 42 |
],
|
| 43 |
)
|
| 44 |
|
| 45 |
+
augmented_image = image
|
| 46 |
|
| 47 |
+
try:
|
| 48 |
+
# Cropping
|
| 49 |
+
if augmentation_option == "Cropping":
|
|
|
|
| 50 |
left = st.slider("Left Crop", 0, image.width // 2, 0)
|
| 51 |
top = st.slider("Top Crop", 0, image.height // 2, 0)
|
|
|
|
| 52 |
right = st.slider("Right Crop", 0, image.width // 2, 0)
|
| 53 |
bottom = st.slider("Bottom Crop", 0, image.height // 2, 0)
|
| 54 |
+
augmented_image = image.crop((left, top, image.width - right, image.height - bottom))
|
| 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":
|
| 62 |
+
augmented_image = ImageOps.flip(image)
|
| 63 |
+
|
| 64 |
+
# Rotation
|
| 65 |
+
elif augmentation_option == "Rotation":
|
| 66 |
+
rotation_angle = st.slider("Rotation (°)", 0, 360, 0)
|
| 67 |
+
if rotation_angle != 0:
|
| 68 |
+
augmented_image = image.rotate(rotation_angle, expand=True)
|
| 69 |
+
|
| 70 |
+
# Zooming In
|
| 71 |
+
elif augmentation_option == "Zooming In":
|
| 72 |
+
scale_factor = st.slider("Zoom Factor (%)", 100, 200, 100) / 100.0
|
| 73 |
+
if scale_factor != 1.0:
|
| 74 |
+
width, height = image.size
|
| 75 |
+
new_width = int(width * scale_factor)
|
| 76 |
+
new_height = int(height * scale_factor)
|
| 77 |
+
zoomed_image = image.resize((new_width, new_height))
|
| 78 |
+
augmented_image = zoomed_image.crop(((new_width - width) // 2, (new_height - height) // 2,
|
| 79 |
+
(new_width + width) // 2, (new_height + height) // 2))
|
| 80 |
+
|
| 81 |
+
# Zooming Out
|
| 82 |
+
elif augmentation_option == "Zooming Out":
|
| 83 |
+
scale_factor = st.slider("Zoom Factor (%)", 50, 100, 100) / 100.0
|
| 84 |
+
if scale_factor != 1.0:
|
| 85 |
+
width, height = image.size
|
| 86 |
+
new_width = int(width * scale_factor)
|
| 87 |
+
new_height = int(height * scale_factor)
|
| 88 |
+
zoomed_image = image.resize((new_width, new_height))
|
| 89 |
+
padded_image = Image.new("RGB", (width, height), (255, 255, 255))
|
| 90 |
+
padded_image.paste(zoomed_image, ((width - new_width) // 2, (height - new_height) // 2))
|
| 91 |
+
augmented_image = padded_image
|
| 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 |
+
if translation_x != 0 or translation_y != 0:
|
| 98 |
+
translation_matrix = (1, 0, translation_x, 0, 1, translation_y)
|
| 99 |
+
augmented_image = image.transform(
|
| 100 |
+
image.size, Image.AFFINE, translation_matrix, fillcolor=(255, 255, 255)
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Shearing
|
| 104 |
+
elif augmentation_option == "Shearing":
|
| 105 |
+
shear_angle = st.slider("Shearing Angle (°)", -45, 45, 0)
|
| 106 |
+
if shear_angle != 0:
|
| 107 |
+
shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
|
| 108 |
+
augmented_image = image.transform(
|
| 109 |
+
(image.width + abs(int(image.height * np.tan(np.radians(shear_angle)))), image.height),
|
| 110 |
+
Image.AFFINE,
|
| 111 |
+
shear_matrix,
|
| 112 |
+
fillcolor=(255, 255, 255),
|
| 113 |
+
)
|
| 114 |
+
except Exception as aug_error:
|
| 115 |
+
st.error(f"Error during augmentation: {aug_error}")
|
| 116 |
|
| 117 |
# Display augmented image
|
| 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}")
|