Spaces:
Sleeping
Sleeping
File size: 7,400 Bytes
258810b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
import numpy as np
import random
import streamlit as st
import cv2
from PIL import Image
import base64
from io import BytesIO
import zipfile
st.markdown("""
<style>
.stButton>button {
color: white;
border-radius: 8px;
padding: 10px 20px;
font-weight: bold;
transition: background-color 0.3s ease;
}
.stButton>button:hover {
background-color: Black;
border-color: white;
color: white;
}
</style>
""", unsafe_allow_html=True)
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode()
def add_bg_from_local(image_file):
encoded_string = encode_image(image_file)
st.markdown(
f"""
<style>
.stApp {{
background-image: url(data:image/{"png"};base64,{encoded_string});
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}}
</style>
""",
unsafe_allow_html=True
)
add_bg_from_local("Images/Gemini_Generated_Image_pnb6j1pnb6j1pnb6.jpg")
st.sidebar.header("Augmentation Options")
apply_translation = st.sidebar.checkbox("Apply Translation", value=True)
apply_rotation = st.sidebar.checkbox("Apply Rotation", value=True)
apply_brightness = st.sidebar.checkbox("Apply Brightness Adjustment", value=True)
if apply_brightness:
brightness_mode = st.sidebar.radio(
"Brightness Adjustment Mode",
options=["Increase Brightness", "Decrease Brightness", "Random Adjustment"],
index=2
)
if brightness_mode == "Increase Brightness":
brightness_range = (1.0, 2.0)
elif brightness_mode == "Decrease Brightness":
brightness_range = (0.1, 1.0)
elif brightness_mode == "Random Adjustment":
st.sidebar.header("Random Adjustment Settings")
brightness_min = st.sidebar.slider(
"Minimum brightness factor",
min_value=0.1, max_value=1.0, value=0.7, key="brightness_min_random"
)
brightness_max = st.sidebar.slider(
"Maximum brightness factor",
min_value=1.0, max_value=2.0, value=1.3, key="brightness_max_random"
)
if brightness_min >= brightness_max:
st.sidebar.error("Minimum brightness factor must be less than maximum brightness factor.")
brightness_range = (brightness_min, brightness_max)
def augment_image(image, tx_range=30, angle_range=15, brightness_range=(0.7, 1.3)):
rows, cols, _ = image.shape
augmented_image = image.copy()
if apply_translation:
tx = random.randint(-tx_range, tx_range)
ty = random.randint(-tx_range, tx_range)
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
augmented_image = cv2.warpAffine(augmented_image, translation_matrix, (cols, rows))
if apply_rotation:
angle = random.randint(-angle_range, angle_range)
rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
augmented_image = cv2.warpAffine(augmented_image, rotation_matrix, (cols, rows))
if apply_brightness:
factor = random.uniform(*brightness_range)
augmented_image = cv2.convertScaleAbs(augmented_image, alpha=factor, beta=0)
return augmented_image
def display_images_in_grid(images, cols=3):
rows = (len(images) + cols - 1) // cols
for row in range(rows):
cols_to_display = images[row * cols : (row + 1) * cols]
columns = st.columns(len(cols_to_display))
for col, img in zip(columns, cols_to_display):
with col:
st.image(img, channels="RGB", use_container_width=True)
st.title("Image Augmentation App")
st.write("Upload an image and generate augmented images. For more control, use Advanced Settings.")
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
image_array = np.array(image)
st.image(image, caption="Original Image", use_container_width=True)
num_augmentations = st.sidebar.number_input("Number of augmented images", min_value=1, max_value=200, value=5, step=1)
advanced_settings = st.sidebar.checkbox("Advanced Settings")
tx_range = 30
angle_range = 15
brightness_range = (0.7, 1.3)
if advanced_settings:
st.sidebar.header("Augmentation Settings")
tx_range = st.sidebar.slider("Translation range (pixels)", min_value=0, max_value=100, value=30, key="tx_range")
angle_range = st.sidebar.slider("Rotation range (degrees)", min_value=0, max_value=90, value=15, key="angle_range")
if not (apply_brightness and brightness_mode == "Random Adjustment"):
if advanced_settings:
brightness_min = st.sidebar.slider(
"Minimum brightness factor",
min_value=0.1, max_value=1.0, value=0.7, key="brightness_min_advanced"
)
brightness_max = st.sidebar.slider(
"Maximum brightness factor",
min_value=1.0, max_value=2.0, value=1.3, key="brightness_max_advanced"
)
if brightness_min >= brightness_max:
st.sidebar.error("Minimum brightness factor must be less than maximum brightness factor.")
brightness_range = (brightness_min, brightness_max)
if apply_translation or apply_rotation or apply_brightness:
if st.sidebar.button("Generate Augmented Images"):
st.write("Augmented Images:")
augmented_images = set()
unique_images = []
while len(unique_images) < num_augmentations:
augmented_image = augment_image(image_array.copy(), tx_range, angle_range, brightness_range)
image_hash = hash(augmented_image.tobytes())
if image_hash not in augmented_images:
augmented_images.add(image_hash)
unique_images.append(augmented_image)
st.session_state.unique_images = unique_images
display_images_in_grid(unique_images, cols=3)
if unique_images:
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
for i, aug_img in enumerate(unique_images):
aug_img = cv2.cvtColor(aug_img, cv2.COLOR_BGR2RGB)
_, img_encoded = cv2.imencode(".jpg", aug_img)
zip_file.writestr(f"augmented_image_{i + 1}.jpg", img_encoded.tobytes())
zip_buffer.seek(0)
st.download_button(
label="Download Augmented Images as ZIP",
data=zip_buffer.getvalue(),
file_name="augmented_images.zip",
mime="application/zip",
)
else:
if st.sidebar.button("Generate Augmented Images"):
st.warning("No augmentation options selected. Please select at least one option (Translation, Rotation, or Brightness).") |