Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +38 -36
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,42 @@
|
|
| 1 |
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
import altair as alt
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import streamlit as st
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
import io
|
| 8 |
+
import zipfile
|
| 9 |
+
|
| 10 |
+
st.title("Image Augmentation App")
|
| 11 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 12 |
+
|
| 13 |
+
if uploaded_image:
|
| 14 |
+
img = np.array(Image.open(uploaded_image))
|
| 15 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 16 |
+
|
| 17 |
+
st.write("**Applying augmentations...**")
|
| 18 |
+
|
| 19 |
+
transformations = {
|
| 20 |
+
"Translated": cv2.warpAffine(img, np.float32([[1, 0, 50], [0, 1, 50]]), (img.shape[1], img.shape[0])),
|
| 21 |
+
"Scaled": cv2.resize(img, None, fx=1.2, fy=1.2),
|
| 22 |
+
"Rotated": cv2.warpAffine(img, cv2.getRotationMatrix2D((img.shape[1] // 2, img.shape[0] // 2), 45, 1), (img.shape[1], img.shape[0])),
|
| 23 |
+
"Cropped": img[50:img.shape[0] - 50, 50:img.shape[1] - 50]
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
augmented_images = []
|
| 27 |
+
zip_buffer = io.BytesIO()
|
| 28 |
+
|
| 29 |
+
with zipfile.ZipFile(zip_buffer, "w") as zipf:
|
| 30 |
+
for i, (name, transformed_img) in enumerate(transformations.items()):
|
| 31 |
+
img_pil = Image.fromarray(cv2.cvtColor(transformed_img, cv2.COLOR_BGR2RGB))
|
| 32 |
+
img_bytes = io.BytesIO()
|
| 33 |
+
img_pil.save(img_bytes, format="JPEG")
|
| 34 |
+
zipf.writestr(f"aug_{i + 1}_{name}.jpg", img_bytes.getvalue())
|
| 35 |
+
augmented_images.append(img_pil)
|
| 36 |
+
|
| 37 |
+
st.write("**Augmentation completed!**")
|
| 38 |
+
st.image(augmented_images, width=150)
|
| 39 |
+
|
| 40 |
+
zip_buffer.seek(0)
|
| 41 |
+
st.download_button("Download Augmented Images", data=zip_buffer, file_name="augmented_images.zip", mime="application/zip")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|