-
Notifications
You must be signed in to change notification settings - Fork 0
/
aug.py
112 lines (93 loc) · 3.47 KB
/
aug.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
import cv2
import numpy as np
import os.path
import errno
import color_aug as ca
import trans_aug as trans
dataset_dir="/home/hoangnt/workspace/Project/DL/License_Plate_Recognition/gray/"
save_dir="/home/hoangnt/workspace/Project/DL/License_Plate_Recognition/auged/"
extension=".jpeg"
class IdentityMetadata():
def __init__(self, base, file):
# dataset base directory
self.base = base
# image file name
self.file = file
def __repr__(self):
return self.image_path()
def image_path(self):
return os.path.join(self.base, self.file)
class IdentityLabels():
def __init__(self, img_name):
# image file name
self.img_name = img_name
def __repr__(self):
return self.label()
def label(self):
return os.path.join(self.img_name)
def load_metadata(path):
metadata = []
for i in os.listdir(path):
metadata.append(IdentityMetadata(path, i))
return np.array(metadata)
def load_label(path):
label = []
for i in os.listdir(path):
label.append(IdentityLabels(i.split('.')[0]))
return np.asarray(label, dtype=np.str)
def create_folder(path):
# create folder with label
#print(len(load_label(dataset_dir)))
for i in range(len(load_label(dataset_dir))):
filename = save_dir + load_label(dataset_dir)[i]
#print(filename)
if not os.path.exists(filename):
try:
os.mkdir(filename)
#print(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
#create_folder(save_dir)
'''
metadata = load_metadata('gray')
#rotate random 10 images
k = 0
while(k < 10):
for i in range(0, metadata.size):
dir_to_save_label = save_dir + load_label(dataset_dir)[i]
img = cv2.imread(str(metadata[i]))
img = trans.rotate_random_image(img)
cv2.imwrite(os.path.join(dir_to_save_label , str(k) + extension),img)
k+=1
'''
'''
for i in range(0, metadata.size):
dir_to_save_label = save_dir + load_label(dataset_dir)[i]
img_gaussian_blur = cv2.imread(str(metadata[i]))
img_blur = cv2.imread(str(metadata[i]))
img_sharpen = cv2.imread(str(metadata[i]))
img_zoom = cv2.imread(str(metadata[i]))
#gaussian blur
img_gaussian_blur = ca.gausian_blur(img_gaussian_blur, 0.50)
cv2.imwrite(os.path.join(dir_to_save_label , 'gaussian-' + load_label(dataset_dir)[i] + extension),img_gaussian_blur)
#zoom
img_zoom = trans.zoom(img_zoom)
cv2.imwrite(os.path.join(dir_to_save_label , 'zoom-' + load_label(dataset_dir)[i] + extension),img_zoom)
'''
dir_label_in_auged = [name for name in os.listdir(save_dir)]
#print(dir_label_in_auged)
for i in range(0, len(dir_label_in_auged)):
metadata = load_metadata(save_dir+dir_label_in_auged[i])
#print(metadata)
for j in range(metadata.size):
#print(metadata[j])
dir_to_save_label = save_dir + dir_label_in_auged[i]
img_gaussian_blur = cv2.imread(str(metadata[j]))
img_zoom = cv2.imread(str(metadata[j]))
#gaussian blur
img_gaussian_blur = ca.gausian_blur(img_gaussian_blur, 0.50)
cv2.imwrite(os.path.join(dir_to_save_label , 'gaussian-' + str(j) + extension),img_gaussian_blur)
#zoom
img_zoom = trans.zoom(img_zoom)
cv2.imwrite(os.path.join(dir_to_save_label , 'zoom-' + str(j) + extension),img_zoom)