Rahatara commited on
Commit
43c52cc
·
verified ·
1 Parent(s): 062ed9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -30
app.py CHANGED
@@ -6,49 +6,43 @@ 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()
 
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,
13
  height_shift_range=0.2,
 
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.Image(shape=None, label="Upload Image", source="upload"), gr.Slider(minimum=1, maximum=20, default=5, label="Number of Augmented Samples")],
43
+ outputs=gr.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()