Rahatara commited on
Commit
253b27d
·
verified ·
1 Parent(s): 68f74d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -27
app.py CHANGED
@@ -1,41 +1,76 @@
1
- import numpy as np
2
  import gradio as gr
3
  from tensorflow.keras.preprocessing.image import img_to_array, ImageDataGenerator
4
  from PIL import Image
 
 
5
 
6
- def augment_images(input_img):
7
- # Define data augmentation parameters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  datagen = ImageDataGenerator(
9
  rotation_range=40,
10
  width_shift_range=0.2,
11
  height_shift_range=0.2,
12
- shear_range=0.2,
13
  zoom_range=0.2,
14
- horizontal_flip=True,
15
- fill_mode='nearest'
16
- )
 
 
17
 
18
- # Convert input image to numpy array
19
- img = Image.open(input_img).convert('RGB')
20
- img = img.resize((256, 256)) # Resize image
21
- x = img_to_array(img)
22
- x = x.reshape((1,) + x.shape)
23
 
24
- # Generate augmented images
25
- augmented_images = []
26
- for _ in datagen.flow(x, batch_size=1, save_to_dir=None, save_prefix='', save_format='jpeg'):
27
- augmented_images.append(_.squeeze())
28
- if len(augmented_images) >= 5: # Generate 5 augmented samples
29
- break
30
 
31
- return augmented_images
32
-
33
- iface = gr.Interface(
34
- fn=augment_images,
35
- inputs=gr.inputs.Image(label="Upload Image"),
36
- outputs=gr.outputs.Image(type="numpy"),
37
- title="Image Data Augmentation App",
38
- description="Upload an image to generate augmented versions."
 
 
 
 
39
  )
40
 
41
- iface.launch()
 
 
 
1
  import gradio as gr
2
  from tensorflow.keras.preprocessing.image import img_to_array, ImageDataGenerator
3
  from PIL import Image
4
+ import os
5
+ import zipfile
6
 
7
+ # Define where augmented images will be temporarily stored
8
+ TEMP_DIR = "temp_augmented_images"
9
+
10
+ # Ensure the temp directory exists
11
+ if not os.path.exists(TEMP_DIR):
12
+ os.makedirs(TEMP_DIR)
13
+
14
+ # Image Augmentation Function
15
+ def augment_image(image_file, datagen, num_duplicates):
16
+ try:
17
+ img = Image.open(image_file).convert('RGB') # Convert to RGB
18
+ img = img.resize((256, 256)) # Resize for consistency
19
+ x = img_to_array(img) # Image to array
20
+ x = x.reshape((1,) + x.shape) # Reshape for data generator
21
+
22
+ # Augment image
23
+ i = 0
24
+ for batch in datagen.flow(x, batch_size=1, save_to_dir=TEMP_DIR, save_prefix="aug", save_format="jpeg"):
25
+ i += 1
26
+ if i >= num_duplicates:
27
+ break
28
+ except Exception as e:
29
+ print(f"Error in augmenting image: {e}")
30
+
31
+ def create_zip_from_temp(directory=TEMP_DIR):
32
+ zip_path = f"{directory}/augmented_images.zip"
33
+ with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
34
+ for root, _, files in os.walk(directory):
35
+ for file in files:
36
+ if file.endswith(".jpeg"): # Ensure only augmented images are added
37
+ zipf.write(os.path.join(root, file), arcname=file)
38
+ return zip_path
39
+
40
+ def process_images(images, num_duplicates):
41
+ # Data generator for augmentation
42
  datagen = ImageDataGenerator(
43
  rotation_range=40,
44
  width_shift_range=0.2,
45
  height_shift_range=0.2,
 
46
  zoom_range=0.2,
47
+ fill_mode='nearest')
48
+
49
+ # Process each uploaded image
50
+ for image_file in images:
51
+ augment_image(image_file, datagen, num_duplicates)
52
 
53
+ # Create a zip file with all augmented images
54
+ zip_file = create_zip_from_temp()
 
 
 
55
 
56
+ # Clean up augmented images to avoid clutter
57
+ for file in os.listdir(TEMP_DIR):
58
+ if file.endswith(".jpeg"): # Clean up only augmented images, not the zip
59
+ os.remove(os.path.join(TEMP_DIR, file))
 
 
60
 
61
+ return zip_file
62
+
63
+ # Gradio Interface
64
+ demo = gr.Interface(
65
+ fn=process_images,
66
+ inputs=[
67
+ gr.Files(type="file", label="Upload Images", accept=["image/jpeg", "image/png"], multiple=True),
68
+ gr.Slider(minimum=1, maximum=20, default=5, label="Number of Duplicates per Image")
69
+ ],
70
+ outputs=gr.File(label="Download Augmented Images"),
71
+ title="Image Augmentation App",
72
+ description="Upload images to augment them with random transformations. Download the augmented images as a zip file."
73
  )
74
 
75
+ if __name__ == "__main__":
76
+ demo.launch()