Phani1008 commited on
Commit
e6e493d
·
verified ·
1 Parent(s): 20286ef

Update pages/Image_Augmentation_Handson.py

Browse files
Files changed (1) hide show
  1. pages/Image_Augmentation_Handson.py +81 -81
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: #1E1E1E;
12
- color: #FFFFFF;
13
  }
14
  </style>
15
  """,
16
  unsafe_allow_html=True,
17
  )
18
 
19
- st.title("🔧 Image Data Augmentation Tool")
20
- st.write("Enhance your images with various augmentation techniques.")
21
 
22
  # Image uploader
23
- uploaded_image = st.file_uploader("📤 Upload an Image (jpg, png, jpeg)", type=["jpg", "png", "jpeg"])
24
 
25
  if uploaded_image:
26
  try:
27
  image = Image.open(uploaded_image)
28
- st.image(image, caption="Uploaded Image", use_column_width=True)
29
 
30
- st.subheader("🎨 Choose Augmentation Options")
31
 
32
  # Augmentation option
33
- augmentation_option = st.selectbox(
34
- "Select an Augmentation Technique:",
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.copy()
50
 
51
- # Cropping
52
- if augmentation_option == "Cropping":
53
- col1, col2 = st.columns(2)
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
- augmented_image = image.crop((left, top, image.width - right, image.height - bottom))
61
-
62
- # Flipping
63
- elif augmentation_option == "Flipping":
64
- flip_option = st.radio("Flip Type", ["Horizontal", "Vertical"])
65
- augmented_image = ImageOps.mirror(image) if flip_option == "Horizontal" else ImageOps.flip(image)
66
-
67
- # Rotation
68
- elif augmentation_option == "Rotation":
69
- rotation_angle = st.slider("Rotation Angle (°)", -180, 180, 0)
70
- augmented_image = image.rotate(rotation_angle, expand=True)
71
-
72
- # Zooming In
73
- elif augmentation_option == "Zooming In":
74
- scale_factor = st.slider("Zoom Factor (%)", 100, 200, 100) / 100.0
75
- if scale_factor != 1.0:
76
- new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
77
- zoomed_image = image.resize(new_size)
78
- augmented_image = zoomed_image.crop(((new_size[0] - image.width) // 2,
79
- (new_size[1] - image.height) // 2,
80
- (new_size[0] + image.width) // 2,
81
- (new_size[1] + image.height) // 2))
82
-
83
- # Zooming Out
84
- elif augmentation_option == "Zooming Out":
85
- scale_factor = st.slider("Zoom Factor (%)", 50, 100, 100) / 100.0
86
- new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
87
- zoomed_image = image.resize(new_size)
88
- padded_image = Image.new("RGB", (image.width, image.height), (255, 255, 255))
89
- padded_image.paste(zoomed_image, ((image.width - new_size[0]) // 2, (image.height - new_size[1]) // 2))
90
- augmented_image = padded_image
91
-
92
- # Translation
93
- elif augmentation_option == "Translation":
94
- translation_x = st.slider("Horizontal Shift (px)", -100, 100, 0)
95
- translation_y = st.slider("Vertical Shift (px)", -100, 100, 0)
96
- augmented_image = image.transform(
97
- image.size, Image.AFFINE, (1, 0, translation_x, 0, 1, translation_y), fillcolor=(255, 255, 255)
98
- )
99
-
100
- # Shearing
101
- elif augmentation_option == "Shearing":
102
- shear_angle = st.slider("Shearing Angle (°)", -45, 45, 0)
103
- shear_matrix = (1, np.tan(np.radians(shear_angle)), 0, 0, 1, 0)
104
- augmented_image = image.transform(
105
- (image.width + abs(int(image.height * np.tan(np.radians(shear_angle)))), image.height),
106
- Image.AFFINE,
107
- shear_matrix,
108
- fillcolor=(255, 255, 255),
109
- )
110
-
111
- # Grayscale
112
- elif augmentation_option == "Grayscale":
113
- augmented_image = ImageOps.grayscale(image)
114
-
115
- # Invert Colors
116
- elif augmentation_option == "Invert Colors":
117
- augmented_image = ImageOps.invert(image)
 
 
 
 
118
 
119
  # Display augmented image
120
- st.subheader("🖼️ Augmented Image Preview")
121
- st.image(augmented_image, caption=f"{augmentation_option} Applied", use_column_width=True)
122
 
123
- # Download button
124
  buffer = io.BytesIO()
125
  augmented_image.save(buffer, format="PNG")
126
  buffer.seek(0)
127
- st.download_button("💾 Download Augmented Image", buffer, file_name="augmented_image.png")
 
 
128
 
129
  except Exception as e:
130
- st.error(f"An error occurred: {e}")
 
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}")