Spaces:
Sleeping
Sleeping
Update pages/Data Collection.py
Browse files- pages/Data Collection.py +84 -0
pages/Data Collection.py
CHANGED
|
@@ -3,6 +3,8 @@ import numpy as np
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import pandas as pd
|
| 5 |
import cv2
|
|
|
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="HomePage",
|
|
@@ -1817,6 +1819,88 @@ elif st.session_state.current_page == "image_transformations":
|
|
| 1817 |
cv2.destroyAllWindows()
|
| 1818 |
""", language="python")
|
| 1819 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1820 |
col1, col2 = st.columns(2)
|
| 1821 |
with col1:
|
| 1822 |
st.markdown("""
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import pandas as pd
|
| 5 |
import cv2
|
| 6 |
+
import zipfile
|
| 7 |
+
import io
|
| 8 |
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="HomePage",
|
|
|
|
| 1819 |
cv2.destroyAllWindows()
|
| 1820 |
""", language="python")
|
| 1821 |
|
| 1822 |
+
|
| 1823 |
+
# Function to apply affine transformations
|
| 1824 |
+
def apply_affine_transformation(image, transformation_type):
|
| 1825 |
+
transformed_images = []
|
| 1826 |
+
rows, cols, _ = image.shape
|
| 1827 |
+
|
| 1828 |
+
for i in range(1, 11): # Generate 10 variations
|
| 1829 |
+
if transformation_type == "Rotation":
|
| 1830 |
+
angle = i * 10
|
| 1831 |
+
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
|
| 1832 |
+
elif transformation_type == "Scaling":
|
| 1833 |
+
scale = 1 + (i * 0.1)
|
| 1834 |
+
M = np.float32([[scale, 0, 0], [0, scale, 0]])
|
| 1835 |
+
elif transformation_type == "Translation":
|
| 1836 |
+
tx, ty = i * 10, i * 10
|
| 1837 |
+
M = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 1838 |
+
elif transformation_type == "Shearing":
|
| 1839 |
+
shear = 0.1 * i
|
| 1840 |
+
M = np.float32([[1, shear, 0], [shear, 1, 0]])
|
| 1841 |
+
elif transformation_type == "Cropping":
|
| 1842 |
+
# Simple cropping: reduce the size incrementally
|
| 1843 |
+
x1, y1 = i * 5, i * 5
|
| 1844 |
+
x2, y2 = cols - i * 5, rows - i * 5
|
| 1845 |
+
transformed_image = image[y1:y2, x1:x2]
|
| 1846 |
+
transformed_images.append(transformed_image)
|
| 1847 |
+
continue # Skip warpAffine for cropping
|
| 1848 |
+
else:
|
| 1849 |
+
st.error("Invalid transformation type!")
|
| 1850 |
+
return []
|
| 1851 |
+
|
| 1852 |
+
transformed_image = cv2.warpAffine(image, M, (cols, rows))
|
| 1853 |
+
transformed_images.append(transformed_image)
|
| 1854 |
+
|
| 1855 |
+
return transformed_images
|
| 1856 |
+
|
| 1857 |
+
# Streamlit App
|
| 1858 |
+
st.title("Affine Transformations with Multiple Buttons")
|
| 1859 |
+
st.write("Upload an image and select a transformation to apply. Each button generates 10 variations.")
|
| 1860 |
+
|
| 1861 |
+
# Image Uploader
|
| 1862 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
| 1863 |
+
|
| 1864 |
+
if uploaded_file:
|
| 1865 |
+
# Read the uploaded file into a numpy array using OpenCV
|
| 1866 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 1867 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 1868 |
+
|
| 1869 |
+
# Display the uploaded image
|
| 1870 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image", use_column_width=True)
|
| 1871 |
+
|
| 1872 |
+
# Transformation Buttons
|
| 1873 |
+
transformations = ["Rotation", "Scaling", "Translation", "Shearing", "Cropping"]
|
| 1874 |
+
|
| 1875 |
+
# Process each transformation on button click
|
| 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}", use_column_width=True)
|
| 1886 |
+
|
| 1887 |
+
# Create ZIP file for download
|
| 1888 |
+
zip_buffer = io.BytesIO()
|
| 1889 |
+
with zipfile.ZipFile(zip_buffer, "w") as zip_file:
|
| 1890 |
+
for i, img in enumerate(transformed_images):
|
| 1891 |
+
# Save each image as bytes
|
| 1892 |
+
_, img_encoded = cv2.imencode('.jpg', img)
|
| 1893 |
+
zip_file.writestr(f"{transformation}_image_{i+1}.jpg", img_encoded.tobytes())
|
| 1894 |
+
|
| 1895 |
+
zip_buffer.seek(0)
|
| 1896 |
+
st.download_button(
|
| 1897 |
+
label=f"Download All {transformation} Images",
|
| 1898 |
+
data=zip_buffer,
|
| 1899 |
+
file_name=f"{transformation}_transformed_images.zip",
|
| 1900 |
+
mime="application/zip"
|
| 1901 |
+
)
|
| 1902 |
+
|
| 1903 |
+
|
| 1904 |
col1, col2 = st.columns(2)
|
| 1905 |
with col1:
|
| 1906 |
st.markdown("""
|