sree4411 commited on
Commit
8427362
·
verified ·
1 Parent(s): 4cd01c5

Update pages/4_Image Augmentation.py

Browse files
Files changed (1) hide show
  1. pages/4_Image Augmentation.py +119 -10
pages/4_Image Augmentation.py CHANGED
@@ -6,7 +6,7 @@ import random
6
  import io
7
  import base64
8
 
9
- st.title(":red[Image Data Augmentation ]")
10
 
11
  # Explanation of Image Augmentation
12
  st.write("""
@@ -14,15 +14,14 @@ st.write("""
14
  Image augmentation refers to a set of techniques used to increase the diversity of training images available for a machine learning model without actually collecting new images. It's commonly used in computer vision tasks to improve the performance of models by making them more robust to variations in the data.
15
 
16
  ### :blue[Common Types of Image Augmentation]:
17
- **Flipping**: Horizontally or vertically flipping an image.\n
18
- **Rotation**: Rotating the image by a certain angle.\n
19
- **Scaling**: Zooming in or out on the image.\n
20
- **Translation**: Shifting the image in the x or y direction.\n
21
- **Shearing**: Distorting the image along one axis.\n
22
- **Color Jittering**: Randomly changing the brightness, contrast, saturation, and hue of the image.\n
23
- **Cropping**: Randomly cropping a portion of the image.\n
24
- **Affine Transformations**: Applying geometric transformations like scaling, rotation, and translation together.
25
-
26
  """)
27
 
28
  # Function to convert numpy array to image and provide download link
@@ -55,6 +54,7 @@ if uploaded_file is not None:
55
  translate = st.checkbox("Translate")
56
  shear = st.checkbox("Shear")
57
  color_jitter = st.checkbox("Color Jittering")
 
58
 
59
 
60
  if flip_horizontally:
@@ -93,7 +93,116 @@ if uploaded_file is not None:
93
  img_array = cv2.convertScaleAbs(img_array, alpha=contrast, beta=brightness * 127)
94
  st.markdown(get_image_download_link(img_array, "color_jittered.jpg", "Download Color Jittered Image"), unsafe_allow_html=True)
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  # Convert numpy array back to image
98
  augmented_image = Image.fromarray(img_array)
99
  st.image(augmented_image, caption='Augmented Image.', use_column_width=True)
 
6
  import io
7
  import base64
8
 
9
+ st.title(":red[Image Data Augmentation]")
10
 
11
  # Explanation of Image Augmentation
12
  st.write("""
 
14
  Image augmentation refers to a set of techniques used to increase the diversity of training images available for a machine learning model without actually collecting new images. It's commonly used in computer vision tasks to improve the performance of models by making them more robust to variations in the data.
15
 
16
  ### :blue[Common Types of Image Augmentation]:
17
+ - **Flipping**: Horizontally or vertically flipping an image.
18
+ - **Rotation**: Rotating the image by a certain angle.
19
+ - **Scaling**: Zooming in or out on the image.
20
+ - **Translation**: Shifting the image in the x or y direction.
21
+ - **Shearing**: Distorting the image along one axis.
22
+ - **Color Jittering**: Randomly changing the brightness, contrast, saturation, and hue of the image.
23
+ - **Cropping**: Randomly cropping a portion of the image.
24
+ - **Affine Transformations**: Applying geometric transformations like scaling, rotation, and translation together.
 
25
  """)
26
 
27
  # Function to convert numpy array to image and provide download link
 
54
  translate = st.checkbox("Translate")
55
  shear = st.checkbox("Shear")
56
  color_jitter = st.checkbox("Color Jittering")
57
+ convert_gray = st.checkbox("Convert to Grayscale")
58
 
59
 
60
  if flip_horizontally:
 
93
  img_array = cv2.convertScaleAbs(img_array, alpha=contrast, beta=brightness * 127)
94
  st.markdown(get_image_download_link(img_array, "color_jittered.jpg", "Download Color Jittered Image"), unsafe_allow_html=True)
95
 
96
+ if convert_gray:
97
+ img_array_gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
98
+ img_array_gray = cv2.cvtColor(img_array_gray, cv2.COLOR_GRAY2RGB) # Convert back to RGB for consistency
99
+ st.image(img_array_gray, caption='Grayscale Image.', use_column_width=True)
100
+ st.markdown(get_image_download_link(img_array_gray, "grayscale.jpg", "Download Grayscale Image"), unsafe_allow_html=True)
101
+
102
+ # Convert numpy array back to image
103
+ augmented_image = Image.fromarray(img_array)
104
+ st.image(augmented_image, caption='Augmented Image.', use_column_width=True)
105
+ import streamlit as st
106
+ from PIL import Image
107
+ import numpy as np
108
+ import cv2
109
+ import random
110
+ import io
111
+ import base64
112
+
113
+ st.title(":red[Image Data Augmentation]")
114
+
115
+ # Explanation of Image Augmentation
116
+ st.write("""
117
+ ## :blue[What is Image Augmentation]?
118
+ Image augmentation refers to a set of techniques used to increase the diversity of training images available for a machine learning model without actually collecting new images. It's commonly used in computer vision tasks to improve the performance of models by making them more robust to variations in the data.
119
+
120
+ ### :blue[Common Types of Image Augmentation]:
121
+ - **Flipping**: Horizontally or vertically flipping an image.
122
+ - **Rotation**: Rotating the image by a certain angle.
123
+ - **Scaling**: Zooming in or out on the image.
124
+ - **Translation**: Shifting the image in the x or y direction.
125
+ - **Shearing**: Distorting the image along one axis.
126
+ - **Color Jittering**: Randomly changing the brightness, contrast, saturation, and hue of the image.
127
+ - **Cropping**: Randomly cropping a portion of the image.
128
+ - **Affine Transformations**: Applying geometric transformations like scaling, rotation, and translation together.
129
+ """)
130
+
131
+ # Function to convert numpy array to image and provide download link
132
+ def get_image_download_link(img_array, filename, text):
133
+ img = Image.fromarray(img_array)
134
+ img = img.convert("RGB") # Convert to RGB mode
135
+ buffered = io.BytesIO()
136
+ img.save(buffered, format="JPEG")
137
+ buffered.seek(0)
138
+ b64 = base64.b64encode(buffered.read()).decode()
139
+ href = f'<a href="data:file/jpg;base64,{b64}" download="{filename}">{text}</a>'
140
+ return href
141
+
142
+ # Upload image
143
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
144
+ if uploaded_file is not None:
145
+ image = Image.open(uploaded_file)
146
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
147
+ st.write("")
148
+ st.write("Select the augmentation operations you want to apply:")
149
+
150
+ # Convert image to numpy array
151
+ img_array = np.array(image)
152
+
153
+ # Create checkboxes for each augmentation operation
154
+ flip_horizontally = st.checkbox("Flip Horizontally")
155
+ flip_vertically = st.checkbox("Flip Vertically")
156
+ rotate = st.checkbox("Rotate 90 degrees")
157
+ scale = st.checkbox("Scale")
158
+ translate = st.checkbox("Translate")
159
+ shear = st.checkbox("Shear")
160
+ color_jitter = st.checkbox("Color Jittering")
161
+ convert_gray = st.checkbox("Convert to Grayscale")
162
 
163
+
164
+ if flip_horizontally:
165
+ img_array = cv2.flip(img_array, 1)
166
+ st.markdown(get_image_download_link(img_array, "flipped_horizontally.jpg", "Download Horizontally Flipped Image"), unsafe_allow_html=True)
167
+
168
+ if flip_vertically:
169
+ img_array = cv2.flip(img_array, 0)
170
+ st.markdown(get_image_download_link(img_array, "flipped_vertically.jpg", "Download Vertically Flipped Image"), unsafe_allow_html=True)
171
+
172
+ if rotate:
173
+ img_array = cv2.rotate(img_array, cv2.ROTATE_90_CLOCKWISE)
174
+ st.markdown(get_image_download_link(img_array, "rotated.jpg", "Download Rotated Image"), unsafe_allow_html=True)
175
+
176
+ if scale:
177
+ scale_factor = st.slider("Scale Factor", 0.5, 2.0, 1.0)
178
+ img_array = cv2.resize(img_array, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
179
+ st.markdown(get_image_download_link(img_array, "scaled.jpg", "Download Scaled Image"), unsafe_allow_html=True)
180
+
181
+ if translate:
182
+ translate_x = st.slider("Translate X", -50, 50, 0)
183
+ translate_y = st.slider("Translate Y", -50, 50, 0)
184
+ M = np.float32([[1, 0, translate_x], [0, 1, translate_y]])
185
+ img_array = cv2.warpAffine(img_array, M, (img_array.shape[1], img_array.shape[0]))
186
+ st.markdown(get_image_download_link(img_array, "translated.jpg", "Download Translated Image"), unsafe_allow_html=True)
187
+
188
+ if shear:
189
+ shear_factor = st.slider("Shear Factor", -0.5, 0.5, 0.0)
190
+ M_shear = np.float32([[1, shear_factor, 0], [shear_factor, 1, 0]])
191
+ img_array = cv2.warpAffine(img_array, M_shear, (img_array.shape[1], img_array.shape[0]))
192
+ st.markdown(get_image_download_link(img_array, "sheared.jpg", "Download Sheared Image"), unsafe_allow_html=True)
193
+
194
+ if color_jitter:
195
+ brightness = st.slider("Brightness", 0.5, 1.5, 1.0)
196
+ contrast = st.slider("Contrast", 0.5, 1.5, 1.0)
197
+ img_array = cv2.convertScaleAbs(img_array, alpha=contrast, beta=brightness * 127)
198
+ st.markdown(get_image_download_link(img_array, "color_jittered.jpg", "Download Color Jittered Image"), unsafe_allow_html=True)
199
+
200
+ if convert_gray:
201
+ img_array_gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
202
+ img_array_gray = cv2.cvtColor(img_array_gray, cv2.COLOR_GRAY2RGB) # Convert back to RGB for consistency
203
+ st.image(img_array_gray, caption='Grayscale Image.', use_column_width=True)
204
+ st.markdown(get_image_download_link(img_array_gray, "grayscale.jpg", "Download Grayscale Image"), unsafe_allow_html=True)
205
+
206
  # Convert numpy array back to image
207
  augmented_image = Image.fromarray(img_array)
208
  st.image(augmented_image, caption='Augmented Image.', use_column_width=True)