developer0hye commited on
Commit
bf0edf4
·
verified ·
1 Parent(s): bd70752

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import albumentations as A
5
+ import random
6
+
7
+ def apply_augmentations(image, flip_h, flip_v, rotate, crop, gray, scale,
8
+ prob_flip_h, prob_flip_v, prob_rotate, prob_crop, prob_gray, prob_scale):
9
+ augmentations = []
10
+
11
+ if flip_h:
12
+ augmentations.append(A.HorizontalFlip(p=float(prob_flip_h)))
13
+ if flip_v:
14
+ augmentations.append(A.VerticalFlip(p=float(prob_flip_v)))
15
+ if rotate:
16
+ augmentations.append(A.Rotate(limit=90, p=float(prob_rotate)))
17
+ if crop:
18
+ augmentations.append(A.RandomResizedCrop(
19
+ size=(image.shape[0], image.shape[1]),
20
+ scale=(0.8, 1.0),
21
+ p=float(prob_crop)
22
+ ))
23
+ if gray:
24
+ augmentations.append(A.ToGray(p=float(prob_gray)))
25
+ if scale:
26
+ scale_factor = random.uniform(0.8, 1.2)
27
+ augmentations.append(A.Resize(
28
+ height=int(image.shape[0] * scale_factor),
29
+ width=int(image.shape[1] * scale_factor),
30
+ p=float(prob_scale)
31
+ ))
32
+
33
+ transform = A.Compose(augmentations)
34
+
35
+ # Generate 9 augmented images
36
+ augmented_images = []
37
+ for _ in range(9):
38
+ augmented = transform(image=image)
39
+ augmented_images.append(augmented['image'])
40
+
41
+ # Find maximum dimensions
42
+ max_height = max(img.shape[0] for img in augmented_images)
43
+ max_width = max(img.shape[1] for img in augmented_images)
44
+
45
+ # Add padding to all images
46
+ padded_images = []
47
+ for img in augmented_images:
48
+ h, w = img.shape[:2]
49
+ pad_top = (max_height - h) // 2
50
+ pad_bottom = max_height - h - pad_top
51
+ pad_left = (max_width - w) // 2
52
+ pad_right = max_width - w - pad_left
53
+
54
+ # Handle both RGB and grayscale images
55
+ if len(img.shape) == 3:
56
+ padded = cv2.copyMakeBorder(img, pad_top, pad_bottom, pad_left, pad_right,
57
+ cv2.BORDER_CONSTANT, value=[128, 128, 128])
58
+ else:
59
+ padded = cv2.copyMakeBorder(img, pad_top, pad_bottom, pad_left, pad_right,
60
+ cv2.BORDER_CONSTANT, value=[128])
61
+ padded_images.append(padded)
62
+
63
+ # Create a 3x3 grid
64
+ rows = []
65
+ for i in range(0, 9, 3):
66
+ row = np.hstack(padded_images[i:i+3])
67
+ rows.append(row)
68
+ grid = np.vstack(rows)
69
+
70
+ return grid
71
+
72
+ def main():
73
+ with gr.Blocks() as demo:
74
+ with gr.Row():
75
+ with gr.Column():
76
+ input_image = gr.Image(label="Input Image")
77
+ with gr.Column():
78
+ output_image = gr.Image(label="Output Image (3x3 Grid)")
79
+
80
+ with gr.Row():
81
+ with gr.Column():
82
+ flip_h = gr.Checkbox(label="Horizontal Flip")
83
+ prob_flip_h = gr.Slider(minimum=0, maximum=1, value=0.5, label="Probability")
84
+
85
+ with gr.Column():
86
+ flip_v = gr.Checkbox(label="Vertical Flip")
87
+ prob_flip_v = gr.Slider(minimum=0, maximum=1, value=0.5, label="Probability")
88
+
89
+ with gr.Column():
90
+ rotate = gr.Checkbox(label="Rotate")
91
+ prob_rotate = gr.Slider(minimum=0, maximum=1, value=0.5, label="Probability")
92
+
93
+ with gr.Column():
94
+ crop = gr.Checkbox(label="Random Crop")
95
+ prob_crop = gr.Slider(minimum=0, maximum=1, value=0.5, label="Probability")
96
+
97
+ with gr.Column():
98
+ gray = gr.Checkbox(label="Grayscale")
99
+ prob_gray = gr.Slider(minimum=0, maximum=1, value=0.5, label="Probability")
100
+
101
+ with gr.Column():
102
+ scale = gr.Checkbox(label="Random Scale (0.8-1.2x)")
103
+ prob_scale = gr.Slider(minimum=0, maximum=1, value=0.5, label="Probability")
104
+
105
+ with gr.Row():
106
+ run_button = gr.Button("Apply Augmentations")
107
+
108
+ run_button.click(
109
+ fn=apply_augmentations,
110
+ inputs=[
111
+ input_image,
112
+ flip_h, flip_v, rotate, crop, gray, scale,
113
+ prob_flip_h, prob_flip_v, prob_rotate, prob_crop, prob_gray, prob_scale
114
+ ],
115
+ outputs=output_image
116
+ )
117
+
118
+ demo.launch()
119
+
120
+ if __name__ == "__main__":
121
+ main()