Spaces:
Build error
Build error
Rename pages/transformations or augumentations on image.py to pages/The Image Augumentation.py
67d1a6c
verified
| import streamlit as st | |
| import numpy as np | |
| import cv2 | |
| # Streamlit header | |
| st.markdown('<h1><center>Image Augumentation</center></h1>', unsafe_allow_html=True) | |
| st.write("""Image Augumentation used to manipulate and alter images by applying operations such as translation, rotation, scaling, and shearing. | |
| These Image Augumentations help to generate new images from the existing ones, thereby increasing the dataset diversity.""") | |
| # Image augmentation functions | |
| def rotate_image(image, angle): | |
| (h, w) = image.shape[:2] # Get image dimensions | |
| center = (w // 2, h // 2) # Define the center of the image | |
| M = cv2.getRotationMatrix2D(center, angle, 1.0) # Rotation matrix | |
| rotated = cv2.warpAffine(image, M, (w, h),borderMode=cv2.BORDER_REFLECT) # Apply rotation | |
| return rotated | |
| def shifting(image, tx, ty): | |
| t_matrix = np.array([[1, 0, tx], | |
| [0, 1, ty]], dtype=np.float32) | |
| t_img = cv2.warpAffine(image, t_matrix, (image.shape[1], image.shape[0])) # Adjust size to original | |
| return t_img | |
| def scaling(image,fx,fy): | |
| scaled_image=cv2.resize(image,None,fx=fx, fy=fy) | |
| return scaled_image | |
| def shearing(image,shx,shy,tsx,tsy): | |
| shearing_matrix = np.array([[1,shx,tsx],[shy,1,tsy]],dtype= np.float32) | |
| sheared_image=cv2.warpAffine(image,shearing_matrix,(image.shape[1], image.shape[0])) | |
| return sheared_image | |
| def cropping(image,x1,x2,y1,y2): | |
| cropped_image=image[x1:x2,y1:y2] | |
| return cropped_image | |
| # Streamlit app | |
| st.markdown("<b>Upload an image and apply various augmentation techniques.</b>", unsafe_allow_html=True) | |
| # File uploader for images | |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"]) | |
| if uploaded_file is not None: | |
| # Read the image using OpenCV directly (ensure it's in BGR format) | |
| image_bgr = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8) | |
| image_bgr = cv2.imdecode(image_bgr, cv2.IMREAD_COLOR) # Read as BGR image | |
| # Display the uploaded image (converted to RGB for Streamlit) | |
| image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) # Convert to RGB for display | |
| st.image(image_rgb, caption="Uploaded Image", use_container_width=True) | |
| st.subheader('Rotation') | |
| # Rotation slider | |
| rotate = st.slider("Rotate Angle", 0, 180, 45) | |
| # Apply rotation based on the slider value | |
| if rotate != 0: | |
| # Apply rotation | |
| rotated_image_bgr = rotate_image(image_bgr, rotate) | |
| # Convert back to RGB for Streamlit | |
| rotated_image_rgb = cv2.cvtColor(rotated_image_bgr, cv2.COLOR_BGR2RGB) | |
| # Display the rotated image | |
| st.image(rotated_image_rgb, caption=f"Rotated Image ({rotate}°)", use_container_width=True) | |
| else: | |
| st.write("No rotation applied.") | |
| st.subheader('Translation') | |
| # Shifting sliders | |
| Tx = st.slider('tx distance', -100, 100, 0) # Allow both positive and negative values | |
| Ty = st.slider('ty distance', -100, 100, 0) | |
| # Apply shifting based on the sliders | |
| if Tx != 0 or Ty != 0: | |
| shifted_image = shifting(image_bgr, Tx, Ty) | |
| shifted_image_rgb = cv2.cvtColor(shifted_image, cv2.COLOR_BGR2RGB) | |
| st.image(shifted_image_rgb, caption=f"Translated Image (Tx: {Tx}, Ty: {Ty})", use_container_width=True) | |
| else: | |
| st.write("No Translation applied.") | |
| st.subheader('Scaling') | |
| #scaling | |
| fx=st.slider('fx distance', 0.5,1.5, 0.0,) | |
| fy=st.slider('fy distance', 0.5, 1.5, 0.0) | |
| # apply scaling based on sliders | |
| if fx!=0 or fy!=0: | |
| scaling_image = scaling(image_bgr,fx,fy) | |
| scaling_image_rgb=cv2.cvtColor(scaling_image, cv2.COLOR_BGR2RGB) | |
| st.image(scaling_image_rgb, caption=f'Scaled Image (fx:{fx},fy:{fy})',use_container_width=True) | |
| else: | |
| st.write('No scaling Applied') | |
| st.subheader('Shearing') | |
| # Shearing sliders | |
| shx = st.slider('Shearing on x-axis', 0.0, 3.0, 0.0, key='slider_shx') | |
| shy = st.slider('Shearing on y-axis', 0.0, 3.0, 0.0, key='slider_shy') | |
| tsx = st.slider('Translation on x-axis (tx)', -100, 100, 0, key='slider_tsx') | |
| tsy = st.slider('Translation on y-axis (ty)', -100, 100, 0, key='slider_tsy') | |
| # Apply shearing on the image | |
| if shx != 0 or shy != 0: | |
| # Assuming 'shearing' is a function that applies the shear transformation | |
| shearing_image = shearing(image_bgr, shx, shy, tsx, tsy) | |
| # Convert the sheared image to RGB for display in Streamlit | |
| shearing_image_rgb = cv2.cvtColor(shearing_image, cv2.COLOR_BGR2RGB) | |
| # Display the sheared image | |
| st.image( | |
| shearing_image_rgb, | |
| caption=f'Sheared image (sx: {shx}, sy: {shy})', | |
| use_container_width=True | |
| ) | |
| else: | |
| st.write("No Shearing applied.") | |
| st.subheader('Cropping') | |
| # Cropping sliders | |
| x1 = st.slider('x1 value', 0, 1000, 0, key='slider_x1') | |
| x2 = st.slider('x2 value', 0, 1000, 0, key='slider_x2') | |
| y1 = st.slider('y1 value', 0, 1000, 0, key='slider_y1') | |
| y2 = st.slider('y2 value', 0, 1000, 0, key='slider_y2') | |
| if x1 != 0 or x2 != 0 or y1 != 0 or y2 != 0: | |
| cropping_image = cropping(image_bgr, x1, x2, y1, y2) | |
| cropping_img_rgb = cv2.cvtColor(cropping_image, cv2.COLOR_BGR2RGB) | |
| # Display the cropped image with proper caption formatting | |
| st.image(cropping_img_rgb, caption=f'cropped image, image[{x1}:{x2}, {y1}:{y2}]', use_container_width=True) | |
| else: | |
| st.write('No Cropping Applied') | |