Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
import numpy as np
|
4 |
import io
|
5 |
import zipfile
|
6 |
|
7 |
-
def
|
8 |
-
"""
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
def
|
13 |
-
"""Simulates
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
def
|
18 |
-
"""
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
def create_downloadable_zip(augmented_images):
|
26 |
"""Creates a ZIP file in memory for downloading."""
|
27 |
zip_buffer = io.BytesIO()
|
28 |
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
|
29 |
-
for idx,
|
30 |
-
img_byte_arr = io.BytesIO(
|
31 |
-
|
|
|
32 |
zip_buffer.seek(0)
|
33 |
return zip_buffer
|
34 |
|
35 |
-
st.title("Batch Image Augmentation
|
36 |
|
37 |
uploaded_files = st.file_uploader("Choose images (1-10)", accept_multiple_files=True, type=["jpg", "jpeg", "png"])
|
38 |
augmentations_count = st.number_input("Number of augmented samples per image", min_value=1, max_value=10, value=3)
|
39 |
|
40 |
-
if uploaded_files
|
41 |
all_augmented_images = []
|
42 |
for uploaded_file in uploaded_files:
|
43 |
image = Image.open(uploaded_file).convert("RGB")
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
for _ in range(augmentations_count):
|
49 |
-
augmented_image_np = latent_space_augmentation(image_np, mock_encoder, mock_decoder)
|
50 |
-
augmented_image = (augmented_image_np * 255).astype(np.uint8) # Denormalize
|
51 |
-
augmented_images_io = io.BytesIO()
|
52 |
-
Image.fromarray(augmented_image).save(augmented_images_io, format="JPEG")
|
53 |
-
all_augmented_images.append(augmented_images_io.getvalue())
|
54 |
-
|
55 |
-
if all_augmented_images:
|
56 |
zip_buffer = create_downloadable_zip(all_augmented_images)
|
57 |
st.download_button(
|
58 |
-
label="Download
|
59 |
data=zip_buffer,
|
60 |
file_name="augmented_images.zip",
|
61 |
mime="application/zip"
|
|
|
1 |
+
Certainly! Below is the complete Streamlit app code that combines both basic image transformations and a simulation of latent space augmentation to generate synthetic images. This version doesn't require training a model and instead uses traditional image processing techniques along with simulated noise addition to create augmented images. Users can upload 1 to 10 images, specify the desired number of augmented samples per original image, and download a ZIP file containing all the generated synthetic images.
|
2 |
+
|
3 |
+
### Complete Streamlit App Code
|
4 |
+
|
5 |
+
```python
|
6 |
import streamlit as st
|
7 |
+
from PIL import Image, ImageEnhance, ImageOps
|
8 |
import numpy as np
|
9 |
import io
|
10 |
import zipfile
|
11 |
|
12 |
+
def apply_basic_augmentations(image):
|
13 |
+
"""Applies basic augmentations such as rotation and color jitter."""
|
14 |
+
image = image.rotate(np.random.uniform(-30, 30))
|
15 |
+
enhancer = ImageEnhance.Color(image)
|
16 |
+
image = enhancer.enhance(np.random.uniform(0.75, 1.25))
|
17 |
+
if np.random.rand() > 0.5:
|
18 |
+
image = ImageOps.mirror(image)
|
19 |
+
return image
|
20 |
|
21 |
+
def simulate_latent_space_noising(image, noise_scale=25):
|
22 |
+
"""Simulates latent space manipulation by adding noise."""
|
23 |
+
image_array = np.array(image)
|
24 |
+
noise = np.random.normal(0, noise_scale, image_array.shape)
|
25 |
+
noised_image_array = np.clip(image_array + noise, 0, 255).astype(np.uint8)
|
26 |
+
return Image.fromarray(noised_image_array)
|
27 |
|
28 |
+
def augment_image(image, augmentations_count):
|
29 |
+
"""Generates augmented versions of a single image."""
|
30 |
+
augmented_images = []
|
31 |
+
for _ in range(augmentations_count):
|
32 |
+
augmented_image = apply_basic_augmentations(image)
|
33 |
+
augmented_image = simulate_latent_space_noising(augmented_image)
|
34 |
+
augmented_images.append(augmented_image)
|
35 |
+
return augmented_images
|
36 |
|
37 |
def create_downloadable_zip(augmented_images):
|
38 |
"""Creates a ZIP file in memory for downloading."""
|
39 |
zip_buffer = io.BytesIO()
|
40 |
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
|
41 |
+
for idx, image in enumerate(augmented_images):
|
42 |
+
img_byte_arr = io.BytesIO()
|
43 |
+
image.save(img_byte_arr, format="JPEG")
|
44 |
+
zip_file.writestr(f"augmented_image_{idx+1}.jpg", img_byte_arr.getvalue())
|
45 |
zip_buffer.seek(0)
|
46 |
return zip_buffer
|
47 |
|
48 |
+
st.title("Batch Image Augmentation for Dataset Creation")
|
49 |
|
50 |
uploaded_files = st.file_uploader("Choose images (1-10)", accept_multiple_files=True, type=["jpg", "jpeg", "png"])
|
51 |
augmentations_count = st.number_input("Number of augmented samples per image", min_value=1, max_value=10, value=3)
|
52 |
|
53 |
+
if uploaded_files:
|
54 |
all_augmented_images = []
|
55 |
for uploaded_file in uploaded_files:
|
56 |
image = Image.open(uploaded_file).convert("RGB")
|
57 |
+
augmented_images = augment_image(image, augmentations_count)
|
58 |
+
all_augmented_images.extend(augmented_images)
|
59 |
+
|
60 |
+
if st.button("Download Augmented Dataset") and all_augmented_images:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
zip_buffer = create_downloadable_zip(all_augmented_images)
|
62 |
st.download_button(
|
63 |
+
label="Download ZIP",
|
64 |
data=zip_buffer,
|
65 |
file_name="augmented_images.zip",
|
66 |
mime="application/zip"
|