-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaugmenting_irregular_images.py
136 lines (102 loc) · 3.84 KB
/
augmenting_irregular_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
"""Augmenting_Irregularimages.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1bfy3YWUQ0ZXlkjeWWDPXtCI7rbIQWY06
"""
# mounting on Google Drive,below 2 lines helps in getting the authorization code by loggin into your Google account
from google.colab import drive
drive.mount('/gdrive')
# Required packages installation
!pip install imgaug==0.4.0
# Importing all the required libraries
import os
import random
import numpy as np
import cv2
import matplotlib.pyplot as plt
import imgaug as ia
import imgaug.augmenters as iaa
"""
This script is used to augment the images of the irregular images
"""
INPUT_DIRECTORY = "/gdrive/Shareddrives/ALDA_Project/data/web_scraping_meghana/irregular"
RESIZED_OUTPUT_DIRECTORY = "/gdrive/Shareddrives/ALDA_Project/data/web_scraping_meghana/irregular_resized"
AUG_OUTPUT_DIRECTORY = "/gdrive/Shareddrives/ALDA_Project/data/web_scraping_meghana/irregular_aug"
IMAGE_SIDE_LENGTH = 128
NUMBER_IMAGES =1
GROUP_NUMBER = 11
# Resizing all images in input_dir
filenames = os.listdir(INPUT_DIRECTORY)
for filename in filenames:
temp = cv2.imread(os.path.join(INPUT_DIRECTORY, filename))
temp = cv2.resize(temp, (IMAGE_SIDE_LENGTH, IMAGE_SIDE_LENGTH))
cv2.imwrite(os.path.join(RESIZED_OUTPUT_DIRECTORY, filename), temp)
# Loading a thousand images into memory
images = np.zeros((1, IMAGE_SIDE_LENGTH, IMAGE_SIDE_LENGTH, 3), dtype = np.uint8)
filenames = os.listdir(RESIZED_OUTPUT_DIRECTORY)
original_images = len(filenames)
counter = 0
while(images.shape[0] < NUMBER_IMAGES +1):
# Choose a random index
choice = random.randint(0, original_images-1)
# Image file to read (from random index chosen)
temp = cv2.imread(os.path.join(RESIZED_OUTPUT_DIRECTORY, filenames[choice]))
# Expand the dimensions of the image you have read in
temp = np.expand_dims(temp, axis = 0)
counter +=1
# Concatenate to image directory
images = np.concatenate((images, temp), axis = 0)
if counter % 100 ==0:
print(str(counter), "images loaded.")
images = images[1:]
print(images.shape)
# Sample 15 images
cols = 9
rows = 5
row_images = []
for i in range(rows):
indices = np.random.rand(cols)
indices = (indices*NUMBER_IMAGES).astype(np.int64)
row = images[indices[0], ...]
for j in range(1, cols):
row = np.hstack((row, images[indices[j]]))
row_images.append(row[:])
image = row_images[0]
for i in range(1, rows):
image = np.vstack((image, row_images[i]))
# Display
plt.figure(figsize=(cols*3, rows*3))
plt.imshow(image)
ia.seed(GROUP_NUMBER)
#Using augmenter to augment the image
sequential_augmenter = iaa.Sequential([
iaa.ChannelShuffle(0.35),
iaa.ScaleX((0.9, 1.1)),
iaa.ScaleY((0.9, 1.1)),
iaa.Sometimes(0.5, iaa.OneOf([iaa.Fliplr(), iaa.Flipud()])),
iaa.Sometimes(0.5, iaa.Rot90((1,3))),
], random_order = True)
augmented_images = sequential_augmenter.augment_images(images)
# Sample 15 images
cols = 9
rows = 5
row_images = []
for i in range(rows):
indices = np.random.rand(cols)
indices = (indices*NUMBER_IMAGES).astype(np.int64)
row = augmented_images[indices[0], ...]
for j in range(1, cols):
row = np.hstack((row, augmented_images[indices[j]]))
row_images.append(row[:])
image = row_images[0]
for i in range(1, rows):
image = np.vstack((image, row_images[i]))
plt.figure(figsize=(cols*3, rows*3))
plt.imshow(image)
#writing the images on the prescribed file path
for i in range(NUMBER_IMAGES):
fname = "irregular_" + str(i) + ".jpg"
cv2.imwrite(os.path.join(AUG_OUTPUT_DIRECTORY, fname), augmented_images[i])
if i % 100 == 0:
print(str(i), "images written to disk")