Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import imgaug.augmenters as iaa
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
def augment_image(image, flip, rotate, brightness, noise_scale, elastic_alpha, elastic_sigma):
|
8 |
+
image = np.array(image)
|
9 |
+
|
10 |
+
# apply augs based on user inputs
|
11 |
+
if flip:
|
12 |
+
flip_aug = iaa.Fliplr(1.0) # flips horizontally 100% of the time
|
13 |
+
image = flip_aug.augment_image(image)
|
14 |
+
|
15 |
+
rotate_aug = iaa.Affine(rotate=rotate) # rotate by specified num of degrees (if you pass in a touple it will select a random value between options)
|
16 |
+
image = rotate_aug.augment_image(image)
|
17 |
+
|
18 |
+
brightness_aug = iaa.Multiply(brightness) # adjust brightness
|
19 |
+
image = brightness_aug.augment_image(image)
|
20 |
+
|
21 |
+
noise_aug = iaa.AdditiveGaussianNoise(scale=(noise_scale)) # eaussian noise
|
22 |
+
image = noise_aug.augment_image(image)
|
23 |
+
|
24 |
+
elastic_aug = iaa.ElasticTransformation(alpha=elastic_alpha, sigma=elastic_sigma) # elastic transformation
|
25 |
+
image = elastic_aug.augment_image(image)
|
26 |
+
|
27 |
+
return image
|
28 |
+
|
29 |
+
def gradio_interface(image, flip, rotate, brightness, noise_scale, elastic_alpha, elastic_sigma):
|
30 |
+
augmented_image = augment_image(image, flip, rotate, brightness, noise_scale, elastic_alpha, elastic_sigma)
|
31 |
+
return augmented_image
|
32 |
+
|
33 |
+
inputs = [
|
34 |
+
gr.Image(type="pil"), # Image input
|
35 |
+
gr.Checkbox(label="Flip Image Horizontally"), # Flip input
|
36 |
+
gr.Slider(minimum=-180, maximum=180, step=1, value=0, label="Rotate Image (degrees)"), # Rotation input
|
37 |
+
gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Adjust Brightness"), # Brightness input
|
38 |
+
gr.Slider(minimum=0, maximum=100, step=1, value=10, label="Gaussian Noise Scale"), # Noise input
|
39 |
+
gr.Slider(minimum=0, maximum=200, step=10, value=100, label="Elastic Transformation Alpha"), # Elastic Alpha input
|
40 |
+
gr.Slider(minimum=0.1, maximum=10.0, step=0.1, value=3.0, label="Elastic Transformation Sigma") # Elastic Sigma input
|
41 |
+
]
|
42 |
+
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=gradio_interface,
|
45 |
+
inputs=inputs,
|
46 |
+
outputs=gr.Image(type="numpy"),
|
47 |
+
title="Image Augmentation Demo",
|
48 |
+
description="Try out different data augmentation techniques on your image.",
|
49 |
+
)
|
50 |
+
|
51 |
+
iface.launch()
|