Rahatara commited on
Commit
97d05ac
1 Parent(s): 666dec2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -34
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 mock_encoder(image):
8
- """Simulates encoding an image into a latent representation."""
9
- # This is a placeholder. In practice, this would be your trained encoder's output.
10
- return np.random.normal(0, 1, (1, 100)), np.random.normal(0, 1, (1, 100)), np.random.normal(0, 1, (1, 100))
 
 
 
 
11
 
12
- def mock_decoder(latent_representation):
13
- """Simulates decoding a latent representation back into an image."""
14
- # Returns a random image for demonstration
15
- return np.random.rand(28, 28, 1) * 255
 
 
16
 
17
- def latent_space_augmentation(image, encoder, decoder, noise_scale=0.1):
18
- """Performs latent space augmentation by adding noise to the latent representation."""
19
- z_mean, z_log_var, _ = encoder(image)
20
- epsilon = np.random.normal(size=z_mean.shape)
21
- z_augmented = z_mean + np.exp(0.5 * z_log_var) * epsilon * noise_scale
22
- augmented_image = decoder(z_augmented)
23
- return np.squeeze(augmented_image)
 
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, image_data in enumerate(augmented_images):
30
- img_byte_arr = io.BytesIO(image_data)
31
- zip_file.writestr(f"augmented_image_{idx+1}.jpeg", img_byte_arr.getvalue())
 
32
  zip_buffer.seek(0)
33
  return zip_buffer
34
 
35
- st.title("Batch Image Augmentation with Latent Space Manipulation")
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 and st.button("Generate Augmented Images"):
41
  all_augmented_images = []
42
  for uploaded_file in uploaded_files:
43
  image = Image.open(uploaded_file).convert("RGB")
44
- image = image.resize((28, 28)) # Resize for simplicity with the mock decoder
45
-
46
- # Convert to numpy for processing
47
- image_np = np.array(image) / 255.0 # Normalize
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 Augmented Dataset",
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"