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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -38
app.py CHANGED
@@ -1,62 +1,61 @@
1
  import streamlit as st
2
- from PIL import Image, ImageEnhance, ImageOps
3
  import numpy as np
4
- import os
5
- import zipfile
6
  import io
 
 
 
 
 
 
7
 
8
- def apply_basic_augmentations(image):
9
- """Applies basic augmentations such as rotation and color jitter."""
10
- image = image.rotate(np.random.uniform(-30, 30))
11
- enhancer = ImageEnhance.Color(image)
12
- image = enhancer.enhance(np.random.uniform(0.75, 1.25))
13
- if np.random.rand() > 0.5:
14
- image = ImageOps.mirror(image)
15
- return image
16
-
17
- def simulate_latent_space_noising(image, noise_scale=25):
18
- """Simulates latent space manipulation by adding noise."""
19
- image_array = np.array(image)
20
- noise = np.random.normal(0, noise_scale, image_array.shape)
21
- noised_image_array = np.clip(image_array + noise, 0, 255).astype(np.uint8)
22
- return Image.fromarray(noised_image_array)
23
-
24
- def augment_image(image, augmentations_count):
25
- """Generates augmented versions of a single image."""
26
- augmented_images = []
27
- for _ in range(augmentations_count):
28
- augmented_image = apply_basic_augmentations(image)
29
- augmented_image = simulate_latent_space_noising(augmented_image)
30
- augmented_images.append(augmented_image)
31
- return augmented_images
32
 
33
  def create_downloadable_zip(augmented_images):
34
  """Creates a ZIP file in memory for downloading."""
35
  zip_buffer = io.BytesIO()
36
  with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
37
- for idx, image in enumerate(augmented_images):
38
- img_byte_arr = io.BytesIO()
39
- image.save(img_byte_arr, format="JPEG")
40
- zip_file.writestr(f"augmented_image_{idx+1}.jpg", img_byte_arr.getvalue())
41
  zip_buffer.seek(0)
42
  return zip_buffer
43
 
44
- st.title("Batch Image Augmentation for Dataset Creation")
45
 
46
  uploaded_files = st.file_uploader("Choose images (1-10)", accept_multiple_files=True, type=["jpg", "jpeg", "png"])
47
  augmentations_count = st.number_input("Number of augmented samples per image", min_value=1, max_value=10, value=3)
48
 
49
- if uploaded_files:
50
  all_augmented_images = []
51
  for uploaded_file in uploaded_files:
52
  image = Image.open(uploaded_file).convert("RGB")
53
- augmented_images = augment_image(image, augmentations_count)
54
- all_augmented_images.extend(augmented_images)
55
-
56
- if st.button("Download Augmented Dataset"):
 
 
 
 
 
 
 
 
57
  zip_buffer = create_downloadable_zip(all_augmented_images)
58
  st.download_button(
59
- label="Download ZIP",
60
  data=zip_buffer,
61
  file_name="augmented_images.zip",
62
  mime="application/zip"
 
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"