Spaces:
Sleeping
Sleeping
Update pages/Data Collection.py
Browse files- pages/Data Collection.py +98 -1
pages/Data Collection.py
CHANGED
|
@@ -1655,7 +1655,104 @@ def Image_Augmentation_page():
|
|
| 1655 |
cv2.waitKey(0)
|
| 1656 |
cv2.destroyAllWindows()
|
| 1657 |
""", language="python")
|
| 1658 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1659 |
if st.button("Back to Home"):
|
| 1660 |
st.session_state['page'] = "home"
|
| 1661 |
|
|
|
|
| 1655 |
cv2.waitKey(0)
|
| 1656 |
cv2.destroyAllWindows()
|
| 1657 |
""", language="python")
|
| 1658 |
+
|
| 1659 |
+
# Function to apply affine transformations
|
| 1660 |
+
def apply_affine_transformation(image, transformation_type):
|
| 1661 |
+
transformed_images = []
|
| 1662 |
+
rows, cols, _ = image.shape
|
| 1663 |
+
|
| 1664 |
+
for i in range(1, 11): # Generate 10 variations
|
| 1665 |
+
if transformation_type == "Rotation":
|
| 1666 |
+
angle = i * 10
|
| 1667 |
+
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
|
| 1668 |
+
elif transformation_type == "Scaling":
|
| 1669 |
+
scale = 1 + (i * 0.05) # Reduced scale increments
|
| 1670 |
+
M = np.float32([[scale, 0, 0], [0, scale, 0]])
|
| 1671 |
+
elif transformation_type == "Translation":
|
| 1672 |
+
tx, ty = i * 5, i * 5 # Reduced translation
|
| 1673 |
+
M = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 1674 |
+
elif transformation_type == "Shearing":
|
| 1675 |
+
shear = 0.05 * i # Reduced shear factor
|
| 1676 |
+
M = np.float32([[1, shear, 0], [shear, 1, 0]])
|
| 1677 |
+
elif transformation_type == "Cropping":
|
| 1678 |
+
# Simple cropping: reduce the size incrementally
|
| 1679 |
+
x1, y1 = i * 5, i * 5
|
| 1680 |
+
x2, y2 = cols - i * 5, rows - i * 5
|
| 1681 |
+
transformed_image = image[y1:y2, x1:x2]
|
| 1682 |
+
transformed_images.append(transformed_image)
|
| 1683 |
+
continue # Skip warpAffine for cropping
|
| 1684 |
+
else:
|
| 1685 |
+
st.error("Invalid transformation type!")
|
| 1686 |
+
return []
|
| 1687 |
+
|
| 1688 |
+
transformed_image = cv2.warpAffine(image, M, (cols, rows))
|
| 1689 |
+
transformed_images.append(transformed_image)
|
| 1690 |
+
|
| 1691 |
+
return transformed_images
|
| 1692 |
+
|
| 1693 |
+
# Streamlit App
|
| 1694 |
+
st.title("Dynamic Affine Transformation Tool")
|
| 1695 |
+
st.write("Select a transformation type to proceed and learn how it works before uploading an image.")
|
| 1696 |
+
|
| 1697 |
+
# Transformation Options
|
| 1698 |
+
transformation = st.selectbox(
|
| 1699 |
+
"Step 1: Select a transformation type:",
|
| 1700 |
+
["Select a Transformation", "Rotation", "Scaling", "Translation", "Shearing", "Cropping"]
|
| 1701 |
+
)
|
| 1702 |
+
|
| 1703 |
+
# Ensure the user selects a valid transformation
|
| 1704 |
+
if transformation != "Select a Transformation":
|
| 1705 |
+
# Provide guidance based on the selected transformation
|
| 1706 |
+
if transformation == "Rotation":
|
| 1707 |
+
st.info("Rotation rotates the image around a fixed point. Angles are applied in steps of 10 degrees.")
|
| 1708 |
+
elif transformation == "Scaling":
|
| 1709 |
+
st.info("Scaling adjusts the size of the image. The scale factor increases incrementally.")
|
| 1710 |
+
elif transformation == "Translation":
|
| 1711 |
+
st.info("Translation shifts the image horizontally and vertically in small steps.")
|
| 1712 |
+
elif transformation == "Shearing":
|
| 1713 |
+
st.info("Shearing skews the image along the x-axis or y-axis, creating a slanted effect.")
|
| 1714 |
+
elif transformation == "Cropping":
|
| 1715 |
+
st.info("Cropping trims the image edges step by step to focus on a smaller region.")
|
| 1716 |
+
|
| 1717 |
+
# Image Uploader (Only appears after selection)
|
| 1718 |
+
uploaded_file = st.file_uploader("Step 2: Now, upload an image", type=["jpg", "jpeg", "png"])
|
| 1719 |
+
|
| 1720 |
+
if uploaded_file:
|
| 1721 |
+
# Read the uploaded file into a numpy array using OpenCV
|
| 1722 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 1723 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 1724 |
+
|
| 1725 |
+
# Display the uploaded image
|
| 1726 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image", use_container_width=True)
|
| 1727 |
+
|
| 1728 |
+
# Automatically apply the transformation after upload
|
| 1729 |
+
transformed_images = apply_affine_transformation(image, transformation)
|
| 1730 |
+
|
| 1731 |
+
if transformed_images:
|
| 1732 |
+
st.write(f"Generated 10 images using {transformation}:")
|
| 1733 |
+
|
| 1734 |
+
# Display all transformed images
|
| 1735 |
+
for i, img in enumerate(transformed_images):
|
| 1736 |
+
st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption=f"{transformation} {i+1}", use_container_width=True)
|
| 1737 |
+
|
| 1738 |
+
# Create ZIP file for download
|
| 1739 |
+
zip_buffer = io.BytesIO()
|
| 1740 |
+
with zipfile.ZipFile(zip_buffer, "w") as zip_file:
|
| 1741 |
+
for i, img in enumerate(transformed_images):
|
| 1742 |
+
# Save each image as bytes
|
| 1743 |
+
_, img_encoded = cv2.imencode('.jpg', img)
|
| 1744 |
+
zip_file.writestr(f"{transformation}image{i+1}.jpg", img_encoded.tobytes())
|
| 1745 |
+
|
| 1746 |
+
zip_buffer.seek(0)
|
| 1747 |
+
st.download_button(
|
| 1748 |
+
label=f"Download All {transformation} Images",
|
| 1749 |
+
data=zip_buffer,
|
| 1750 |
+
file_name=f"{transformation}_transformed_images.zip",
|
| 1751 |
+
mime="application/zip"
|
| 1752 |
+
)
|
| 1753 |
+
else:
|
| 1754 |
+
st.warning("Please select a valid transformation type to proceed.")
|
| 1755 |
+
|
| 1756 |
if st.button("Back to Home"):
|
| 1757 |
st.session_state['page'] = "home"
|
| 1758 |
|