sree4411 commited on
Commit
42c89c4
·
verified ·
1 Parent(s): a7d097b

Update pages/4_Image Augmentation.py

Browse files
Files changed (1) hide show
  1. pages/4_Image Augmentation.py +99 -27
pages/4_Image Augmentation.py CHANGED
@@ -1,33 +1,105 @@
1
  import streamlit as st
2
- from PIL import Image, ImageEnhance
 
 
 
 
3
 
4
- # Title of the app
5
- st.title("Image Data Augmentation")
6
 
7
- # File uploader to upload images
8
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  if uploaded_file is not None:
11
- # Open the uploaded image
12
  image = Image.open(uploaded_file)
13
- st.image(image, caption='Uploaded Image', use_column_width=True)
14
-
15
- st.write("Augment your image below:")
16
-
17
- # Slider for adjusting brightness
18
- brightness = st.slider("Brightness", 0.5, 3.0, 1.0)
19
- enhancer = ImageEnhance.Brightness(image)
20
- img_brightness = enhancer.enhance(brightness)
21
- st.image(img_brightness, caption='Brightness Adjusted', use_column_width=True)
22
-
23
- # Slider for adjusting contrast
24
- contrast = st.slider("Contrast", 0.5, 3.0, 1.0)
25
- enhancer = ImageEnhance.Contrast(image)
26
- img_contrast = enhancer.enhance(contrast)
27
- st.image(img_contrast, caption='Contrast Adjusted', use_column_width=True)
28
-
29
- # Slider for adjusting sharpness
30
- sharpness = st.slider("Sharpness", 0.5, 3.0, 1.0)
31
- enhancer = ImageEnhance.Sharpness(image)
32
- img_sharpness = enhancer.enhance(sharpness)
33
- st.image(img_sharpness, caption='Sharpness Adjusted', use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import cv2
5
+ import random
6
+ import io
7
 
8
+ st.title("Interactive Image Augmentation with Streamlit")
 
9
 
10
+ # Explanation of Image Augmentation
11
+ st.write("""
12
+ ## What is Image Augmentation?
13
+ 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.
14
 
15
+ ### Common Types of Image Augmentation:
16
+ 1. **Flipping**: Horizontally or vertically flipping an image.
17
+ 2. **Rotation**: Rotating the image by a certain angle.
18
+ 3. **Scaling**: Zooming in or out on the image.
19
+ 4. **Translation**: Shifting the image in the x or y direction.
20
+ 5. **Shearing**: Distorting the image along one axis.
21
+ 6. **Color Jittering**: Randomly changing the brightness, contrast, saturation, and hue of the image.
22
+ 7. **Noise Addition**: Adding random noise to the image to simulate variations.
23
+ 8. **Cropping**: Randomly cropping a portion of the image.
24
+ 9. **Blurring**: Applying different types of blurs (e.g., Gaussian blur).
25
+ 10. **Affine Transformations**: Applying geometric transformations like scaling, rotation, and translation together.
26
+ 11. **Elastic Transformations**: Randomly deforming the image using displacement fields.
27
+ 12. **Cutout**: Randomly masking out a section of the image.
28
+ """)
29
+
30
+ # Function to convert numpy array to image and provide download link
31
+ def get_image_download_link(img_array, filename, text):
32
+ img = Image.fromarray(img_array)
33
+ buffered = io.BytesIO()
34
+ img.save(buffered, format="JPEG")
35
+ buffered.seek(0)
36
+ b64 = base64.b64encode(buffered.read()).decode()
37
+ href = f'<a href="data:file/jpg;base64,{b64}" download="{filename}">{text}</a>'
38
+ return href
39
+
40
+ # Upload image
41
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
42
  if uploaded_file is not None:
 
43
  image = Image.open(uploaded_file)
44
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
45
+ st.write("")
46
+ st.write("Select the augmentation operations you want to apply:")
47
+
48
+ # Convert image to numpy array
49
+ img_array = np.array(image)
50
+
51
+ # Create checkboxes for each augmentation operation
52
+ flip_horizontally = st.checkbox("Flip Horizontally")
53
+ flip_vertically = st.checkbox("Flip Vertically")
54
+ rotate = st.checkbox("Rotate 90 degrees")
55
+ scale = st.checkbox("Scale")
56
+ translate = st.checkbox("Translate")
57
+ shear = st.checkbox("Shear")
58
+ color_jitter = st.checkbox("Color Jittering")
59
+ add_noise = st.checkbox("Add Noise")
60
+
61
+ if flip_horizontally:
62
+ img_array = cv2.flip(img_array, 1)
63
+ st.markdown(get_image_download_link(img_array, "flipped_horizontally.jpg", "Download Horizontally Flipped Image"), unsafe_allow_html=True)
64
+
65
+ if flip_vertically:
66
+ img_array = cv2.flip(img_array, 0)
67
+ st.markdown(get_image_download_link(img_array, "flipped_vertically.jpg", "Download Vertically Flipped Image"), unsafe_allow_html=True)
68
+
69
+ if rotate:
70
+ img_array = cv2.rotate(img_array, cv2.ROTATE_90_CLOCKWISE)
71
+ st.markdown(get_image_download_link(img_array, "rotated.jpg", "Download Rotated Image"), unsafe_allow_html=True)
72
+
73
+ if scale:
74
+ scale_factor = st.slider("Scale Factor", 0.5, 2.0, 1.0)
75
+ img_array = cv2.resize(img_array, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
76
+ st.markdown(get_image_download_link(img_array, "scaled.jpg", "Download Scaled Image"), unsafe_allow_html=True)
77
+
78
+ if translate:
79
+ translate_x = st.slider("Translate X", -50, 50, 0)
80
+ translate_y = st.slider("Translate Y", -50, 50, 0)
81
+ M = np.float32([[1, 0, translate_x], [0, 1, translate_y]])
82
+ img_array = cv2.warpAffine(img_array, M, (img_array.shape[1], img_array.shape[0]))
83
+ st.markdown(get_image_download_link(img_array, "translated.jpg", "Download Translated Image"), unsafe_allow_html=True)
84
+
85
+ if shear:
86
+ shear_factor = st.slider("Shear Factor", -0.5, 0.5, 0.0)
87
+ M_shear = np.float32([[1, shear_factor, 0], [shear_factor, 1, 0]])
88
+ img_array = cv2.warpAffine(img_array, M_shear, (img_array.shape[1], img_array.shape[0]))
89
+ st.markdown(get_image_download_link(img_array, "sheared.jpg", "Download Sheared Image"), unsafe_allow_html=True)
90
+
91
+ if color_jitter:
92
+ brightness = st.slider("Brightness", 0.5, 1.5, 1.0)
93
+ contrast = st.slider("Contrast", 0.5, 1.5, 1.0)
94
+ img_array = cv2.convertScaleAbs(img_array, alpha=contrast, beta=brightness * 127)
95
+ st.markdown(get_image_download_link(img_array, "color_jittered.jpg", "Download Color Jittered Image"), unsafe_allow_html=True)
96
+
97
+ if add_noise:
98
+ noise = np.random.randn(*img_array.shape) * 10
99
+ img_array = img_array + noise
100
+ img_array = np.clip(img_array, 0, 255).astype(np.uint8)
101
+ st.markdown(get_image_download_link(img_array, "noisy.jpg", "Download Noisy Image"), unsafe_allow_html=True)
102
+
103
+ # Convert numpy array back to image
104
+ augmented_image = Image.fromarray(img_array)
105
+ st.image(augmented_image, caption='Augmented Image.', use_column_width=True)