maximuspowers commited on
Commit
fc9bdef
·
verified ·
1 Parent(s): 2bc6dee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -48
app.py CHANGED
@@ -1,61 +1,50 @@
1
- # Define basic float and int types to patch sctypes without numpy
2
- if not hasattr(__builtins__, 'sctypes'):
3
- sctypes = {
4
- "float": [float],
5
- "int": [int],
6
- "uint": [int] # Handle 'uint' as normal 'int' since it's not natively supported without numpy
7
- }
8
-
9
- # Now, import imgaug after patching
10
- import imgaug.augmenters as iaa
11
- import cv2
12
- import matplotlib.pyplot as plt
13
  import gradio as gr
 
 
 
14
 
15
- def augment_image(image, flip, rotate, brightness, noise_scale, elastic_alpha, elastic_sigma):
16
- # Ensure the image is a NumPy array (required by imgaug)
17
  image = np.array(image)
18
 
19
- # Apply augmentations based on user inputs
20
  if flip:
21
- flip_aug = iaa.Fliplr(1.0) # flips horizontally 100% of the time
22
- image = flip_aug.augment_image(image)
23
-
24
- rotate_aug = iaa.Affine(rotate=rotate) # Rotate by specified degrees
25
- image = rotate_aug.augment_image(image)
26
-
27
- brightness_aug = iaa.Multiply(brightness) # Adjust brightness
28
- image = brightness_aug.augment_image(image)
29
-
30
- noise_aug = iaa.AdditiveGaussianNoise(scale=(noise_scale)) # Gaussian noise
31
- image = noise_aug.augment_image(image)
32
-
33
- elastic_aug = iaa.ElasticTransformation(alpha=elastic_alpha, sigma=elastic_sigma) # Elastic transformation
34
- image = elastic_aug.augment_image(image)
35
 
36
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- def gradio_interface(image, flip, rotate, brightness, noise_scale, elastic_alpha, elastic_sigma):
39
- augmented_image = augment_image(image, flip, rotate, brightness, noise_scale, elastic_alpha, elastic_sigma)
 
40
  return augmented_image
41
 
42
- # Gradio UI
43
- inputs = [
44
- gr.Image(type="pil"), # Image input
45
- gr.Checkbox(label="Flip Image Horizontally"), # Flip input
46
- gr.Slider(minimum=-180, maximum=180, step=1, value=0, label="Rotate Image (degrees)"), # Rotation input
47
- gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Adjust Brightness"), # Brightness input
48
- gr.Slider(minimum=0, maximum=100, step=1, value=10, label="Gaussian Noise Scale"), # Noise input
49
- gr.Slider(minimum=0, maximum=200, step=10, value=100, label="Elastic Transformation Alpha"), # Elastic Alpha input
50
- gr.Slider(minimum=0.1, maximum=10.0, step=0.1, value=3.0, label="Elastic Transformation Sigma") # Elastic Sigma input
51
- ]
52
-
53
  iface = gr.Interface(
54
- fn=gradio_interface,
55
- inputs=inputs,
56
- outputs=gr.Image(type="numpy"),
57
- title="Image Augmentation Demo",
58
- description="Try out different data augmentation techniques on your image.",
 
 
 
 
 
 
59
  )
60
 
61
  iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import albumentations as A
3
+ import cv2
4
+ import numpy as np
5
 
6
+ # Function for image augmentation
7
+ def augment_image(image, flip=False, rotate=0, brightness=1.0, noise=0, elastic=False):
8
  image = np.array(image)
9
 
10
+ aug_list = []
11
  if flip:
12
+ aug_list.append(A.HorizontalFlip(p=1.0))
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ if rotate:
15
+ aug_list.append(A.Rotate(limit=rotate, p=1.0))
16
+
17
+ if brightness != 1.0:
18
+ aug_list.append(A.RandomBrightnessContrast(brightness_limit=(brightness-1, brightness-1), p=1.0))
19
+
20
+ if noise > 0:
21
+ aug_list.append(A.GaussNoise(var_limit=(noise, noise), p=1.0))
22
+
23
+ if elastic:
24
+ aug_list.append(A.ElasticTransform(alpha=1, sigma=50, alpha_affine=50, p=1.0))
25
+
26
+ aug = A.Compose(aug_list)
27
+ augmented = aug(image=image)
28
+
29
+ return augmented["image"]
30
 
31
+ # Gradio Interface
32
+ def image_augmentor_interface(image, flip, rotate, brightness, noise, elastic):
33
+ augmented_image = augment_image(image, flip, rotate, brightness, noise, elastic)
34
  return augmented_image
35
 
 
 
 
 
 
 
 
 
 
 
 
36
  iface = gr.Interface(
37
+ fn=image_augmentor_interface,
38
+ inputs=[
39
+ gr.Image(type="numpy"),
40
+ gr.Checkbox(label="Flip Horizontally"),
41
+ gr.Slider(0, 360, label="Rotate Degrees"),
42
+ gr.Slider(0.5, 1.5, step=0.1, label="Brightness Adjustment"),
43
+ gr.Slider(0, 100, step=1, label="Noise Scale"),
44
+ gr.Checkbox(label="Elastic Distortion")
45
+ ],
46
+ outputs="image",
47
+ title="Image Augmentation with Gradio"
48
  )
49
 
50
  iface.launch()