Rahatara commited on
Commit
717e6fa
·
verified ·
1 Parent(s): 253b27d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -74
app.py CHANGED
@@ -1,76 +1,15 @@
 
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()
 
1
+ import numpy as np
2
  import gradio as gr
 
 
 
 
3
 
4
+ def sepia(input_img):
5
+ sepia_filter = np.array([
6
+ [0.393, 0.769, 0.189],
7
+ [0.349, 0.686, 0.168],
8
+ [0.272, 0.534, 0.131]
9
+ ])
10
+ sepia_img = input_img.dot(sepia_filter.T)
11
+ sepia_img /= sepia_img.max()
12
+ return sepia_img
13
+
14
+ demo = gr.Interface(sepia, gr.Image(), "image")
15
+ demo.launch()