Rahatara commited on
Commit
4661f75
1 Parent(s): c6b3b2e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Image Augmentation Function
10
+ def augment_images(image_files, num_duplicates):
11
+ datagen = ImageDataGenerator(
12
+ rotation_range=40,
13
+ width_shift_range=0.2,
14
+ height_shift_range=0.2,
15
+ zoom_range=0.2,
16
+ shear_range=0.2,
17
+ horizontal_flip=True,
18
+ fill_mode='nearest')
19
+
20
+ # Create a temporary directory to store augmented images
21
+ with tempfile.TemporaryDirectory() as temp_dir:
22
+ for image_file in image_files:
23
+ img = Image.open(image_file).convert('RGB') # Convert to RGB for consistency
24
+ img = img.resize((256, 256)) # Resize image
25
+ x = img_to_array(img) # Convert the image to a numpy array
26
+ x = x.reshape((1,) + x.shape) # Reshape for the data generator
27
+
28
+ i = 0
29
+ for _ in datagen.flow(x, batch_size=1, save_to_dir=temp_dir, save_prefix='aug', save_format='jpeg'):
30
+ i += 1
31
+ if i >= num_duplicates:
32
+ break
33
+
34
+ # Zip the augmented images
35
+ zip_name = tempfile.mktemp(suffix='.zip')
36
+ with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
37
+ for root, dirs, files in os.walk(temp_dir):
38
+ for file in files:
39
+ zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(temp_dir, '..')))
40
+ return zip_name
41
+
42
+ # Gradio Interface
43
+ def gradio_interface(image_files, num_duplicates):
44
+ zip_file_path = augment_images(image_files, num_duplicates)
45
+ return zip_file_path
46
+
47
+ iface = gr.Interface(fn=gradio_interface,
48
+ inputs=[gr.inputs.Image(type="file", label="Upload Images", accept="image/*", multiple=True),
49
+ gr.inputs.Number(default=5, label="Number of Duplicates", min_value=1, max_value=20)],
50
+ outputs=gr.outputs.File(label="Download Augmented Images"),
51
+ title="Image Augmentation App",
52
+ description="Upload images to generate augmented versions.")
53
+
54
+ iface.launch()