Spaces:
Sleeping
Sleeping
Update pages/Data Collection.py
Browse files- pages/Data Collection.py +28 -20
pages/Data Collection.py
CHANGED
|
@@ -1820,6 +1820,12 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1820 |
""", language="python")
|
| 1821 |
|
| 1822 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1823 |
# Function to apply affine transformations
|
| 1824 |
def apply_affine_transformation(image, transformation_type):
|
| 1825 |
transformed_images = []
|
|
@@ -1830,13 +1836,13 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1830 |
angle = i * 10
|
| 1831 |
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
|
| 1832 |
elif transformation_type == "Scaling":
|
| 1833 |
-
scale = 1 + (i * 0.
|
| 1834 |
M = np.float32([[scale, 0, 0], [0, scale, 0]])
|
| 1835 |
elif transformation_type == "Translation":
|
| 1836 |
-
tx, ty = i *
|
| 1837 |
M = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 1838 |
elif transformation_type == "Shearing":
|
| 1839 |
-
shear = 0.
|
| 1840 |
M = np.float32([[1, shear, 0], [shear, 1, 0]])
|
| 1841 |
elif transformation_type == "Cropping":
|
| 1842 |
# Simple cropping: reduce the size incrementally
|
|
@@ -1855,34 +1861,37 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1855 |
return transformed_images
|
| 1856 |
|
| 1857 |
# Streamlit App
|
| 1858 |
-
st.title("Affine Transformations with
|
| 1859 |
-
st.write("
|
| 1860 |
|
| 1861 |
-
#
|
| 1862 |
-
|
|
|
|
|
|
|
|
|
|
| 1863 |
|
| 1864 |
-
|
| 1865 |
-
|
| 1866 |
-
|
| 1867 |
-
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 1868 |
|
| 1869 |
-
|
| 1870 |
-
|
|
|
|
|
|
|
| 1871 |
|
| 1872 |
-
|
| 1873 |
-
|
| 1874 |
|
| 1875 |
-
|
| 1876 |
-
for transformation in transformations:
|
| 1877 |
if st.button(f"Apply {transformation}"):
|
| 1878 |
transformed_images = apply_affine_transformation(image, transformation)
|
| 1879 |
|
| 1880 |
if transformed_images:
|
| 1881 |
st.write(f"Generated 10 images using {transformation}:")
|
| 1882 |
-
|
| 1883 |
# Display all transformed images
|
| 1884 |
for i, img in enumerate(transformed_images):
|
| 1885 |
-
st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption=f"{transformation} {i+1}",
|
| 1886 |
|
| 1887 |
# Create ZIP file for download
|
| 1888 |
zip_buffer = io.BytesIO()
|
|
@@ -1900,7 +1909,6 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1900 |
mime="application/zip"
|
| 1901 |
)
|
| 1902 |
|
| 1903 |
-
|
| 1904 |
col1, col2 = st.columns(2)
|
| 1905 |
with col1:
|
| 1906 |
st.markdown("""
|
|
|
|
| 1820 |
""", language="python")
|
| 1821 |
|
| 1822 |
|
| 1823 |
+
import streamlit as st
|
| 1824 |
+
import numpy as np
|
| 1825 |
+
import cv2
|
| 1826 |
+
import zipfile
|
| 1827 |
+
import io
|
| 1828 |
+
|
| 1829 |
# Function to apply affine transformations
|
| 1830 |
def apply_affine_transformation(image, transformation_type):
|
| 1831 |
transformed_images = []
|
|
|
|
| 1836 |
angle = i * 10
|
| 1837 |
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
|
| 1838 |
elif transformation_type == "Scaling":
|
| 1839 |
+
scale = 1 + (i * 0.05) # Reduced scale increments
|
| 1840 |
M = np.float32([[scale, 0, 0], [0, scale, 0]])
|
| 1841 |
elif transformation_type == "Translation":
|
| 1842 |
+
tx, ty = i * 5, i * 5 # Reduced translation
|
| 1843 |
M = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 1844 |
elif transformation_type == "Shearing":
|
| 1845 |
+
shear = 0.05 * i # Reduced shear factor
|
| 1846 |
M = np.float32([[1, shear, 0], [shear, 1, 0]])
|
| 1847 |
elif transformation_type == "Cropping":
|
| 1848 |
# Simple cropping: reduce the size incrementally
|
|
|
|
| 1861 |
return transformed_images
|
| 1862 |
|
| 1863 |
# Streamlit App
|
| 1864 |
+
st.title("Affine Transformations with Selection First")
|
| 1865 |
+
st.write("Select a transformation and then upload an image to apply it dynamically.")
|
| 1866 |
|
| 1867 |
+
# Transformation Options
|
| 1868 |
+
transformation = st.selectbox(
|
| 1869 |
+
"Select a transformation:",
|
| 1870 |
+
["Rotation", "Scaling", "Translation", "Shearing", "Cropping"]
|
| 1871 |
+
)
|
| 1872 |
|
| 1873 |
+
# Image Uploader (Only appears after selection)
|
| 1874 |
+
if transformation:
|
| 1875 |
+
uploaded_file = st.file_uploader("Now, upload an image", type=["jpg", "jpeg", "png"])
|
|
|
|
| 1876 |
|
| 1877 |
+
if uploaded_file:
|
| 1878 |
+
# Read the uploaded file into a numpy array using OpenCV
|
| 1879 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 1880 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 1881 |
|
| 1882 |
+
# Display the uploaded image
|
| 1883 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image", use_container_width=True)
|
| 1884 |
|
| 1885 |
+
# Apply Transformation
|
|
|
|
| 1886 |
if st.button(f"Apply {transformation}"):
|
| 1887 |
transformed_images = apply_affine_transformation(image, transformation)
|
| 1888 |
|
| 1889 |
if transformed_images:
|
| 1890 |
st.write(f"Generated 10 images using {transformation}:")
|
| 1891 |
+
|
| 1892 |
# Display all transformed images
|
| 1893 |
for i, img in enumerate(transformed_images):
|
| 1894 |
+
st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption=f"{transformation} {i+1}", use_container_width=True)
|
| 1895 |
|
| 1896 |
# Create ZIP file for download
|
| 1897 |
zip_buffer = io.BytesIO()
|
|
|
|
| 1909 |
mime="application/zip"
|
| 1910 |
)
|
| 1911 |
|
|
|
|
| 1912 |
col1, col2 = st.columns(2)
|
| 1913 |
with col1:
|
| 1914 |
st.markdown("""
|