Rahatara commited on
Commit
1fd6cdb
1 Parent(s): 18120d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -33
app.py CHANGED
@@ -1,50 +1,63 @@
1
  import streamlit as st
2
  from PIL import Image, ImageEnhance, ImageOps
3
  import numpy as np
 
 
 
4
 
5
  def apply_basic_augmentations(image):
6
  """Applies basic augmentations such as rotation and color jitter."""
7
- # Rotate the image
8
  image = image.rotate(np.random.uniform(-30, 30))
9
-
10
- # Apply color jitter
11
  enhancer = ImageEnhance.Color(image)
12
  image = enhancer.enhance(np.random.uniform(0.75, 1.25))
13
-
14
- # Mirror image randomly
15
  if np.random.rand() > 0.5:
16
  image = ImageOps.mirror(image)
17
-
18
  return image
19
 
20
  def simulate_latent_space_noising(image, noise_scale=25):
21
- """Simulates latent space manipulation by adding noise to the image."""
22
- # Convert image to numpy array
23
  image_array = np.array(image)
24
-
25
- # Generate noise
26
  noise = np.random.normal(0, noise_scale, image_array.shape)
27
-
28
- # Add noise to image
29
  noised_image_array = np.clip(image_array + noise, 0, 255).astype(np.uint8)
30
-
31
- # Convert back to PIL image
32
- noised_image = Image.fromarray(noised_image_array)
33
-
34
- return noised_image
35
-
36
- st.title("Hybrid Image Augmentation Demo")
37
-
38
- uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
39
- if uploaded_image is not None:
40
- image = Image.open(uploaded_image)
41
- st.image(image, caption="Uploaded Image", use_column_width=True)
42
-
43
- if st.button("Augment Image"):
44
- # Apply basic transformations
45
- transformed_image = apply_basic_augmentations(image)
46
-
47
- # Simulate VAE latent space manipulation by adding noise
48
- augmented_image = simulate_latent_space_noising(transformed_image)
49
-
50
- st.image(augmented_image, caption="Augmented Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"
63
+ )