Team Ai
Apppublic

sree4411/Zero_To_Hero_In_MachineLearning

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
4_Image Augmentation.py100 linesDownload Raw Back to pages
1import streamlit as st2from PIL import Image3import numpy as np4import cv25import random6import io7import base648 9st.title(":red[Image Data Augmentation]")10 11# Explanation of Image Augmentation12st.write("""13## :blue[What is Image Augmentation]?14Image 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 link28def get_image_download_link(img_array, filename, text):29    img = Image.fromarray(img_array)30    img = img.convert("RGB")  # Convert to RGB mode31    buffered = io.BytesIO()32    img.save(buffered, format="JPEG")33    buffered.seek(0)34    b64 = base64.b64encode(buffered.read()).decode()35    href = f'<a href="data:file/jpg;base64,{b64}" download="{filename}">{text}</a>'36    return href37 38# Upload image39uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])40if uploaded_file is not None:41    image = Image.open(uploaded_file)42    st.image(image, caption='Uploaded Image.', use_container_width=True)43    st.write("")44    st.write("Select the augmentation operations you want to apply:")45 46    # Convert image to numpy array47    img_array = np.array(image)48 49    # Create checkboxes for each augmentation operation50    flip_horizontally = st.checkbox("Flip Horizontally")51    flip_vertically = st.checkbox("Flip Vertically")52    rotate = st.checkbox("Rotate 90 degrees")53    scale = st.checkbox("Scale")54    translate = st.checkbox("Translate")55    shear = st.checkbox("Shear")56    color_jitter = st.checkbox("Color Jittering")57    58    59 60    if flip_horizontally:61        img_array = cv2.flip(img_array, 1)62        st.markdown(get_image_download_link(img_array, "flipped_horizontally.jpg", "Download Horizontally Flipped Image"), unsafe_allow_html=True)63 64    if flip_vertically:65        img_array = cv2.flip(img_array, 0)66        st.markdown(get_image_download_link(img_array, "flipped_vertically.jpg", "Download Vertically Flipped Image"), unsafe_allow_html=True)67 68    if rotate:69        img_array = cv2.rotate(img_array, cv2.ROTATE_90_CLOCKWISE)70        st.markdown(get_image_download_link(img_array, "rotated.jpg", "Download Rotated Image"), unsafe_allow_html=True)71 72    if scale:73        scale_factor = st.slider("Scale Factor", 0.5, 2.0, 1.0)74        img_array = cv2.resize(img_array, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)75        st.markdown(get_image_download_link(img_array, "scaled.jpg", "Download Scaled Image"), unsafe_allow_html=True)76 77    if translate:78        translate_x = st.slider("Translate X", -50, 50, 0)79        translate_y = st.slider("Translate Y", -50, 50, 0)80        M = np.float32([[1, 0, translate_x], [0, 1, translate_y]])81        img_array = cv2.warpAffine(img_array, M, (img_array.shape[1], img_array.shape[0]))82        st.markdown(get_image_download_link(img_array, "translated.jpg", "Download Translated Image"), unsafe_allow_html=True)83 84    if shear:85        shear_factor = st.slider("Shear Factor", -0.5, 0.5, 0.0)86        M_shear = np.float32([[1, shear_factor, 0], [shear_factor, 1, 0]])87        img_array = cv2.warpAffine(img_array, M_shear, (img_array.shape[1], img_array.shape[0]))88        st.markdown(get_image_download_link(img_array, "sheared.jpg", "Download Sheared Image"), unsafe_allow_html=True)89 90    if color_jitter:91        brightness = st.slider("Brightness", 0.5, 1.5, 1.0)92        contrast = st.slider("Contrast", 0.5, 1.5, 1.0)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 image98    augmented_image = Image.fromarray(img_array)99    st.image(augmented_image, caption='Augmented Image.', use_container_width=True)100