Rahatara commited on
Commit
8aa7053
1 Parent(s): 12f0a31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -46
app.py CHANGED
@@ -1,57 +1,44 @@
1
  import gradio as gr
2
  import numpy as np
3
- from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array
4
  from PIL import Image
5
  import io
6
- import zipfile
7
 
8
- def augment_images(image_file, num_duplicates):
9
  """
10
- Augments uploaded images based on specified transformations and number of duplicates,
11
- then compresses them into a ZIP file for download.
12
  """
13
- # Augmentation configuration
14
- datagen = ImageDataGenerator(
15
- rotation_range=40,
16
- width_shift_range=0.2,
17
- height_shift_range=0.2,
18
- shear_range=0.2,
19
- zoom_range=0.2,
20
- horizontal_flip=True,
21
- fill_mode='nearest')
22
-
23
- # Initialize ZIP file in memory
24
- zip_buffer = io.BytesIO()
25
-
26
- with zipfile.ZipFile(zip_buffer, 'a', zipfile.ZIP_DEFLATED) as zipf:
27
- img = Image.open(image_file.name).convert('RGB')
28
- img = img.resize((256, 256)) # Resize image
29
- x = img_to_array(img) # Convert image to np.array
30
- x = np.expand_dims(x, axis=0) # Add batch dimension
31
-
32
- # Generate and save augmented images
33
- for i in range(num_duplicates):
34
- it = datagen.flow(x, batch_size=1)
35
- batch = next(it)
36
- image = Image.fromarray(batch[0].astype('uint8'), 'RGB')
37
- img_byte_arr = io.BytesIO()
38
- image.save(img_byte_arr, format='JPEG')
39
- img_byte_arr.seek(0)
40
- zipf.writestr(f"augmented_image_{i}.jpeg", img_byte_arr.getvalue())
41
-
42
- # Prepare ZIP file for download
43
- zip_buffer.seek(0)
44
-
45
- return zip_buffer, 'augmented_images.zip'
46
 
47
- # Create the Gradio interface
48
- iface = gr.Interface(
49
- fn=augment_images,
50
- inputs=[gr.inputs.Image(type='file', label="Upload Image"), gr.inputs.Number(default=5, label="Number of Augmented Images")],
51
- outputs=[gr.outputs.File(label="Download ZIP of Augmented Images"), "text"],
52
- title="Image Augmentation Tool",
53
- description="Upload an image and select the number of augmented images to generate. Download the result as a ZIP file."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  )
55
 
56
  if __name__ == "__main__":
57
- iface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ import time
4
  from PIL import Image
5
  import io
 
6
 
7
+ def sepia(input_img):
8
  """
9
+ Applies a sepia filter to the input image.
 
10
  """
11
+ sepia_filter = np.array([
12
+ [0.393, 0.769, 0.189],
13
+ [0.349, 0.686, 0.168],
14
+ [0.272, 0.534, 0.131]
15
+ ])
16
+ sepia_img = input_img.dot(sepia_filter.T)
17
+ sepia_img /= sepia_img.max()
18
+ return sepia_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ def fake_diffusion_and_sepia(steps):
21
+ """
22
+ A generator function that simulates a fake diffusion process for a specified number of steps.
23
+ After the final step, applies a sepia filter to the image.
24
+ """
25
+ rng = np.random.default_rng()
26
+ for i in range(steps):
27
+ # Simulate the diffusion process
28
+ time.sleep(1) # Wait to simulate processing time
29
+ image = rng.random(size=(256, 256, 3)) # Generate a random image
30
+ if i == steps - 1: # Apply sepia filter on the last step
31
+ image = sepia(image)
32
+ yield image
33
+
34
+ # Define Gradio interface
35
+ demo = gr.Interface(
36
+ fn=fake_diffusion_and_sepia,
37
+ inputs=gr.Slider(minimum=1, maximum=10, default=5, step=1, label="Number of Steps"),
38
+ outputs=gr.Image(type="numpy", label="Sepia Image"),
39
+ title="Fake Diffusion with Sepia Filter",
40
+ description="Generates a series of images simulating a diffusion process. The final image is processed with a sepia filter."
41
  )
42
 
43
  if __name__ == "__main__":
44
+ demo.launch()