Update app.py
Browse files
app.py
CHANGED
@@ -6,49 +6,43 @@ import os
|
|
6 |
import zipfile
|
7 |
import tempfile
|
8 |
|
9 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
21 |
with tempfile.TemporaryDirectory() as temp_dir:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
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,
|
38 |
for file in files:
|
39 |
-
zipf.write(os.path.join(root, file),
|
40 |
-
|
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(
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
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()
|