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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -27
app.py CHANGED
@@ -1,12 +1,10 @@
 
1
  import gradio as gr
2
  from tensorflow.keras.preprocessing.image import img_to_array, ImageDataGenerator
3
  from PIL import Image
4
- import numpy as np
5
- import os
6
- import zipfile
7
- import tempfile
8
 
9
- def augment_images(image_file, num_duplicates):
 
10
  datagen = ImageDataGenerator(
11
  rotation_range=40,
12
  width_shift_range=0.2,
@@ -14,35 +12,30 @@ def augment_images(image_file, num_duplicates):
14
  shear_range=0.2,
15
  zoom_range=0.2,
16
  horizontal_flip=True,
17
- fill_mode='nearest')
 
18
 
19
- img = Image.open(image_file).convert('RGB') # Convert to RGB
 
20
  img = img.resize((256, 256)) # Resize image
21
- x = img_to_array(img) # Convert image to numpy array
22
- x = x.reshape((1,) + x.shape) # Reshape for data generator
23
 
24
- with tempfile.TemporaryDirectory() as temp_dir:
25
- i = 0
26
- for _ in datagen.flow(x, batch_size=1, save_to_dir=temp_dir, save_prefix='aug', save_format='jpeg'):
27
- i += 1
28
- if i >= num_duplicates:
29
- break
30
-
31
- # Zip the augmented images
32
- zip_name = tempfile.mktemp(suffix='.zip')
33
- with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
34
- for root, _, files in os.walk(temp_dir):
35
- for file in files:
36
- zipf.write(os.path.join(root, file), arcname=file)
37
 
38
- return zip_name
39
 
40
  iface = gr.Interface(
41
  fn=augment_images,
42
- inputs=[gr.inputs.Image(label="Upload Image"), gr.inputs.Slider(minimum=1, maximum=20, default=5, label="Number of Augmented Samples")],
43
- outputs=gr.outputs.File(label="Download Augmented Images"),
44
- title="Image Augmentation App",
45
- description="Upload an image to generate augmented versions. Select the number of augmented duplicates you want for the image."
46
  )
47
 
48
  iface.launch()
 
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,
 
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()