sree4411 commited on
Commit
487962c
·
verified ·
1 Parent(s): 62f51fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import base64
8
+
9
+ st.title(":red[Image Data Augmentation]")
10
+
11
+ # Explanation of Image Augmentation
12
+ st.write("""
13
+ ## :blue[What is Image Augmentation]?
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
+ ### :blue[Common Types of Image Augmentation]:
16
+ - **Flipping**: Horizontally or vertically flipping an image.
17
+ - **Rotation**: Rotating the image by a certain angle.
18
+ - **Scaling**: Zooming in or out on the image.
19
+ - **Translation**: Shifting the image in the x or y direction.
20
+ - **Shearing**: Distorting the image along one axis.
21
+ - **Color Jittering**: Randomly changing the brightness, contrast, saturation, and hue of the image.
22
+ - **Cropping**: Randomly cropping a portion of the image.
23
+ - **Affine Transformations**: Applying geometric transformations like scaling, rotation, and translation together.
24
+ """)
25
+
26
+ # Function to convert numpy array to image and provide download link
27
+ def get_image_download_link(img_array, filename, text):
28
+ img = Image.fromarray(img_array)
29
+ img = img.convert("RGB") # Convert to RGB mode
30
+ buffered = io.BytesIO()
31
+ img.save(buffered, format="JPEG")
32
+ buffered.seek(0)
33
+ b64 = base64.b64encode(buffered.read()).decode()
34
+ href = f'<a href="data:file/jpg;base64,{b64}" download="{filename}">{text}</a>'
35
+ return href
36
+
37
+ # Upload image
38
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
39
+ if uploaded_file is not None:
40
+ image = Image.open(uploaded_file)
41
+ st.image(image, caption='Uploaded Image.', use_container_width=True)
42
+ st.write("")
43
+ st.write("Select the augmentation operations you want to apply:")
44
+
45
+ # Convert image to numpy array
46
+ img_array = np.array(image)
47
+
48
+ # Create checkboxes for each augmentation operation
49
+ flip_horizontally = st.checkbox("Flip Horizontally")
50
+ flip_vertically = st.checkbox("Flip Vertically")
51
+ rotate = st.checkbox("Rotate 90 degrees")
52
+ scale = st.checkbox("Scale")
53
+ translate = st.checkbox("Translate")
54
+ shear = st.checkbox("Shear")
55
+ color_jitter = st.checkbox("Color Jittering")
56
+
57
+
58
+
59
+ if flip_horizontally:
60
+ img_array = cv2.flip(img_array, 1)
61
+ st.markdown(get_image_download_link(img_array, "flipped_horizontally.jpg", "Download Horizontally Flipped Image"), unsafe_allow_html=True)
62
+
63
+ if flip_vertically:
64
+ img_array = cv2.flip(img_array, 0)
65
+ st.markdown(get_image_download_link(img_array, "flipped_vertically.jpg", "Download Vertically Flipped Image"), unsafe_allow_html=True)
66
+
67
+ if rotate:
68
+ img_array = cv2.rotate(img_array, cv2.ROTATE_90_CLOCKWISE)
69
+ st.markdown(get_image_download_link(img_array, "rotated.jpg", "Download Rotated Image"), unsafe_allow_html=True)
70
+
71
+ if scale:
72
+ scale_factor = st.slider("Scale Factor", 0.5, 2.0, 1.0)
73
+ img_array = cv2.resize(img_array, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
74
+ st.markdown(get_image_download_link(img_array, "scaled.jpg", "Download Scaled Image"), unsafe_allow_html=True)
75
+
76
+ if translate:
77
+ translate_x = st.slider("Translate X", -50, 50, 0)
78
+ translate_y = st.slider("Translate Y", -50, 50, 0)
79
+ M = np.float32([[1, 0, translate_x], [0, 1, translate_y]])
80
+ img_array = cv2.warpAffine(img_array, M, (img_array.shape[1], img_array.shape[0]))
81
+ st.markdown(get_image_download_link(img_array, "translated.jpg", "Download Translated Image"), unsafe_allow_html=True)
82
+
83
+ if shear:
84
+ shear_factor = st.slider("Shear Factor", -0.5, 0.5, 0.0)
85
+ M_shear = np.float32([[1, shear_factor, 0], [shear_factor, 1, 0]])
86
+ img_array = cv2.warpAffine(img_array, M_shear, (img_array.shape[1], img_array.shape[0]))
87
+ st.markdown(get_image_download_link(img_array, "sheared.jpg", "Download Sheared Image"), unsafe_allow_html=True)
88
+
89
+ if color_jitter:
90
+ brightness = st.slider("Brightness", 0.5, 1.5, 1.0)
91
+ contrast = st.slider("Contrast", 0.5, 1.5, 1.0)
92
+ img_array = cv2.convertScaleAbs(img_array, alpha=contrast, beta=brightness * 127)
93
+ st.markdown(get_image_download_link(img_array, "color_jittered.jpg", "Download Color Jittered Image"), unsafe_allow_html=True)
94
+
95
+
96
+ # Convert numpy array back to image
97
+ augmented_image = Image.fromarray(img_array)
98
+ st.image(augmented_image, caption='Augmented Image.', use_container_width=True)