NeonSamurai commited on
Commit
258810b
·
verified ·
1 Parent(s): f2fc4c4

Upload Home.py

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